From 70faf51a980921cecbc5cc6115c34ce616b8d5ff Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 5 Oct 2019 16:02:31 -0400 Subject: [PATCH] SongodaCore update. --- pom.xml | 47 +- .../com/songoda/epichoppers/EpicHoppers.java | 177 ++--- .../epichoppers/command/AbstractCommand.java | 59 -- .../epichoppers/command/CommandManager.java | 89 --- .../epichoppers/command/TabManager.java | 51 -- .../{command => }/commands/CommandBook.java | 22 +- .../{command => }/commands/CommandBoost.java | 43 +- .../commands/CommandEpicHoppers.java | 21 +- .../{command => }/commands/CommandGive.java | 31 +- .../{command => }/commands/CommandReload.java | 18 +- .../commands/CommandSettings.java | 20 +- .../songoda/epichoppers/economy/Economy.java | 12 - .../economy/PlayerPointsEconomy.java | 38 - .../epichoppers/economy/ReserveEconomy.java | 32 - .../epichoppers/economy/VaultEconomy.java | 35 - .../songoda/epichoppers/gui/GUICrafting.java | 72 +- .../songoda/epichoppers/gui/GUIFilter.java | 111 ++- .../songoda/epichoppers/gui/GUIOverview.java | 239 +++--- .../epichoppers/handlers/TeleportHandler.java | 11 +- .../songoda/epichoppers/hopper/Hopper.java | 32 +- .../hopper/levels/modules/Module.java | 22 +- .../levels/modules/ModuleAutoCrafting.java | 62 +- .../hopper/levels/modules/ModuleAutoSell.java | 17 +- .../levels/modules/ModuleBlockBreak.java | 16 +- .../hopper/levels/modules/ModuleSuction.java | 5 +- .../epichoppers/listeners/BlockListeners.java | 6 +- .../listeners/HopperListeners.java | 21 +- .../listeners/InteractListeners.java | 2 +- .../epichoppers/settings/Settings.java | 121 +++ .../songoda/epichoppers/storage/Storage.java | 17 +- .../storage/types/StorageMysql.java | 197 ----- .../storage/types/StorageYaml.java | 40 +- .../songoda/epichoppers/tasks/HopTask.java | 4 +- .../epichoppers/utils/ConfigWrapper.java | 67 -- .../songoda/epichoppers/utils/Methods.java | 108 +-- .../songoda/epichoppers/utils/Metrics.java | 695 ------------------ .../epichoppers/utils/MySQLDatabase.java | 54 -- .../epichoppers/utils/ServerVersion.java | 27 - .../utils/StorageContainerCache.java | 38 +- .../epichoppers/utils/gui/AbstractGUI.java | 220 ------ .../epichoppers/utils/gui/Clickable.java | 11 - .../epichoppers/utils/gui/OnClose.java | 10 - .../songoda/epichoppers/utils/gui/Range.java | 51 -- .../epichoppers/utils/locale/Locale.java | 302 -------- .../epichoppers/utils/locale/Message.java | 115 --- .../epichoppers/utils/settings/Category.java | 25 - .../epichoppers/utils/settings/Setting.java | 173 ----- .../utils/settings/SettingsManager.java | 311 -------- .../utils/updateModules/LocaleModule.java | 33 - 49 files changed, 605 insertions(+), 3325 deletions(-) delete mode 100644 src/main/java/com/songoda/epichoppers/command/AbstractCommand.java delete mode 100644 src/main/java/com/songoda/epichoppers/command/CommandManager.java delete mode 100644 src/main/java/com/songoda/epichoppers/command/TabManager.java rename src/main/java/com/songoda/epichoppers/{command => }/commands/CommandBook.java (65%) rename src/main/java/com/songoda/epichoppers/{command => }/commands/CommandBoost.java (68%) rename src/main/java/com/songoda/epichoppers/{command => }/commands/CommandEpicHoppers.java (61%) rename src/main/java/com/songoda/epichoppers/{command => }/commands/CommandGive.java (76%) rename src/main/java/com/songoda/epichoppers/{command => }/commands/CommandReload.java (59%) rename src/main/java/com/songoda/epichoppers/{command => }/commands/CommandSettings.java (50%) delete mode 100644 src/main/java/com/songoda/epichoppers/economy/Economy.java delete mode 100644 src/main/java/com/songoda/epichoppers/economy/PlayerPointsEconomy.java delete mode 100644 src/main/java/com/songoda/epichoppers/economy/ReserveEconomy.java delete mode 100644 src/main/java/com/songoda/epichoppers/economy/VaultEconomy.java create mode 100644 src/main/java/com/songoda/epichoppers/settings/Settings.java delete mode 100644 src/main/java/com/songoda/epichoppers/storage/types/StorageMysql.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/ConfigWrapper.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/Metrics.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/ServerVersion.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/gui/AbstractGUI.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/gui/Clickable.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/gui/OnClose.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/gui/Range.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/locale/Locale.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/locale/Message.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/settings/Category.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/settings/Setting.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/settings/SettingsManager.java delete mode 100644 src/main/java/com/songoda/epichoppers/utils/updateModules/LocaleModule.java diff --git a/pom.xml b/pom.xml index 8223a28..141989c 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ false - com.songoda:songodaupdater + com.songoda:SongodaCore @@ -45,6 +45,12 @@ + + + com.songoda.core + ${project.groupId}.epichoppers.core + + @@ -73,13 +79,9 @@ com.songoda - songodaupdater - 1 - - - com.songoda - UltimateStacker - 1.9.6 + SongodaCore + LATEST + compile net.arcaniax @@ -87,23 +89,6 @@ 2.1.0 provided - - xyz.wildseries - wildstacker - b13 - provided - - - net.milkbowl - vault - 1.7.1 - provided - - - org.black_ixx - playerpoints - 2.1.4 - com.songoda abledskyblock @@ -122,9 +107,15 @@ 1.19.5 - net.tnemc - Reserve - 0.1.3.0 + com.songoda + UltimateStacker + 1.9.6 + provided + + + com.bgsoftware + WildStacker + 2-9-0 provided diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java index b72359c..74559f8 100644 --- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.java @@ -1,12 +1,16 @@ package com.songoda.epichoppers; +import com.songoda.core.SongodaCore; +import com.songoda.core.SongodaPlugin; +import com.songoda.core.commands.CommandManager; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.configuration.Config; +import com.songoda.core.gui.GuiManager; +import com.songoda.core.hooks.EconomyManager; +import com.songoda.core.locale.Locale; import com.songoda.epichoppers.boost.BoostData; import com.songoda.epichoppers.boost.BoostManager; -import com.songoda.epichoppers.command.CommandManager; -import com.songoda.epichoppers.economy.Economy; -import com.songoda.epichoppers.economy.PlayerPointsEconomy; -import com.songoda.epichoppers.economy.ReserveEconomy; -import com.songoda.epichoppers.economy.VaultEconomy; +import com.songoda.epichoppers.commands.*; import com.songoda.epichoppers.enchantment.Enchantment; import com.songoda.epichoppers.handlers.TeleportHandler; import com.songoda.epichoppers.hopper.Filter; @@ -14,63 +18,43 @@ import com.songoda.epichoppers.hopper.HopperBuilder; import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.Level; import com.songoda.epichoppers.hopper.levels.LevelManager; -import com.songoda.epichoppers.hopper.levels.modules.Module; -import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; -import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoSell; -import com.songoda.epichoppers.hopper.levels.modules.ModuleBlockBreak; -import com.songoda.epichoppers.hopper.levels.modules.ModuleSuction; +import com.songoda.epichoppers.hopper.levels.modules.*; import com.songoda.epichoppers.listeners.*; import com.songoda.epichoppers.player.PlayerDataManager; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.storage.Storage; import com.songoda.epichoppers.storage.StorageRow; -import com.songoda.epichoppers.storage.types.StorageMysql; import com.songoda.epichoppers.storage.types.StorageYaml; import com.songoda.epichoppers.tasks.HopTask; -import com.songoda.epichoppers.utils.*; -import com.songoda.epichoppers.utils.locale.Locale; -import com.songoda.epichoppers.utils.settings.Setting; -import com.songoda.epichoppers.utils.settings.SettingsManager; -import com.songoda.epichoppers.utils.updateModules.LocaleModule; -import com.songoda.update.Plugin; -import com.songoda.update.SongodaUpdate; -import org.apache.commons.lang.ArrayUtils; +import com.songoda.epichoppers.utils.Methods; +import com.songoda.epichoppers.utils.TeleportTrigger; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.command.CommandSender; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.UUID; +import java.util.*; -public class EpicHoppers extends JavaPlugin { - private static CommandSender console = Bukkit.getConsoleSender(); +public class EpicHoppers extends SongodaPlugin { private static EpicHoppers INSTANCE; - private ServerVersion serverVersion = ServerVersion.fromPackageName(Bukkit.getServer().getClass().getPackage().getName()); + private Config levelsConfig = new Config(this, "levels.yml"); public Enchantment enchantmentHandler; - private SettingsManager settingsManager; - private ConfigWrapper levelsFile = new ConfigWrapper(this, "", "levels.yml"); - private Locale locale; + private final GuiManager guiManager = new GuiManager(this); private HopperManager hopperManager; private CommandManager commandManager; private LevelManager levelManager; private BoostManager boostManager; private PlayerDataManager playerDataManager; - private Economy economy; - private TeleportHandler teleportHandler; private Storage storage; @@ -83,42 +67,46 @@ public class EpicHoppers extends JavaPlugin { } @Override - public void onEnable() { + public void onPluginLoad() { INSTANCE = this; + } - console.sendMessage(Methods.formatText("&a=============================")); - console.sendMessage(Methods.formatText("&7EpicHoppers " + this.getDescription().getVersion() + " by &5Songoda <3&7!")); - console.sendMessage(Methods.formatText("&7Action: &aEnabling&7...")); + @Override + public void onPluginDisable() { + this.saveToFile(); + this.storage.closeConnection(); + } - this.settingsManager = new SettingsManager(this); - this.settingsManager.setupConfig(); + @Override + public void onPluginEnable() { + // Run Songoda Updater + SongodaCore.registerPlugin(this, 22, CompatibleMaterial.HOPPER); - // Setup language - new Locale(this, "en_US"); - this.locale = Locale.getLocale(getConfig().getString("System.Language Mode")); + // Load Economy + EconomyManager.load(); - // Running Songoda Updater - Plugin plugin = new Plugin(this, 15); - plugin.addModule(new LocaleModule()); - SongodaUpdate.load(plugin); + // Setup Config + Settings.setupConfig(); + this.setLocale(Settings.LANGUGE_MODE.getString(), false); + + // Set Economy & Hologram preference + EconomyManager.getManager().setPreferredHook(Settings.ECONOMY_PLUGIN.getString()); + + // Register commands + this.commandManager = new CommandManager(this); + this.commandManager.addCommand(new CommandEpicHoppers(this)) + .addSubCommands( + new CommandBook(this), + new CommandBoost(this), + new CommandGive(this), + new CommandReload(this), + new CommandSettings(this) + ); this.enchantmentHandler = new Enchantment(); this.hopperManager = new HopperManager(); this.playerDataManager = new PlayerDataManager(); this.boostManager = new BoostManager(); - this.commandManager = new CommandManager(this); - - PluginManager pluginManager = Bukkit.getPluginManager(); - - // Setup Economy - if (Setting.VAULT_ECONOMY.getBoolean() && pluginManager.isPluginEnabled("Vault")) { - // try to load Vault for economy - if (!((VaultEconomy) (this.economy = new VaultEconomy())).isLoaded()) - this.economy = null; // vault load error - } else if (Setting.RESERVE_ECONOMY.getBoolean() && pluginManager.isPluginEnabled("Reserve")) - this.economy = new ReserveEconomy(); - else if (Setting.PLAYER_POINTS_ECONOMY.getBoolean() && pluginManager.isPluginEnabled("PlayerPoints")) - this.economy = new PlayerPointsEconomy(); this.loadLevelManager(); this.checkStorage(); @@ -130,6 +118,8 @@ public class EpicHoppers extends JavaPlugin { this.teleportHandler = new TeleportHandler(this); // Register Listeners + guiManager.init(); + PluginManager pluginManager = Bukkit.getPluginManager(); pluginManager.registerEvents(new HopperListeners(this), this); pluginManager.registerEvents(new EntityListeners(this), this); pluginManager.registerEvents(new BlockListeners(this), this); @@ -143,48 +133,24 @@ public class EpicHoppers extends JavaPlugin { if (pluginManager.isPluginEnabled("EpicFarming")) epicfarming = true; // Start auto save - int saveInterval = Setting.AUTOSAVE.getInt() * 60 * 20; + int saveInterval = Settings.AUTOSAVE.getInt() * 60 * 20; Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, saveInterval, saveInterval); - - // Start Metrics - new Metrics(this); - - console.sendMessage(Methods.formatText("&a=============================")); } @Override - public void onDisable() { - this.saveToFile(); - this.storage.closeConnection(); - - console.sendMessage(Methods.formatText("&a=============================")); - console.sendMessage(Methods.formatText("&7EpicHoppers " + this.getDescription().getVersion() + " by &5Songoda <3!")); - console.sendMessage(Methods.formatText("&7Action: &cDisabling&7...")); - console.sendMessage(Methods.formatText("&a=============================")); + public void onConfigReload() { + this.setLocale(getConfig().getString("System.Language Mode"), true); + this.locale.reloadMessages(); + loadLevelManager(); } - 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(); + @Override + public List getExtraConfig() { + return Collections.singletonList(levelsConfig); } private void checkStorage() { - if (getConfig().getBoolean("Database.Activate Mysql Support")) { - this.storage = new StorageMysql(this); - } else { - this.storage = new StorageYaml(this); - } + this.storage = new StorageYaml(this); } /* @@ -192,7 +158,7 @@ public class EpicHoppers extends JavaPlugin { */ private void saveToFile() { // double-check that we're ok to save - if(EpicHoppers.getInstance().getLevelManager() == null) + if (EpicHoppers.getInstance().getLevelManager() == null) return; checkStorage(); @@ -280,6 +246,7 @@ public class EpicHoppers extends JavaPlugin { private void loadLevelManager() { if (!new File(this.getDataFolder(), "levels.yml").exists()) this.saveResource("levels.yml", false); + levelsConfig.load(); // Load an instance of LevelManager levelManager = new LevelManager(); @@ -287,10 +254,10 @@ public class EpicHoppers extends JavaPlugin { * Register Levels into LevelManager from configuration. */ levelManager.clear(); - for (String levelName : levelsFile.getConfig().getKeys(false)) { + for (String levelName : levelsConfig.getKeys(false)) { int level = Integer.valueOf(levelName.split("-")[1]); - ConfigurationSection levels = levelsFile.getConfig().getConfigurationSection(levelName); + ConfigurationSection levels = levelsConfig.getConfigurationSection(levelName); int radius = levels.getInt("Range"); int amount = levels.getInt("Amount"); @@ -319,13 +286,6 @@ public class EpicHoppers extends JavaPlugin { } } - public void reload() { - this.locale = Locale.getLocale(getConfig().getString("System.Language Mode")); - this.locale.reloadMessages(); - this.settingsManager.reloadConfig(); - loadLevelManager(); - } - public ItemStack newHopperItem(Level level) { ItemStack item = new ItemStack(Material.HOPPER, 1); ItemMeta itemmeta = item.getItemMeta(); @@ -362,18 +322,14 @@ public class EpicHoppers extends JavaPlugin { return hopperManager; } - public SettingsManager getSettingsManager() { - return settingsManager; - } - - public Economy getEconomy() { - return economy; - } - public PlayerDataManager getPlayerDataManager() { return playerDataManager; } + public GuiManager getGuiManager() { + return guiManager; + } + public boolean isLiquidtanks() { return liquidtanks; } @@ -381,5 +337,4 @@ public class EpicHoppers extends JavaPlugin { public boolean isEpicFarming() { return epicfarming; } - } diff --git a/src/main/java/com/songoda/epichoppers/command/AbstractCommand.java b/src/main/java/com/songoda/epichoppers/command/AbstractCommand.java deleted file mode 100644 index ba74ba9..0000000 --- a/src/main/java/com/songoda/epichoppers/command/AbstractCommand.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.songoda.epichoppers.command; - -import com.songoda.epichoppers.EpicHoppers; -import org.bukkit.command.CommandSender; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public abstract class AbstractCommand { - - private final AbstractCommand parent; - private final boolean noConsole; - private String command; - - private List subCommand = new ArrayList<>(); - - protected AbstractCommand(AbstractCommand parent, boolean noConsole, String... command) { - if (parent != null) { - this.subCommand = Arrays.asList(command); - } else { - this.command = Arrays.asList(command).get(0); - } - this.parent = parent; - this.noConsole = noConsole; - } - - public AbstractCommand getParent() { - return parent; - } - - public String getCommand() { - return command; - } - - public List getSubCommand() { - return subCommand; - } - - public void addSubCommand(String command) { - subCommand.add(command); - } - - protected abstract ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args); - - protected abstract List onTab(EpicHoppers instance, CommandSender sender, String... args); - - public abstract String getPermissionNode(); - - public abstract String getSyntax(); - - public abstract String getDescription(); - - public boolean isNoConsole() { - return noConsole; - } - - public enum ReturnType {SUCCESS, FAILURE, SYNTAX_ERROR} -} diff --git a/src/main/java/com/songoda/epichoppers/command/CommandManager.java b/src/main/java/com/songoda/epichoppers/command/CommandManager.java deleted file mode 100644 index 6f9f978..0000000 --- a/src/main/java/com/songoda/epichoppers/command/CommandManager.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.songoda.epichoppers.command; - -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.command.commands.*; -import com.songoda.epichoppers.utils.Methods; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class CommandManager implements CommandExecutor { - - private EpicHoppers instance; - private TabManager tabManager; - - private List commands = new ArrayList<>(); - - public CommandManager(EpicHoppers instance) { - this.instance = instance; - this.tabManager = new TabManager(this); - - instance.getCommand("EpicHoppers").setExecutor(this); - - AbstractCommand commandEpicHoppers = addCommand(new CommandEpicHoppers()); - - addCommand(new CommandReload(commandEpicHoppers)); - addCommand(new CommandSettings(commandEpicHoppers)); - addCommand(new CommandGive(commandEpicHoppers)); - addCommand(new CommandBoost(commandEpicHoppers)); - addCommand(new CommandBook(commandEpicHoppers)); - - for (AbstractCommand abstractCommand : commands) { - if (abstractCommand.getParent() != null) continue; - instance.getCommand(abstractCommand.getCommand()).setTabCompleter(tabManager); - } - } - - private AbstractCommand addCommand(AbstractCommand abstractCommand) { - commands.add(abstractCommand); - return abstractCommand; - } - - @Override - public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - for (AbstractCommand abstractCommand : commands) { - if (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName().toLowerCase())) { - if (strings.length == 0) { - processRequirements(abstractCommand, commandSender, strings); - return true; - } - } else if (strings.length != 0 && abstractCommand.getParent() != null && abstractCommand.getParent().getCommand().equalsIgnoreCase(command.getName())) { - String cmd = strings[0]; - String cmd2 = strings.length >= 2 ? String.join(" ", strings[0], strings[1]) : null; - for (String cmds : abstractCommand.getSubCommand()) { - if (cmd.equalsIgnoreCase(cmds) || (cmd2 != null && cmd2.equalsIgnoreCase(cmds))) { - processRequirements(abstractCommand, commandSender, strings); - return true; - } - } - } - } - instance.getLocale().newMessage("&7The command you entered does not exist or is spelt incorrectly.").sendPrefixedMessage(commandSender); - return true; - } - - private void processRequirements(AbstractCommand command, CommandSender sender, String[] strings) { - if (!(sender instanceof Player) && command.isNoConsole()) { - sender.sendMessage("You must be a player to use this commands."); - return; - } - if (command.getPermissionNode() == null || sender.hasPermission(command.getPermissionNode())) { - AbstractCommand.ReturnType returnType = command.runCommand(instance, sender, strings); - if (returnType == AbstractCommand.ReturnType.SYNTAX_ERROR) { - instance.getLocale().newMessage("&cInvalid Syntax!").sendPrefixedMessage(sender); - instance.getLocale().newMessage("&7The valid syntax is: &6" + command.getSyntax() + "&7.").sendPrefixedMessage(sender); - } - return; - } - instance.getLocale().getMessage("event.general.nopermission").sendPrefixedMessage(sender); - } - - public List getCommands() { - return Collections.unmodifiableList(commands); - } -} diff --git a/src/main/java/com/songoda/epichoppers/command/TabManager.java b/src/main/java/com/songoda/epichoppers/command/TabManager.java deleted file mode 100644 index 546035e..0000000 --- a/src/main/java/com/songoda/epichoppers/command/TabManager.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.songoda.epichoppers.command; - -import com.songoda.epichoppers.EpicHoppers; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabCompleter; - -import java.util.ArrayList; -import java.util.List; - -public class TabManager implements TabCompleter { - - private final CommandManager commandManager; - - TabManager(CommandManager commandManager) { - this.commandManager = commandManager; - } - - @Override - public List onTabComplete(CommandSender sender, Command command, String alias, String[] strings) { - for (AbstractCommand abstractCommand : commandManager.getCommands()) { - if (abstractCommand.getCommand() != null && abstractCommand.getCommand().equalsIgnoreCase(command.getName().toLowerCase())) { - if (strings.length == 1) { - List subs = new ArrayList<>(); - for (AbstractCommand ac : commandManager.getCommands()) { - if (ac.getSubCommand() == null) continue; - subs.addAll(ac.getSubCommand()); - } - subs.removeIf(s -> !s.toLowerCase().startsWith(strings[0].toLowerCase())); - return subs; - } - } else if (strings.length != 0 && abstractCommand.getParent() != null && abstractCommand.getParent().getCommand().equalsIgnoreCase(command.getName().toLowerCase())) { - String cmd = strings[0]; - String cmd2 = strings.length >= 2 ? String.join(" ", strings[0], strings[1]) : null; - for (String cmds : abstractCommand.getSubCommand()) { - if (cmd.equalsIgnoreCase(cmds) || (cmd2 != null && cmd2.equalsIgnoreCase(cmds))) { - List list = abstractCommand.onTab(EpicHoppers.getInstance(), sender, strings); - String str = strings[strings.length - 1]; - if (list != null && str != null && str.length() >= 1) { - try { - list.removeIf(s -> !s.toLowerCase().startsWith(str.toLowerCase())); - } catch (UnsupportedOperationException ignored) {} - } - return list; - } - } - } - } - return null; - } -} diff --git a/src/main/java/com/songoda/epichoppers/command/commands/CommandBook.java b/src/main/java/com/songoda/epichoppers/commands/CommandBook.java similarity index 65% rename from src/main/java/com/songoda/epichoppers/command/commands/CommandBook.java rename to src/main/java/com/songoda/epichoppers/commands/CommandBook.java index 4ecd4cb..36d0612 100644 --- a/src/main/java/com/songoda/epichoppers/command/commands/CommandBook.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandBook.java @@ -1,8 +1,7 @@ -package com.songoda.epichoppers.command.commands; +package com.songoda.epichoppers.commands; +import com.songoda.core.commands.AbstractCommand; import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.command.AbstractCommand; -import com.songoda.epichoppers.utils.Methods; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -11,30 +10,33 @@ import java.util.List; public class CommandBook extends AbstractCommand { - public CommandBook(AbstractCommand parent) { - super(parent, false, "book"); + final EpicHoppers instance; + + public CommandBook(EpicHoppers instance) { + super(false, "book"); + this.instance = instance; } @Override - protected ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args) { - if (args.length == 1) { + protected ReturnType runCommand(CommandSender sender, String... args) { + if (args.length == 0) { if (sender instanceof Player) { ((Player) sender).getInventory().addItem(instance.enchantmentHandler.getbook()); return ReturnType.SUCCESS; } - } else if (Bukkit.getPlayerExact(args[1]) == null) { + } else if (Bukkit.getPlayerExact(args[0]) == null) { instance.getLocale().newMessage("&cThat username does not exist, or the user is not online!") .sendPrefixedMessage(sender); return ReturnType.FAILURE; } else { - Bukkit.getPlayerExact(args[1]).getInventory().addItem(instance.enchantmentHandler.getbook()); + Bukkit.getPlayerExact(args[0]).getInventory().addItem(instance.enchantmentHandler.getbook()); return ReturnType.SUCCESS; } return ReturnType.FAILURE; } @Override - protected List onTab(EpicHoppers instance, CommandSender sender, String... args) { + protected List onTab(CommandSender sender, String... args) { return null; } diff --git a/src/main/java/com/songoda/epichoppers/command/commands/CommandBoost.java b/src/main/java/com/songoda/epichoppers/commands/CommandBoost.java similarity index 68% rename from src/main/java/com/songoda/epichoppers/command/commands/CommandBoost.java rename to src/main/java/com/songoda/epichoppers/commands/CommandBoost.java index 90dbffd..4af16dc 100644 --- a/src/main/java/com/songoda/epichoppers/command/commands/CommandBoost.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandBoost.java @@ -1,36 +1,41 @@ -package com.songoda.epichoppers.command.commands; +package com.songoda.epichoppers.commands; +import com.songoda.core.commands.AbstractCommand; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.boost.BoostData; -import com.songoda.epichoppers.command.AbstractCommand; import com.songoda.epichoppers.utils.Methods; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class CommandBoost extends AbstractCommand { - public CommandBoost(AbstractCommand parent) { - super(parent, false, "boost"); + final EpicHoppers instance; + + public CommandBoost(EpicHoppers instance) { + super(false, "boost"); + this.instance = instance; } @Override - protected ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args) { - if (args.length < 3) { + protected ReturnType runCommand(CommandSender sender, String... args) { + if (args.length < 2) { instance.getLocale().newMessage("&7Syntax error...").sendPrefixedMessage(sender); return ReturnType.SYNTAX_ERROR; } - if (!Methods.isInt(args[2])) { - instance.getLocale().newMessage("&6" + args[2] + " &7is not a number...").sendPrefixedMessage(sender); + if (!Methods.isInt(args[1])) { + instance.getLocale().newMessage("&6" + args[1] + " &7is not a number...").sendPrefixedMessage(sender); return ReturnType.SYNTAX_ERROR; } long duration = 0L; - if (args.length > 3) { - for (int i = 1; i < args.length; i++) { + if (args.length > 2) { + for (int i = 0; i < args.length; i++) { String line = args[i]; long time = Methods.parseTime(line); duration += time; @@ -38,30 +43,30 @@ public class CommandBoost extends AbstractCommand { } } - Player player = Bukkit.getPlayer(args[1]); + Player player = Bukkit.getPlayer(args[0]); if (player == null) { instance.getLocale().newMessage("&cThat player does not exist or is not online...").sendPrefixedMessage(sender); return ReturnType.FAILURE; } - BoostData boostData = new BoostData(Integer.parseInt(args[2]), duration == 0L ? Long.MAX_VALUE : System.currentTimeMillis() + duration, player.getUniqueId()); + BoostData boostData = new BoostData(Integer.parseInt(args[1]), duration == 0L ? Long.MAX_VALUE : System.currentTimeMillis() + duration, player.getUniqueId()); instance.getBoostManager().addBoostToPlayer(boostData); - instance.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[1]).getName() - + "'s &7hopper transfer rates by &6" + args[2] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender); + instance.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[0]).getName() + + "'s &7hopper transfer rates by &6" + args[1] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender); return ReturnType.SUCCESS; } @Override - protected List onTab(EpicHoppers instance, CommandSender sender, String... args) { - if (args.length == 2) { + protected List onTab(CommandSender sender, String... args) { + if (args.length == 1) { List players = new ArrayList<>(); for (Player player : Bukkit.getOnlinePlayers()) { players.add(player.getName()); } return players; - } else if (args.length == 3) { + } else if (args.length == 2) { return Arrays.asList("1", "2", "3", "4", "5"); - } else if (args.length == 4) { + } else if (args.length == 3) { return Arrays.asList("1m", "1h", "1d"); } return null; diff --git a/src/main/java/com/songoda/epichoppers/command/commands/CommandEpicHoppers.java b/src/main/java/com/songoda/epichoppers/commands/CommandEpicHoppers.java similarity index 61% rename from src/main/java/com/songoda/epichoppers/command/commands/CommandEpicHoppers.java rename to src/main/java/com/songoda/epichoppers/commands/CommandEpicHoppers.java index c0be484..de8f5ed 100644 --- a/src/main/java/com/songoda/epichoppers/command/commands/CommandEpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandEpicHoppers.java @@ -1,27 +1,30 @@ -package com.songoda.epichoppers.command.commands; +package com.songoda.epichoppers.commands; +import com.songoda.core.commands.AbstractCommand; import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.command.AbstractCommand; -import com.songoda.epichoppers.utils.Methods; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import java.util.List; public class CommandEpicHoppers extends AbstractCommand { - public CommandEpicHoppers() { - super(null, false, "EpicHoppers"); + final EpicHoppers instance; + + public CommandEpicHoppers(EpicHoppers instance) { + super(false, "EpicHoppers"); + this.instance = instance; } @Override - protected ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args) { + protected AbstractCommand.ReturnType runCommand(CommandSender sender, String... args) { sender.sendMessage(""); instance.getLocale().newMessage("&7Version " + instance.getDescription().getVersion() + " Created with <3 by &5&l&oSongoda").sendPrefixedMessage(sender); - for (AbstractCommand command : instance.getCommandManager().getCommands()) { + for (AbstractCommand command : instance.getCommandManager().getAllCommands()) { if (command.getPermissionNode() == null || sender.hasPermission(command.getPermissionNode())) { - sender.sendMessage(Methods.formatText("&8 - &a" + command.getSyntax() + "&7 - " + command.getDescription())); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8 - &a" + command.getSyntax() + "&7 - " + command.getDescription())); } } sender.sendMessage(""); @@ -30,7 +33,7 @@ public class CommandEpicHoppers extends AbstractCommand { } @Override - protected List onTab(EpicHoppers instance, CommandSender sender, String... args) { + protected List onTab(CommandSender sender, String... args) { return null; } diff --git a/src/main/java/com/songoda/epichoppers/command/commands/CommandGive.java b/src/main/java/com/songoda/epichoppers/commands/CommandGive.java similarity index 76% rename from src/main/java/com/songoda/epichoppers/command/commands/CommandGive.java rename to src/main/java/com/songoda/epichoppers/commands/CommandGive.java index 99743dd..0d76954 100644 --- a/src/main/java/com/songoda/epichoppers/command/commands/CommandGive.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandGive.java @@ -1,7 +1,7 @@ -package com.songoda.epichoppers.command.commands; +package com.songoda.epichoppers.commands; +import com.songoda.core.commands.AbstractCommand; import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.command.AbstractCommand; import com.songoda.epichoppers.hopper.levels.Level; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -13,36 +13,39 @@ import java.util.List; public class CommandGive extends AbstractCommand { - public CommandGive(AbstractCommand parent) { - super(parent, false, "give"); + final EpicHoppers instance; + + public CommandGive(EpicHoppers instance) { + super(false, "give"); + this.instance = instance; } @Override - protected ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args) { - if (args.length <= 2) { + protected ReturnType runCommand(CommandSender sender, String... args) { + if (args.length <= 1) { return ReturnType.SYNTAX_ERROR; } - if (Bukkit.getPlayerExact(args[1]) == null) { + if (Bukkit.getPlayerExact(args[0]) == null) { instance.getLocale().newMessage("&cThat username does not exist, or the user is not online!").sendPrefixedMessage(sender); return ReturnType.FAILURE; } Level level = instance.getLevelManager().getLowestLevel(); Player player; - if (Bukkit.getPlayer(args[1]) == null) { + if (Bukkit.getPlayer(args[0]) == null) { instance.getLocale().newMessage("&cThat player does not exist or is currently offline.").sendPrefixedMessage(sender); return ReturnType.FAILURE; } else { - player = Bukkit.getPlayer(args[1]); + player = Bukkit.getPlayer(args[0]); } - if (!instance.getLevelManager().isLevel(Integer.parseInt(args[2]))) { + if (!instance.getLevelManager().isLevel(Integer.parseInt(args[1]))) { instance.getLocale().newMessage("&cNot a valid level... The current valid levels are: &4" + level.getLevel() + "-" + instance.getLevelManager().getHighestLevel().getLevel() + "&c.") .sendPrefixedMessage(sender); return ReturnType.FAILURE; } else { - level = instance.getLevelManager().getLevel(Integer.parseInt(args[2])); + level = instance.getLevelManager().getLevel(Integer.parseInt(args[1])); } player.getInventory().addItem(instance.newHopperItem(level)); instance.getLocale().getMessage("command.give.success").processPlaceholder("level", level.getLevel()).sendPrefixedMessage(player); @@ -51,14 +54,14 @@ public class CommandGive extends AbstractCommand { } @Override - protected List onTab(EpicHoppers instance, CommandSender sender, String... args) { - if (args.length == 2) { + protected List onTab(CommandSender sender, String... args) { + if (args.length == 1) { List players = new ArrayList<>(); for (Player player : Bukkit.getOnlinePlayers()) { players.add(player.getName()); } return players; - } else if (args.length == 3) { + } else if (args.length == 2) { return Arrays.asList("1", "2", "3", "4", "5"); } return null; diff --git a/src/main/java/com/songoda/epichoppers/command/commands/CommandReload.java b/src/main/java/com/songoda/epichoppers/commands/CommandReload.java similarity index 59% rename from src/main/java/com/songoda/epichoppers/command/commands/CommandReload.java rename to src/main/java/com/songoda/epichoppers/commands/CommandReload.java index 7816010..a582d03 100644 --- a/src/main/java/com/songoda/epichoppers/command/commands/CommandReload.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandReload.java @@ -1,27 +1,29 @@ -package com.songoda.epichoppers.command.commands; +package com.songoda.epichoppers.commands; +import com.songoda.core.commands.AbstractCommand; import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.command.AbstractCommand; -import com.songoda.epichoppers.utils.Methods; import org.bukkit.command.CommandSender; import java.util.List; public class CommandReload extends AbstractCommand { - public CommandReload(AbstractCommand parent) { - super(parent, false, "reload"); + final EpicHoppers instance; + + public CommandReload(EpicHoppers instance) { + super(false, "reload"); + this.instance = instance; } @Override - protected ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args) { - instance.reload(); + protected ReturnType runCommand(CommandSender sender, String... args) { + instance.reloadConfig(); instance.getLocale().getMessage("&7Configuration and Language files reloaded.").sendPrefixedMessage(sender); return ReturnType.SUCCESS; } @Override - protected List onTab(EpicHoppers instance, CommandSender sender, String... args) { + protected List onTab(CommandSender sender, String... args) { return null; } diff --git a/src/main/java/com/songoda/epichoppers/command/commands/CommandSettings.java b/src/main/java/com/songoda/epichoppers/commands/CommandSettings.java similarity index 50% rename from src/main/java/com/songoda/epichoppers/command/commands/CommandSettings.java rename to src/main/java/com/songoda/epichoppers/commands/CommandSettings.java index b7abf0f..505292b 100644 --- a/src/main/java/com/songoda/epichoppers/command/commands/CommandSettings.java +++ b/src/main/java/com/songoda/epichoppers/commands/CommandSettings.java @@ -1,7 +1,8 @@ -package com.songoda.epichoppers.command.commands; +package com.songoda.epichoppers.commands; +import com.songoda.core.commands.AbstractCommand; +import com.songoda.core.configuration.editor.PluginConfigGui; import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.command.AbstractCommand; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -9,19 +10,22 @@ import java.util.List; public class CommandSettings extends AbstractCommand { - public CommandSettings(AbstractCommand parent) { - super(parent, true, "settings"); + final EpicHoppers instance; + + public CommandSettings(EpicHoppers instance) { + super(true, "settings"); + this.instance = instance; } @Override - protected ReturnType runCommand(EpicHoppers instance, CommandSender sender, String... args) { - Player p = (Player) sender; - instance.getSettingsManager().openSettingsManager(p); + protected ReturnType runCommand(CommandSender sender, String... args) { + Player player = (Player) sender; + instance.getGuiManager().showGUI((Player) sender, new PluginConfigGui(instance)); return ReturnType.SUCCESS; } @Override - protected List onTab(EpicHoppers instance, CommandSender sender, String... args) { + protected List onTab(CommandSender sender, String... args) { return null; } diff --git a/src/main/java/com/songoda/epichoppers/economy/Economy.java b/src/main/java/com/songoda/epichoppers/economy/Economy.java deleted file mode 100644 index f28ff95..0000000 --- a/src/main/java/com/songoda/epichoppers/economy/Economy.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.songoda.epichoppers.economy; - -import org.bukkit.OfflinePlayer; - -public interface Economy { - - boolean hasBalance(OfflinePlayer player, double cost); - - boolean withdrawBalance(OfflinePlayer player, double cost); - - boolean deposit(OfflinePlayer player, double amount); -} diff --git a/src/main/java/com/songoda/epichoppers/economy/PlayerPointsEconomy.java b/src/main/java/com/songoda/epichoppers/economy/PlayerPointsEconomy.java deleted file mode 100644 index 0fe455f..0000000 --- a/src/main/java/com/songoda/epichoppers/economy/PlayerPointsEconomy.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.songoda.epichoppers.economy; - -import org.black_ixx.playerpoints.PlayerPoints; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; - -public class PlayerPointsEconomy implements Economy { - - private final PlayerPoints playerPoints; - - public PlayerPointsEconomy() { - this.playerPoints = (PlayerPoints) Bukkit.getServer().getPluginManager().getPlugin("PlayerPoints"); - } - - private int convertAmount(double amount) { - return (int) Math.ceil(amount); - } - - @Override - public boolean hasBalance(OfflinePlayer player, double cost) { - int amount = convertAmount(cost); - return playerPoints.getAPI().look(player.getUniqueId()) >= amount; - - } - - @Override - public boolean withdrawBalance(OfflinePlayer player, double cost) { - int amount = convertAmount(cost); - return playerPoints.getAPI().take(player.getUniqueId(), amount); - - } - - @Override - public boolean deposit(OfflinePlayer player, double amount) { - int amt = convertAmount(amount); - return playerPoints.getAPI().give(player.getUniqueId(), amt); - } -} diff --git a/src/main/java/com/songoda/epichoppers/economy/ReserveEconomy.java b/src/main/java/com/songoda/epichoppers/economy/ReserveEconomy.java deleted file mode 100644 index 6cc05e8..0000000 --- a/src/main/java/com/songoda/epichoppers/economy/ReserveEconomy.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.songoda.epichoppers.economy; - -import net.tnemc.core.Reserve; -import net.tnemc.core.economy.EconomyAPI; -import org.bukkit.OfflinePlayer; - -import java.math.BigDecimal; - -public class ReserveEconomy implements Economy { - - EconomyAPI economyAPI; - - public ReserveEconomy() { - if (Reserve.instance().economyProvided()) - economyAPI = Reserve.instance().economy(); - } - - @Override - public boolean hasBalance(OfflinePlayer player, double cost) { - return economyAPI.hasHoldings(player.getUniqueId(), new BigDecimal(cost)); - } - - @Override - public boolean withdrawBalance(OfflinePlayer player, double cost) { - return economyAPI.removeHoldings(player.getUniqueId(), new BigDecimal(cost)); - } - - @Override - public boolean deposit(OfflinePlayer player, double amount) { - return economyAPI.addHoldings(player.getUniqueId(), new BigDecimal(amount)); - } -} diff --git a/src/main/java/com/songoda/epichoppers/economy/VaultEconomy.java b/src/main/java/com/songoda/epichoppers/economy/VaultEconomy.java deleted file mode 100644 index f4e8c62..0000000 --- a/src/main/java/com/songoda/epichoppers/economy/VaultEconomy.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.songoda.epichoppers.economy; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.plugin.RegisteredServiceProvider; - -public class VaultEconomy implements Economy { - private final net.milkbowl.vault.economy.Economy vault; - - public VaultEconomy() { - // separate this out to weed out some conflicts in 1.8 - RegisteredServiceProvider rsp = Bukkit.getServicesManager(). - getRegistration(net.milkbowl.vault.economy.Economy.class); - this.vault = (net.milkbowl.vault.economy.Economy) (rsp == null ? null : rsp.getProvider()); - } - - public boolean isLoaded() { - return vault != null; - } - - @Override - public boolean hasBalance(OfflinePlayer player, double cost) { - return vault != null && vault.has(player, cost); - } - - @Override - public boolean withdrawBalance(OfflinePlayer player, double cost) { - return vault != null && vault.withdrawPlayer(player, cost).transactionSuccess(); - } - - @Override - public boolean deposit(OfflinePlayer player, double amount) { - return vault != null && vault.depositPlayer(player, amount).transactionSuccess(); - } -} diff --git a/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java b/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java index 889bae8..ddf22ca 100644 --- a/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java +++ b/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java @@ -1,65 +1,37 @@ package com.songoda.epichoppers.gui; -import com.songoda.epichoppers.EpicHoppers; +import com.songoda.core.gui.Gui; +import com.songoda.core.gui.GuiUtils; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.gui.AbstractGUI; -import com.songoda.epichoppers.utils.gui.Range; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; -public class GUICrafting extends AbstractGUI { +public class GUICrafting extends Gui { - private final EpicHoppers plugin; - private final Hopper hopper; - private final ModuleAutoCrafting module; + public GUICrafting(ModuleAutoCrafting module, Hopper hopper, Player player) { + setRows(3); + setTitle(Methods.formatName(hopper.getLevel().getLevel(), false) + Methods.formatText(" &8-&f Crafting")); + setOnClose((event) -> module.setAutoCrafting(hopper, player, inventory.getItem(13))); + setAcceptsItems(true); - public GUICrafting(EpicHoppers plugin, ModuleAutoCrafting autoCrafting, Hopper hopper, Player player) { - super(player); - this.plugin = plugin; - this.hopper = hopper; - this.module = autoCrafting; + ItemStack glass1 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_1.getMaterial()); + ItemStack glass2 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_2.getMaterial()); + ItemStack glass3 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_3.getMaterial()); - init(Methods.formatName(hopper.getLevel().getLevel(), false) + " &8-&f Crafting", 27); - } + setDefaultItem(glass1); - @Override - protected void constructGUI() { - int nu = 0; - while (nu != 27) { - inventory.setItem(nu, Methods.getGlass()); - nu++; - } + GuiUtils.mirrorFill(this, 0, 0, true, true, glass2); + GuiUtils.mirrorFill(this, 0, 1, true, true, glass2); + GuiUtils.mirrorFill(this, 0, 2, true, true, glass3); + GuiUtils.mirrorFill(this, 1, 0, false, true, glass2); + GuiUtils.mirrorFill(this, 1, 1, false, true, glass3); - inventory.setItem(0, Methods.getBackgroundGlass(true)); - inventory.setItem(1, Methods.getBackgroundGlass(true)); - inventory.setItem(2, Methods.getBackgroundGlass(false)); - inventory.setItem(6, Methods.getBackgroundGlass(false)); - inventory.setItem(7, Methods.getBackgroundGlass(true)); - inventory.setItem(8, Methods.getBackgroundGlass(true)); - inventory.setItem(9, Methods.getBackgroundGlass(true)); - inventory.setItem(10, Methods.getBackgroundGlass(false)); - inventory.setItem(16, Methods.getBackgroundGlass(false)); - inventory.setItem(17, Methods.getBackgroundGlass(true)); - inventory.setItem(18, Methods.getBackgroundGlass(true)); - inventory.setItem(19, Methods.getBackgroundGlass(true)); - inventory.setItem(20, Methods.getBackgroundGlass(false)); - inventory.setItem(24, Methods.getBackgroundGlass(false)); - inventory.setItem(25, Methods.getBackgroundGlass(true)); - inventory.setItem(26, Methods.getBackgroundGlass(true)); + setButton(13, module.getAutoCrafting(hopper), + (event) -> module.setAutoCrafting(hopper, player, inventory.getItem(13))); - inventory.setItem(13, module.getAutoCrafting(hopper)); - - addDraggable(new Range(13, 13, null, false), true); - } - - @Override - protected void registerClickables() { - } - - @Override - protected void registerOnCloses() { - registerOnClose(((player, inventory) -> - module.setAutoCrafting(hopper, player, inventory.getItem(13)))); + setUnlocked(13); } } diff --git a/src/main/java/com/songoda/epichoppers/gui/GUIFilter.java b/src/main/java/com/songoda/epichoppers/gui/GUIFilter.java index edb0517..cf5138d 100644 --- a/src/main/java/com/songoda/epichoppers/gui/GUIFilter.java +++ b/src/main/java/com/songoda/epichoppers/gui/GUIFilter.java @@ -1,13 +1,14 @@ package com.songoda.epichoppers.gui; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.gui.Gui; +import com.songoda.core.gui.GuiUtils; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Filter; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.player.SyncType; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; -import com.songoda.epichoppers.utils.gui.AbstractGUI; -import com.songoda.epichoppers.utils.gui.Range; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -18,74 +19,67 @@ import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.List; -public class GUIFilter extends AbstractGUI { +public class GUIFilter extends Gui { - private final EpicHoppers plugin; private final Hopper hopper; public GUIFilter(EpicHoppers plugin, Hopper hopper, Player player) { - super(player); - this.plugin = plugin; this.hopper = hopper; - init(Methods.formatText(Methods.formatName(hopper.getLevel().getLevel(), false) + " &8-&f Filter"), 54); - } + setRows(6); + setTitle(Methods.formatText(Methods.formatName(hopper.getLevel().getLevel(), false) + " &8-&f Filter")); + setDefaultItem(null); + setAcceptsItems(true); - @Override - protected void constructGUI() { + setOnClose((event) -> compile()); Filter filter = hopper.getFilter(); - inventory.setItem(6, Methods.getBackgroundGlass(true)); - inventory.setItem(7, Methods.getBackgroundGlass(true)); - inventory.setItem(8, Methods.getBackgroundGlass(true)); - inventory.setItem(15, Methods.getBackgroundGlass(true)); - inventory.setItem(17, Methods.getBackgroundGlass(true)); - inventory.setItem(24, Methods.getBackgroundGlass(true)); - inventory.setItem(25, Methods.getGlass()); - inventory.setItem(26, Methods.getBackgroundGlass(true)); - inventory.setItem(33, Methods.getBackgroundGlass(true)); - inventory.setItem(34, Methods.getGlass()); - inventory.setItem(35, Methods.getBackgroundGlass(true)); - inventory.setItem(42, Methods.getBackgroundGlass(true)); - inventory.setItem(44, Methods.getBackgroundGlass(true)); - inventory.setItem(51, Methods.getBackgroundGlass(true)); - inventory.setItem(52, Methods.getBackgroundGlass(true)); - inventory.setItem(53, Methods.getBackgroundGlass(true)); + ItemStack glass1 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_1.getMaterial()); + ItemStack glass2 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_2.getMaterial()); - ItemStack it = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.WHITE_STAINED_GLASS_PANE : Material.valueOf("STAINED_GLASS_PANE"), 1); + GuiUtils.mirrorFill(this, 0, 6, true, false, glass2); + GuiUtils.mirrorFill(this, 0, 7, true, false, glass2); + GuiUtils.mirrorFill(this, 0, 8, true, false, glass2); + GuiUtils.mirrorFill(this, 1, 6, true, false, glass2); + GuiUtils.mirrorFill(this, 1, 8, true, false, glass2); + GuiUtils.mirrorFill(this, 2, 6, true, false, glass2); + GuiUtils.mirrorFill(this, 2, 7, true, false, glass1); + GuiUtils.mirrorFill(this, 2, 8, true, false, glass2); + + ItemStack it = CompatibleMaterial.WHITE_STAINED_GLASS_PANE.getItem(); ItemMeta itm = it.getItemMeta(); itm.setDisplayName(plugin.getLocale().getMessage("interface.filter.whitelist").getMessage()); it.setItemMeta(itm); int[] whiteSlots = {0, 1, 45, 46}; for (int nu : whiteSlots) { - inventory.setItem(nu, it); + setItem(nu, it); } int[] awhite = {9, 10, 18, 19, 27, 28, 36, 37}; int num = 0; for (ItemStack m : filter.getWhiteList()) { if (num >= filter.getWhiteList().size()) break; - inventory.setItem(awhite[num], new ItemStack(m)); + setItem(awhite[num], new ItemStack(m)); num++; } - it = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.BLACK_STAINED_GLASS_PANE : Material.valueOf("STAINED_GLASS_PANE"), 1, (short) 15); + it = CompatibleMaterial.BLACK_STAINED_GLASS_PANE.getItem(); itm = it.getItemMeta(); itm.setDisplayName(plugin.getLocale().getMessage("interface.filter.blacklist").getMessage()); it.setItemMeta(itm); int[] blackSlots = {2, 3, 47, 48}; for (int nu : blackSlots) { - inventory.setItem(nu, it); + setItem(nu, it); } int[] ablack = {11, 12, 20, 21, 29, 30, 38, 39}; num = 0; for (ItemStack m : filter.getBlackList()) { if (num >= filter.getBlackList().size()) break; - inventory.setItem(ablack[num], new ItemStack(m)); + setItem(ablack[num], new ItemStack(m)); num++; } @@ -96,14 +90,14 @@ public class GUIFilter extends AbstractGUI { int[] avoid = {4, 5, 49, 50}; for (int nu : avoid) { - inventory.setItem(nu, it); + setItem(nu, it); } int[] voidSlots = {13, 14, 22, 23, 31, 32, 40, 41}; num = 0; for (ItemStack m : filter.getVoidList()) { if (num >= filter.getVoidList().size()) break; - inventory.setItem(voidSlots[num], new ItemStack(m)); + setItem(voidSlots[num], new ItemStack(m)); num++; } @@ -118,7 +112,7 @@ public class GUIFilter extends AbstractGUI { itemmetaInfo.setLore(loreInfo); itemInfo.setItemMeta(itemmetaInfo); - inventory.setItem(16, itemInfo); + setItem(16, itemInfo); ItemStack hook = new ItemStack(Material.TRIPWIRE_HOOK, 1); @@ -133,32 +127,28 @@ public class GUIFilter extends AbstractGUI { } hookmeta.setLore(lorehook); hook.setItemMeta(hookmeta); - inventory.setItem(43, hook); + setButton(43, hook, + (event) -> { + if (event.clickType == ClickType.RIGHT) { + plugin.getLocale().getMessage("event.hopper.desync").sendPrefixedMessage(player); + hopper.getFilter().setEndPoint(null); + } else { + plugin.getPlayerDataManager().getPlayerData(player).setSyncType(SyncType.FILTERED); + plugin.getLocale().getMessage("event.hopper.syncnext").sendPrefixedMessage(player); + hopper.timeout(player); + } + player.closeInventory(); + }); - addDraggable(new Range(9, 14, null, false), true); - addDraggable(new Range(18, 23, null, false), true); - addDraggable(new Range(27, 32, null, false), true); - addDraggable(new Range(36, 41, null, false), true); - } - - @Override - protected void registerClickables() { - registerClickable(43, ((player, inventory, cursor, slot, type) -> { - if (type == ClickType.RIGHT) { - plugin.getLocale().getMessage("event.hopper.desync").sendPrefixedMessage(player); - hopper.getFilter().setEndPoint(null); - } else { - plugin.getPlayerDataManager().getPlayerData(player).setSyncType(SyncType.FILTERED); - plugin.getLocale().getMessage("event.hopper.syncnext").sendPrefixedMessage(player); - hopper.timeout(player); - } - player.closeInventory(); - })); + setUnlockedRange(9, 14); + setUnlockedRange(18, 23); + setUnlockedRange(27, 32); + setUnlockedRange(36, 41); } - private void compile(Player p) { - ItemStack[] items = p.getOpenInventory().getTopInventory().getContents(); + private void compile() { + ItemStack[] items = inventory.getContents(); Filter filter = hopper.getFilter(); @@ -212,9 +202,4 @@ public class GUIFilter extends AbstractGUI { filter.setBlackList(oblack); filter.setVoidList(ovoid); } - - @Override - protected void registerOnCloses() { - registerOnClose(((player, inventory) -> compile(player))); - } } diff --git a/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java b/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java index 4b77d93..c904fdf 100644 --- a/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epichoppers/gui/GUIOverview.java @@ -1,16 +1,19 @@ package com.songoda.epichoppers.gui; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.ServerVersion; +import com.songoda.core.gui.Gui; +import com.songoda.core.gui.GuiUtils; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.boost.BoostData; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.hopper.levels.Level; import com.songoda.epichoppers.hopper.levels.modules.Module; import com.songoda.epichoppers.player.SyncType; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.CostType; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.TeleportTrigger; -import com.songoda.epichoppers.utils.gui.AbstractGUI; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -24,28 +27,38 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -public class GUIOverview extends AbstractGUI { +public class GUIOverview extends Gui { private final EpicHoppers plugin; private final Hopper hopper; + private final Player player; private int task; public GUIOverview(EpicHoppers plugin, Hopper hopper, Player player) { - super(player); this.plugin = plugin; this.hopper = hopper; + this.player = player; - - init(Methods.formatName(hopper.getLevel().getLevel(), false), 27); + setRows(3); + setTitle(Methods.formatName(hopper.getLevel().getLevel(), false)); runTask(); + constructGUI(); + this.setOnClose(action -> Bukkit.getScheduler().cancelTask(task)); } - @Override - protected void constructGUI() { - inventory.clear(); - resetClickables(); - registerClickables(); + private void constructGUI() { + ItemStack glass1 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_1.getMaterial()); + ItemStack glass2 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_2.getMaterial()); + ItemStack glass3 = GuiUtils.getBorderItem(Settings.GLASS_TYPE_3.getMaterial()); + + setDefaultItem(glass1); + + GuiUtils.mirrorFill(this, 0, 0, true, true, glass2); + GuiUtils.mirrorFill(this, 0, 1, true, true, glass2); + GuiUtils.mirrorFill(this, 0, 2, true, true, glass3); + GuiUtils.mirrorFill(this, 1, 0, false, true, glass2); + GuiUtils.mirrorFill(this, 1, 1, false, true, glass3); plugin.getPlayerDataManager().getPlayerData(player).setLastHopper(hopper); @@ -67,7 +80,7 @@ public class GUIOverview extends AbstractGUI { perlmeta.setLore(loreperl); perl.setItemMeta(perlmeta); - ItemStack filter = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.COMPARATOR : Material.valueOf("REDSTONE_COMPARATOR"), 1); + ItemStack filter = new ItemStack(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.COMPARATOR : Material.valueOf("REDSTONE_COMPARATOR"), 1); ItemMeta filtermeta = filter.getItemMeta(); filtermeta.setDisplayName(plugin.getLocale().getMessage("interface.hopper.filtertitle").getMessage()); ArrayList lorefilter = new ArrayList<>(); @@ -85,7 +98,8 @@ public class GUIOverview extends AbstractGUI { List lore = level.getDescription(); if (plugin.getConfig().getBoolean("Main.Allow hopper Upgrading")) { lore.add(""); - if (nextLevel == null) lore.add(plugin.getLocale().getMessage("interface.hopper.alreadymaxed").getMessage()); + if (nextLevel == null) + lore.add(plugin.getLocale().getMessage("interface.hopper.alreadymaxed").getMessage()); else { lore.add(plugin.getLocale().getMessage("interface.hopper.nextlevel").processPlaceholder("level", nextLevel.getLevel()).getMessage()); lore.addAll(nextLevel.getDescription()); @@ -119,12 +133,6 @@ public class GUIOverview extends AbstractGUI { hookmeta.setLore(lorehook); hook.setItemMeta(hookmeta); - int nu = 0; - while (nu != 27) { - inventory.setItem(nu, Methods.getGlass()); - nu++; - } - Map layouts = new HashMap<>(); layouts.put(1, new Integer[]{22}); layouts.put(2, new Integer[]{22, 4}); @@ -153,89 +161,90 @@ public class GUIOverview extends AbstractGUI { int slot = layout[ii]; if (ii == 0) { - inventory.setItem(slot, hook); + setButton(slot, hook, + (event) -> { + if (event.clickType == ClickType.RIGHT) { + plugin.getLocale().getMessage("event.hopper.desync").sendPrefixedMessage(player); + hopper.clearLinkedBlocks(); + } else { + if (hopper.getLastPlayerOpened() != null && !hopper.getLastPlayerOpened().equals(player.getUniqueId())) { + plugin.getLocale().getMessage("event.hopper.syncdidnotplace").sendPrefixedMessage(player); + return; + } + plugin.getPlayerDataManager().getPlayerData(player).setSyncType(SyncType.REGULAR); + hopper.clearLinkedBlocks(); + plugin.getLocale().getMessage("event.hopper.syncnext").sendPrefixedMessage(player); + hopper.timeout(player); + } + player.closeInventory(); + }); } else if (canTeleport) { - inventory.setItem(slot, perl); + setButton(slot, perl, + (event) -> { + if (event.clickType == ClickType.LEFT) { + if (hopper.getLinkedBlocks() != null) { + plugin.getTeleportHandler().tpEntity(player, hopper); + player.closeInventory(); + } + } else { + if (hopper.getTeleportTrigger() == TeleportTrigger.DISABLED) { + hopper.setTeleportTrigger(TeleportTrigger.SNEAK); + } else if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK) { + hopper.setTeleportTrigger(TeleportTrigger.WALK_ON); + } else if (hopper.getTeleportTrigger() == TeleportTrigger.WALK_ON) { + hopper.setTeleportTrigger(TeleportTrigger.DISABLED); + } + constructGUI(); + } + }); canTeleport = false; } else if (canFilter) { - inventory.setItem(slot, filter); + setButton(slot, filter, + (event) -> guiManager.showGUI(player, new GUIFilter(plugin, hopper, player))); canFilter = false; } else { if (modules.isEmpty()) break; Module module = modules.get(0); modules.remove(module); - inventory.setItem(slot, module.getGUIButton(hopper)); + setButton(slot, module.getGUIButton(hopper), + (event) -> module.runButtonPress(player, hopper, event.clickType)); } } - if (plugin.getConfig().getBoolean("Main.Allow hopper Upgrading")) { - ItemStack itemXP = new ItemStack(Material.valueOf(plugin.getConfig().getString("Interfaces.XP Icon")), 1); - ItemMeta itemmetaXP = itemXP.getItemMeta(); - itemmetaXP.setDisplayName(plugin.getLocale().getMessage("interface.hopper.upgradewithxp").getMessage()); - ArrayList loreXP = new ArrayList<>(); - if (nextLevel != null) - loreXP.add(plugin.getLocale().getMessage("interface.hopper.upgradewithxplore") - .processPlaceholder("cost", nextLevel.getCostExperience()).getMessage()); - else - loreXP.add(plugin.getLocale().getMessage("interface.hopper.alreadymaxed").getMessage()); - itemmetaXP.setLore(loreXP); - itemXP.setItemMeta(itemmetaXP); - - ItemStack itemECO = new ItemStack(Material.valueOf(plugin.getConfig().getString("Interfaces.Economy Icon")), 1); - ItemMeta itemmetaECO = itemECO.getItemMeta(); - itemmetaECO.setDisplayName(plugin.getLocale().getMessage("interface.hopper.upgradewitheconomy") - .getMessage()); - ArrayList loreECO = new ArrayList<>(); - if (nextLevel != null) - loreECO.add(plugin.getLocale().getMessage("interface.hopper.upgradewitheconomylore") - .processPlaceholder("cost", Methods.formatEconomy(nextLevel.getCostEconomy())) - .getMessage()); - else - loreECO.add(plugin.getLocale().getMessage("interface.hopper.alreadymaxed").getMessage()); - itemmetaECO.setLore(loreECO); - itemECO.setItemMeta(itemmetaECO); - - if (plugin.getConfig().getBoolean("Main.Upgrade With XP") - && player.hasPermission("EpicHoppers.Upgrade.XP") - && level.getCostExperience() != -1) { - inventory.setItem(11, itemXP); - - registerClickable(11, ((player, inventory, cursor, slot, type) -> { - hopper.upgrade(player, CostType.EXPERIENCE); - this.hopper.overview(player); - })); + if (Settings.HOPPER_UPGRADING.getBoolean()) { + if (Settings.UPGRADE_WITH_XP.getBoolean() + && level.getCostExperience() != -1 + && player.hasPermission("EpicHoppers.Upgrade.XP")) { + setButton(1, 2, GuiUtils.createButtonItem( + Settings.XP_ICON.getMaterial(CompatibleMaterial.EXPERIENCE_BOTTLE), + plugin.getLocale().getMessage("interface.hopper.upgradewithxp").getMessage(), + nextLevel != null + ? plugin.getLocale().getMessage("interface.hopper.upgradewithxplore") + .processPlaceholder("cost", nextLevel.getCostExperience()).getMessage() + : plugin.getLocale().getMessage("interface.hopper.alreadymaxed").getMessage()), + (event) -> { + hopper.upgrade(player, CostType.EXPERIENCE); + hopper.overview(guiManager, player); + }); } - - if (plugin.getConfig().getBoolean("Main.Upgrade With Economy") - && player.hasPermission("EpicHoppers.Upgrade.ECO") - && level.getCostEconomy() != -1) { - inventory.setItem(15, itemECO); - - registerClickable(15, ((player, inventory, cursor, slot, type) -> { - hopper.upgrade(player, CostType.ECONOMY); - this.hopper.overview(player); - })); + if (Settings.UPGRADE_WITH_ECONOMY.getBoolean() + && level.getCostEconomy() != -1 + && player.hasPermission("EpicHoppers.Upgrade.ECO")) { + setButton(1, 6, GuiUtils.createButtonItem( + Settings.ECO_ICON.getMaterial(CompatibleMaterial.SUNFLOWER), + plugin.getLocale().getMessage("interface.hopper.upgradewitheconomy").getMessage(), + nextLevel != null + ? plugin.getLocale().getMessage("interface.hopper.upgradewitheconomylore") + .processPlaceholder("cost", Methods.formatEconomy(nextLevel.getCostEconomy())).getMessage() + : plugin.getLocale().getMessage("interface.hopper.alreadymaxed").getMessage()), + (event) -> { + hopper.upgrade(player, CostType.ECONOMY); + hopper.overview(guiManager, player); + }); } } - inventory.setItem(13, item); - - inventory.setItem(0, Methods.getBackgroundGlass(true)); - inventory.setItem(1, Methods.getBackgroundGlass(true)); - inventory.setItem(2, Methods.getBackgroundGlass(false)); - inventory.setItem(6, Methods.getBackgroundGlass(false)); - inventory.setItem(7, Methods.getBackgroundGlass(true)); - inventory.setItem(8, Methods.getBackgroundGlass(true)); - inventory.setItem(9, Methods.getBackgroundGlass(true)); - inventory.setItem(10, Methods.getBackgroundGlass(false)); - inventory.setItem(16, Methods.getBackgroundGlass(false)); - inventory.setItem(17, Methods.getBackgroundGlass(true)); - inventory.setItem(18, Methods.getBackgroundGlass(true)); - inventory.setItem(19, Methods.getBackgroundGlass(true)); - inventory.setItem(20, Methods.getBackgroundGlass(false)); - inventory.setItem(24, Methods.getBackgroundGlass(false)); - inventory.setItem(25, Methods.getBackgroundGlass(true)); - inventory.setItem(26, Methods.getBackgroundGlass(true)); + setItem(13, item); hopper.setLastPlayerOpened(player.getUniqueId()); } @@ -243,60 +252,4 @@ public class GUIOverview extends AbstractGUI { private void runTask() { task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::constructGUI, 5L, 5L); } - - @Override - protected void registerClickables() { - - registerClickable(3, 23, ((player, inventory, cursor, slot, type) -> { - for (Module module : hopper.getLevel().getRegisteredModules()) { - if (module.getGUIButton(hopper) != null && !module.getGUIButton(hopper).getItemMeta() - .getDisplayName().equalsIgnoreCase(inventory.getItem(slot).getItemMeta().getDisplayName())) - continue; - - module.runButtonPress(player, hopper, type); - } - if (inventory.getItem(slot).getItemMeta() - .getDisplayName().equals(plugin.getLocale().getMessage("interface.hopper.filtertitle").getMessage())) { - new GUIFilter(plugin, hopper, player); - } else if (inventory.getItem(slot).getItemMeta() - .getDisplayName().equals(plugin.getLocale().getMessage("interface.hopper.perltitle").getMessage())) { - if (type == ClickType.LEFT) { - if (hopper.getLinkedBlocks() != null) { - plugin.getTeleportHandler().tpEntity(player, hopper); - player.closeInventory(); - } - } else { - if (hopper.getTeleportTrigger() == TeleportTrigger.DISABLED) { - hopper.setTeleportTrigger(TeleportTrigger.SNEAK); - } else if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK) { - hopper.setTeleportTrigger(TeleportTrigger.WALK_ON); - } else if (hopper.getTeleportTrigger() == TeleportTrigger.WALK_ON) { - hopper.setTeleportTrigger(TeleportTrigger.DISABLED); - } - constructGUI(); - } - } else if (inventory.getItem(slot).getItemMeta() - .getDisplayName().equals(plugin.getLocale().getMessage("interface.hopper.synchopper").getMessage())) { - if (type == ClickType.RIGHT) { - plugin.getLocale().getMessage("event.hopper.desync").sendPrefixedMessage(player); - hopper.clearLinkedBlocks(); - } else { - if (hopper.getLastPlayerOpened() != null && !hopper.getLastPlayerOpened().equals(player.getUniqueId())) { - plugin.getLocale().getMessage("event.hopper.syncdidnotplace").sendPrefixedMessage(player); - return; - } - plugin.getPlayerDataManager().getPlayerData(player).setSyncType(SyncType.REGULAR); - hopper.clearLinkedBlocks(); - plugin.getLocale().getMessage("event.hopper.syncnext").sendPrefixedMessage(player); - hopper.timeout(player); - } - player.closeInventory(); - } - })); - } - - @Override - protected void registerOnCloses() { - registerOnClose(((player1, inventory1) -> Bukkit.getScheduler().cancelTask(task))); - } } diff --git a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java b/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java index f3c82ec..81f1071 100644 --- a/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java +++ b/src/main/java/com/songoda/epichoppers/handlers/TeleportHandler.java @@ -1,9 +1,9 @@ package com.songoda.epichoppers.handlers; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.TeleportTrigger; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -74,7 +74,8 @@ public class TeleportHandler { /** * Recursively gets the next hopper in the linked hopper chain - * @param lastHopper The previous hopper found in the chain + * + * @param lastHopper The previous hopper found in the chain * @param currentChainLength The current length of the chain, used to cap the search length * @return The hopper at the end of the chain (or up to 15 in depth) */ @@ -98,14 +99,14 @@ public class TeleportHandler { location.setPitch(entity.getLocation().getPitch()); location.setDirection(entity.getLocation().getDirection()); - if (this.instance.isServerVersionAtLeast(ServerVersion.V1_12)) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) { Methods.doParticles(entity, location); Methods.doParticles(entity, entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation()); } entity.teleport(location); - if (this.instance.isServerVersionAtLeast(ServerVersion.V1_12)) - entity.getWorld().playSound(entity.getLocation(), this.instance.isServerVersion(ServerVersion.V1_12) ? Sound.valueOf("ENTITY_ENDERMEN_TELEPORT") : Sound.ENTITY_ENDERMAN_TELEPORT, 10, 10); + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) + entity.getWorld().playSound(entity.getLocation(), ServerVersion.isServerVersion(ServerVersion.V1_12) ? Sound.valueOf("ENTITY_ENDERMEN_TELEPORT") : Sound.ENTITY_ENDERMAN_TELEPORT, 10, 10); } } diff --git a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java index b7c4388..aba7ea7 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java +++ b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java @@ -1,26 +1,20 @@ package com.songoda.epichoppers.hopper; +import com.songoda.core.compatibility.ServerVersion; +import com.songoda.core.gui.GuiManager; +import com.songoda.core.hooks.EconomyManager; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.gui.GUIOverview; import com.songoda.epichoppers.hopper.levels.Level; import com.songoda.epichoppers.player.PlayerData; import com.songoda.epichoppers.utils.CostType; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.TeleportTrigger; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Sound; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; /** * Created by songoda on 3/14/2017. @@ -42,7 +36,7 @@ public class Hopper { this.location = location; } - public void overview(Player player) { + public void overview(GuiManager guiManager, Player player) { if (lastPlayerOpened != null && lastPlayerOpened != player.getUniqueId() && Bukkit.getPlayer(lastPlayerOpened) != null) { @@ -52,7 +46,7 @@ public class Hopper { EpicHoppers instance = EpicHoppers.getInstance(); if (!player.hasPermission("epichoppers.overview")) return; - new GUIOverview(instance, this, player); + guiManager.showGUI(player, new GUIOverview(instance, this, player)); } public void upgrade(Player player, CostType type) { @@ -63,15 +57,15 @@ public class Hopper { int cost = type == CostType.ECONOMY ? level.getCostEconomy() : level.getCostExperience(); if (type == CostType.ECONOMY) { - if (plugin.getEconomy() == null) { + if (!EconomyManager.isEnabled()) { player.sendMessage("Economy not enabled."); return; } - if (!plugin.getEconomy().hasBalance(player, cost)) { + if (!EconomyManager.hasBalance(player, cost)) { plugin.getInstance().getLocale().getMessage("event.upgrade.cannotafford").sendPrefixedMessage(player); return; } - plugin.getEconomy().withdrawBalance(player, cost); + EconomyManager.withdrawBalance(player, cost); upgradeFinal(level, player); } else if (type == CostType.EXPERIENCE) { if (player.getLevel() >= cost || player.getGameMode() == GameMode.CREATIVE) { @@ -98,7 +92,7 @@ public class Hopper { } Location loc = location.clone().add(.5, .5, .5); - if (!plugin.isServerVersionAtLeast(ServerVersion.V1_12)) return; + if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) return; player.getWorld().spawnParticle(org.bukkit.Particle.valueOf(plugin.getConfig().getString("Main.Upgrade Particle Type")), loc, 200, .5, .5, .5); @@ -107,7 +101,7 @@ public class Hopper { } else { player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 2F, 25.0F); - if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13)) return; + if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) return; player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 2F, 25.0F); Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.2F, 35.0F), 5L); @@ -117,7 +111,7 @@ public class Hopper { private void syncName() { org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) location.getBlock().getState(); - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_10)) + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_10)) hopper.setCustomName(Methods.formatName(level.getLevel(), false)); hopper.update(true); } diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/Module.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/Module.java index ef15109..09e0211 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/Module.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/Module.java @@ -1,8 +1,8 @@ package com.songoda.epichoppers.hopper.levels.modules; +import com.songoda.core.configuration.Config; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; -import com.songoda.epichoppers.utils.ConfigWrapper; import com.songoda.epichoppers.utils.Methods; import com.songoda.epichoppers.utils.StorageContainerCache; import org.bukkit.Material; @@ -17,15 +17,19 @@ import java.util.Map; public abstract class Module { - private static final Map configs = new HashMap<>(); + private static final Map configs = new HashMap<>(); protected final EpicHoppers plugin; - private final ConfigWrapper config; + private final Config config; public Module(EpicHoppers plugin) { this.plugin = plugin; - if (!configs.containsKey(getName())) - configs.put(getName(), new ConfigWrapper(plugin, File.separator + "modules", getName() + ".yml")); + if (!configs.containsKey(getName())) { + Config config = new Config(plugin, File.separator + "modules", getName() + ".yml"); + configs.put(getName(), config); + config.load(); + + } this.config = configs.get(getName()); } @@ -46,7 +50,7 @@ public abstract class Module { } public void saveData(Hopper hopper, String setting, Object value, Object toCache) { - config.getConfig().set("data." + Methods.serializeLocation(hopper.getLocation()) + "." + setting, value); + config.set("data." + Methods.serializeLocation(hopper.getLocation()) + "." + setting, value); modifyDataCache(hopper, setting, toCache); } @@ -59,17 +63,17 @@ public abstract class Module { if (hopper.isDataCachedInModuleCache(cacheStr)) return hopper.getDataFromModuleCache(cacheStr); - Object data = config.getConfig().get("data." + Methods.serializeLocation(hopper.getLocation()) + "." + setting); + Object data = config.get("data." + Methods.serializeLocation(hopper.getLocation()) + "." + setting); modifyDataCache(hopper, setting, data); return data; } public void clearData(Hopper hopper) { - config.getConfig().set("data." + Methods.serializeLocation(hopper.getLocation()), null); + config.set("data." + Methods.serializeLocation(hopper.getLocation()), null); hopper.clearModuleCache(); } public void saveDataToFile() { - config.saveConfig(); + config.save(); } } diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java index 3cd2220..2a6cd25 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoCrafting.java @@ -1,12 +1,12 @@ package com.songoda.epichoppers.hopper.levels.modules; +import com.songoda.core.compatibility.CompatibleMaterial; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.gui.GUICrafting; import com.songoda.epichoppers.hopper.Hopper; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.StorageContainerCache; -import com.songoda.epichoppers.utils.settings.Setting; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -17,12 +17,7 @@ import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.inventory.meta.ItemMeta; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; @@ -35,7 +30,7 @@ public class ModuleAutoCrafting extends Module { public ModuleAutoCrafting(EpicHoppers plugin) { super(plugin); - crafterEjection = Setting.AUTOCRAFT_JAM_EJECT.getBoolean(); + crafterEjection = Settings.AUTOCRAFT_JAM_EJECT.getBoolean(); } @Override @@ -50,9 +45,9 @@ public class ModuleAutoCrafting extends Module { return; // jam check: is this hopper gummed up? - if(crafterEjection) { + if (crafterEjection) { final List allMaterials = getRecipes(toCraft).getAllMaterials(); - if(Stream.of(hopperCache.cachedInventory) + if (Stream.of(hopperCache.cachedInventory) .allMatch(item -> item != null && allMaterials.stream().anyMatch(mat -> mat == item.getType()))) { // Crafter can't function if there's nowhere to put the output // ¯\_(ツ)_/¯ @@ -68,7 +63,7 @@ public class ModuleAutoCrafting extends Module { for (SimpleRecipe recipe : getRecipes(toCraft).recipes) { // Do we have enough to craft this recipe? - for(ItemStack item : recipe.recipe) { + for (ItemStack item : recipe.recipe) { int amountHave = 0; for (ItemStack hopperItem : hopperCache.cachedInventory) { if (hopperItem != null && Methods.isSimilarMaterial(hopperItem, item)) @@ -86,7 +81,7 @@ public class ModuleAutoCrafting extends Module { return; // We're good! Remove the items used to craft! - for(ItemStack item : recipe.recipe) { + for (ItemStack item : recipe.recipe) { hopperCache.removeItems(item); } } @@ -94,7 +89,7 @@ public class ModuleAutoCrafting extends Module { @Override public ItemStack getGUIButton(Hopper hopper) { - ItemStack crafting = new ItemStack(EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CRAFTING_TABLE : Material.valueOf("WORKBENCH"), 1); + ItemStack crafting = CompatibleMaterial.CRAFTING_TABLE.getItem(); ItemMeta craftingmeta = crafting.getItemMeta(); craftingmeta.setDisplayName(EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.craftingtitle") .getMessage()); @@ -111,7 +106,7 @@ public class ModuleAutoCrafting extends Module { @Override public void runButtonPress(Player player, Hopper hopper, ClickType type) { - new GUICrafting(EpicHoppers.getInstance(), this, hopper, player); + EpicHoppers.getInstance().getGuiManager().showGUI(player, new GUICrafting(this, hopper, player)); } @Override @@ -151,7 +146,8 @@ public class ModuleAutoCrafting extends Module { ItemStack stack = recipe.getResult(); if (Methods.isSimilarMaterial(stack, toCraft)) recipes.addRecipe(recipe); - } catch (Throwable ignored) {} + } catch (Throwable ignored) { + } } } @@ -167,7 +163,8 @@ public class ModuleAutoCrafting extends Module { } if (!fromLog) { Material log = Material.getMaterial(toType.substring(0, toType.length() - 6) + "LOG"); - if(log != null) recipes.addRecipe(Collections.singletonList(new ItemStack(log)), new ItemStack(toCraft.getType(), 4)); + if (log != null) + recipes.addRecipe(Collections.singletonList(new ItemStack(log)), new ItemStack(toCraft.getType(), 4)); } } @@ -221,7 +218,7 @@ public class ModuleAutoCrafting extends Module { } public Recipes(Collection recipes) { - addRecipes(recipes); + addRecipes(recipes); } public List getRecipes() { @@ -234,7 +231,8 @@ public class ModuleAutoCrafting extends Module { public void addRecipe(Recipe recipe) { if (recipe instanceof ShapelessRecipe) { - addRecipe(((ShapelessRecipe) recipe).getIngredientList(), recipe.getResult());; + addRecipe(((ShapelessRecipe) recipe).getIngredientList(), recipe.getResult()); + ; } else if (recipe instanceof ShapedRecipe) { addRecipe(new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values()), recipe.getResult()); } @@ -244,22 +242,22 @@ public class ModuleAutoCrafting extends Module { // consense the recipe into a list of materials and how many of each Map mergedRecipe = new HashMap<>(); ingredientMap.stream() - .filter(item -> item != null) - .forEach(item -> { - ItemStack mergedItem = mergedRecipe.get(item.getType()); - if (mergedItem == null) { - mergedRecipe.put(item.getType(), item); - } else { - mergedItem.setAmount(mergedItem.getAmount() + 1); - } - }); + .filter(item -> item != null) + .forEach(item -> { + ItemStack mergedItem = mergedRecipe.get(item.getType()); + if (mergedItem == null) { + mergedRecipe.put(item.getType(), item); + } else { + mergedItem.setAmount(mergedItem.getAmount() + 1); + } + }); this.recipes.add(new SimpleRecipe(mergedRecipe.values(), result)); // Also keep a tally of what materials are possible for this craftable mergedRecipe.keySet().stream() - .filter(itemType -> itemType != null && !allTypes.contains(itemType)) - .forEach(itemType -> { - allTypes.add(itemType); - }); + .filter(itemType -> itemType != null && !allTypes.contains(itemType)) + .forEach(itemType -> { + allTypes.add(itemType); + }); } public void addRecipes(Collection recipes) { diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSell.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSell.java index 45bc9dc..95332fa 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSell.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleAutoSell.java @@ -1,11 +1,12 @@ package com.songoda.epichoppers.hopper.levels.modules; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.hooks.EconomyManager; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.StorageContainerCache; -import com.songoda.epichoppers.utils.settings.Setting; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -29,7 +30,7 @@ public class ModuleAutoSell extends Module { public ModuleAutoSell(EpicHoppers plugin, int timeOut) { super(plugin); this.timeOut = timeOut * 20; - this.hopperTickRate = Setting.HOP_TICKS.getInt(); + this.hopperTickRate = Settings.HOP_TICKS.getInt(); if (cachedSellPrices == null) cachedSellPrices = plugin.getConfig().getStringList("Main.AutoSell Prices"); } @@ -52,17 +53,17 @@ public class ModuleAutoSell extends Module { int amountSold = 0; double totalValue = 0; - if (plugin.getEconomy() == null) return; + if (!EconomyManager.isEnabled()) return; OfflinePlayer player = Bukkit.getOfflinePlayer(hopper.getPlacedBy()); - // -1 + // -1 for (int i = 0; i < hopperCache.cachedInventory.length; i++) { final ItemStack itemStack = hopperCache.cachedInventory[i]; if (itemStack == null) continue; double value; - if (Setting.AUTOSELL_SHOPGUIPLUS.getBoolean() && player.isOnline()) { + if (Settings.AUTOSELL_SHOPGUIPLUS.getBoolean() && player.isOnline()) { try { ItemStack clone = itemStack.clone(); clone.setAmount(1); @@ -84,7 +85,7 @@ public class ModuleAutoSell extends Module { } if (totalValue != 0) - plugin.getEconomy().deposit(player, totalValue); + EconomyManager.deposit(player, totalValue); if (totalValue != 0 && player.isOnline() && isNotifying(hopper)) { plugin.getLocale().getMessage("event.hopper.autosell") .processPlaceholder("items", amountSold) @@ -100,7 +101,7 @@ public class ModuleAutoSell extends Module { @Override public ItemStack getGUIButton(Hopper hopper) { - ItemStack sell = new ItemStack(EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? Material.SUNFLOWER : Material.valueOf("DOUBLE_PLANT"), 1); + ItemStack sell = CompatibleMaterial.SUNFLOWER.getItem(); ItemMeta sellmeta = sell.getItemMeta(); sellmeta.setDisplayName(EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.selltitle").getMessage()); ArrayList loreSell = new ArrayList<>(); diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleBlockBreak.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleBlockBreak.java index 4f5af86..cfe3e4b 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleBlockBreak.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleBlockBreak.java @@ -1,9 +1,9 @@ package com.songoda.epichoppers.hopper.levels.modules; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.StorageContainerCache; import org.bukkit.Location; import org.bukkit.Material; @@ -37,7 +37,7 @@ public class ModuleBlockBreak extends Module { if (cachedBlacklistTypes.isEmpty()) { cachedBlacklistTypes.addAll(plugin.getConfig().getStringList("Main.BlockBreak Blacklisted Blocks")); } - if (plugin.isServerVersionAtLeast(ServerVersion.V1_9) && cachedParticleEffectType == null) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9) && cachedParticleEffectType == null) { try { cachedParticleEffectType = Particle.valueOf(plugin.getConfig().getString("Main.BlockBreak Particle Type")); } catch (Exception e) { @@ -59,9 +59,9 @@ public class ModuleBlockBreak extends Module { // don't try to break stuff if we can't grab stuff // (for simplicity, just assume that no empty slots mean there's a good chance we won't be able to pick something new up) - if(Stream.of(hopperCache.cachedInventory) + if (Stream.of(hopperCache.cachedInventory) .allMatch(item -> item != null && item.getAmount() > 0)) - return; + return; Integer tick = blockTick.get(hopper); if (tick == null) { @@ -89,13 +89,13 @@ public class ModuleBlockBreak extends Module { return; // Let's break the block! - if (plugin.isServerVersionAtLeast(ServerVersion.V1_9)) + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) above.getWorld().playSound(above.getLocation(), Sound.BLOCK_STONE_BREAK, 1F, 1F); Location locationAbove = above.getLocation(); locationAbove.add(.5, .5, .5); // fancy break particle effects :} - if (plugin.isServerVersionAtLeast(ServerVersion.V1_9)) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) { float xx = (float) (0 + (Math.random() * .5)); float yy = (float) (0 + (Math.random() * .5)); float zz = (float) (0 + (Math.random() * .5)); @@ -103,9 +103,9 @@ public class ModuleBlockBreak extends Module { } boolean waterlogged = false; - if (plugin.isServerVersionAtLeast(ServerVersion.V1_13) + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) && above.getBlockData() instanceof org.bukkit.block.data.Waterlogged - && ((org.bukkit.block.data.Waterlogged)above.getBlockData()).isWaterlogged()) { + && ((org.bukkit.block.data.Waterlogged) above.getBlockData()).isWaterlogged()) { waterlogged = true; } diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java index 3597dfb..11655b7 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java @@ -1,10 +1,10 @@ package com.songoda.epichoppers.hopper.levels.modules; import com.bgsoftware.wildstacker.api.WildStackerAPI; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import com.songoda.epichoppers.utils.StorageContainerCache; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; @@ -33,6 +33,7 @@ public class ModuleSuction extends Module { private final static boolean ultimateStacker = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker"); private static boolean oldUltimateStacker; private static Method oldUltimateStacker_updateItemAmount; + static { if (ultimateStacker) { try { @@ -128,7 +129,7 @@ public class ModuleSuction extends Module { () -> blacklist.remove(item.getUniqueId()), 10L); } - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) { float xx = (float) (0 + (Math.random() * .1)); float yy = (float) (0 + (Math.random() * .1)); float zz = (float) (0 + (Math.random() * .1)); diff --git a/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java b/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java index 9e891ab..c0e26ce 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/BlockListeners.java @@ -1,11 +1,11 @@ package com.songoda.epichoppers.listeners; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.hopper.HopperBuilder; import com.songoda.epichoppers.hopper.levels.Level; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import org.bukkit.ChatColor; import org.bukkit.Chunk; import org.bukkit.Location; @@ -154,7 +154,7 @@ public class BlockListeners implements Listener { if (location.getBlock().getType() != Material.CHEST) return; if (event.getBlock().getType().name().contains("SHULKER") - || (instance.isServerVersionAtLeast(ServerVersion.V1_13) ? event.getBlock().getType() == Material.SPAWNER : event.getBlock().getType() == Material.valueOf("MOB_SPAWNER")) + || (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ? event.getBlock().getType() == Material.SPAWNER : event.getBlock().getType() == Material.valueOf("MOB_SPAWNER")) || event.getBlock().getType() == Material.HOPPER || event.getBlock().getType() == Material.DISPENSER) { return; @@ -179,7 +179,7 @@ public class BlockListeners implements Listener { } } - if (instance.isServerVersionAtLeast(ServerVersion.V1_12)) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) { event.setDropItems(false); return; } diff --git a/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java b/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java index e15b4ab..4e76487 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/HopperListeners.java @@ -1,24 +1,21 @@ package com.songoda.epichoppers.listeners; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.hopper.levels.modules.Module; import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; import com.songoda.epichoppers.utils.HopperDirection; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.BlockState; -import org.bukkit.block.Chest; -import org.bukkit.block.DoubleChest; import org.bukkit.entity.Minecart; import org.bukkit.entity.minecart.HopperMinecart; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; /** @@ -45,11 +42,11 @@ public class HopperListeners implements Listener { // Let EpicHoppers take over if the hopper is pointing down though if (destination.getHolder() instanceof HopperMinecart && source.getHolder() instanceof org.bukkit.block.Hopper - && HopperDirection.getDirection(((org.bukkit.block.Hopper)source.getHolder()).getRawData()) != HopperDirection.DOWN) + && HopperDirection.getDirection(((org.bukkit.block.Hopper) source.getHolder()).getRawData()) != HopperDirection.DOWN) return; // Shulker boxes have a mind of their own and relentlessly steal items from hoppers - if (this.instance.isServerVersionAtLeast(ServerVersion.V1_11) + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11) && destination.getHolder() instanceof org.bukkit.block.ShulkerBox && source.getHolder() instanceof org.bukkit.block.Hopper) { event.setCancelled(true); @@ -95,13 +92,13 @@ public class HopperListeners implements Listener { if (!toHopper.getFilter().getWhiteList().isEmpty()) { // is this item on the whitelist? allowItem = toHopper.getFilter().getWhiteList().stream().anyMatch(item -> Methods.isSimilarMaterial(toMove, item)); - if(!allowItem) { + if (!allowItem) { // can we change the item to something else? searchReplacement: - for(ItemStack sourceItem : source.getContents()) { - if(sourceItem != null && Methods.canMove(destination, sourceItem)) { - for(ItemStack item : toHopper.getFilter().getWhiteList()) { - if(Methods.isSimilarMaterial(sourceItem, item)) { + for (ItemStack sourceItem : source.getContents()) { + if (sourceItem != null && Methods.canMove(destination, sourceItem)) { + for (ItemStack item : toHopper.getFilter().getWhiteList()) { + if (Methods.isSimilarMaterial(sourceItem, item)) { moveInstead = new ItemStack(sourceItem); moveInstead.setAmount(1); break searchReplacement; @@ -148,7 +145,7 @@ public class HopperListeners implements Listener { if (!(source.getHolder() instanceof org.bukkit.block.Hopper)) return; - if(destinationLocation == null) + if (destinationLocation == null) return; // Handle hopper push events elsewhere diff --git a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java index 4f3610b..5aab8b3 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/InteractListeners.java @@ -91,7 +91,7 @@ public class InteractListeners implements Listener { Hopper hopper = instance.getHopperManager().getHopper(e.getClickedBlock()); playerData.setLastHopper(hopper); if (!player.getInventory().getItemInHand().getType().name().contains("PICKAXE")) { - hopper.overview(player); + hopper.overview(instance.getGuiManager(), player); e.setCancelled(true); return; } diff --git a/src/main/java/com/songoda/epichoppers/settings/Settings.java b/src/main/java/com/songoda/epichoppers/settings/Settings.java new file mode 100644 index 0000000..3f9e4db --- /dev/null +++ b/src/main/java/com/songoda/epichoppers/settings/Settings.java @@ -0,0 +1,121 @@ +package com.songoda.epichoppers.settings; + +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.configuration.Config; +import com.songoda.core.configuration.ConfigSetting; +import com.songoda.core.hooks.EconomyManager; +import com.songoda.epichoppers.EpicHoppers; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class Settings { + + static final Config config = EpicHoppers.getInstance().getCoreConfig(); + + public static final ConfigSetting HOPPER_UPGRADING = new ConfigSetting(config, "Main.Allow hopper Upgrading", true, + "Should hoppers be upgradable?"); + + public static final ConfigSetting UPGRADE_WITH_ECONOMY = new ConfigSetting(config, "Main.Upgrade With Economy", true, + "Should you be able to upgrade hoppers with economy?"); + + public static final ConfigSetting UPGRADE_WITH_XP = new ConfigSetting(config, "Main.Upgrade With XP", true, + "Should you be able to upgrade hoppers with experience?"); + + public static final ConfigSetting TELEPORT = new ConfigSetting(config, "Main.Allow Players To Teleport Through Hoppers", true, + "Should players be able to teleport through hoppers?"); + + public static final ConfigSetting ENDERCHESTS = new ConfigSetting(config, "Main.Support Enderchests", true, + "Should hoppers dump items into a player enderchests?"); + + public static final ConfigSetting PARTICLE_TYPE = new ConfigSetting(config, "Main.Upgrade Particle Type", "SPELL_WITCH", + "The type of particle shown when a hopper is upgraded."); + + public static final ConfigSetting HOP_TICKS = new ConfigSetting(config, "Main.Amount of Ticks Between Hops", 8L, + "The amount of ticks between hopper transfers."); + + public static final ConfigSetting AUTOSAVE = new ConfigSetting(config, "Main.Auto Save Interval In Seconds", 15, + "The amount of time in between saving to file.", + "This is purely a safety function to prevent against unplanned crashes or", + "restarts. With that said it is advised to keep this enabled.", + "If however you enjoy living on the edge, feel free to turn it off."); + + public static final ConfigSetting TELEPORT_TICKS = new ConfigSetting(config, "Main.Amount of Ticks Between Teleport", 10L, + "The cooldown between teleports. It prevents players", + "from getting stuck in a teleport loop."); + + public static final ConfigSetting SYNC_TIMEOUT = new ConfigSetting(config, "Main.Timeout When Syncing Hoppers", 300L, + "The amount of time in ticks a player has between hitting the hopper", + "Link button and performing the link. When the time is up the link event is canceled."); + + public static final ConfigSetting MAX_CHUNK = new ConfigSetting(config, "Main.Max Hoppers Per Chunk", -1, + "The maximum amount of hoppers per chunk."); + + public static final ConfigSetting BLOCKBREAK_PARTICLE = new ConfigSetting(config, "Main.BlockBreak Particle Type", "LAVA", + "The particle shown when the block break module performs a block break."); + + public static final ConfigSetting BLACKLIST = new ConfigSetting(config, "Main.BlockBreak Blacklisted Blocks", + Arrays.asList("BEDROCK", "END_PORTAL", "ENDER_PORTAL", "END_PORTAL_FRAME", "ENDER_PORTAL_FRAME", "PISTON_HEAD", "PISTON_EXTENSION", "RAIL", "RAILS", "ACTIVATOR_RAIL", "DETECTOR_RAIL", "POWERED_RAIL"), + "Anything listed here will not be broken by the block break module."); + + public static final ConfigSetting AUTOCRAFT_JAM_EJECT = new ConfigSetting(config, "Main.AutoCraft Jam Eject", false, + "AutoCraft module needs a free slot to craft items with.", + "Normally, crafting hoppers won't grab items that would fill that slot.", + "This option ejects items if that last slot is forcibly filled"); + + public static final ConfigSetting AUTOSELL_PRICES = new ConfigSetting(config, "Main.AutoSell Prices", + Arrays.asList("STONE,0.50", "COBBLESTONE,0.20", "IRON_ORE,0.35", "COAL_ORE,0.20"), + "These are the prices used by the auto sell module."); + + public static final ConfigSetting AUTOSELL_SHOPGUIPLUS = new ConfigSetting(config, "Main.Use ShopGuiPlus for Prices", false, + "Should prices be grabbed from ShopGuiPlus?", + "If ShopGuiPlus is not enabled or the player is offline the default price list will be used.", + "If this is something that you do not want then you should empty the default list."); + + public static final ConfigSetting ECONOMY_PLUGIN = new ConfigSetting(config, "Main.Economy", EconomyManager.getEconomy() == null ? "Vault" : EconomyManager.getEconomy().getName(), + "Which economy plugin should be used?", + "Supported plugins you have installed: \"" + EconomyManager.getManager().getRegisteredPlugins().stream().collect(Collectors.joining("\", \"")) + "\"."); + + public static final ConfigSetting ECO_ICON = new ConfigSetting(config, "Interfaces.Economy Icon", "SUNFLOWER"); + public static final ConfigSetting XP_ICON = new ConfigSetting(config, "Interfaces.XP Icon", "EXPERIENCE_BOTTLE"); + + public static final ConfigSetting GLASS_TYPE_1 = new ConfigSetting(config, "Interfaces.Glass Type 1", "GRAY_STAINED_GLASS_PANE"); + public static final ConfigSetting GLASS_TYPE_2 = new ConfigSetting(config, "Interfaces.Glass Type 2", "BLUE_STAINED_GLASS_PANE"); + public static final ConfigSetting GLASS_TYPE_3 = new ConfigSetting(config, "Interfaces.Glass Type 3", "LIGHT_BLUE_STAINED_GLASS_PANE"); + + public static final ConfigSetting LANGUGE_MODE = new ConfigSetting(config, "System.Language Mode", "en_US", + "The enabled language file.", + "More language files (if available) can be found in the plugins data folder."); + + /** + * In order to set dynamic economy comment correctly, this needs to be + * called after EconomyManager load + */ + public static void setupConfig() { + config.load(); + config.setAutoremove(true).setAutosave(true); + + // convert glass pane settings + int color; + if ((color = GLASS_TYPE_1.getInt(-1)) != -1) { + config.set(GLASS_TYPE_1.getKey(), CompatibleMaterial.getGlassPaneColor(color).name()); + } + if ((color = GLASS_TYPE_2.getInt(-1)) != -1) { + config.set(GLASS_TYPE_2.getKey(), CompatibleMaterial.getGlassPaneColor(color).name()); + } + if ((color = GLASS_TYPE_3.getInt(-1)) != -1) { + config.set(GLASS_TYPE_3.getKey(), CompatibleMaterial.getGlassPaneColor(color).name()); + } + + // convert economy settings + if (config.getBoolean("Economy.Use Vault Economy") && EconomyManager.getManager().isEnabled("Vault")) { + config.set("Main.Economy", "Vault"); + } else if (config.getBoolean("Economy.Use Reserve Economy") && EconomyManager.getManager().isEnabled("Reserve")) { + config.set("Main.Economy", "Reserve"); + } else if (config.getBoolean("Economy.Use Player Points Economy") && EconomyManager.getManager().isEnabled("PlayerPoints")) { + config.set("Main.Economy", "PlayerPoints"); + } + + config.saveChanges(); + } +} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/storage/Storage.java b/src/main/java/com/songoda/epichoppers/storage/Storage.java index 01c0915..eb45574 100644 --- a/src/main/java/com/songoda/epichoppers/storage/Storage.java +++ b/src/main/java/com/songoda/epichoppers/storage/Storage.java @@ -1,26 +1,23 @@ package com.songoda.epichoppers.storage; +import com.songoda.core.configuration.Config; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.boost.BoostData; import com.songoda.epichoppers.hopper.Hopper; -import com.songoda.epichoppers.utils.ConfigWrapper; import com.songoda.epichoppers.utils.Methods; -import org.bukkit.Material; import java.util.ArrayList; import java.util.List; public abstract class Storage { - protected final EpicHoppers instance; - protected final ConfigWrapper dataFile; + protected final EpicHoppers plugin; + protected final Config dataFile; - public Storage(EpicHoppers instance) { - this.instance = instance; - this.dataFile = new ConfigWrapper(instance, "", "data.yml"); - this.dataFile.createNewFile(null, "EpicHoppers Data File"); - this.dataFile.getConfig().options().copyDefaults(true); - this.dataFile.saveConfig(); + public Storage(EpicHoppers plugin) { + this.plugin = plugin; + this.dataFile = new Config(plugin, "data.yml"); + this.dataFile.load(); } public abstract boolean containsGroup(String group); diff --git a/src/main/java/com/songoda/epichoppers/storage/types/StorageMysql.java b/src/main/java/com/songoda/epichoppers/storage/types/StorageMysql.java deleted file mode 100644 index b7cd52e..0000000 --- a/src/main/java/com/songoda/epichoppers/storage/types/StorageMysql.java +++ /dev/null @@ -1,197 +0,0 @@ -package com.songoda.epichoppers.storage.types; - -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.utils.MySQLDatabase; -import com.songoda.epichoppers.storage.Storage; -import com.songoda.epichoppers.storage.StorageItem; -import com.songoda.epichoppers.storage.StorageRow; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class StorageMysql extends Storage { - - private static Map toSave = new HashMap<>(); - private static Map lastSave = null; - private MySQLDatabase database; - - public StorageMysql(EpicHoppers instance) { - super(instance); - this.database = new MySQLDatabase(instance); - } - - @Override - public boolean containsGroup(String group) { - try { - DatabaseMetaData dbm = database.getConnection().getMetaData(); - ResultSet rs = dbm.getTables(null, null, instance.getConfig().getString("Database.Prefix") + group, null); - if (rs.next()) { - return true; - } - } catch (SQLException e) { - e.printStackTrace(); - } - return false; - - } - - @Override - public List getRowsByGroup(String group) { - List rows = new ArrayList<>(); - try { - ResultSet set = database.getConnection().createStatement().executeQuery(String.format("SELECT * FROM `" + instance.getConfig().getString("Database.Prefix") + "%s`", group)); - while (set.next()) { - Map items = new HashMap<>(); - - String key = set.getString(1); - for (int i = 2; i <= set.getMetaData().getColumnCount(); i++) { - if (set.getObject(i) == null || set.getObject(i) == "") continue; - StorageItem item = new StorageItem(set.getObject(i)); - items.put(set.getMetaData().getColumnName(i), item); - } - StorageRow row = new StorageRow(key, items); - rows.add(row); - } - } catch (SQLException e) { - e.printStackTrace(); - } - return rows; - } - - @Override - public void prepareSaveItem(String group, StorageItem... items) { - toSave.put(group + "]" + items[0].asObject().toString(), items); - } - - @Override - public void doSave() { - this.updateData(instance); - - if (lastSave == null) - lastSave = new HashMap<>(toSave); - - if (toSave.isEmpty()) return; - Map nextSave = new HashMap<>(toSave); - - this.makeBackup(); - this.save(); - - toSave.clear(); - lastSave.clear(); - lastSave.putAll(nextSave); - } - - @Override - public void save() { - try { - Statement stmt = database.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); - - last: - for (Map.Entry last : lastSave.entrySet()) { - String lastKey = last.getKey().split("]")[0]; - String lastValue = last.getValue()[0].asObject().toString(); - - for (Map.Entry to : toSave.entrySet()) { - String toKey = to.getKey().split("]")[0]; - if (!toKey.equals(lastKey) - || !to.getValue()[0].asObject().equals(lastValue) - || to.getValue().length != last.getValue().length) - continue; - toSave.remove(to.getKey()); - for (int i = 0; i < to.getValue().length; i ++) { - if ((to.getValue()[i].asObject() != null && last.getValue()[i].asObject() == null) - || (last.getValue()[i].asObject() == null && to.getValue()[i].asObject() != null) - || (last.getValue()[i].asObject() != null && to.getValue()[i].asObject() != null - && !to.getValue()[i].asObject().toString().equals(last.getValue()[i].asObject().toString()))) { - //Update - StorageItem[] items = to.getValue(); - StringBuilder sql = new StringBuilder(String.format("UPDATE `" + instance.getConfig().getString("Database.Prefix") + "%s`", toKey)); - - sql.append(" SET"); - - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - String key = item.getKey().split("]")[0]; - sql.append(String.format("`%s` = '%s', ", key, item.asObject().toString())); - } - - sql = new StringBuilder(sql.substring(0, sql.length() - 2)); - - sql.append(String.format(" WHERE `%s`='%s'", last.getValue()[0].getKey(), last.getValue()[0].asObject().toString())); - - stmt.addBatch(sql.toString()); - - continue last; - } - } - // Already up to date. - - continue last; - } - //Was not found delete. - StringBuilder sql = new StringBuilder(String.format("DELETE FROM `" + instance.getConfig().getString("Database.Prefix") + "%s`", lastKey)); - sql.append(String.format(" WHERE `%s`='%s'", last.getValue()[0].getKey(), last.getValue()[0].asObject().toString())); - stmt.addBatch(sql.toString()); - - } - - for (Map.Entry to : toSave.entrySet()) { - String toKey = to.getKey().split("]")[0]; - //Add - StorageItem[] items = to.getValue(); - StringBuilder sql = new StringBuilder(String.format("INSERT INTO `" + instance.getConfig().getString("Database.Prefix") + "%s`", toKey)); - - sql.append(" ("); - - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - String key = item.getKey().split("]")[0]; - sql.append(String.format("`%s`, ", key)); - } - - sql = new StringBuilder(sql.substring(0, sql.length() - 2)); - - sql.append(") VALUES ("); - - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - sql.append(String.format("'%s', ", item.asObject().toString())); - } - - sql = new StringBuilder(sql.substring(0, sql.length() - 2)); - - sql.append(");"); - - stmt.addBatch(sql.toString()); - } - - stmt.executeBatch(); - - toSave.clear(); - - } catch (SQLException e) { - e.printStackTrace(); - } - } - - @Override - public void makeBackup() { - - } - - @Override - public void closeConnection() { - try { - database.getConnection().close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } -} - diff --git a/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java b/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java index 382573f..21c3304 100644 --- a/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java +++ b/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java @@ -12,30 +12,30 @@ import java.util.*; public class StorageYaml extends Storage { - private static final Map toSave = new HashMap<>(); - private static Map lastSave = null; + private final Map toSave = new HashMap<>(); + private Map lastSave = null; - public StorageYaml(EpicHoppers instance) { - super(instance); + public StorageYaml(EpicHoppers plugin) { + super(plugin); } @Override public boolean containsGroup(String group) { - return dataFile.getConfig().contains("data." + group); + return dataFile.contains("data." + group); } @Override public List getRowsByGroup(String group) { List rows = new ArrayList<>(); - ConfigurationSection currentSection = dataFile.getConfig().getConfigurationSection("data." + group); + ConfigurationSection currentSection = dataFile.getConfigurationSection("data." + group); for (String key : currentSection.getKeys(false)) { Map items = new HashMap<>(); - ConfigurationSection currentSection2 = dataFile.getConfig().getConfigurationSection("data." + group + "." + key); + ConfigurationSection currentSection2 = dataFile.getConfigurationSection("data." + group + "." + key); for (String key2 : currentSection2.getKeys(false)) { String path = "data." + group + "." + key + "." + key2; - items.put(key2, new StorageItem(dataFile.getConfig().get(path) instanceof MemorySection - ? convertToInLineList(path) : dataFile.getConfig().get(path))); + items.put(key2, new StorageItem(dataFile.get(path) instanceof MemorySection + ? convertToInLineList(path) : dataFile.get(path))); } if (items.isEmpty()) continue; StorageRow row = new StorageRow(key, items); @@ -46,8 +46,8 @@ public class StorageYaml extends Storage { private String convertToInLineList(String path) { StringBuilder converted = new StringBuilder(); - for (String key : dataFile.getConfig().getConfigurationSection(path).getKeys(false)) { - converted.append(key).append(":").append(dataFile.getConfig().getInt(path + "." + key)).append(";"); + for (String key : dataFile.getConfigurationSection(path).getKeys(false)) { + converted.append(key).append(":").append(dataFile.getInt(path + "." + key)).append(";"); } return converted.toString(); } @@ -62,7 +62,7 @@ public class StorageYaml extends Storage { @Override public void doSave() { - this.updateData(instance); + this.updateData(plugin); if (lastSave == null) lastSave = new HashMap<>(toSave); @@ -85,19 +85,19 @@ public class StorageYaml extends Storage { if (toSave.containsKey(entry.getKey())) { Object newValue = toSave.get(entry.getKey()); if (!entry.getValue().equals(newValue)) { - dataFile.getConfig().set(entry.getKey(), newValue); + dataFile.set(entry.getKey(), newValue); } toSave.remove(entry.getKey()); } else { - dataFile.getConfig().set(entry.getKey(), null); + dataFile.set(entry.getKey(), null); } } for (Map.Entry entry : toSave.entrySet()) { - dataFile.getConfig().set(entry.getKey(), entry.getValue()); + dataFile.set(entry.getKey(), entry.getValue()); } - dataFile.saveConfig(); + dataFile.save(); } catch (NullPointerException e) { e.printStackTrace(); } @@ -105,15 +105,15 @@ public class StorageYaml extends Storage { @Override public void makeBackup() { - File data = new File(instance.getDataFolder(), "data.yml"); - File dataClone = new File(instance.getDataFolder(), "data-backup-" + System.currentTimeMillis() + ".yml"); + File data = new File(plugin.getDataFolder(), "data.yml"); + File dataClone = new File(plugin.getDataFolder(), "data-backup-" + System.currentTimeMillis() + ".yml"); try { copyFile(data, dataClone); } catch (IOException e) { e.printStackTrace(); } Deque backups = new ArrayDeque<>(); - for (File file : Objects.requireNonNull(instance.getDataFolder().listFiles())) { + for (File file : Objects.requireNonNull(plugin.getDataFolder().listFiles())) { if (file.getName().toLowerCase().contains("data-backup")) { backups.add(file); } @@ -125,7 +125,7 @@ public class StorageYaml extends Storage { @Override public void closeConnection() { - dataFile.saveConfig(); + dataFile.save(); } private static void copyFile(File source, File dest) throws IOException { diff --git a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java index 6bab0ab..84340a2 100644 --- a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java +++ b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java @@ -5,10 +5,10 @@ import com.songoda.epichoppers.boost.BoostData; import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.modules.Module; import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; +import com.songoda.epichoppers.settings.Settings; import com.songoda.epichoppers.utils.HopperDirection; import com.songoda.epichoppers.utils.Methods; import com.songoda.epichoppers.utils.StorageContainerCache; -import com.songoda.epichoppers.utils.settings.Setting; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -46,7 +46,7 @@ public class HopTask extends BukkitRunnable { public HopTask(EpicHoppers plug) { plugin = plug; - this.hopTicks = Math.max(1, Setting.HOP_TICKS.getInt() / 2); // Purposeful integer division. Don't go below 1. + this.hopTicks = Math.max(1, Settings.HOP_TICKS.getInt() / 2); // Purposeful integer division. Don't go below 1. this.runTaskTimer(plugin, 0, 2); if ((this.hasFabledSkyBlock = (fabledSkyblockPlugin = Bukkit.getPluginManager().getPlugin("FabledSkyBlock")) != null)) { try { diff --git a/src/main/java/com/songoda/epichoppers/utils/ConfigWrapper.java b/src/main/java/com/songoda/epichoppers/utils/ConfigWrapper.java deleted file mode 100644 index 41d6c6c..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/ConfigWrapper.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.songoda.epichoppers.utils; - -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.java.JavaPlugin; - -import java.io.File; -import java.io.IOException; -import java.util.logging.Level; - -/** - * ConfigWrapper made by @clip - */ -public class ConfigWrapper { - - private final JavaPlugin plugin; - private final String folderName, fileName; - private FileConfiguration config; - private File configFile; - - public ConfigWrapper(final JavaPlugin instance, final String folderName, final String fileName) { - this.plugin = instance; - this.folderName = folderName; - this.fileName = fileName; - } - - public void createNewFile(final String message, final String header) { - reloadConfig(); - saveConfig(); - loadConfig(header); - - if (message != null) { - plugin.getLogger().info(message); - } - } - - public FileConfiguration getConfig() { - if (config == null) { - reloadConfig(); - } - return config; - } - - public void loadConfig(final String header) { - config.options().header(header); - config.options().copyDefaults(true); - saveConfig(); - } - - public void reloadConfig() { - if (configFile == null) { - configFile = new File(plugin.getDataFolder() + folderName, fileName); - } - config = YamlConfiguration.loadConfiguration(configFile); - } - - public void saveConfig() { - if (config == null || configFile == null) { - return; - } - try { - getConfig().save(configFile); - } catch (final IOException ex) { - plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/Methods.java b/src/main/java/com/songoda/epichoppers/utils/Methods.java index acf0602..72242aa 100644 --- a/src/main/java/com/songoda/epichoppers/utils/Methods.java +++ b/src/main/java/com/songoda/epichoppers/utils/Methods.java @@ -1,5 +1,6 @@ package com.songoda.epichoppers.utils; +import com.songoda.core.compatibility.ServerVersion; import com.songoda.epichoppers.EpicHoppers; import java.lang.reflect.Method; import org.bukkit.Bukkit; @@ -28,82 +29,6 @@ public class Methods { private static final Map serializeCache = new HashMap<>(); - public static boolean isLegacyFuel(Material material) { - - if (material == null) return false; - switch(material.ordinal() + 1) { - case 6: - case 7: - case 18: - case 26: - case 36: - case 48: - case 54: - case 55: - case 59: - case 66: - case 73: - case 85: - case 86: - case 97: - case 100: - case 101: - case 108: - case 127: - case 135: - case 136: - case 137: - case 144: - case 147: - case 152: - case 163: - case 164: - case 165: - case 172: - case 174: - case 184: - case 185: - case 186: - case 187: - case 188: - case 189: - case 190: - case 191: - case 192: - case 193: - case 206: - case 260: - case 262: - case 267: - case 268: - case 269: - case 270: - case 279: - case 280: - case 289: - case 322: - case 323: - case 326: - case 332: - case 345: - case 368: - case 424: - case 426: - case 427: - case 428: - case 429: - case 430: - case 443: - case 444: - case 445: - case 446: - case 447: - return true; - default: - return false; - } - } - public static boolean isSync(Player p) { if (p.getItemInHand().hasItemMeta() && p.getItemInHand().getType() != Material.AIR @@ -118,37 +43,8 @@ public class Methods { return false; } - public static ItemStack getGlass() { - EpicHoppers instance = EpicHoppers.getInstance(); - return Methods.getGlass(instance.getConfig().getBoolean("Interfaces.Replace Glass Type 1 With Rainbow Glass"), instance.getConfig().getInt("Interfaces.Glass Type 1")); - } - - public static ItemStack getBackgroundGlass(boolean type) { - EpicHoppers instance = EpicHoppers.getInstance(); - if (type) - return getGlass(false, instance.getConfig().getInt("Interfaces.Glass Type 2")); - else - return getGlass(false, instance.getConfig().getInt("Interfaces.Glass Type 3")); - } - - private static ItemStack getGlass(Boolean rainbow, int type) { - int randomNum = 1 + (int) (Math.random() * 6); - ItemStack glass; - if (rainbow) { - glass = new ItemStack(EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? - Material.LEGACY_STAINED_GLASS_PANE : Material.valueOf("STAINED_GLASS_PANE"), 1, (short) randomNum); - } else { - glass = new ItemStack(EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? - Material.LEGACY_STAINED_GLASS_PANE : Material.valueOf("STAINED_GLASS_PANE"), 1, (short) type); - } - ItemMeta glassmeta = glass.getItemMeta(); - glassmeta.setDisplayName("§l"); - glass.setItemMeta(glassmeta); - return glass; - } - public static boolean isSimilarMaterial(ItemStack is1, ItemStack is2) { - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13)) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) { return is1.getType() == is2.getType(); } else { return is1.getType() == is2.getType() && (is1.getDurability() == -1 || is2.getDurability() == -1 || is1.getDurability() == is2.getDurability()); diff --git a/src/main/java/com/songoda/epichoppers/utils/Metrics.java b/src/main/java/com/songoda/epichoppers/utils/Metrics.java deleted file mode 100644 index 792f0cd..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/Metrics.java +++ /dev/null @@ -1,695 +0,0 @@ -package com.songoda.epichoppers.utils; - -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.RegisteredServiceProvider; -import org.bukkit.plugin.ServicePriority; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - -import javax.net.ssl.HttpsURLConnection; -import java.io.*; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.concurrent.Callable; -import java.util.logging.Level; -import java.util.zip.GZIPOutputStream; - -/** - * bStats collects some data for plugin authors. - *

- * Check out https://bStats.org/ to learn more about bStats! - */ -@SuppressWarnings({"WeakerAccess", "unused"}) -public class Metrics { - - static { - // You can use the property to disable the check in your test environment - if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) { - // Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D - final String defaultPackage = new String( - new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's', '.', 'b', 'u', 'k', 'k', 'i', 't'}); - final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'}); - // We want to make sure nobody just copy & pastes the example and use the wrong package names - if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage)) { - throw new IllegalStateException("bStats Metrics class has not been relocated correctly!"); - } - } - } - - // The version of this bStats class - public static final int B_STATS_VERSION = 1; - - // The url to which the data is sent - private static final String URL = "https://bStats.org/submitData/bukkit"; - - // Is bStats enabled on this server? - private boolean enabled; - - // Should failed requests be logged? - private static boolean logFailedRequests; - - // Should the sent data be logged? - private static boolean logSentData; - - // Should the response text be logged? - private static boolean logResponseStatusText; - - // The uuid of the server - private static String serverUUID; - - // The plugin - private final Plugin plugin; - - // A list with all custom charts - private final List charts = new ArrayList<>(); - - /** - * Class constructor. - * - * @param plugin The plugin which stats should be submitted. - */ - public Metrics(Plugin plugin) { - if (plugin == null) { - throw new IllegalArgumentException("Plugin cannot be null!"); - } - this.plugin = plugin; - - // Get the config file - File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); - File configFile = new File(bStatsFolder, "config.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); - - // Check if the config file exists - if (!config.isSet("serverUuid")) { - - // Add default values - config.addDefault("enabled", true); - // Every server gets it's unique random id. - config.addDefault("serverUuid", UUID.randomUUID().toString()); - // Should failed request be logged? - config.addDefault("logFailedRequests", false); - // Should the sent data be logged? - config.addDefault("logSentData", false); - // Should the response text be logged? - config.addDefault("logResponseStatusText", false); - - // Inform the server owners about bStats - config.options().header( - "bStats collects some data for plugin authors like how many servers are using their plugins.\n" + - "To honor their work, you should not disable it.\n" + - "This has nearly blacklist effect on the server performance!\n" + - "Check out https://bStats.org/ to learn more :)" - ).copyDefaults(true); - try { - config.save(configFile); - } catch (IOException ignored) { } - } - - // Load the data - enabled = config.getBoolean("enabled", true); - serverUUID = config.getString("serverUuid"); - logFailedRequests = config.getBoolean("logFailedRequests", false); - logSentData = config.getBoolean("logSentData", false); - logResponseStatusText = config.getBoolean("logResponseStatusText", false); - - if (enabled) { - boolean found = false; - // Search for all other bStats Metrics classes to see if we are the first one - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - service.getField("B_STATS_VERSION"); // Our identifier :) - found = true; // We aren't the first - break; - } catch (NoSuchFieldException ignored) { } - } - // Register our service - Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal); - if (!found) { - // We are the first! - startSubmitting(); - } - } - } - - /** - * Checks if bStats is enabled. - * - * @return Whether bStats is enabled or not. - */ - public boolean isEnabled() { - return enabled; - } - - /** - * Adds a custom chart. - * - * @param chart The chart to add. - */ - public void addCustomChart(CustomChart chart) { - if (chart == null) { - throw new IllegalArgumentException("Chart cannot be null!"); - } - charts.add(chart); - } - - /** - * Starts the Scheduler which submits our data every 30 minutes. - */ - private void startSubmitting() { - final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags - timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - if (!plugin.isEnabled()) { // Plugin was disabled - timer.cancel(); - return; - } - // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler - // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) - Bukkit.getScheduler().runTask(plugin, () -> submitData()); - } - }, 1000 * 60 * 5, 1000 * 60 * 30); - // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start - // WARNING: Changing the frequency has blacklist effect but your plugin WILL be blocked/deleted! - // WARNING: Just don't do it! - } - - /** - * Gets the plugin specific data. - * This method is called using Reflection. - * - * @return The plugin specific data. - */ - public JSONObject getPluginData() { - JSONObject data = new JSONObject(); - - String pluginName = plugin.getDescription().getName(); - String pluginVersion = plugin.getDescription().getVersion(); - - data.put("pluginName", pluginName); // Append the name of the plugin - data.put("pluginVersion", pluginVersion); // Append the version of the plugin - JSONArray customCharts = new JSONArray(); - for (CustomChart customChart : charts) { - // Add the data of the custom charts - JSONObject chart = customChart.getRequestJsonObject(); - if (chart == null) { // If the chart is null, we skip it - continue; - } - customCharts.add(chart); - } - data.put("customCharts", customCharts); - - return data; - } - - /** - * Gets the server specific data. - * - * @return The server specific data. - */ - private JSONObject getServerData() { - // Minecraft specific data - int playerAmount; - try { - // Around MC 1.8 the return type was changed to a collection from an array, - // This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection; - Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers"); - playerAmount = onlinePlayersMethod.getReturnType().equals(Collection.class) - ? ((Collection) onlinePlayersMethod.invoke(Bukkit.getServer())).size() - : ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length; - } catch (Exception e) { - playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed - } - int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; - String bukkitVersion = Bukkit.getVersion(); - - // OS/Java specific data - String javaVersion = System.getProperty("java.version"); - String osName = System.getProperty("os.name"); - String osArch = System.getProperty("os.arch"); - String osVersion = System.getProperty("os.version"); - int coreCount = Runtime.getRuntime().availableProcessors(); - - JSONObject data = new JSONObject(); - - data.put("serverUUID", serverUUID); - - data.put("playerAmount", playerAmount); - data.put("onlineMode", onlineMode); - data.put("bukkitVersion", bukkitVersion); - - data.put("javaVersion", javaVersion); - data.put("osName", osName); - data.put("osArch", osArch); - data.put("osVersion", osVersion); - data.put("coreCount", coreCount); - - return data; - } - - /** - * Collects the data and sends it afterwards. - */ - private void submitData() { - final JSONObject data = getServerData(); - - JSONArray pluginData = new JSONArray(); - // Search for all other bStats Metrics classes to get their plugin data - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - service.getField("B_STATS_VERSION"); // Our identifier :) - - for (RegisteredServiceProvider provider : Bukkit.getServicesManager().getRegistrations(service)) { - try { - pluginData.add(provider.getService().getMethod("getPluginData").invoke(provider.getProvider())); - } catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { } - } - } catch (NoSuchFieldException ignored) { } - } - - data.put("plugins", pluginData); - - // Create a new thread for the connection to the bStats server - new Thread(new Runnable() { - @Override - public void run() { - try { - // Send the data - sendData(plugin, data); - } catch (Exception e) { - // Something went wrong! :( - if (logFailedRequests) { - plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e); - } - } - } - }).start(); - } - - /** - * Sends the data to the bStats server. - * - * @param plugin Any plugin. It's just used to get a logger instance. - * @param data The data to send. - * @throws Exception If the request failed. - */ - private static void sendData(Plugin plugin, JSONObject data) throws Exception { - if (data == null) { - throw new IllegalArgumentException("Data cannot be null!"); - } - if (Bukkit.isPrimaryThread()) { - throw new IllegalAccessException("This method must not be called from the main thread!"); - } - if (logSentData) { - plugin.getLogger().info("Sending data to bStats: " + data.toString()); - } - HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection(); - - // Compress the data to save bandwidth - byte[] compressedData = compress(data.toString()); - - // Add headers - connection.setRequestMethod("POST"); - connection.addRequestProperty("Accept", "application/json"); - connection.addRequestProperty("Connection", "close"); - connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request - connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length)); - connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format - connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION); - - // Send data - connection.setDoOutput(true); - DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); - outputStream.write(compressedData); - outputStream.flush(); - outputStream.close(); - - InputStream inputStream = connection.getInputStream(); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - - StringBuilder builder = new StringBuilder(); - String line; - while ((line = bufferedReader.readLine()) != null) { - builder.append(line); - } - bufferedReader.close(); - if (logResponseStatusText) { - plugin.getLogger().info("Sent data to bStats and received response: " + builder.toString()); - } - } - - /** - * Gzips the given String. - * - * @param str The string to gzip. - * @return The gzipped String. - * @throws IOException If the compression failed. - */ - private static byte[] compress(final String str) throws IOException { - if (str == null) { - return null; - } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - GZIPOutputStream gzip = new GZIPOutputStream(outputStream); - gzip.write(str.getBytes(StandardCharsets.UTF_8)); - gzip.close(); - return outputStream.toByteArray(); - } - - /** - * Represents a custom chart. - */ - public static abstract class CustomChart { - - // The id of the chart - final String chartId; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - */ - CustomChart(String chartId) { - if (chartId == null || chartId.isEmpty()) { - throw new IllegalArgumentException("ChartId cannot be null or empty!"); - } - this.chartId = chartId; - } - - private JSONObject getRequestJsonObject() { - JSONObject chart = new JSONObject(); - chart.put("chartId", chartId); - try { - JSONObject data = getChartData(); - if (data == null) { - // If the data is null we don't send the chart. - return null; - } - chart.put("data", data); - } catch (Throwable t) { - if (logFailedRequests) { - Bukkit.getLogger().log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t); - } - return null; - } - return chart; - } - - protected abstract JSONObject getChartData() throws Exception; - - } - - /** - * Represents a custom simple pie. - */ - public static class SimplePie extends CustomChart { - - private final Callable callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SimplePie(String chartId, Callable callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - String value = callable.call(); - if (value == null || value.isEmpty()) { - // Null = skip the chart - return null; - } - data.put("value", value); - return data; - } - } - - /** - * Represents a custom advanced pie. - */ - public static class AdvancedPie extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public AdvancedPie(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == 0) { - continue; // Skip this invalid - } - allSkipped = false; - values.put(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - data.put("values", values); - return data; - } - } - - /** - * Represents a custom drilldown pie. - */ - public static class DrilldownPie extends CustomChart { - - private final Callable>> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public DrilldownPie(String chartId, Callable>> callable) { - super(chartId); - this.callable = callable; - } - - @Override - public JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); - Map> map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean reallyAllSkipped = true; - for (Map.Entry> entryValues : map.entrySet()) { - JSONObject value = new JSONObject(); - boolean allSkipped = true; - for (Map.Entry valueEntry : map.get(entryValues.getKey()).entrySet()) { - value.put(valueEntry.getKey(), valueEntry.getValue()); - allSkipped = false; - } - if (!allSkipped) { - reallyAllSkipped = false; - values.put(entryValues.getKey(), value); - } - } - if (reallyAllSkipped) { - // Null = skip the chart - return null; - } - data.put("values", values); - return data; - } - } - - /** - * Represents a custom single line chart. - */ - public static class SingleLineChart extends CustomChart { - - private final Callable callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SingleLineChart(String chartId, Callable callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - int value = callable.call(); - if (value == 0) { - // Null = skip the chart - return null; - } - data.put("value", value); - return data; - } - - } - - /** - * Represents a custom multi line chart. - */ - public static class MultiLineChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public MultiLineChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == 0) { - continue; // Skip this invalid - } - allSkipped = false; - values.put(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - data.put("values", values); - return data; - } - - } - - /** - * Represents a custom simple bar chart. - */ - public static class SimpleBarChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SimpleBarChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - for (Map.Entry entry : map.entrySet()) { - JSONArray categoryValues = new JSONArray(); - categoryValues.add(entry.getValue()); - values.put(entry.getKey(), categoryValues); - } - data.put("values", values); - return data; - } - - } - - /** - * Represents a custom advanced bar chart. - */ - public static class AdvancedBarChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public AdvancedBarChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue().length == 0) { - continue; // Skip this invalid - } - allSkipped = false; - JSONArray categoryValues = new JSONArray(); - for (int categoryValue : entry.getValue()) { - categoryValues.add(categoryValue); - } - values.put(entry.getKey(), categoryValues); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - data.put("values", values); - return data; - } - } - -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java b/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java deleted file mode 100644 index 4dfb20e..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.songoda.epichoppers.utils; - -import com.songoda.epichoppers.EpicHoppers; -import org.bukkit.Bukkit; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MySQLDatabase { - - private final EpicHoppers instance; - - private Connection connection; - - public MySQLDatabase(EpicHoppers instance) { - this.instance = instance; - try { - Class.forName("com.mysql.jdbc.Driver"); - - String url = "jdbc:mysql://" + instance.getConfig().getString("Database.IP") + ":" + instance.getConfig().getString("Database.Port") + "/" + instance.getConfig().getString("Database.Database Name") + "?autoReconnect=true&useSSL=false"; - this.connection = DriverManager.getConnection(url, instance.getConfig().getString("Database.Username"), instance.getConfig().getString("Database.Password")); - - //ToDo: This is sloppy - connection.createStatement().execute( - "CREATE TABLE IF NOT EXISTS `" + instance.getConfig().getString("Database.Prefix") + "sync` (\n" + - "\t`location` TEXT NULL,\n" + - "\t`level` INT NULL,\n" + - "\t`block` TEXT NULL,\n" + - "\t`placedby` TEXT NULL,\n" + - "\t`player` TEXT NULL,\n" + - "\t`teleporttrigger` TEXT NULL,\n" + - "\t`whitelist` TEXT NULL,\n" + - "\t`blacklist` TEXT NULL,\n" + - "\t`void` TEXT NULL,\n" + - "\t`black` TEXT NULL\n" + - ")"); - - connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `" + instance.getConfig().getString("Database.Prefix") + "boosts` (\n" + - "\t`endtime` TEXT NULL,\n" + - "\t`amount` INT NULL,\n" + - "\t`uuid` TEXT NULL\n" + - ")"); - - } catch (ClassNotFoundException | SQLException e) { - Bukkit.getLogger().severe("Database connection failed."); - Bukkit.getLogger().severe(e.getMessage()); - } - } - - public Connection getConnection() { - return connection; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/ServerVersion.java b/src/main/java/com/songoda/epichoppers/utils/ServerVersion.java deleted file mode 100644 index 34f6ca5..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/ServerVersion.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.songoda.epichoppers.utils; - -public enum ServerVersion { - - UNKNOWN("unknown_server_version"), - V1_7("org.bukkit.craftbukkit.v1_7"), - V1_8("org.bukkit.craftbukkit.v1_8"), - V1_9("org.bukkit.craftbukkit.v1_9"), - V1_10("org.bukkit.craftbukkit.v1_10"), - V1_11("org.bukkit.craftbukkit.v1_11"), - V1_12("org.bukkit.craftbukkit.v1_12"), - V1_13("org.bukkit.craftbukkit.v1_13"), - V1_14("org.bukkit.craftbukkit.v1_14"); - - - private final String packagePrefix; - - ServerVersion(String packagePrefix) { - this.packagePrefix = packagePrefix; - } - - public static ServerVersion fromPackageName(String packageName) { - for (ServerVersion version : values()) - if (packageName.startsWith(version.packagePrefix)) return version; - return ServerVersion.UNKNOWN; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java b/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java index 7e2b869..34f7e9c 100644 --- a/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java +++ b/src/main/java/com/songoda/epichoppers/utils/StorageContainerCache.java @@ -1,8 +1,7 @@ package com.songoda.epichoppers.utils; -import com.songoda.epichoppers.EpicHoppers; -import java.util.HashMap; -import java.util.Map; +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.compatibility.ServerVersion; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -13,6 +12,9 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; +import java.util.HashMap; +import java.util.Map; + /** * Persistent storage intended for streamlining read/write for storage * containers in large batches @@ -26,10 +28,10 @@ public class StorageContainerCache { Cache cache = inventoryCache.get(b); if (cache == null) { Material type = b.getType(); - if(type == Material.CHEST || type == Material.TRAPPED_CHEST) { + if (type == Material.CHEST || type == Material.TRAPPED_CHEST) { Block b2 = findAdjacentDoubleChest(b); //System.out.println("Adjacent to " + b + " = " + b2); - if(b2 != null && (cache = inventoryCache.get(b2)) != null) { + if (b2 != null && (cache = inventoryCache.get(b2)) != null) { return cache; } } @@ -49,14 +51,14 @@ public class StorageContainerCache { * @return */ public static Block findAdjacentDoubleChest(Block block) { - if(EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13)) { + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) { final BlockData d = block.getBlockData(); if (d instanceof Chest) { final Chest c = (Chest) d; - if(c.getType() != Chest.Type.SINGLE) { + if (c.getType() != Chest.Type.SINGLE) { // this is a double chest - check the other chest for registration data Block other = null; - switch(c.getFacing()) { + switch (c.getFacing()) { case SOUTH: other = block.getRelative(c.getType() != Chest.Type.RIGHT ? BlockFace.WEST : BlockFace.EAST); break; @@ -195,16 +197,16 @@ public class StorageContainerCache { /** * Add a number of items to this container's inventory later. * - * @param item item to add + * @param item item to add * @param amountToAdd how many of this item to attempt to add * @return how many items were added */ public int addAny(ItemStack item, int amountToAdd) { - + // Don't transfer shulker boxes into other shulker boxes, that's a bad idea. if (type.name().contains("SHULKER_BOX") && item.getType().name().contains("SHULKER_BOX")) return 0; - + int totalAdded = 0; if (cachedInventory != null && item != null) { final int maxStack = item.getMaxStackSize(); @@ -245,11 +247,11 @@ public class StorageContainerCache { public boolean addItem(ItemStack item) { if (cachedInventory == null || item == null || item.getAmount() <= 0) return false; - + // Don't transfer shulker boxes into other shulker boxes, that's a bad idea. if (type.name().contains("SHULKER_BOX") && item.getType().name().contains("SHULKER_BOX")) return false; - + // grab the amount to move and the max item stack size int toAdd = item.getAmount(); final int maxStack = item.getMaxStackSize(); @@ -278,12 +280,12 @@ public class StorageContainerCache { case "BLAST_FURNACE": case "BURNING_FURNACE": case "FURNACE": { - + check = new boolean[3]; - - boolean isFuel = !item.getType().name().contains("LOG") && (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? item.getType().isFuel() : Methods.isLegacyFuel(item.getType())); + + boolean isFuel = !item.getType().name().contains("LOG") && CompatibleMaterial.getMaterial(item.getType()).isFuel(); // fuel is 2nd slot, input is first - if (isFuel) + if (isFuel) check[1] = true; else check[0] = true; @@ -310,7 +312,7 @@ public class StorageContainerCache { // free space! toAdd -= Math.min(maxStack - cacheItem.getAmount(), toAdd); check[i] = true; - } else + } else check[i] = false; } } diff --git a/src/main/java/com/songoda/epichoppers/utils/gui/AbstractGUI.java b/src/main/java/com/songoda/epichoppers/utils/gui/AbstractGUI.java deleted file mode 100644 index 67a0f1b..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/gui/AbstractGUI.java +++ /dev/null @@ -1,220 +0,0 @@ -package com.songoda.epichoppers.utils.gui; - -import com.songoda.epichoppers.EpicHoppers; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.plugin.java.JavaPlugin; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.bukkit.event.EventPriority; - -public abstract class AbstractGUI implements Listener { - - private static boolean listenersInitialized = false; - protected Player player; - protected Inventory inventory = null; - protected boolean cancelBottom = false; - private Map clickables = new HashMap<>(); - private List onCloses = new ArrayList<>(); - private Map draggableRanges = new HashMap<>(); - - public AbstractGUI(Player player) { - this.player = player; - } - - public static void initializeListeners(JavaPlugin plugin) { - if (listenersInitialized) return; - - Bukkit.getPluginManager().registerEvents(new Listener() { - @EventHandler - public void onClickGUI(InventoryClickEvent event) { - Inventory inventory = event.getClickedInventory(); - if (inventory == null) return; - AbstractGUI gui = getGUIFromInventory(inventory); - Player player = (Player) event.getWhoClicked(); - - boolean bottom = false; - - InventoryType type = event.getClickedInventory().getType(); - if (type != InventoryType.CHEST && type != InventoryType.PLAYER) return; - - if (gui == null && event.getWhoClicked().getOpenInventory().getTopInventory() != null) { - Inventory top = event.getWhoClicked().getOpenInventory().getTopInventory(); - gui = getGUIFromInventory(top); - - if (gui != null && gui.cancelBottom) event.setCancelled(true); - bottom = true; - } - - if (gui == null) return; - - if (!bottom) event.setCancelled(true); - - if (!gui.draggableRanges.isEmpty() && !bottom) { - for (Map.Entry entry : gui.draggableRanges.entrySet()) { - Range range = entry.getKey(); - if (range.getMax() == range.getMin() && event.getSlot() == range.getMin() - || event.getSlot() >= range.getMin() && event.getSlot() <= range.getMax()) { - event.setCancelled(!entry.getValue()); - if (!entry.getValue()) break; - } - } - } - - Map entries = new HashMap<>(gui.clickables); - - for (Map.Entry entry : entries.entrySet()) { - Range range = entry.getKey(); - if (range.isBottom() && !bottom || !range.isBottom() && bottom || range.getClickType() != null && range.getClickType() != event.getClick()) - continue; - if (event.getSlot() >= range.getMin() && event.getSlot() <= range.getMax()) { - entry.getValue().Clickable(player, inventory, event.getCursor(), event.getSlot(), event.getClick()); - player.playSound(player.getLocation(), entry.getKey().getOnClickSound(), 1F, 1F); - } - } - } - - @EventHandler(priority = EventPriority.LOW) - public void onCloseGUI(InventoryCloseEvent event) { - Inventory inventory = event.getInventory(); - AbstractGUI gui = getGUIFromInventory(inventory); - - if (gui == null || gui.inventory == null) return; - - for (OnClose onClose : gui.onCloses) { - onClose.OnClose((Player) event.getPlayer(), inventory); - } - } - - private AbstractGUI getGUIFromInventory(Inventory inventory) { - if (inventory.getHolder() == null) return null; - InventoryHolder holder = inventory.getHolder(); - if (!(holder instanceof GUIHolder)) return null; - - return ((AbstractGUI.GUIHolder) holder).getGUI(); - } - }, plugin); - listenersInitialized = true; - } - - protected void init(String title, int slots) { - if(title.length() > 32) - title = title.substring(0, 31); - if (inventory == null - || inventory.getSize() != slots - || !ChatColor.translateAlternateColorCodes('&', title).equals(player.getOpenInventory().getTitle())) { - this.inventory = Bukkit.getServer().createInventory(new GUIHolder(), slots, ChatColor.translateAlternateColorCodes('&', title)); - registerClickables(); - registerOnCloses(); - } - constructGUI(); - initializeListeners(EpicHoppers.getInstance()); - player.openInventory(inventory); - } - - protected abstract void constructGUI(); - - protected void addDraggable(Range range, boolean option) { - this.draggableRanges.put(range, option); - } - - protected void removeDraggable() { - this.draggableRanges.clear(); - } - - protected abstract void registerClickables(); - - protected abstract void registerOnCloses(); - - protected ItemStack createButton(int slot, Inventory inventory, ItemStack item, String name, String... lore) { - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); - if (lore != null && lore.length != 0) { - List newLore = new ArrayList<>(); - for (String line : lore) newLore.add(ChatColor.translateAlternateColorCodes('&', line)); - meta.setLore(newLore); - } - item.setItemMeta(meta); - inventory.setItem(slot, item); - return item; - } - - protected ItemStack createButton(int slot, ItemStack item, String name, String... lore) { - return createButton(slot, inventory, item, name, lore); - } - - protected ItemStack createButton(int slot, Inventory inventory, Material material, String name, String... lore) { - return createButton(slot, inventory, new ItemStack(material), name, lore); - } - - protected ItemStack createButton(int slot, Material material, String name, String... lore) { - return createButton(slot, inventory, new ItemStack(material), name, lore); - } - - protected ItemStack createButton(int slot, Material material, String name, ArrayList lore) { - return createButton(slot, material, name, lore.toArray(new String[0])); - } - - protected void registerClickable(int min, int max, ClickType clickType, boolean bottom, Clickable clickable) { - clickables.put(new Range(min, max, clickType, bottom), clickable); - } - - protected void registerClickable(int min, int max, ClickType clickType, Clickable clickable) { - registerClickable(min, max, clickType, false, clickable); - } - - protected void registerClickable(int slot, ClickType clickType, Clickable clickable) { - registerClickable(slot, slot, clickType, false, clickable); - } - - protected void registerClickable(int min, int max, Clickable clickable) { - registerClickable(min, max, null, false, clickable); - } - - protected void registerClickable(int slot, boolean bottom, Clickable clickable) { - registerClickable(slot, slot, null, bottom, clickable); - } - - protected void registerClickable(int slot, Clickable clickable) { - registerClickable(slot, slot, null, false, clickable); - } - - protected void resetClickables() { - clickables.clear(); - } - - protected void registerOnClose(OnClose onClose) { - onCloses.add(onClose); - } - - public Inventory getInventory() { - return inventory; - } - - public class GUIHolder implements InventoryHolder { - - @Override - public Inventory getInventory() { - return inventory; - } - - public AbstractGUI getGUI() { - return AbstractGUI.this; - } - } -} diff --git a/src/main/java/com/songoda/epichoppers/utils/gui/Clickable.java b/src/main/java/com/songoda/epichoppers/utils/gui/Clickable.java deleted file mode 100644 index 2d0d558..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/gui/Clickable.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.songoda.epichoppers.utils.gui; - -import org.bukkit.entity.Player; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -public interface Clickable { - - void Clickable(Player player, Inventory inventory, ItemStack cursor, int slot, ClickType type); -} diff --git a/src/main/java/com/songoda/epichoppers/utils/gui/OnClose.java b/src/main/java/com/songoda/epichoppers/utils/gui/OnClose.java deleted file mode 100644 index af076cd..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/gui/OnClose.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.songoda.epichoppers.utils.gui; - -import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; - -public interface OnClose { - - void OnClose(Player player, Inventory inventory); - -} diff --git a/src/main/java/com/songoda/epichoppers/utils/gui/Range.java b/src/main/java/com/songoda/epichoppers/utils/gui/Range.java deleted file mode 100644 index 30e1194..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/gui/Range.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.songoda.epichoppers.utils.gui; - -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.utils.ServerVersion; -import org.bukkit.Sound; -import org.bukkit.event.inventory.ClickType; - -public class Range { - - private int min; - private int max; - private ClickType clickType; - private boolean bottom; - private Sound onClickSound; - - public Range(int min, int max, ClickType clickType, boolean bottom) { - this.min = min; - this.max = max; - this.clickType = clickType; - this.bottom = bottom; - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) onClickSound = Sound.UI_BUTTON_CLICK; - } - - public Range(int min, int max, Sound onClickSound, ClickType clickType, boolean bottom) { - this.min = min; - this.max = max; - this.onClickSound = onClickSound; - this.clickType = clickType; - this.bottom = bottom; - } - - public int getMin() { - return min; - } - - public int getMax() { - return max; - } - - public ClickType getClickType() { - return clickType; - } - - public boolean isBottom() { - return bottom; - } - - public Sound getOnClickSound() { - return onClickSound; - } -} diff --git a/src/main/java/com/songoda/epichoppers/utils/locale/Locale.java b/src/main/java/com/songoda/epichoppers/utils/locale/Locale.java deleted file mode 100644 index 3adc965..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/locale/Locale.java +++ /dev/null @@ -1,302 +0,0 @@ -package com.songoda.epichoppers.utils.locale; - -import org.bukkit.plugin.java.JavaPlugin; - -import java.io.*; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -/** - * Assists in the utilization of localization files. - * Created to be used by the Songoda Team. - * - * @author Brianna O'Keefe - Songoda - */ -public class Locale { - - private static final List LOCALES = new ArrayList<>(); - private static final Pattern NODE_PATTERN = Pattern.compile("(\\w+(?:\\.{1}\\w+)*)\\s*=\\s*\"(.*)\""); - private static final String FILE_EXTENSION = ".lang"; - private static JavaPlugin plugin; - private static File localeFolder; - - private final Map nodes = new HashMap<>(); - - private static String defaultLocale; - - private File file; - private String name; - - /** - * Instantiate the Locale class for future use - * - * @param name the name of the instantiated language - */ - private Locale(String name) { - if (plugin == null) - return; - - this.name = name; - - String fileName = name + FILE_EXTENSION; - this.file = new File(localeFolder, fileName); - - if (!this.reloadMessages()) return; - - plugin.getLogger().info("Loaded locale \"" + fileName + "\""); - } - - /** - * Initialize the class to load all existing language files and update them. - * This must be called before any other methods in this class as otherwise - * the methods will fail to invoke - * - * @param plugin the plugin instance - * @param defaultLocale the default language - */ - public Locale(JavaPlugin plugin, String defaultLocale) { - - Locale.plugin = plugin; - Locale.localeFolder = new File(plugin.getDataFolder(), "locales/"); - - if (!localeFolder.exists()) localeFolder.mkdirs(); - - //Save the default locale file. - Locale.defaultLocale = defaultLocale; - saveLocale(defaultLocale); - - for (File file : localeFolder.listFiles()) { - String fileName = file.getName(); - if (!fileName.endsWith(FILE_EXTENSION)) continue; - - String name = fileName.substring(0, fileName.lastIndexOf('.')); - - if (name.split("_").length != 2) continue; - if (localeLoaded(name)) continue; - - LOCALES.add(new Locale(name)); - } - } - - /** - * Save a locale file from the InputStream, to the locale folder - * - * @param fileName the name of the file to save - * @return true if the operation was successful, false otherwise - */ - public static boolean saveLocale(String fileName) { - return saveLocale(plugin.getResource(defaultLocale + FILE_EXTENSION), fileName); - } - - - /** - * Save a locale file from the InputStream, to the locale folder - * - * @param in file to save - * @param fileName the name of the file to save - * @return true if the operation was successful, false otherwise - */ - public static boolean saveLocale(InputStream in, String fileName) { - if (!localeFolder.exists()) localeFolder.mkdirs(); - - if (!fileName.endsWith(FILE_EXTENSION)) - fileName = (fileName.lastIndexOf(".") == -1 ? fileName : fileName.substring(0, fileName.lastIndexOf('.'))) + FILE_EXTENSION; - - File destinationFile = new File(localeFolder, fileName); - if (destinationFile.exists()) - return compareFiles(in, destinationFile); - - try (OutputStream outputStream = new FileOutputStream(destinationFile)) { - copy(in, outputStream); - - fileName = fileName.substring(0, fileName.lastIndexOf('.')); - - if (fileName.split("_").length != 2) return false; - - LOCALES.add(new Locale(fileName)); - if (defaultLocale == null) defaultLocale = fileName; - return true; - } catch (IOException e) { - return false; - } - } - - // Write new changes to existing files, if any at all - private static boolean compareFiles(InputStream in, File existingFile) { - InputStream defaultFile = - in == null ? plugin.getResource((defaultLocale != null ? defaultLocale : "en_US") + FILE_EXTENSION) : in; - - boolean changed = false; - - List defaultLines, existingLines; - try (BufferedReader defaultReader = new BufferedReader(new InputStreamReader(defaultFile)); - BufferedReader existingReader = new BufferedReader(new FileReader(existingFile)); - BufferedWriter writer = new BufferedWriter(new FileWriter(existingFile, true))) { - defaultLines = defaultReader.lines().collect(Collectors.toList()); - existingLines = existingReader.lines().map(s -> s.split("\\s*=")[0]).collect(Collectors.toList()); - - for (String defaultValue : defaultLines) { - if (defaultValue.isEmpty() || defaultValue.startsWith("#")) continue; - - String key = defaultValue.split("\\s*=")[0]; - - if (!existingLines.contains(key)) { - if (!changed) { - writer.newLine(); - writer.newLine(); - // Leave a note alerting the user of the newly added messages. - writer.write("# New messages for " + plugin.getName() + " v" + plugin.getDescription().getVersion() + "."); - - // If changes were found outside of the default file leave a note explaining that. - if (in == null) { - writer.newLine(); - writer.write("# These translations were found untranslated, join"); - writer.newLine(); - writer.write("# our translation Discord https://discord.gg/f7fpZEf"); - writer.newLine(); - writer.write("# to request an official update!"); - } - } - - writer.newLine(); - writer.write(defaultValue); - - changed = true; - } - } - if (in != null && !changed) compareFiles(null, existingFile); - } catch (IOException e) { - return false; - } - - return changed; - } - - - /** - * Check whether a locale exists and is registered or not - * - * @param name the whole language tag (i.e. "en_US") - * @return true if it exists - */ - public static boolean localeLoaded(String name) { - for (Locale locale : LOCALES) - if (locale.getName().equals(name)) return true; - return false; - } - - - /** - * Get a locale by its entire proper name (i.e. "en_US") - * - * @param name the full name of the locale - * @return locale of the specified name - */ - public static Locale getLocale(String name) { - for (Locale locale : LOCALES) - if (locale.getName().equalsIgnoreCase(name)) return locale; - return null; - } - - /** - * Clear the previous message cache and load new messages directly from file - * - * @return reload messages from file - */ - public boolean reloadMessages() { - if (!this.file.exists()) { - plugin.getLogger().warning("Could not find file for locale \"" + this.name + "\""); - return false; - } - - this.nodes.clear(); // Clear previous data (if any) - - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line; - for (int lineNumber = 0; (line = reader.readLine()) != null; lineNumber++) { - if (line.trim().isEmpty() || line.startsWith("#") /* Comment */) continue; - - Matcher matcher = NODE_PATTERN.matcher(line); - if (!matcher.find()) { - System.err.println("Invalid locale syntax at (line=" + lineNumber + ")"); - continue; - } - - nodes.put(matcher.group(1), matcher.group(2)); - } - } catch (IOException e) { - e.printStackTrace(); - return false; - } - return true; - } - - /** - * Supply the Message object with the plugins prefix. - * - * @param message message to be applied - * @return applied message - */ - private Message supplyPrefix(Message message) { - return message.setPrefix(this.nodes.getOrDefault("general.nametag.prefix", "[Plugin]")); - } - - /** - * Create a new unsaved Message - * - * @param message the message to create - * @return the created message - */ - public Message newMessage(String message) { - return supplyPrefix(new Message(message)); - } - - /** - * Get a message set for a specific node. - * - * @param node the node to get - * @return the message for the specified node - */ - public Message getMessage(String node) { - return this.getMessageOrDefault(node, node); - } - - /** - * Get a message set for a specific node - * - * @param node the node to get - * @param defaultValue the default value given that a value for the node was not found - * @return the message for the specified node. Default if none found - */ - public Message getMessageOrDefault(String node, String defaultValue) { - return supplyPrefix(new Message(this.nodes.getOrDefault(node, defaultValue))); - } - - /** - * Return the locale name (i.e. "en_US") - * - * @return the locale name - */ - public String getName() { - return name; - } - - private static void copy(InputStream input, OutputStream output) { - int n; - byte[] buffer = new byte[1024 * 4]; - - try { - while ((n = input.read(buffer)) != -1) { - output.write(buffer, 0, n); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/src/main/java/com/songoda/epichoppers/utils/locale/Message.java b/src/main/java/com/songoda/epichoppers/utils/locale/Message.java deleted file mode 100644 index 9e717d4..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/locale/Message.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.songoda.epichoppers.utils.locale; - -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -/** - * The Message object. This holds the message to be sent - * as well as the plugins prefix so that they can both be - * easily manipulated then deployed - */ -public class Message { - - private String prefix = null; - private String message; - - /** - * create a new message - * - * @param message the message text - */ - public Message(String message) { - this.message = message; - } - - /** - * Format and send the held message to a player - * - * @param player player to send the message to - */ - public void sendMessage(Player player) { - player.sendMessage(this.getMessage()); - } - - /** - * Format and send the held message with the - * appended plugin prefix to a player - * - * @param player player to send the message to - */ - public void sendPrefixedMessage(Player player) { - player.sendMessage(this.getPrefixedMessage()); - } - - /** - * Format and send the held message to a player - * - * @param sender command sender to send the message to - */ - public void sendMessage(CommandSender sender) { - sender.sendMessage(this.getMessage()); - } - - /** - * Format and send the held message with the - * appended plugin prefix to a command sender - * - * @param sender command sender to send the message to - */ - public void sendPrefixedMessage(CommandSender sender) { - sender.sendMessage(this.getPrefixedMessage()); - } - - /** - * Format the held message and append the plugins - * prefix - * - * @return the prefixed message - */ - public String getPrefixedMessage() { - return ChatColor.translateAlternateColorCodes('&',(prefix == null ? "" : this.prefix) - + " " + this.message); - } - - /** - * Get and format the held message - * - * @return the message - */ - public String getMessage() { - return ChatColor.translateAlternateColorCodes('&', this.message); - } - - /** - * Get the held message - * - * @return the message - */ - public String getUnformattedMessage() { - return this.message; - } - - /** - * Replace the provided placeholder with the - * provided object - * - * @param placeholder the placeholder to replace - * @param replacement the replacement object - * @return the modified Message - */ - public Message processPlaceholder(String placeholder, Object replacement) { - this.message = message.replace("%" + placeholder + "%", replacement.toString()); - return this; - } - - Message setPrefix(String prefix) { - this.prefix = prefix; - return this; - } - - @Override - public String toString() { - return this.message; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/settings/Category.java b/src/main/java/com/songoda/epichoppers/utils/settings/Category.java deleted file mode 100644 index 961dfc6..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/settings/Category.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.songoda.epichoppers.utils.settings; - -public enum Category { - - ECONOMY("Settings regarding economy.", - "Only one economy option can be used at a time. If you enable more than", - "one of these the first one will be used."), - MAIN("General settings and options."), - INTERFACES("These settings allow you to alter the way interfaces look.", - "They are used in GUI's to make patterns, change them up then open up a", - "GUI to see how it works."), - DATABASE("Settings regarding the Database."), - SYSTEM("System related settings."); - - private String[] comments; - - - Category(String... comments) { - this.comments = comments; - } - - public String[] getComments() { - return comments; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/settings/Setting.java b/src/main/java/com/songoda/epichoppers/utils/settings/Setting.java deleted file mode 100644 index 4d4dcae..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/settings/Setting.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.songoda.epichoppers.utils.settings; - -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.utils.ServerVersion; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public enum Setting { - - HOPPER_UPGRADING("Main.Allow hopper Upgrading", true, - "Should hoppers be upgradable?"), - - UPGRADE_WITH_ECONOMY("Main.Upgrade With Economy", true, - "Should you be able to upgrade hoppers with economy?"), - - UPGRADE_WITH_XP("Main.Upgrade With XP", true, - "Should you be able to upgrade hoppers with experience?"), - - TELEPORT("Main.Allow Players To Teleport Through Hoppers", true, - "Should players be able to teleport through hoppers?"), - - ENDERCHESTS("Main.Support Enderchests", true, - "Should hoppers dump items into a player enderchests?"), - - PARTICLE_TYPE("Main.Upgrade Particle Type", "SPELL_WITCH", - "The type of particle shown when a hopper is upgraded."), - - HOP_TICKS("Main.Amount of Ticks Between Hops", 8L, - "The amount of ticks between hopper transfers."), - - AUTOSAVE("Main.Auto Save Interval In Seconds", 15, - "The amount of time in between saving to file.", - "This is purely a safety function to prevent against unplanned crashes or", - "restarts. With that said it is advised to keep this enabled.", - "If however you enjoy living on the edge, feel free to turn it off."), - - TELEPORT_TICKS("Main.Amount of Ticks Between Teleport", 10L, - "The cooldown between teleports. It prevents players", - "from getting stuck in a teleport loop."), - - SYNC_TIMEOUT("Main.Timeout When Syncing Hoppers", 300L, - "The amount of time in ticks a player has between hitting the hopper", - "Link button and performing the link. When the time is up the link event is canceled."), - - MAX_CHUNK("Main.Max Hoppers Per Chunk", -1, - "The maximum amount of hoppers per chunk."), - - BLOCKBREAK_PARTICLE("Main.BlockBreak Particle Type", "LAVA", - "The particle shown when the block break module performs a block break."), - - BLACKLIST("Main.BlockBreak Blacklisted Blocks", - Arrays.asList("BEDROCK", "END_PORTAL", "ENDER_PORTAL", "END_PORTAL_FRAME", "ENDER_PORTAL_FRAME", "PISTON_HEAD", "PISTON_EXTENSION", "RAIL", "RAILS", "ACTIVATOR_RAIL", "DETECTOR_RAIL", "POWERED_RAIL"), - "Anything listed here will not be broken by the block break module."), - - AUTOCRAFT_JAM_EJECT("Main.AutoCraft Jam Eject", false, - "AutoCraft module needs a free slot to craft items with.", - "Normally, crafting hoppers won't grab items that would fill that slot.", - "This option ejects items if that last slot is forcibly filled"), - - AUTOSELL_PRICES("Main.AutoSell Prices", - Arrays.asList("STONE,0.50", "COBBLESTONE,0.20", "IRON_ORE,0.35", "COAL_ORE,0.20"), - "These are the prices used by the auto sell module."), - - AUTOSELL_SHOPGUIPLUS("Main.Use ShopGuiPlus for Prices", false, - "Should prices be grabbed from ShopGuiPlus?", - "If ShopGuiPlus is not enabled or the player is offline the default price list will be used.", - "If this is something that you do not want then you should empty the default list."), - - VAULT_ECONOMY("Economy.Use Vault Economy", true, - "Should Vault be used?"), - - RESERVE_ECONOMY("Economy.Use Reserve Economy", true, - "Should Reserve be used?"), - - PLAYER_POINTS_ECONOMY("Economy.Use Player Points Economy", false, - "Should PlayerPoints be used?"), - - RAINBOW("Interfaces.Replace Glass Type 1 With Rainbow Glass", false), - ECO_ICON("Interfaces.Economy Icon", EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "SUNFLOWER" : "DOUBLE_PLANT"), - XP_ICON("Interfaces.XP Icon", EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "EXPERIENCE_BOTTLE" : "EXP_BOTTLE"), - GLASS_TYPE_1("Interfaces.Glass Type 1", 7), - GLASS_TYPE_2("Interfaces.Glass Type 2", 11), - GLASS_TYPE_3("Interfaces.Glass Type 3", 3), - - DATABASE_SUPPORT("Database.Activate Mysql Support", false, - "Should MySQL be used for data storage?"), - - DATABASE_IP("Database.IP", "127.0.0.1", - "MySQL IP"), - - DATABASE_PORT("Database.Port", 3306, - "MySQL Port"), - - DATABASE_NAME("Database.Database Name", "EpicHoppers", - "The database you are inserting data into."), - - DATABASE_PREFIX("Database.Prefix", "EH-", - "The prefix for tables inserted into the database."), - - DATABASE_USERNAME("Database.Username", "PUT_USERNAME_HERE", - "MySQL Username"), - - DATABASE_PASSWORD("Database.Password", "PUT_PASSWORD_HERE", - "MySQL Password"), - - LANGUGE_MODE("System.Language Mode", "en_US", - "The enabled language file.", - "More language files (if available) can be found in the plugins data folder."); - - private String setting; - private Object option; - private String[] comments; - - Setting(String setting, Object option, String... comments) { - this.setting = setting; - this.option = option; - this.comments = comments; - } - - Setting(String setting, Object option) { - this.setting = setting; - this.option = option; - this.comments = null; - } - - public static Setting getSetting(String setting) { - List settings = Arrays.stream(values()).filter(setting1 -> setting1.setting.equals(setting)).collect(Collectors.toList()); - if (settings.isEmpty()) return null; - return settings.get(0); - } - - public String getSetting() { - return setting; - } - - public Object getOption() { - return option; - } - - public String[] getComments() { - return comments; - } - - public List getStringList() { - return EpicHoppers.getInstance().getConfig().getStringList(setting); - } - - public boolean getBoolean() { - return EpicHoppers.getInstance().getConfig().getBoolean(setting); - } - - public int getInt() { - return EpicHoppers.getInstance().getConfig().getInt(setting); - } - - public long getLong() { - return EpicHoppers.getInstance().getConfig().getLong(setting); - } - - public String getString() { - return EpicHoppers.getInstance().getConfig().getString(setting); - } - - public char getChar() { - return EpicHoppers.getInstance().getConfig().getString(setting).charAt(0); - } - - public double getDouble() { - return EpicHoppers.getInstance().getConfig().getDouble(setting); - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/settings/SettingsManager.java b/src/main/java/com/songoda/epichoppers/utils/settings/SettingsManager.java deleted file mode 100644 index 9583c6d..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/settings/SettingsManager.java +++ /dev/null @@ -1,311 +0,0 @@ -package com.songoda.epichoppers.utils.settings; - -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.ServerVersion; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.player.AsyncPlayerChatEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -import java.io.*; -import java.util.*; - -/** - * Created by songoda on 6/4/2017. - */ -public class SettingsManager implements Listener { - - private final EpicHoppers plugin; - private Map cat = new HashMap<>(); - private Map current = new HashMap<>(); - - public SettingsManager(EpicHoppers plugin) { - this.plugin = plugin; - Bukkit.getPluginManager().registerEvents(this, plugin); - } - - @EventHandler - public void onInventoryClick(InventoryClickEvent event) { - if (event.getView().getType() != InventoryType.CHEST) return; - ItemStack clickedItem = event.getCurrentItem(); - - if (event.getInventory() != event.getWhoClicked().getOpenInventory().getTopInventory() - || clickedItem == null || !clickedItem.hasItemMeta() - || !clickedItem.getItemMeta().hasDisplayName()) { - return; - } - - if (event.getView().getTitle().equals(plugin.getName() + " Settings Manager")) { - event.setCancelled(true); - if (clickedItem.getType().name().contains("STAINED_GLASS")) return; - - String type = ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName()); - this.cat.put((Player) event.getWhoClicked(), type); - this.openEditor((Player) event.getWhoClicked()); - } else if (event.getView().getTitle().equals(plugin.getName() + " Settings Editor")) { - event.setCancelled(true); - if (clickedItem.getType().name().contains("STAINED_GLASS")) return; - - Player player = (Player) event.getWhoClicked(); - - String key = cat.get(player) + "." + ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName()); - - if (plugin.getConfig().get(key).getClass().getName().equals("java.lang.Boolean")) { - this.plugin.getConfig().set(key, !plugin.getConfig().getBoolean(key)); - this.finishEditing(player); - } else { - this.editObject(player, key); - } - } - } - - @EventHandler - public void onChat(AsyncPlayerChatEvent event) { - Player player = event.getPlayer(); - if (!current.containsKey(player)) return; - - String value = current.get(player); - FileConfiguration config = plugin.getConfig(); - if (config.isLong(value)) { - config.set(value, Long.parseLong(event.getMessage())); - } else if (config.isInt(value)) { - config.set(value, Integer.parseInt(event.getMessage())); - } else if (config.isDouble(value)) { - config.set(value, Double.parseDouble(event.getMessage())); - } else if (config.isString(value)) { - config.set(value, event.getMessage()); - } - - Bukkit.getScheduler().scheduleSyncDelayedTask(EpicHoppers.getInstance(), () -> - this.finishEditing(player), 0L); - - event.setCancelled(true); - } - - private void finishEditing(Player player) { - this.current.remove(player); - this.saveConfig(); - this.openEditor(player); - } - - private void editObject(Player player, String current) { - this.current.put(player, ChatColor.stripColor(current)); - - player.closeInventory(); - player.sendMessage(""); - player.sendMessage(Methods.formatText("&7Please enter a value for &6" + current + "&7.")); - if (plugin.getConfig().isInt(current) || plugin.getConfig().isDouble(current)) { - player.sendMessage(Methods.formatText("&cUse only numbers.")); - } - player.sendMessage(""); - } - - public void openSettingsManager(Player player) { - Inventory inventory = Bukkit.createInventory(null, 27, plugin.getName() + " Settings Manager"); - ItemStack glass = Methods.getGlass(); - for (int i = 0; i < inventory.getSize(); i++) { - inventory.setItem(i, glass); - } - - int slot = 10; - for (String key : plugin.getConfig().getDefaultSection().getKeys(false)) { - ItemStack item = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.LEGACY_WOOL : Material.valueOf("WOOL"), 1, (byte) (slot - 9)); - ItemMeta meta = item.getItemMeta(); - meta.setLore(Collections.singletonList(Methods.formatText("&6Click To Edit This Category."))); - meta.setDisplayName(Methods.formatText("&f&l" + key)); - item.setItemMeta(meta); - inventory.setItem(slot, item); - slot++; - } - - player.openInventory(inventory); - } - - private void openEditor(Player player) { - Inventory inventory = Bukkit.createInventory(null, 54, plugin.getName() + " Settings Editor"); - FileConfiguration config = plugin.getConfig(); - - int slot = 0; - for (String key : config.getConfigurationSection(cat.get(player)).getKeys(true)) { - String fKey = cat.get(player) + "." + key; - ItemStack item = new ItemStack(Material.DIAMOND_HELMET); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Methods.formatText("&6" + key)); - - List lore = new ArrayList<>(); - if (config.isBoolean(fKey)) { - item.setType(Material.LEVER); - lore.add(Methods.formatText(config.getBoolean(fKey) ? "&atrue" : "&cfalse")); - } else if (config.isString(fKey)) { - item.setType(Material.PAPER); - lore.add(Methods.formatText("&7" + config.getString(fKey))); - } else if (config.isInt(fKey)) { - item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); - lore.add(Methods.formatText("&7" + config.getInt(fKey))); - } else if (config.isLong(fKey)) { - item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); - lore.add(Methods.formatText("&7" + config.getLong(fKey))); - } else if (config.isDouble(fKey)) { - item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); - lore.add(Methods.formatText("&7" + config.getDouble(fKey))); - } - - Setting setting = Setting.getSetting(fKey); - - if (setting != null && setting.getComments() != null) { - lore.add(""); - - String comment = String.join(" ", setting.getComments()); - - int lastIndex = 0; - for (int n = 0; n < comment.length(); n++) { - if (n - lastIndex < 30) - continue; - - if (comment.charAt(n) == ' ') { - lore.add(Methods.formatText("&8" + comment.substring(lastIndex, n).trim())); - lastIndex = n; - } - } - - if (lastIndex - comment.length() < 30) - lore.add(Methods.formatText("&8" + comment.substring(lastIndex).trim())); - - } - - meta.setLore(lore); - item.setItemMeta(meta); - - inventory.setItem(slot, item); - slot++; - } - - player.openInventory(inventory); - } - - public void reloadConfig() { - plugin.reloadConfig(); - this.setupConfig(); - } - - public void setupConfig() { - FileConfiguration config = plugin.getConfig(); - - for (Setting setting : Setting.values()) { - config.addDefault(setting.getSetting(), setting.getOption()); - } - plugin.getConfig().options().copyDefaults(true); - saveConfig(); - } - - private void saveConfig() { - String dump = plugin.getConfig().saveToString(); - - StringBuilder config = new StringBuilder(); - - BufferedReader bufReader = new BufferedReader(new StringReader(dump)); - - try { - boolean first = true; - - String line; - int currentTab = 0; - String category = ""; - - while ((line = bufReader.readLine()) != null) { - if (line.trim().startsWith("#")) continue; - - int tabChange = line.length() - line.trim().length(); - if (currentTab != tabChange) { - category = category.contains(".") && tabChange != 0 ? category.substring(0, category.indexOf(".")) : ""; - currentTab = tabChange; - } - - if (line.endsWith(":")) { - bufReader.mark(1000); - String found = bufReader.readLine(); - bufReader.reset(); - - if (!found.trim().startsWith("-")) { - - String newCategory = line.substring(0, line.length() - 1).trim(); - - if (category.equals("")) - category = newCategory; - else - category += "." + newCategory; - - currentTab = tabChange + 2; - - if (!first) { - config.append("\n\n"); - } else { - first = false; - } - - if (!category.contains(".")) - config.append("#").append("\n"); - try { - Category categoryObj = Category.valueOf(category.toUpperCase() - .replace(" ", "_") - .replace(".", "_")); - - config.append(new String(new char[tabChange]).replace('\0', ' ')); - for (String l : categoryObj.getComments()) - config.append("# ").append(l).append("\n"); - } catch (IllegalArgumentException e) { - config.append("# ").append(category).append("\n"); - } - if (!category.contains(".")) - config.append("#").append("\n"); - - config.append(line).append("\n"); - - continue; - } - } - - if (line.trim().startsWith("-")) { - config.append(line).append("\n"); - continue; - } - - String key = category + "." + (line.split(":")[0].trim()); - for (Setting setting : Setting.values()) { - if (!setting.getSetting().equals(key) || setting.getComments() == null) continue; - config.append(" ").append("\n"); - for (String l : setting.getComments()) { - config.append(new String(new char[currentTab]).replace('\0', ' ')); - config.append("# ").append(l).append("\n"); - } - } - config.append(line).append("\n"); - } - } catch (IOException e) { - e.printStackTrace(); - } - - try { - if (!plugin.getDataFolder().exists()) - plugin.getDataFolder().mkdir(); - BufferedWriter writer = - new BufferedWriter(new FileWriter(new File(plugin.getDataFolder() + File.separator + "config.yml"))); - writer.write(config.toString()); - writer.flush(); - writer.close(); - - } catch (IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epichoppers/utils/updateModules/LocaleModule.java b/src/main/java/com/songoda/epichoppers/utils/updateModules/LocaleModule.java deleted file mode 100644 index 51856bb..0000000 --- a/src/main/java/com/songoda/epichoppers/utils/updateModules/LocaleModule.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.songoda.epichoppers.utils.updateModules; - -import com.songoda.epichoppers.utils.locale.Locale; -import com.songoda.update.Module; -import com.songoda.update.Plugin; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; - -public class LocaleModule implements Module { - - @Override - public void run(Plugin plugin) { - JSONObject json = plugin.getJson(); - try { - JSONArray files = (JSONArray) json.get("neededFiles"); - for (Object o : files) { - JSONObject file = (JSONObject) o; - - if (file.get("type").equals("locale")) { - InputStream in = new URL((String) file.get("link")).openStream(); - Locale.saveLocale(in, (String) file.get("name")); - } - } - } catch (IOException e) { - e.printStackTrace(); - } - } - -}