Minor refactoring to improve code style and readability of main class

Still very hard to read and understand and very long - But at least a bit more consistent.

I've marked two unused methods as deprecated instead of removing them in case some
third-party plugin is using them.
This commit is contained in:
Christian Koop 2023-03-29 20:43:35 +02:00
parent 9847c567df
commit f65cefe388
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
1 changed files with 109 additions and 111 deletions

View File

@ -15,7 +15,17 @@ import com.songoda.core.hooks.HologramManager;
import com.songoda.core.utils.TextUtils; import com.songoda.core.utils.TextUtils;
import com.songoda.ultimatekits.category.Category; import com.songoda.ultimatekits.category.Category;
import com.songoda.ultimatekits.category.CategoryManager; import com.songoda.ultimatekits.category.CategoryManager;
import com.songoda.ultimatekits.commands.*; import com.songoda.ultimatekits.commands.CommandCategories;
import com.songoda.ultimatekits.commands.CommandCrate;
import com.songoda.ultimatekits.commands.CommandCreatekit;
import com.songoda.ultimatekits.commands.CommandEdit;
import com.songoda.ultimatekits.commands.CommandKey;
import com.songoda.ultimatekits.commands.CommandKit;
import com.songoda.ultimatekits.commands.CommandPreviewKit;
import com.songoda.ultimatekits.commands.CommandReload;
import com.songoda.ultimatekits.commands.CommandRemove;
import com.songoda.ultimatekits.commands.CommandSet;
import com.songoda.ultimatekits.commands.CommandSettings;
import com.songoda.ultimatekits.conversion.Convert; import com.songoda.ultimatekits.conversion.Convert;
import com.songoda.ultimatekits.crate.Crate; import com.songoda.ultimatekits.crate.Crate;
import com.songoda.ultimatekits.crate.CrateManager; import com.songoda.ultimatekits.crate.CrateManager;
@ -57,7 +67,6 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class UltimateKits extends SongodaPlugin { public class UltimateKits extends SongodaPlugin {
private static UltimateKits INSTANCE; private static UltimateKits INSTANCE;
private final Config kitConfig = new Config(this, "kit.yml"); private final Config kitConfig = new Config(this, "kit.yml");
@ -77,16 +86,10 @@ public class UltimateKits extends SongodaPlugin {
private CategoryManager categoryManager; private CategoryManager categoryManager;
private DatabaseConnector databaseConnector; private DatabaseConnector databaseConnector;
private DataMigrationManager dataMigrationManager;
private DataManager dataManager; private DataManager dataManager;
private boolean loaded = false; private boolean loaded = false;
/**
* Grab instance of UltimateKits
*
* @return instance of UltimateKits
*/
public static UltimateKits getInstance() { public static UltimateKits getInstance() {
return INSTANCE; return INSTANCE;
} }
@ -117,30 +120,30 @@ public class UltimateKits extends SongodaPlugin {
this.crateManager = new CrateManager(); this.crateManager = new CrateManager();
this.categoryManager = new CategoryManager(this); this.categoryManager = new CategoryManager(this);
kitConfig.load(); this.kitConfig.load();
Convert.runKitConversions(); Convert.runKitConversions();
categoryConfig.load(); this.categoryConfig.load();
// load kits // load kits
dataFile.load(); this.dataFile.load();
keyFile.load(); this.keyFile.load();
crateFile.load(); this.crateFile.load();
checkKeyDefaults(); checkKeyDefaults();
checkCrateDefaults(); checkCrateDefaults();
keyFile.saveChanges(); this.keyFile.saveChanges();
crateFile.saveChanges(); this.crateFile.saveChanges();
// setup commands // setup commands
this.commandManager = new CommandManager(this); this.commandManager = new CommandManager(this);
this.commandManager.addCommand(new CommandKit(this, guiManager)); this.commandManager.addCommand(new CommandKit(this, this.guiManager));
this.commandManager.addCommand(new CommandPreviewKit(this, guiManager)); this.commandManager.addCommand(new CommandPreviewKit(this, this.guiManager));
this.commandManager.addMainCommand("KitAdmin") this.commandManager.addMainCommand("KitAdmin")
.addSubCommand(new CommandReload(this)) .addSubCommand(new CommandReload(this))
.addSubCommand(new CommandSettings(this, guiManager)) .addSubCommand(new CommandSettings(this, this.guiManager))
.addSubCommand(new CommandCreatekit(this, guiManager)) .addSubCommand(new CommandCreatekit(this, this.guiManager))
.addSubCommand(new CommandCategories(this, guiManager)) .addSubCommand(new CommandCategories(this, this.guiManager))
.addSubCommand(new CommandEdit(this, guiManager)) .addSubCommand(new CommandEdit(this, this.guiManager))
.addSubCommand(new CommandKey(this)) .addSubCommand(new CommandKey(this))
.addSubCommand(new CommandSet(this)) .addSubCommand(new CommandSet(this))
.addSubCommand(new CommandRemove(this)) .addSubCommand(new CommandRemove(this))
@ -149,13 +152,13 @@ public class UltimateKits extends SongodaPlugin {
// Event registration // Event registration
this.guiManager.init();
PluginManager pluginManager = getServer().getPluginManager(); PluginManager pluginManager = getServer().getPluginManager();
guiManager.init();
pluginManager.registerEvents(new BlockListeners(this), this); pluginManager.registerEvents(new BlockListeners(this), this);
pluginManager.registerEvents(new ChunkListeners(this), this); pluginManager.registerEvents(new ChunkListeners(this), this);
pluginManager.registerEvents(new ChatListeners(), this); pluginManager.registerEvents(new ChatListeners(), this);
pluginManager.registerEvents(new EntityListeners(this), this); pluginManager.registerEvents(new EntityListeners(this), this);
pluginManager.registerEvents(new InteractListeners(this, guiManager), this); pluginManager.registerEvents(new InteractListeners(this, this.guiManager), this);
pluginManager.registerEvents(new PlayerListeners(), this); pluginManager.registerEvents(new PlayerListeners(), this);
try { try {
@ -176,11 +179,10 @@ public class UltimateKits extends SongodaPlugin {
} }
this.dataManager = new DataManager(this.databaseConnector, this); this.dataManager = new DataManager(this.databaseConnector, this);
this.dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager, DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
new _1_InitialMigration(), new _1_InitialMigration(),
new _2_DuplicateMigration(this.databaseConnector instanceof SQLiteConnector)); new _2_DuplicateMigration(this.databaseConnector instanceof SQLiteConnector));
this.dataMigrationManager.runMigrations(); dataMigrationManager.runMigrations();
} catch (Exception ex) { } catch (Exception ex) {
this.getLogger().severe("Fatal error trying to connect to database. " + 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."); "Please make sure all your connection settings are correct and try again. Plugin has been disabled.");
@ -188,17 +190,17 @@ public class UltimateKits extends SongodaPlugin {
return; return;
} }
displayItemHandler.start(); this.displayItemHandler.start();
particleHandler.start(); this.particleHandler.start();
} }
@Override @Override
public void onDataLoad() { public void onDataLoad() {
//Empty categories from manager. // Empty categories from manager
categoryManager.clearCategories(); categoryManager.clearCategories();
/* /*
* Register categories into CategoryManager from Configuration. * Register categories into CategoryManager from Configuration
*/ */
if (categoryConfig.getConfigurationSection("Categories") != null) { if (categoryConfig.getConfigurationSection("Categories") != null) {
for (String key : categoryConfig.getConfigurationSection("Categories").getKeys(false)) { for (String key : categoryConfig.getConfigurationSection("Categories").getKeys(false)) {
@ -214,11 +216,11 @@ public class UltimateKits extends SongodaPlugin {
} }
} }
//Empty kits from manager. // Empty kits from manager.
kitManager.clearKits(); kitManager.clearKits();
/* /*
* Register kits into KitManager from Configuration. * Register kits into KitManager from Configuration
*/ */
if (kitConfig.getConfigurationSection("Kits") != null) { if (kitConfig.getConfigurationSection("Kits") != null) {
for (String kitName : kitConfig.getConfigurationSection("Kits").getKeys(false)) { for (String kitName : kitConfig.getConfigurationSection("Kits").getKeys(false)) {
@ -254,7 +256,7 @@ public class UltimateKits extends SongodaPlugin {
} }
/* /*
* Register legacy kit locations into KitManager from Configuration. * Register legacy kit locations into KitManager from Configuration
*/ */
if (dataFile.contains("BlockData")) { if (dataFile.contains("BlockData")) {
for (String key : dataFile.getConfigurationSection("BlockData").getKeys(false)) { for (String key : dataFile.getConfigurationSection("BlockData").getKeys(false)) {
@ -275,7 +277,7 @@ public class UltimateKits extends SongodaPlugin {
} }
/* /*
* Register kit locations into KitManager from Configuration. * Register kit locations into KitManager from Configuration
*/ */
Bukkit.getScheduler().runTaskLater(this, () -> Bukkit.getScheduler().runTaskLater(this, () ->
this.dataManager.getBlockData((blockData) -> { this.dataManager.getBlockData((blockData) -> {
@ -287,16 +289,16 @@ public class UltimateKits extends SongodaPlugin {
} }
}), 20L); }), 20L);
//Apply default keys. // Apply default keys
checkKeyDefaults(); checkKeyDefaults();
checkCrateDefaults(); checkCrateDefaults();
//Empty keys from manager. // Empty keys from manager
keyManager.clear(); keyManager.clear();
crateManager.clear(); crateManager.clear();
/* /*
* Register keys into KitManager from Configuration. * Register keys into KitManager from Configuration
*/ */
if (keyFile.contains("Keys")) { if (keyFile.contains("Keys")) {
for (String keyName : keyFile.getConfigurationSection("Keys").getKeys(false)) { for (String keyName : keyFile.getConfigurationSection("Keys").getKeys(false)) {
@ -311,7 +313,7 @@ public class UltimateKits extends SongodaPlugin {
/* /*
* Register Crates * Register Crates
* */ */
if (crateFile.contains("Crates")) { if (crateFile.contains("Crates")) {
for (String crateName : crateFile.getConfigurationSection("Crates").getKeys(false)) { for (String crateName : crateFile.getConfigurationSection("Crates").getKeys(false)) {
int amt = crateFile.getInt("Crates." + crateName + ".Item Amount"); int amt = crateFile.getInt("Crates." + crateName + ".Item Amount");
@ -327,26 +329,26 @@ public class UltimateKits extends SongodaPlugin {
@Override @Override
public void onPluginDisable() { public void onPluginDisable() {
saveKits(false); saveKits(false);
dataFile.save(); this.dataFile.save();
dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations()); this.dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations());
kitManager.clearKits(); this.kitManager.clearKits();
HologramManager.removeAllHolograms(); HologramManager.removeAllHolograms();
} }
@Override @Override
public List<Config> getExtraConfig() { public List<Config> getExtraConfig() {
return Arrays.asList(kitConfig, keyFile, categoryConfig, crateFile); return Arrays.asList(this.kitConfig, this.keyFile, this.categoryConfig, this.crateFile);
} }
@Override @Override
public void onConfigReload() { public void onConfigReload() {
setLocale(Settings.LANGUGE_MODE.getString(), true); setLocale(Settings.LANGUGE_MODE.getString(), true);
dataManager.bulkUpdateBlockData(getKitManager().getKitLocations()); this.dataManager.bulkUpdateBlockData(getKitManager().getKitLocations());
kitConfig.load(); this.kitConfig.load();
categoryConfig.load(); this.categoryConfig.load();
keyFile.load(); this.keyFile.load();
crateFile.load(); this.crateFile.load();
onDataLoad(); onDataLoad();
} }
@ -376,6 +378,7 @@ public class UltimateKits extends SongodaPlugin {
if (data.isDisplayingItems()) { if (data.isDisplayingItems()) {
multi += .25; multi += .25;
} }
Material type = location.getBlock().getType(); Material type = location.getBlock().getType();
if (type == Material.TRAPPED_CHEST if (type == Material.TRAPPED_CHEST
|| type == Material.CHEST || type == Material.CHEST
@ -383,6 +386,7 @@ public class UltimateKits extends SongodaPlugin {
|| type == Material.ENDER_CHEST) { || type == Material.ENDER_CHEST) {
multi -= .10; multi -= .10;
} }
location.add(0, multi, 0); location.add(0, multi, 0);
return location; return location;
} }
@ -408,15 +412,12 @@ public class UltimateKits extends SongodaPlugin {
private List<String> formatHologram(KitBlockData data) { private List<String> formatHologram(KitBlockData data) {
getDataManager().updateBlockData(data); getDataManager().updateBlockData(data);
KitType kitType = data.getType();
ArrayList<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
List<String> order = Settings.HOLOGRAM_LAYOUT.getStringList();
Kit kit = data.getKit(); Kit kit = data.getKit();
KitType kitType = data.getType();
for (String o : order) { for (String o : Settings.HOLOGRAM_LAYOUT.getStringList()) {
switch (o.toUpperCase()) { switch (o.toUpperCase()) {
case "{TITLE}": case "{TITLE}":
String title = kit.getTitle(); String title = kit.getTitle();
@ -426,6 +427,7 @@ public class UltimateKits extends SongodaPlugin {
lines.add(ChatColor.DARK_PURPLE + TextUtils.formatText(title)); lines.add(ChatColor.DARK_PURPLE + TextUtils.formatText(title));
} }
break; break;
case "{RIGHT-CLICK}": case "{RIGHT-CLICK}":
if (kitType == KitType.CRATE) { if (kitType == KitType.CRATE) {
lines.add(getLocale().getMessage("interface.hologram.crate").getMessage()); lines.add(getLocale().getMessage("interface.hologram.crate").getMessage());
@ -443,6 +445,7 @@ public class UltimateKits extends SongodaPlugin {
.getMessage()); .getMessage());
} }
break; break;
case "{LEFT-CLICK}": case "{LEFT-CLICK}":
if (kitType == KitType.CLAIM) { if (kitType == KitType.CLAIM) {
lines.add(getLocale().getMessage("interface.hologram.daily").getMessage()); lines.add(getLocale().getMessage("interface.hologram.daily").getMessage());
@ -454,6 +457,7 @@ public class UltimateKits extends SongodaPlugin {
lines.add(getLocale().getMessage("interface.hologram.preview").getMessage()); lines.add(getLocale().getMessage("interface.hologram.preview").getMessage());
} }
break; break;
default: default:
lines.add(ChatColor.translateAlternateColorCodes('&', o)); lines.add(ChatColor.translateAlternateColorCodes('&', o));
break; break;
@ -463,26 +467,26 @@ public class UltimateKits extends SongodaPlugin {
return lines; return lines;
} }
/* /**
* Saves registered kits to file. * Saves registered kits to file
*/ */
public void saveKits(boolean force) { public void saveKits(boolean force) {
if (!loaded && !force) return; if (!loaded && !force) return;
// If we're changing the order the file needs to be wiped. // If we're changing the order the file needs to be wiped
if (kitManager.hasOrderChanged()) { if (kitManager.hasOrderChanged()) {
kitConfig.clearConfig(true); kitConfig.clearConfig(true);
kitManager.savedOrderChange(); kitManager.savedOrderChange();
} }
// Hot fix for kit file resets. // Hot fix for kit file resets
if (kitConfig.contains("Kits")) if (kitConfig.contains("Kits"))
for (String kitName : kitConfig.getConfigurationSection("Kits").getKeys(false)) { for (String kitName : kitConfig.getConfigurationSection("Kits").getKeys(false)) {
if (kitManager.getKits().stream().noneMatch(kit -> kit.getKey().equals(kitName))) if (kitManager.getKits().stream().noneMatch(kit -> kit.getKey().equals(kitName)))
kitConfig.set("Kits." + kitName, null); kitConfig.set("Kits." + kitName, null);
} }
// Hot fix for category file resets. // Hot fix for category file resets
if (categoryConfig.contains("Categories")) if (categoryConfig.contains("Categories"))
for (String key : categoryConfig.getConfigurationSection("Categories").getKeys(false)) { for (String key : categoryConfig.getConfigurationSection("Categories").getKeys(false)) {
if (categoryManager.getCategories().stream().noneMatch(category -> category.getKey().equals(key))) if (categoryManager.getCategories().stream().noneMatch(category -> category.getKey().equals(key)))
@ -490,7 +494,7 @@ public class UltimateKits extends SongodaPlugin {
} }
/* /*
* Save kits from KitManager to Configuration. * Save kits from KitManager to Configuration
*/ */
for (Kit kit : kitManager.getKits()) { for (Kit kit : kitManager.getKits()) {
kitConfig.set("Kits." + kit.getKey() + ".delay", kit.getDelay()); kitConfig.set("Kits." + kit.getKey() + ".delay", kit.getDelay());
@ -515,7 +519,7 @@ public class UltimateKits extends SongodaPlugin {
} }
/* /*
* Save categories from CategoryManager to Configuration. * Save categories from CategoryManager to Configuration
*/ */
for (Category category : categoryManager.getCategories()) { for (Category category : categoryManager.getCategories()) {
categoryConfig.set("Categories." + category.getKey() + ".name", category.getName()); categoryConfig.set("Categories." + category.getKey() + ".name", category.getName());
@ -527,93 +531,87 @@ public class UltimateKits extends SongodaPlugin {
categoryConfig.saveChanges(); categoryConfig.saveChanges();
} }
/* /**
* Insert default key list into config. * Insert default key list into config
*/ */
private void checkKeyDefaults() { private void checkKeyDefaults() {
if (keyFile.contains("Keys")) return; if (this.keyFile.contains("Keys")) {
keyFile.set("Keys.Regular.Item Amount", 3); return;
keyFile.set("Keys.Regular.Amount overrides", Collections.singletonList("Tools:2")); }
keyFile.set("Keys.Regular.Amount of kit received", 1);
keyFile.set("Keys.Ultra.Item Amount", -1); this.keyFile.set("Keys.Regular.Item Amount", 3);
keyFile.set("Keys.Ultra.Amount of kit received", 1); this.keyFile.set("Keys.Regular.Amount overrides", Collections.singletonList("Tools:2"));
keyFile.set("Keys.Insane.Item Amount", -1); this.keyFile.set("Keys.Regular.Amount of kit received", 1);
keyFile.set("Keys.Insane.Amount of kit received", 2); this.keyFile.set("Keys.Ultra.Item Amount", -1);
keyFile.set("Keys.Insane.Enchanted", true); this.keyFile.set("Keys.Ultra.Amount of kit received", 1);
this.keyFile.set("Keys.Insane.Item Amount", -1);
this.keyFile.set("Keys.Insane.Amount of kit received", 2);
this.keyFile.set("Keys.Insane.Enchanted", true);
} }
private void checkCrateDefaults() { private void checkCrateDefaults() {
if (crateFile.contains("Crates")) return; if (this.crateFile.contains("Crates")) {
crateFile.set("Crates.Regular.Item Amount", 3); return;
crateFile.set("Crates.Regular.Amount overrides", Collections.singletonList("Tools:2")); }
crateFile.set("Crates.Regular.Amount of kit received", 1);
crateFile.set("Crates.Ultra.Item Amount", -1); this.crateFile.set("Crates.Regular.Item Amount", 3);
crateFile.set("Crates.Ultra.Amount of kit received", 1); this.crateFile.set("Crates.Regular.Amount overrides", Collections.singletonList("Tools:2"));
crateFile.set("Crates.Insane.Item Amount", -1); this.crateFile.set("Crates.Regular.Amount of kit received", 1);
crateFile.set("Crates.Insane.Amount of kit received", 2); this.crateFile.set("Crates.Ultra.Item Amount", -1);
this.crateFile.set("Crates.Ultra.Amount of kit received", 1);
this.crateFile.set("Crates.Insane.Item Amount", -1);
this.crateFile.set("Crates.Insane.Amount of kit received", 2);
} }
/**
* Get instance of KitManager
*
* @return instance of KitManager
*/
public KitManager getKitManager() { public KitManager getKitManager() {
return kitManager; return this.kitManager;
} }
/**
* Get instance of KeyManager
*
* @return instance of KeyManager
*/
public KeyManager getKeyManager() { public KeyManager getKeyManager() {
return keyManager; return this.keyManager;
} }
public CrateManager getCrateManager() { public CrateManager getCrateManager() {
return crateManager; return this.crateManager;
} }
/**
* Grab instance of Kit File Configuration Wrapper
*
* @return instance of KitFile
*/
public Config getKitConfig() { public Config getKitConfig() {
return kitConfig; return this.kitConfig;
}
public Config getDataFile() {
return this.dataFile;
} }
/** /**
* Grab instance of Data File Configuration Wrapper * @deprecated Will be made private or removed completely in the future.
*
* @return instance of DataFile
*/ */
public Config getDataFile() { @Deprecated
return dataFile;
}
public CommandManager getCommandManager() { public CommandManager getCommandManager() {
return commandManager; return this.commandManager;
} }
public GuiManager getGuiManager() { public GuiManager getGuiManager() {
return guiManager; return this.guiManager;
} }
public DisplayItemHandler getDisplayItemHandler() { public DisplayItemHandler getDisplayItemHandler() {
return displayItemHandler; return this.displayItemHandler;
} }
/**
* @deprecated Will be made private or removed completely in the future.
*/
@Deprecated
public DatabaseConnector getDatabaseConnector() { public DatabaseConnector getDatabaseConnector() {
return databaseConnector; return this.databaseConnector;
} }
public DataManager getDataManager() { public DataManager getDataManager() {
return dataManager; return this.dataManager;
} }
public CategoryManager getCategoryManager() { public CategoryManager getCategoryManager() {
return categoryManager; return this.categoryManager;
} }
} }