2018-09-12 15:49:58 +00:00
package xyz.etztech.deluxegroups ;
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
import org.bukkit.OfflinePlayer ;
import org.bukkit.configuration.file.FileConfiguration ;
import org.bukkit.configuration.file.YamlConfiguration ;
import org.bukkit.entity.Player ;
2018-10-20 06:36:57 +00:00
import org.bukkit.plugin.java.JavaPlugin ;
import org.dynmap.DynmapAPI ;
2018-09-12 15:49:58 +00:00
import xyz.etztech.core.api.IMinecraftManager ;
import xyz.etztech.deluxegroups.command.CommandGroup ;
import xyz.etztech.deluxegroups.command.CommandMain ;
import xyz.etztech.deluxegroups.listeners.AsyncPlayerChatListener ;
import xyz.etztech.deluxegroups.listeners.SessionListener ;
import java.io.File ;
import java.io.IOException ;
import java.io.PrintWriter ;
import java.util.* ;
import java.util.logging.Logger ;
2018-09-29 04:14:20 +00:00
2018-10-20 06:36:57 +00:00
public class DeluxeGroups extends JavaPlugin {
2018-09-12 15:49:58 +00:00
private static DeluxeGroups instance ;
public static FileConfiguration config ;
private Logger log = Logger . getLogger ( "Minecraft" ) ;
2018-10-20 06:36:57 +00:00
private static Database database ;
2018-09-12 15:49:58 +00:00
// Objects that can be reloaded
2018-10-20 06:36:57 +00:00
private AsyncPlayerChatListener chatListener ;
2018-09-12 15:49:58 +00:00
// Dynmap API
2018-10-20 06:36:57 +00:00
private static DynmapAPI dynmap = null ;
2018-09-12 15:49:58 +00:00
// MinecraftManager API
private static IMinecraftManager minecraftManager = null ;
2018-10-20 06:36:57 +00:00
public void onEnable ( ) {
2018-09-12 15:49:58 +00:00
instance = this ;
2018-10-20 06:36:57 +00:00
database = new Database ( this ) ;
2018-09-12 15:49:58 +00:00
saveDefaultConfig ( ) ;
reloadConfig ( ) ;
try {
File logs = new File ( getDataFolder ( ) . getAbsolutePath ( ) + "/logs/" ) ;
logs . mkdir ( ) ;
} catch ( Exception e ) { }
if ( isEnabled ( ) ) {
// Dynmap integration
if ( Bukkit . getPluginManager ( ) . isPluginEnabled ( "dynmap" ) ) {
2018-10-20 06:36:57 +00:00
dynmap = ( DynmapAPI ) Bukkit . getPluginManager ( ) . getPlugin ( "dynmap" ) ;
2018-09-29 04:14:20 +00:00
DeluxeUtil . setupDynmap ( dynmap ) ;
2018-09-12 15:49:58 +00:00
getServer ( ) . getPluginManager ( ) . registerEvents ( new SessionListener ( ) , this ) ;
}
// MCM integration
if ( Bukkit . getPluginManager ( ) . isPluginEnabled ( "MinecraftManager" ) ) {
minecraftManager = ( IMinecraftManager ) Bukkit . getPluginManager ( ) . getPlugin ( "MinecraftManager" ) ;
Bukkit . getConsoleSender ( ) . sendMessage ( ChatColor . RED + "IMPORTANT: Disabling MinecraftManager global logging. All global logging will be handled by DeluxeGroups." ) ;
minecraftManager . logOverride ( true ) ;
}
// Add Commands
CommandMain cmdMain = new CommandMain ( this ) ;
this . getCommand ( "deluxegroups" ) . setExecutor ( cmdMain ) ;
CommandGroup cmdGroup = new CommandGroup ( this ) ;
this . getCommand ( "group" ) . setExecutor ( cmdGroup ) ;
// Add Listeners
chatListener = new AsyncPlayerChatListener ( this ) ;
getServer ( ) . getPluginManager ( ) . registerEvents ( chatListener , this ) ;
}
}
2018-10-20 06:36:57 +00:00
public void onDisable ( ) { }
2018-09-12 15:49:58 +00:00
public void loadConfig ( ) {
config = Bukkit . getPluginManager ( ) . getPlugin ( "DeluxeGroups" ) . getConfig ( ) ;
}
@Override
public void reloadConfig ( ) {
super . reloadConfig ( ) ;
loadConfig ( ) ;
2018-10-20 06:36:57 +00:00
convertConfig ( ) ;
2018-09-12 15:49:58 +00:00
if ( chatListener ! = null ) {
chatListener . reload ( ) ;
}
}
2018-10-20 06:36:57 +00:00
@Deprecated
public boolean convertConfig ( ) {
2018-09-12 15:49:58 +00:00
File groupf = new File ( getDataFolder ( ) , "groups.yml" ) ;
if ( ! groupf . exists ( ) ) {
2018-10-20 06:36:57 +00:00
return false ;
2018-09-12 15:49:58 +00:00
}
2018-10-20 06:36:57 +00:00
log ( "Converting old groups.yml to H2." ) ;
FileConfiguration groupConfig = new YamlConfiguration ( ) ;
2018-09-12 15:49:58 +00:00
try {
groupConfig . load ( groupf ) ;
int loaded = 0 ;
DeluxeGroup group ;
for ( String key : groupConfig . getKeys ( false ) ) {
group = new DeluxeGroup ( ) ;
group . setName ( groupConfig . getString ( key + ".name" ) ) ;
group . setPassword ( groupConfig . getString ( key + ".password" ) ) ;
group . setPermanent ( groupConfig . getBoolean ( key + ".permanent" ) ) ;
2018-10-20 06:36:57 +00:00
int id = database . addGroup ( group ) ;
2018-09-12 15:49:58 +00:00
for ( String uuid : groupConfig . getStringList ( key + ".players" ) ) {
2018-10-20 06:36:57 +00:00
database . addPlayer ( uuid , id ) ;
2018-09-12 15:49:58 +00:00
}
loaded + + ;
}
2018-10-20 06:36:57 +00:00
log ( "Converted " + loaded + " groups to H2! The groups.yml will be deleted next time the server is stopped." ) ;
groupf . deleteOnExit ( ) ;
return true ;
2018-09-12 15:49:58 +00:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2018-10-20 06:36:57 +00:00
return false ;
2018-09-12 15:49:58 +00:00
}
2018-10-20 06:36:57 +00:00
2018-09-12 15:49:58 +00:00
public void log ( String message ) {
log . info ( "[DeluxeGroups]: " + message ) ;
}
public static DeluxeGroups getInstance ( ) {
return instance ;
}
2018-10-20 06:36:57 +00:00
public static Database getDatabase ( ) {
return database ;
2018-09-12 15:49:58 +00:00
}
2018-10-20 06:36:57 +00:00
public static DynmapAPI getDynmap ( ) {
2018-09-12 15:49:58 +00:00
return dynmap ;
}
public static IMinecraftManager getMinecraftManager ( ) {
return minecraftManager ;
}
}