2018-11-06 04:33:10 +01:00
package com.songoda.ultimatestacker ;
import com.songoda.ultimatestacker.command.CommandManager ;
import com.songoda.ultimatestacker.entity.EntityStack ;
2018-11-06 06:09:40 +01:00
import com.songoda.ultimatestacker.entity.EntityStackManager ;
2018-11-06 04:33:10 +01:00
import com.songoda.ultimatestacker.events.* ;
import com.songoda.ultimatestacker.handlers.HologramHandler ;
import com.songoda.ultimatestacker.spawner.SpawnerStack ;
import com.songoda.ultimatestacker.spawner.SpawnerStackManager ;
import com.songoda.ultimatestacker.storage.Storage ;
import com.songoda.ultimatestacker.storage.StorageItem ;
import com.songoda.ultimatestacker.storage.StorageRow ;
import com.songoda.ultimatestacker.storage.types.StorageMysql ;
import com.songoda.ultimatestacker.storage.types.StorageYaml ;
import com.songoda.ultimatestacker.tasks.StackingTask ;
2019-01-10 21:27:01 +01:00
import com.songoda.ultimatestacker.utils.ConfigWrapper ;
import com.songoda.ultimatestacker.utils.Methods ;
2018-11-06 05:41:58 +01:00
import com.songoda.ultimatestacker.utils.ServerVersion ;
2018-11-06 04:33:10 +01:00
import com.songoda.ultimatestacker.utils.SettingsManager ;
2018-11-06 05:41:58 +01:00
import org.apache.commons.lang.ArrayUtils ;
2018-11-09 05:11:49 +01:00
import org.bukkit.* ;
2018-11-06 04:33:10 +01:00
import org.bukkit.command.ConsoleCommandSender ;
import org.bukkit.entity.Entity ;
import org.bukkit.entity.EntityType ;
import org.bukkit.plugin.java.JavaPlugin ;
import org.json.simple.JSONArray ;
import org.json.simple.JSONObject ;
import org.json.simple.parser.JSONParser ;
import java.io.InputStream ;
import java.io.InputStreamReader ;
import java.net.URL ;
import java.net.URLConnection ;
import java.util.UUID ;
public class UltimateStacker extends JavaPlugin {
private static UltimateStacker INSTANCE ;
private References references ;
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 ;
private CommandManager commandManager ;
private StackingTask stackingTask ;
private HologramHandler hologramHandler ;
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
private Storage storage ;
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 ( ) {
this . saveToFile ( ) ;
this . storage . closeConnection ( ) ;
ConsoleCommandSender console = Bukkit . getConsoleSender ( ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
console . sendMessage ( Methods . formatText ( " &7UltimateStacker " + this . getDescription ( ) . getVersion ( ) + " by &5Brianna <3! " ) ) ;
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
private boolean checkVersion ( ) {
int maxVersion = 12 ; // also supports 1.8 and higher
int currentVersion = Integer . parseInt ( Bukkit . getServer ( ) . getClass ( ) . getPackage ( ) . getName ( ) . split ( " \\ . " ) [ 3 ] . split ( " _ " ) [ 1 ] ) ;
if ( currentVersion > maxVersion ) {
Bukkit . getScheduler ( ) . scheduleSyncDelayedTask ( this , ( ) - > {
Bukkit . getConsoleSender ( ) . sendMessage ( " " ) ;
Bukkit . getConsoleSender ( ) . sendMessage ( ChatColor . RED + " You installed the legacy (1.8 - 1.12) only version of " + this . getDescription ( ) . getName ( ) + " on a 1. " + currentVersion + " server. Since you are on the wrong version we disabled the plugin for you. Please install correct version to continue using " + this . getDescription ( ) . getName ( ) + " . " ) ;
Bukkit . getConsoleSender ( ) . sendMessage ( " " ) ;
} , 20L ) ;
return false ;
}
return true ;
}
@Override
2018-11-06 04:33:10 +01:00
public void onEnable ( ) {
INSTANCE = this ;
2018-11-09 05:11:49 +01:00
// Check to make sure the Bukkit version is compatible.
if ( ! checkVersion ( ) ) return ;
2018-11-09 05:22:56 +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============================= " ) ) ;
console . sendMessage ( Methods . formatText ( " &7UltimateStacker " + this . getDescription ( ) . getVersion ( ) + " by &5Brianna <3&7! " ) ) ;
console . sendMessage ( Methods . formatText ( " &7Action: &aEnabling&7... " ) ) ;
2018-11-06 04:33:10 +01:00
this . settingsManager = new SettingsManager ( this ) ;
this . commandManager = new CommandManager ( this ) ;
settingsManager . updateSettings ( ) ;
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 ) ;
mobFile . getConfig ( ) . addDefault ( " Mobs. " + value . name ( ) + " .Max Stack Size " , - 1 ) ;
}
}
mobFile . getConfig ( ) . options ( ) . copyDefaults ( true ) ;
mobFile . saveConfig ( ) ;
for ( Material value : Material . values ( ) ) {
itemFile . getConfig ( ) . addDefault ( " Items. " + value . name ( ) + " .Has Hologram " , true ) ;
itemFile . getConfig ( ) . addDefault ( " Items. " + value . name ( ) + " .Max Stack Size " , - 1 ) ;
2019-01-10 21:27:01 +01:00
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 ( ) ;
getConfig ( ) . options ( ) . copyDefaults ( true ) ;
saveConfig ( ) ;
String langMode = getConfig ( ) . getString ( " System.Language Mode " ) ;
Locale . init ( this ) ;
Locale . saveDefaultLocale ( " en_US " ) ;
this . locale = Locale . getLocale ( getConfig ( ) . getString ( " System.Language Mode " , langMode ) ) ;
if ( getConfig ( ) . getBoolean ( " System.Download Needed Data Files " ) ) {
this . update ( ) ;
}
this . references = new References ( ) ;
this . spawnerStackManager = new SpawnerStackManager ( ) ;
this . entityStackManager = new EntityStackManager ( ) ;
this . stackingTask = new StackingTask ( this ) ;
this . stackingTask . startTask ( ) ;
checkStorage ( ) ;
Bukkit . getScheduler ( ) . runTaskLater ( this , ( ) - > {
if ( storage . containsGroup ( " entities " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " entities " ) ) {
Entity entity = getEntityByUniqueId ( UUID . fromString ( row . getKey ( ) ) ) ;
if ( entity = = null ) continue ;
EntityStack stack = new EntityStack (
entity ,
row . get ( " amount " ) . asInt ( ) ) ;
this . entityStackManager . addStack ( stack ) ;
}
}
if ( storage . containsGroup ( " spawners " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " spawners " ) ) {
try {
2019-01-10 21:27:01 +01:00
Location location = Methods . unserializeLocation ( row . getKey ( ) ) ;
2018-11-06 04:33:10 +01:00
if ( location . getWorld ( ) = = null | | ! location . getBlock ( ) . getType ( ) . name ( ) . contains ( " SPAWNER " ) ) {
if ( location . getWorld ( ) ! = null & & ! location . getBlock ( ) . getType ( ) . name ( ) . contains ( " SPAWNER " ) ) {
this . hologramHandler . despawn ( location . getBlock ( ) ) ;
}
}
SpawnerStack stack = new SpawnerStack (
location ,
row . get ( " amount " ) . asInt ( ) ) ;
this . spawnerStackManager . addSpawner ( stack ) ;
} catch ( Exception e ) {
console . sendMessage ( " Failed to load spawner. " ) ;
e . printStackTrace ( ) ;
}
}
}
for ( SpawnerStack stack : spawnerStackManager . getStacks ( ) ) {
2019-01-10 21:27:01 +01:00
storage . prepareSaveItem ( " spawners " , new StorageItem ( " location " , Methods . serializeLocation ( stack . getLocation ( ) ) ) ,
2018-11-06 04:33:10 +01:00
new StorageItem ( " amount " , stack . getAmount ( ) ) ) ;
}
// Save data initially so that if the person reloads again fast they don't lose all their data.
this . saveToFile ( ) ;
2019-01-10 21:27:01 +01:00
if ( Bukkit . getPluginManager ( ) . isPluginEnabled ( " Arconix " ) ) {
this . hologramHandler = new HologramHandler ( this ) ;
}
2018-11-06 04:33:10 +01:00
} , 10 ) ;
2019-01-10 22:45:13 +01:00
if ( isServerVersionAtLeast ( ServerVersion . V1_10 ) )
Bukkit . getPluginManager ( ) . registerEvents ( new BreedListeners ( this ) , this ) ;
2018-11-06 04:33:10 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new SpawnerListeners ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new BlockListeners ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new DeathListeners ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new ShearListeners ( this ) , this ) ;
2018-11-08 04:26:38 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new InteractListeners ( this ) , this ) ;
2018-11-25 22:28:58 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new InventoryListeners ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new EntityListeners ( this ) , this ) ;
2018-11-06 04:33:10 +01:00
Bukkit . getScheduler ( ) . runTaskTimerAsynchronously ( this , this : : saveToFile , 6000 , 6000 ) ;
2019-01-10 21:27:01 +01:00
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
2018-11-06 04:33:10 +01:00
}
private void update ( ) {
try {
URL url = new URL ( " http://update.songoda.com/index.php?plugin= " + getDescription ( ) . getName ( ) + " &version= " + getDescription ( ) . getVersion ( ) ) ;
URLConnection urlConnection = url . openConnection ( ) ;
InputStream is = urlConnection . getInputStream ( ) ;
InputStreamReader isr = new InputStreamReader ( is ) ;
int numCharsRead ;
char [ ] charArray = new char [ 1024 ] ;
StringBuffer sb = new StringBuffer ( ) ;
while ( ( numCharsRead = isr . read ( charArray ) ) > 0 ) {
sb . append ( charArray , 0 , numCharsRead ) ;
}
String jsonString = sb . toString ( ) ;
JSONObject json = ( JSONObject ) new JSONParser ( ) . parse ( jsonString ) ;
JSONArray files = ( JSONArray ) json . get ( " neededFiles " ) ;
for ( Object o : files ) {
JSONObject file = ( JSONObject ) o ;
switch ( ( String ) file . get ( " type " ) ) {
case " locale " :
InputStream in = new URL ( ( String ) file . get ( " link " ) ) . openStream ( ) ;
Locale . saveDefaultLocale ( in , ( String ) file . get ( " name " ) ) ;
break ;
}
}
} catch ( Exception e ) {
System . out . println ( " Failed to update. " ) ;
//e.printStackTrace();
}
}
private void checkStorage ( ) {
if ( getConfig ( ) . getBoolean ( " Database.Activate Mysql Support " ) ) {
this . storage = new StorageMysql ( this ) ;
} else {
this . storage = new StorageYaml ( this ) ;
}
}
private void saveToFile ( ) {
this . storage . closeConnection ( ) ;
checkStorage ( ) ;
for ( EntityStack stack : entityStackManager . getStacks ( ) . values ( ) ) {
storage . prepareSaveItem ( " entities " , new StorageItem ( " uuid " , stack . getEntity ( ) . getUniqueId ( ) . toString ( ) ) ,
new StorageItem ( " amount " , stack . getAmount ( ) ) ) ;
}
for ( SpawnerStack stack : spawnerStackManager . getStacks ( ) ) {
2019-01-10 21:27:01 +01:00
storage . prepareSaveItem ( " spawners " , new StorageItem ( " location " , Methods . serializeLocation ( stack . getLocation ( ) ) ) ,
2018-11-06 04:33:10 +01:00
new StorageItem ( " amount " , stack . getAmount ( ) ) ) ;
}
storage . doSave ( ) ;
}
public void reload ( ) {
2018-12-01 01:09:18 +01:00
settingsManager . updateSettings ( ) ;
2018-11-06 04:33:10 +01:00
String langMode = getConfig ( ) . getString ( " System.Language Mode " ) ;
this . locale = Locale . getLocale ( getConfig ( ) . getString ( " System.Language Mode " , langMode ) ) ;
this . locale . reloadMessages ( ) ;
this . mobFile = new ConfigWrapper ( this , " " , " mobs.yml " ) ;
this . itemFile = new ConfigWrapper ( this , " " , " items.yml " ) ;
this . spawnerFile = new ConfigWrapper ( this , " " , " spawners.yml " ) ;
this . references = new References ( ) ;
this . reloadConfig ( ) ;
}
public Entity getEntityByUniqueId ( UUID uniqueId ) {
for ( World world : Bukkit . getWorlds ( ) ) {
for ( Entity entity : world . getLivingEntities ( ) ) {
if ( entity . getUniqueId ( ) . equals ( uniqueId ) )
return entity ;
}
}
return null ;
}
2018-11-08 04:26:38 +01:00
public boolean spawnersEnabled ( ) {
if ( this . getServer ( ) . getPluginManager ( ) . isPluginEnabled ( " EpicSpawners " ) ) return false ;
return this . getConfig ( ) . getBoolean ( " Main.Stack Spawners " ) ;
}
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 HologramHandler getHologramHandler ( ) {
return hologramHandler ;
}
public References getReferences ( ) {
return references ;
}
public Locale getLocale ( ) {
return locale ;
}
public CommandManager getCommandManager ( ) {
return commandManager ;
}
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 ;
}
2018-11-06 04:33:10 +01:00
}