2018-11-06 04:33:10 +01:00
package com.songoda.ultimatestacker ;
import com.songoda.ultimatestacker.command.CommandManager ;
2019-08-02 15:59:10 +02:00
import com.songoda.ultimatestacker.database.* ;
2018-11-06 04:33:10 +01:00
import com.songoda.ultimatestacker.entity.EntityStack ;
2018-11-06 06:09:40 +01:00
import com.songoda.ultimatestacker.entity.EntityStackManager ;
2019-01-23 19:01:31 +01:00
import com.songoda.ultimatestacker.hologram.Hologram ;
import com.songoda.ultimatestacker.hologram.HologramHolographicDisplays ;
2019-06-10 02:47:29 +02:00
import com.songoda.ultimatestacker.hook.StackerHook ;
import com.songoda.ultimatestacker.hook.hooks.JobsHook ;
2019-06-07 19:48:17 +02:00
import com.songoda.ultimatestacker.listeners.* ;
2019-07-23 16:55:49 +02:00
import com.songoda.ultimatestacker.lootables.LootablesManager ;
2018-11-06 04:33:10 +01:00
import com.songoda.ultimatestacker.spawner.SpawnerStack ;
import com.songoda.ultimatestacker.spawner.SpawnerStackManager ;
import com.songoda.ultimatestacker.storage.Storage ;
import com.songoda.ultimatestacker.storage.StorageRow ;
import com.songoda.ultimatestacker.storage.types.StorageYaml ;
import com.songoda.ultimatestacker.tasks.StackingTask ;
2019-07-31 06:29:10 +02:00
import com.songoda.ultimatestacker.utils.* ;
2019-07-19 04:19:33 +02:00
import com.songoda.ultimatestacker.utils.locale.Locale ;
2019-06-07 19:48:17 +02:00
import com.songoda.ultimatestacker.utils.settings.Setting ;
2019-05-23 22:15:04 +02:00
import com.songoda.ultimatestacker.utils.settings.SettingsManager ;
2019-04-26 10:06:18 +02:00
import com.songoda.ultimatestacker.utils.updateModules.LocaleModule ;
import com.songoda.update.Plugin ;
import com.songoda.update.SongodaUpdate ;
2019-06-07 19:48:17 +02:00
import org.apache.commons.lang.ArrayUtils ;
import org.bukkit.Bukkit ;
import org.bukkit.Location ;
import org.bukkit.Material ;
2018-11-06 04:33:10 +01:00
import org.bukkit.command.ConsoleCommandSender ;
import org.bukkit.entity.EntityType ;
2019-06-10 02:47:29 +02:00
import org.bukkit.entity.Player ;
2019-01-23 19:01:31 +01:00
import org.bukkit.plugin.PluginManager ;
2018-11-06 04:33:10 +01:00
import org.bukkit.plugin.java.JavaPlugin ;
2019-04-28 01:20:33 +02:00
2019-08-02 15:59:10 +02:00
import java.io.File ;
2019-06-10 02:47:29 +02:00
import java.util.ArrayList ;
import java.util.List ;
2018-11-06 04:33:10 +01:00
public class UltimateStacker extends JavaPlugin {
private static UltimateStacker INSTANCE ;
private ConfigWrapper mobFile = new ConfigWrapper ( this , " " , " mobs.yml " ) ;
private ConfigWrapper itemFile = new ConfigWrapper ( this , " " , " items.yml " ) ;
private ConfigWrapper spawnerFile = new ConfigWrapper ( this , " " , " spawners.yml " ) ;
private Locale locale ;
private SettingsManager settingsManager ;
private EntityStackManager entityStackManager ;
private SpawnerStackManager spawnerStackManager ;
2019-07-23 16:55:49 +02:00
private LootablesManager lootablesManager ;
2018-11-06 04:33:10 +01:00
private CommandManager commandManager ;
private StackingTask stackingTask ;
2019-01-23 19:01:31 +01:00
private Hologram hologram ;
2018-11-06 05:41:58 +01:00
2019-08-02 15:59:10 +02:00
private DatabaseConnector databaseConnector ;
private DataMigrationManager dataMigrationManager ;
private DataManager dataManager ;
2019-07-31 06:29:10 +02:00
private EntityUtils entityUtils ;
2019-06-10 02:47:29 +02:00
private List < StackerHook > stackerHooks = new ArrayList < > ( ) ;
2018-11-06 05:41:58 +01:00
private ServerVersion serverVersion = ServerVersion . fromPackageName ( Bukkit . getServer ( ) . getClass ( ) . getPackage ( ) . getName ( ) ) ;
2018-11-06 04:33:10 +01:00
2018-11-06 06:09:40 +01:00
public static UltimateStacker getInstance ( ) {
return INSTANCE ;
}
2018-11-06 04:33:10 +01:00
public void onDisable ( ) {
2019-08-02 15:59:10 +02:00
this . dataManager . bulkUpdateSpawners ( this . spawnerStackManager . getStacks ( ) ) ;
2018-11-06 04:33:10 +01:00
ConsoleCommandSender console = Bukkit . getConsoleSender ( ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
2019-01-15 08:29:27 +01:00
console . sendMessage ( Methods . formatText ( " &7UltimateStacker " + this . getDescription ( ) . getVersion ( ) + " by &5Songoda <3! " ) ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &7Action: &cDisabling&7... " ) ) ;
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
2018-11-06 04:33:10 +01:00
}
2018-11-09 05:11:49 +01:00
@Override
2018-11-06 04:33:10 +01:00
public void onEnable ( ) {
INSTANCE = this ;
2018-11-09 05:11:49 +01:00
2018-11-06 04:33:10 +01:00
ConsoleCommandSender console = Bukkit . getConsoleSender ( ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
2019-01-15 08:29:27 +01:00
console . sendMessage ( Methods . formatText ( " &7UltimateStacker " + this . getDescription ( ) . getVersion ( ) + " by &5Songoda <3&7! " ) ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &7Action: &aEnabling&7... " ) ) ;
2018-11-06 04:33:10 +01:00
this . settingsManager = new SettingsManager ( this ) ;
2019-05-23 22:15:04 +02:00
this . settingsManager . setupConfig ( ) ;
2018-11-06 04:33:10 +01:00
2019-05-23 22:15:04 +02:00
this . commandManager = new CommandManager ( this ) ;
2018-11-06 04:33:10 +01:00
2019-07-31 06:29:10 +02:00
this . entityUtils = new EntityUtils ( ) ;
2019-07-23 16:55:49 +02:00
this . lootablesManager = new LootablesManager ( ) ;
this . lootablesManager . createDefaultLootables ( ) ;
this . getLootablesManager ( ) . getLootManager ( ) . loadLootables ( ) ;
2019-07-08 18:39:57 +02:00
2018-11-06 04:33:10 +01:00
for ( EntityType value : EntityType . values ( ) ) {
2018-11-06 05:53:27 +01:00
if ( value . isSpawnable ( ) & & value . isAlive ( ) & & ! value . toString ( ) . contains ( " ARMOR " ) ) {
2018-11-06 04:33:10 +01:00
mobFile . getConfig ( ) . addDefault ( " Mobs. " + value . name ( ) + " .Enabled " , true ) ;
2019-04-03 19:31:23 +02:00
mobFile . getConfig ( ) . addDefault ( " Mobs. " + value . name ( ) + " .Display Name " , Methods . formatText ( value . name ( ) . toLowerCase ( ) . replace ( " _ " , " " ) , true ) ) ;
2018-11-06 04:33:10 +01:00
mobFile . getConfig ( ) . addDefault ( " Mobs. " + value . name ( ) + " .Max Stack Size " , - 1 ) ;
}
}
mobFile . getConfig ( ) . options ( ) . copyDefaults ( true ) ;
mobFile . saveConfig ( ) ;
for ( Material value : Material . values ( ) ) {
2019-04-11 08:54:59 +02:00
itemFile . getConfig ( ) . addDefault ( " Items. " + value . name ( ) + " .Has Hologram " , true ) ;
itemFile . getConfig ( ) . addDefault ( " Items. " + value . name ( ) + " .Max Stack Size " , - 1 ) ;
itemFile . getConfig ( ) . addDefault ( " Items. " + value . name ( ) + " .Display Name " , Methods . formatText ( value . name ( ) . toLowerCase ( ) . replace ( " _ " , " " ) , true ) ) ;
2018-11-06 04:33:10 +01:00
}
itemFile . getConfig ( ) . options ( ) . copyDefaults ( true ) ;
itemFile . saveConfig ( ) ;
for ( EntityType value : EntityType . values ( ) ) {
2018-11-06 05:53:27 +01:00
if ( value . isSpawnable ( ) & & value . isAlive ( ) & & ! value . toString ( ) . contains ( " ARMOR " ) ) {
2018-11-06 04:33:10 +01:00
spawnerFile . getConfig ( ) . addDefault ( " Spawners. " + value . name ( ) + " .Max Stack Size " , - 1 ) ;
2019-01-10 21:27:01 +01:00
spawnerFile . getConfig ( ) . addDefault ( " Spawners. " + value . name ( ) + " .Display Name " , Methods . formatText ( value . name ( ) . toLowerCase ( ) . replace ( " _ " , " " ) , true ) ) ;
2018-11-06 04:33:10 +01:00
}
}
spawnerFile . getConfig ( ) . options ( ) . copyDefaults ( true ) ;
spawnerFile . saveConfig ( ) ;
2019-07-19 04:19:33 +02:00
new Locale ( this , " en_US " ) ;
this . locale = Locale . getLocale ( getConfig ( ) . getString ( " System.Language Mode " ) ) ;
2018-11-06 04:33:10 +01:00
2019-04-26 10:06:18 +02:00
//Running Songoda Updater
Plugin plugin = new Plugin ( this , 16 ) ;
plugin . addModule ( new LocaleModule ( ) ) ;
SongodaUpdate . load ( plugin ) ;
2018-11-06 04:33:10 +01:00
this . spawnerStackManager = new SpawnerStackManager ( ) ;
this . entityStackManager = new EntityStackManager ( ) ;
this . stackingTask = new StackingTask ( this ) ;
2019-01-23 19:01:31 +01:00
PluginManager pluginManager = Bukkit . getPluginManager ( ) ;
2019-01-10 22:45:13 +01:00
if ( isServerVersionAtLeast ( ServerVersion . V1_10 ) )
2019-06-07 19:48:17 +02:00
pluginManager . registerEvents ( new BreedListeners ( this ) , this ) ;
pluginManager . registerEvents ( new BlockListeners ( this ) , this ) ;
pluginManager . registerEvents ( new DeathListeners ( this ) , this ) ;
pluginManager . registerEvents ( new ShearListeners ( this ) , this ) ;
pluginManager . registerEvents ( new InteractListeners ( this ) , this ) ;
pluginManager . registerEvents ( new EntityListeners ( this ) , this ) ;
pluginManager . registerEvents ( new ItemListeners ( this ) , this ) ;
pluginManager . registerEvents ( new TameListeners ( this ) , this ) ;
2019-07-17 14:56:50 +02:00
pluginManager . registerEvents ( new SpawnerListeners ( this ) , this ) ;
2019-06-07 19:48:17 +02:00
pluginManager . registerEvents ( new SheepDyeListeners ( this ) , this ) ;
2018-11-06 04:33:10 +01:00
2019-06-20 09:33:29 +02:00
if ( Setting . CLEAR_LAG . getBoolean ( ) & & pluginManager . isPluginEnabled ( " ClearLag " ) )
pluginManager . registerEvents ( new ClearLagListeners ( this ) , this ) ;
2019-01-23 19:01:31 +01:00
// Register Hologram Plugin
2019-06-07 19:48:17 +02:00
if ( Setting . SPAWNER_HOLOGRAMS . getBoolean ( ) ) {
2019-01-27 21:33:28 +01:00
if ( pluginManager . isPluginEnabled ( " HolographicDisplays " ) )
hologram = new HologramHolographicDisplays ( this ) ;
}
2018-11-06 04:33:10 +01:00
2019-06-10 02:47:29 +02:00
// Register Hooks
if ( pluginManager . isPluginEnabled ( " Jobs " ) ) {
stackerHooks . add ( new JobsHook ( ) ) ;
}
2019-03-07 20:00:13 +01:00
// Starting Metrics
new Metrics ( this ) ;
2019-08-02 15:59:10 +02:00
// Legacy Data
Bukkit . getScheduler ( ) . runTaskLater ( this , ( ) - > {
File folder = getDataFolder ( ) ;
File dataFile = new File ( folder , " data.yml " ) ;
if ( dataFile . exists ( ) ) {
Storage storage = new StorageYaml ( this ) ;
if ( storage . containsGroup ( " spawners " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " spawners " ) ) {
try {
Location location = Methods . unserializeLocation ( row . getKey ( ) ) ;
SpawnerStack stack = new SpawnerStack (
location ,
row . get ( " amount " ) . asInt ( ) ) ;
getDataManager ( ) . createSpawner ( stack ) ;
} catch ( Exception e ) {
console . sendMessage ( " Failed to load spawner. " ) ;
e . printStackTrace ( ) ;
}
}
}
dataFile . delete ( ) ;
}
} , 10 ) ;
// Database stuff, go!
try {
if ( Setting . MYSQL_ENABLED . getBoolean ( ) ) {
String hostname = Setting . MYSQL_HOSTNAME . getString ( ) ;
int port = Setting . MYSQL_PORT . getInt ( ) ;
String database = Setting . MYSQL_DATABASE . getString ( ) ;
String username = Setting . MYSQL_USERNAME . getString ( ) ;
String password = Setting . MYSQL_PASSWORD . getString ( ) ;
boolean useSSL = Setting . MYSQL_USE_SSL . getBoolean ( ) ;
this . databaseConnector = new MySQLConnector ( this , hostname , port , database , username , password , useSSL ) ;
this . getLogger ( ) . info ( " Data handler connected using MySQL. " ) ;
} else {
this . databaseConnector = new SQLiteConnector ( this ) ;
this . getLogger ( ) . info ( " Data handler connected using SQLite. " ) ;
}
} catch ( Exception ex ) {
this . getLogger ( ) . severe ( " Fatal error trying to connect to database. Please make sure all your connection settings are correct and try again. Plugin has been disabled. " ) ;
Bukkit . getPluginManager ( ) . disablePlugin ( this ) ;
}
this . dataManager = new DataManager ( this . databaseConnector , this ) ;
this . dataMigrationManager = new DataMigrationManager ( this . databaseConnector , this . dataManager ) ;
this . dataMigrationManager . runMigrations ( ) ;
Bukkit . getScheduler ( ) . runTaskLater ( this , ( ) - > {
this . dataManager . getSpawners ( ( spawners ) - > {
this . spawnerStackManager . addSpawners ( spawners ) ;
2019-08-04 02:51:28 +02:00
if ( hologram ! = null )
this . hologram . loadHolograms ( ) ;
2019-08-02 15:59:10 +02:00
} ) ;
} , 20L ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
2018-11-06 04:33:10 +01:00
}
2019-06-10 02:47:29 +02:00
public void addExp ( Player player , EntityStack stack ) {
for ( StackerHook stackerHook : stackerHooks ) {
stackerHook . applyExperience ( player , stack ) ;
}
}
2018-11-06 04:33:10 +01:00
public void reload ( ) {
2019-07-19 04:19:33 +02:00
this . locale = Locale . getLocale ( getConfig ( ) . getString ( " System.Language Mode " ) ) ;
2018-11-06 04:33:10 +01:00
this . locale . reloadMessages ( ) ;
2019-07-31 06:29:10 +02:00
this . entityUtils = new EntityUtils ( ) ;
this . stackingTask . cancel ( ) ;
this . stackingTask = new StackingTask ( this ) ;
2018-11-06 04:33:10 +01:00
this . mobFile = new ConfigWrapper ( this , " " , " mobs.yml " ) ;
this . itemFile = new ConfigWrapper ( this , " " , " items.yml " ) ;
this . spawnerFile = new ConfigWrapper ( this , " " , " spawners.yml " ) ;
2019-05-23 22:15:04 +02:00
this . settingsManager . reloadConfig ( ) ;
2019-07-23 16:55:49 +02:00
this . getLootablesManager ( ) . getLootManager ( ) . loadLootables ( ) ;
2018-11-06 04:33:10 +01:00
}
2018-11-08 04:26:38 +01:00
public boolean spawnersEnabled ( ) {
2019-06-07 19:48:17 +02:00
return ! this . getServer ( ) . getPluginManager ( ) . isPluginEnabled ( " EpicSpawners " ) & & Setting . SPAWNERS_ENABLED . getBoolean ( ) ;
2018-11-08 04:26:38 +01:00
}
2019-06-20 09:33:29 +02:00
public Hologram getHologram ( ) {
return hologram ;
}
2018-11-06 05:41:58 +01:00
public ServerVersion getServerVersion ( ) {
return serverVersion ;
}
public boolean isServerVersion ( ServerVersion version ) {
return serverVersion = = version ;
}
public boolean isServerVersion ( ServerVersion . . . versions ) {
return ArrayUtils . contains ( versions , serverVersion ) ;
}
public boolean isServerVersionAtLeast ( ServerVersion version ) {
return serverVersion . ordinal ( ) > = version . ordinal ( ) ;
}
2018-11-06 04:33:10 +01:00
public Locale getLocale ( ) {
return locale ;
}
public CommandManager getCommandManager ( ) {
return commandManager ;
}
2019-07-23 16:55:49 +02:00
public LootablesManager getLootablesManager ( ) {
return lootablesManager ;
2019-07-08 18:39:57 +02:00
}
2018-11-06 04:33:10 +01:00
public EntityStackManager getEntityStackManager ( ) {
return entityStackManager ;
}
public SpawnerStackManager getSpawnerStackManager ( ) {
return spawnerStackManager ;
}
public StackingTask getStackingTask ( ) {
return stackingTask ;
}
2018-11-09 22:26:23 +01:00
public SettingsManager getSettingsManager ( ) {
return settingsManager ;
}
2018-11-06 04:33:10 +01:00
public ConfigWrapper getMobFile ( ) {
return mobFile ;
}
public ConfigWrapper getItemFile ( ) {
return itemFile ;
}
2018-11-06 05:53:27 +01:00
public ConfigWrapper getSpawnerFile ( ) {
return spawnerFile ;
}
2019-07-31 06:29:10 +02:00
public EntityUtils getEntityUtils ( ) {
return entityUtils ;
}
2019-08-02 15:59:10 +02:00
public DatabaseConnector getDatabaseConnector ( ) {
return databaseConnector ;
}
public DataManager getDataManager ( ) {
return dataManager ;
}
2018-11-06 04:33:10 +01:00
}