From 6f29eea10cd67a30e37678e2070e022e3ecaab2b Mon Sep 17 00:00:00 2001 From: Brianna Date: Thu, 11 Jul 2019 02:21:23 -0400 Subject: [PATCH] Added module saving system. Autocrafting and blockbreak settings will not be saved. --- .gitlab-ci.yml | 2 +- .../com/songoda/epichoppers/EpicHoppers.java | 26 +++--- .../songoda/epichoppers/gui/GUICrafting.java | 11 ++- .../songoda/epichoppers/hopper/Hopper.java | 79 ++++++------------- .../epichoppers/hopper/HopperBuilder.java | 16 ---- .../epichoppers/hopper/HopperManager.java | 7 ++ .../epichoppers/hopper/levels/Level.java | 10 +-- .../hopper/levels/LevelManager.java | 4 +- .../hopper/levels/modules/Module.java | 59 ++++++++++++-- .../levels/modules/ModuleAutoCrafting.java | 58 ++++++++++---- .../hopper/levels/modules/ModuleAutoSell.java | 45 ++++++----- .../levels/modules/ModuleBlockBreak.java | 48 ++++++----- .../hopper/levels/modules/ModuleSuction.java | 18 +++-- .../songoda/epichoppers/storage/Storage.java | 4 +- .../epichoppers/utils/MySQLDatabase.java | 3 - 15 files changed, 214 insertions(+), 176 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2eadbc7..9b1415c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ stages: variables: name: "EpicHoppers" path: "/builds/$CI_PROJECT_PATH" - version: "4.0.4" + version: "4.1" build: stage: build diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java index 5f1e29e..674853e 100644 --- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.java @@ -9,7 +9,6 @@ import com.songoda.epichoppers.economy.VaultEconomy; import com.songoda.epichoppers.enchantment.Enchantment; import com.songoda.epichoppers.handlers.TeleportHandler; import com.songoda.epichoppers.hopper.Filter; -import com.songoda.epichoppers.hopper.Hopper; import com.songoda.epichoppers.hopper.HopperBuilder; import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.Level; @@ -236,6 +235,10 @@ public class EpicHoppers extends JavaPlugin { private void saveToFile() { checkStorage(); + for (Level level : EpicHoppers.getInstance().getLevelManager().getLevels().values()) + for (Module module : level.getRegisteredModules()) + module.saveDataToFile(); + storage.doSave(); } @@ -281,14 +284,6 @@ public class EpicHoppers extends JavaPlugin { TeleportTrigger teleportTrigger = TeleportTrigger.valueOf(row.get("teleporttrigger").asString() == null ? "DISABLED" : row.get("teleporttrigger").asString()); - String autoCraftingStr = row.get("autocrafting").asString() == null ? "AIR" : row.get("autocrafting").asString(); - String[] autoCraftingParts = autoCraftingStr.split(":"); - ItemStack autoCrafting = new ItemStack(Material.valueOf(autoCraftingParts[0]), 1, Short.parseShort(autoCraftingParts.length == 2 ? autoCraftingParts[1] : "0")); - - boolean autoSell = row.get("autosell").asBoolean(); - - boolean autoBreak = row.get("autobreak").asBoolean(); - hopperManager.addHopper(new HopperBuilder(location) .setLevel(level) .setLastPlayerOpened(lastPlayer) @@ -296,9 +291,6 @@ public class EpicHoppers extends JavaPlugin { .addLinkedBlocks(blocks.toArray(new Location[0])) .setFilter(filter) .setTeleportTrigger(teleportTrigger) - .setAutoCrafting(autoCrafting) - .setAutoSelling(autoSell) - .setAutoBreaking(autoBreak) .build()); } } @@ -351,17 +343,17 @@ public class EpicHoppers extends JavaPlugin { for (String key : levels.getKeys(false)) { if (key.equals("Suction") && levels.getInt("Suction") != 0) { - modules.add(new ModuleSuction(levels.getInt("Suction"))); + modules.add(new ModuleSuction(this, levels.getInt("Suction"))); } else if (key.equals("BlockBreak") && levels.getInt("BlockBreak") != 0) { - modules.add(new ModuleBlockBreak(levels.getInt("BlockBreak"))); + modules.add(new ModuleBlockBreak(this, levels.getInt("BlockBreak"))); } else if (key.equals("AutoCrafting")) { - modules.add(new ModuleAutoCrafting()); + modules.add(new ModuleAutoCrafting(this)); } else if (key.equals("AutoSell")) { - modules.add(new ModuleAutoSell(autoSell)); + modules.add(new ModuleAutoSell(this, autoSell)); } } - levelManager.addLevel(level, costExperiance, costEconomy, radius, amount, filter, teleport, linkAmount, autoSell, modules); + levelManager.addLevel(level, costExperiance, costEconomy, radius, amount, filter, teleport, linkAmount, modules); } } diff --git a/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java b/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java index 97401c1..8918242 100644 --- a/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java +++ b/src/main/java/com/songoda/epichoppers/gui/GUICrafting.java @@ -2,6 +2,7 @@ package com.songoda.epichoppers.gui; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; +import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; import com.songoda.epichoppers.utils.Methods; import com.songoda.epichoppers.utils.gui.AbstractGUI; import com.songoda.epichoppers.utils.gui.Range; @@ -13,11 +14,13 @@ public class GUICrafting extends AbstractGUI { private final EpicHoppers plugin; private final Hopper hopper; + private final ModuleAutoCrafting module; - public GUICrafting(EpicHoppers plugin, Hopper hopper, Player player) { + public GUICrafting(EpicHoppers plugin, ModuleAutoCrafting autoCrafting, Hopper hopper, Player player) { super(player); this.plugin = plugin; this.hopper = hopper; + this.module = autoCrafting; init(Methods.formatName(hopper.getLevel().getLevel(), false) + " &8-&f Crafting", 27); } @@ -48,7 +51,8 @@ public class GUICrafting extends AbstractGUI { inventory.setItem(25, Methods.getBackgroundGlass(true)); inventory.setItem(26, Methods.getBackgroundGlass(true)); - inventory.setItem(13, hopper.getAutoCrafting() == null ? new ItemStack(Material.AIR) : hopper.getAutoCrafting()); + inventory.setItem(13, module.getAutoCrafting(hopper) == null + ? new ItemStack(Material.AIR) : module.getAutoCrafting(hopper)); addDraggable(new Range(13, 13, null, false), true); } @@ -60,7 +64,6 @@ public class GUICrafting extends AbstractGUI { @Override protected void registerOnCloses() { registerOnClose(((player, inventory) -> - hopper.setAutoCrafting(player, inventory.getItem(13)))); - + module.setAutoCrafting(hopper, player, inventory.getItem(13)))); } } diff --git a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java index fdd6d9c..694effc 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/Hopper.java +++ b/src/main/java/com/songoda/epichoppers/hopper/Hopper.java @@ -8,18 +8,11 @@ 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 org.bukkit.inventory.ItemStack; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; /** * Created by songoda on 3/14/2017. @@ -33,11 +26,10 @@ public class Hopper { private List linkedBlocks = new ArrayList<>(); private Filter filter = new Filter(); private TeleportTrigger teleportTrigger = TeleportTrigger.DISABLED; - private ItemStack autoCrafting = null; - private int autoSellTimer = -9999; - private boolean autoBreaking = false; private int transferTick = 0; + private Map moduleCache = new HashMap<>(); + public Hopper(Location location) { this.location = location; } @@ -173,7 +165,7 @@ public class Hopper { /** * Ticks a hopper to determine when it can transfer items next * - * @param maxTick The maximum amount the hopper can be ticked before next transferring items + * @param maxTick The maximum amount the hopper can be ticked before next transferring items * @param allowLooping If true, the hopper is allowed to transfer items if the tick is also valid * @return true if the hopper should transfer an item, otherwise false */ @@ -234,27 +226,6 @@ public class Hopper { lastPlayerOpened = uuid; } - public ItemStack getAutoCrafting() { - return autoCrafting; - } - - public void setAutoCrafting(ItemStack autoCrafting) { - this.autoCrafting = autoCrafting; - } - - public void setAutoCrafting(Player player, ItemStack autoCrafting) { - this.autoCrafting = autoCrafting; - if (autoCrafting != null) { - int excess = autoCrafting.getAmount() - 1; - autoCrafting.setAmount(1); - if (excess > 0 && player != null) { - ItemStack item = autoCrafting.clone(); - item.setAmount(excess); - player.getInventory().addItem(item); - } - } - } - public TeleportTrigger getTeleportTrigger() { return teleportTrigger; } @@ -264,26 +235,6 @@ public class Hopper { this.teleportTrigger = teleportTrigger; } - public int getAutoSellTimer() { - return autoSellTimer; - } - - public void setAutoSellTimer(int autoSellTimer) { - this.autoSellTimer = autoSellTimer; - } - - public boolean isAutoBreaking() { - return autoBreaking; - } - - public void setAutoBreaking(boolean autoBreaking) { - this.autoBreaking = autoBreaking; - } - - public void toggleAutoBreaking() { - this.autoBreaking = !autoBreaking; - } - public List getLinkedBlocks() { return new ArrayList<>(linkedBlocks); } @@ -307,4 +258,24 @@ public class Hopper { public void setFilter(Filter filter) { this.filter = filter; } + + public Object getDataFromModuleCache(String key) { + return this.moduleCache.getOrDefault(key, null); + } + + public void addDataToModuleCache(String key, Object data) { + this.moduleCache.put(key, data); + } + + public boolean isDataCachedInModuleCache(String key) { + return this.moduleCache.containsKey(key); + } + + public void removeDataFromModuleCache(String key) { + this.moduleCache.remove(key); + } + + public void clearModuleCache() { + this.moduleCache.clear(); + } } diff --git a/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java b/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java index aebfe43..adf1331 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java +++ b/src/main/java/com/songoda/epichoppers/hopper/HopperBuilder.java @@ -5,7 +5,6 @@ import com.songoda.epichoppers.utils.TeleportTrigger; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; -import org.bukkit.inventory.ItemStack; import java.util.UUID; @@ -61,21 +60,6 @@ public class HopperBuilder { return this; } - public HopperBuilder setAutoCrafting(ItemStack autoCrafting) { - this.hopper.setAutoCrafting(autoCrafting); - return this; - } - - public HopperBuilder setAutoSelling(boolean autoSelling) { - this.hopper.setAutoSellTimer(autoSelling ? 0 : -9999); - return this; - } - - public HopperBuilder setAutoBreaking(boolean autoBreaking) { - this.hopper.setAutoBreaking(autoBreaking); - return this; - } - public Hopper build() { return this.hopper; } diff --git a/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java b/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java index f8c59c5..026383b 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java +++ b/src/main/java/com/songoda/epichoppers/hopper/HopperManager.java @@ -1,5 +1,8 @@ package com.songoda.epichoppers.hopper; +import com.songoda.epichoppers.EpicHoppers; +import com.songoda.epichoppers.hopper.levels.Level; +import com.songoda.epichoppers.hopper.levels.modules.Module; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -33,6 +36,10 @@ public class HopperManager { for (Hopper hopper : this.registeredHoppers.values()) hopper.removeLinkedBlock(location); + for (Level level : EpicHoppers.getInstance().getLevelManager().getLevels().values()) + for (Module module : level.getRegisteredModules()) + module.clearData(removed); + return removed; } diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/Level.java b/src/main/java/com/songoda/epichoppers/hopper/levels/Level.java index e097002..88a761e 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/Level.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/Level.java @@ -10,10 +10,10 @@ public class Level { private final ArrayList registeredModules; private final List description = new ArrayList<>(); - private int level, costExperience, costEconomy, range, amount, linkAmount, autoSell; + private int level, costExperience, costEconomy, range, amount, linkAmount; private boolean filter, teleport; - Level(int level, int costExperience, int costEconomy, int range, int amount, boolean filter, boolean teleport, int linkAmount, int autoSell, ArrayList registeredModules) { + Level(int level, int costExperience, int costEconomy, int range, int amount, boolean filter, boolean teleport, int linkAmount, ArrayList registeredModules) { this.level = level; this.costExperience = costExperience; this.costEconomy = costEconomy; @@ -22,7 +22,6 @@ public class Level { this.filter = filter; this.teleport = teleport; this.linkAmount = linkAmount; - this.autoSell = autoSell; this.registeredModules = registeredModules; buildDescription(); @@ -74,11 +73,6 @@ public class Level { } - public int getAutoSell() { - return autoSell; - } - - public int getLinkAmount() { return linkAmount; } diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java b/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java index 8a04db6..e001399 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/LevelManager.java @@ -11,8 +11,8 @@ public class LevelManager { private final NavigableMap registeredLevels = new TreeMap<>(); - public void addLevel(int level, int costExperience, int costEconomy, int range, int amount, boolean filter, boolean teleport, int linkAmount, int autoSell, ArrayList modules) { - registeredLevels.put(level, new Level(level, costExperience, costEconomy, range, amount, filter, teleport, linkAmount, autoSell, modules)); + public void addLevel(int level, int costExperience, int costEconomy, int range, int amount, boolean filter, boolean teleport, int linkAmount, ArrayList modules) { + registeredLevels.put(level, new Level(level, costExperience, costEconomy, range, amount, filter, teleport, linkAmount, modules)); } 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 9fac94c..86cf194 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,25 +1,70 @@ package com.songoda.epichoppers.hopper.levels.modules; +import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.hopper.Hopper; +import com.songoda.epichoppers.utils.ConfigWrapper; +import com.songoda.epichoppers.utils.Methods; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import java.io.File; +import java.util.HashMap; import java.util.List; +import java.util.Map; -public interface Module { +public abstract class Module { - String getName(); + protected final EpicHoppers plugin; + private ConfigWrapper config; - void run(Hopper hopper, Inventory hopperInventory); + public Module(EpicHoppers plugin) { + this.plugin = plugin; + this.config = new ConfigWrapper(plugin, File.separator + "modules", getName() + ".yml"); + } - ItemStack getGUIButton(Hopper hopper); + abstract String getName(); - void runButtonPress(Player player, Hopper hopper); + public abstract void run(Hopper hopper, Inventory hopperInventory); - List getBlockedItems(Hopper hopper); + public abstract ItemStack getGUIButton(Hopper hopper); - String getDescription(); + public abstract void runButtonPress(Player player, Hopper hopper); + public abstract List getBlockedItems(Hopper hopper); + + public abstract String getDescription(); + + public void saveData(Hopper hopper, String setting, Object value) { + saveData(hopper, setting, value, value); + } + + public void saveData(Hopper hopper, String setting, Object value, Object toCache) { + config.getConfig().set("data." + Methods.serializeLocation(hopper.getLocation()) + "." + setting, value); + modifyDataCache(hopper, setting, toCache); + } + + public void modifyDataCache(Hopper hopper, String setting, Object value) { + hopper.addDataToModuleCache(setting, value); + } + + protected Object getData(Hopper hopper, String setting) { + if (hopper.isDataCachedInModuleCache(setting)) + return hopper.getDataFromModuleCache(setting); + + Object data = config.getConfig().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); + hopper.clearModuleCache(); + } + + public void saveDataToFile() { + config.saveConfig(); + } } 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 8d0dcf9..a9e3cd7 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 @@ -8,11 +8,7 @@ import com.songoda.epichoppers.utils.ServerVersion; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.Recipe; -import org.bukkit.inventory.ShapedRecipe; -import org.bukkit.inventory.ShapelessRecipe; +import org.bukkit.inventory.*; import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; @@ -20,25 +16,29 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class ModuleAutoCrafting implements Module { +public class ModuleAutoCrafting extends Module { private final Map cachedRecipes = new HashMap<>(); + public ModuleAutoCrafting(EpicHoppers plugin) { + super(plugin); + } + @Override public String getName() { return "AutoCrafting"; } public void run(Hopper hopper, Inventory hopperInventory) { - if (hopper.getAutoCrafting() == null + if (getAutoCrafting(hopper) == null || hopperInventory == null || hopperInventory.getSize() == 0 - || !canMove(hopperInventory, new ItemStack(hopper.getAutoCrafting())) - || cachedRecipes.get(hopper.getAutoCrafting()) == null) + || !canMove(hopperInventory, new ItemStack(getAutoCrafting(hopper))) + || cachedRecipes.get(getAutoCrafting(hopper)) == null) return; top: - for (Recipe recipe : cachedRecipes.get(hopper.getAutoCrafting()).getRecipes()) { + for (Recipe recipe : cachedRecipes.get(getAutoCrafting(hopper)).getRecipes()) { if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) continue; @@ -113,14 +113,15 @@ public class ModuleAutoCrafting implements Module { @Override public void runButtonPress(Player player, Hopper hopper) { - new GUICrafting(EpicHoppers.getInstance(), hopper, player); + new GUICrafting(EpicHoppers.getInstance(), this, hopper, player); } + @Override public List getBlockedItems(Hopper hopper) { List materials = new ArrayList<>(); - if (hopper.getAutoCrafting() != null) { + if (getAutoCrafting(hopper) != null) { - ItemStack itemStack = hopper.getAutoCrafting(); + ItemStack itemStack = getAutoCrafting(hopper); if (itemStack.getType() == Material.AIR) return materials; @@ -157,7 +158,36 @@ public class ModuleAutoCrafting implements Module { @Override public String getDescription() { - return EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.crafting", EpicHoppers.getInstance().getLocale().getMessage("general.word.enabled")); + return EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.crafting", + EpicHoppers.getInstance().getLocale().getMessage("general.word.enabled")); + } + + public ItemStack getAutoCrafting(Hopper hopper) { + Object autocrafting = getData(hopper, "autocrafting"); + return autocrafting instanceof ItemStack ? (ItemStack) autocrafting : decode((String) autocrafting); + } + + public void setAutoCrafting(Hopper hopper, Player player, ItemStack autoCrafting) { + saveData(hopper, "autocrafting", encode(autoCrafting), autoCrafting); + int excess = autoCrafting.getAmount() - 1; + autoCrafting.setAmount(1); + if (excess > 0 && player != null) { + ItemStack item = autoCrafting.clone(); + item.setAmount(excess); + player.getInventory().addItem(item); + } + } + + public String encode(ItemStack item) { + return item.getType() == Material.AIR ? null : item.getType().name() + + (item.getDurability() == 0 ? "" : ":" + item.getDurability()); + } + + public ItemStack decode(String string) { + String autoCraftingStr = string == null ? "AIR" : string; + String[] autoCraftingParts = autoCraftingStr.split(":"); + return new ItemStack(Material.valueOf(autoCraftingParts[0]), + 1, Short.parseShort(autoCraftingParts.length == 2 ? autoCraftingParts[1] : "0")); } private boolean canMove(Inventory inventory, ItemStack item) { 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 95b1bd2..294dc7d 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 @@ -17,29 +17,33 @@ import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.List; -public class ModuleAutoSell implements Module { +public class ModuleAutoSell extends Module { private int timeOut; private int hopperTickRate; - public ModuleAutoSell(int timeOut) { + public ModuleAutoSell(EpicHoppers plugin, int timeOut) { + super(plugin); EpicHoppers instance = EpicHoppers.getInstance(); this.timeOut = timeOut * 20; this.hopperTickRate = (int) instance.getConfig().getLong("Main.Amount of Ticks Between Hops"); } - + @Override public String getName() { return "AutoSell"; } + @Override public void run(Hopper hopper, Inventory hopperInventory) { if (hopperInventory == null) return; - if (hopper.getAutoSellTimer() == -9999) return; + int currentTime = getTime(hopper); - if (hopper.getAutoSellTimer() <= 0) { + if (currentTime == -9999) return; + + if (currentTime <= 0) { EpicHoppers instance = EpicHoppers.getInstance(); if (instance.getEconomy() == null) return; @@ -59,7 +63,7 @@ public class ModuleAutoSell implements Module { ItemStack clone = itemStack.clone(); clone.setAmount(1); value = net.brcdev.shopgui.ShopGuiPlusApi.getItemStackPriceSell(player.getPlayer(), clone); - } catch (Exception e){ + } catch (Exception e) { value = 0; } } else @@ -68,27 +72,27 @@ public class ModuleAutoSell implements Module { if (value == 0) continue; - instance.getEconomy().deposit(player, value * itemStack.getAmount()); + instance.getEconomy().deposit(player, value * itemStack.getAmount()); hopperInventory.removeItem(itemStack); updateComparators = true; } - hopper.setAutoSellTimer(timeOut); + modifyDataCache(hopper, "time", timeOut); if (updateComparators) HopTask.updateAdjacentComparators(hopper.getLocation()); } - hopper.setAutoSellTimer(hopper.getAutoSellTimer() - hopperTickRate); + modifyDataCache(hopper, "time", getTime(hopper) - hopperTickRate); } - + @Override public ItemStack getGUIButton(Hopper hopper) { Hopper eHopper = hopper; ItemStack sell = new ItemStack(EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? Material.SUNFLOWER : Material.valueOf("DOUBLE_PLANT"), 1); ItemMeta sellmeta = sell.getItemMeta(); sellmeta.setDisplayName(EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.selltitle")); ArrayList loresell = new ArrayList<>(); - String[] parts = EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.selllore", eHopper.getAutoSellTimer() == -9999 ? "\u221E" : (int) Math.floor(eHopper.getAutoSellTimer() / 20)).split("\\|"); + String[] parts = EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.selllore", getTime(hopper) == -9999 ? "\u221E" : (int) Math.floor(getTime(hopper) / 20)).split("\\|"); for (String line : parts) { loresell.add(Methods.formatText(line)); } @@ -97,23 +101,28 @@ public class ModuleAutoSell implements Module { return sell; } - + @Override public void runButtonPress(Player player, Hopper hopper) { - Hopper eHopper = hopper; - if (eHopper.getAutoSellTimer() == -9999) { - eHopper.setAutoSellTimer(0); + if (getTime(hopper) == -9999) { + saveData(hopper, "time", 0); } else { - eHopper.setAutoSellTimer(-9999); + saveData(hopper, "time", -9999); } } - + @Override public List getBlockedItems(Hopper hopper) { return null; } - + @Override public String getDescription() { return EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.autosell", (int) Math.floor(timeOut / 20)); } + + public int getTime(Hopper hopper) { + Object time = getData(hopper, "time"); + if (time == null) return -9999; + return (int) time; + } } 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 ecfe22f..4f102fb 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 @@ -20,25 +20,26 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class ModuleBlockBreak implements Module { +public class ModuleBlockBreak extends Module { private final int amount; private Map blockTick = new HashMap<>(); - public ModuleBlockBreak(int amount) { + public ModuleBlockBreak(EpicHoppers plugin, int amount) { + super(plugin); this.amount = amount; } - + @Override public String getName() { return "BlockBreak"; } - + @Override public void run(Hopper hopper, Inventory hopperInventory) { Block block = hopper.getLocation().getBlock(); - if (!hopper.isAutoBreaking()) + if (!isEnabled(hopper)) return; if (!blockTick.containsKey(block)) { @@ -59,11 +60,11 @@ public class ModuleBlockBreak implements Module { return; // Don't break farm items from EpicFarming - if (EpicHoppers.getInstance().isEpicFarming() && com.songoda.epicfarming.EpicFarmingPlugin.getInstance().getFarmManager().getFarm(above) != null) + if (plugin.isEpicFarming() && com.songoda.epicfarming.EpicFarmingPlugin.getInstance().getFarmManager().getFarm(above) != null) return; - if (!EpicHoppers.getInstance().getConfig().getStringList("Main.BlockBreak Blacklisted Blocks").contains(above.getType().name())) { - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) + if (!plugin.getConfig().getStringList("Main.BlockBreak Blacklisted Blocks").contains(above.getType().name())) { + if (plugin.isServerVersionAtLeast(ServerVersion.V1_9)) above.getWorld().playSound(above.getLocation(), Sound.BLOCK_STONE_BREAK, 1F, 1F); Location locationAbove = above.getLocation(); locationAbove.add(.5, .5, .5); @@ -71,11 +72,11 @@ public class ModuleBlockBreak implements Module { float xx = (float) (0 + (Math.random() * .5)); float yy = (float) (0 + (Math.random() * .5)); float zz = (float) (0 + (Math.random() * .5)); - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) - above.getWorld().spawnParticle(Particle.valueOf(EpicHoppers.getInstance().getConfig().getString("Main.BlockBreak Particle Type")), locationAbove, 15, xx, yy, zz); + if (plugin.isServerVersionAtLeast(ServerVersion.V1_9)) + above.getWorld().spawnParticle(Particle.valueOf(plugin.getConfig().getString("Main.BlockBreak Particle Type")), locationAbove, 15, xx, yy, zz); boolean waterlogged = false; - if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (plugin.isServerVersionAtLeast(ServerVersion.V1_13) && above.getBlockData() instanceof org.bukkit.block.data.Waterlogged && ((org.bukkit.block.data.Waterlogged)above.getBlockData()).isWaterlogged()) { waterlogged = true; @@ -89,15 +90,15 @@ public class ModuleBlockBreak implements Module { blockTick.remove(block); } - + @Override public ItemStack getGUIButton(Hopper hopper) { ItemStack block = new ItemStack(Material.IRON_ORE, 1); ItemMeta blockmeta = block.getItemMeta(); - blockmeta.setDisplayName(EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.blocktitle")); + blockmeta.setDisplayName(plugin.getLocale().getMessage("interface.hopper.blocktitle")); ArrayList loreblock = new ArrayList<>(); - String[] parts = EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.blocklore", - hopper.isAutoBreaking() ? EpicHoppers.getInstance().getLocale().getMessage("general.word.enabled") - : EpicHoppers.getInstance().getLocale().getMessage("general.word.disabled")).split("\\|"); + String[] parts = plugin.getLocale().getMessage("interface.hopper.blocklore", + isEnabled(hopper) ? plugin.getLocale().getMessage("general.word.enabled") + : plugin.getLocale().getMessage("general.word.disabled")).split("\\|"); for (String line : parts) { loreblock.add(Methods.formatText(line)); } @@ -106,18 +107,23 @@ public class ModuleBlockBreak implements Module { return block; } - + @Override public void runButtonPress(Player player, Hopper hopper) { - hopper.toggleAutoBreaking(); + saveData(hopper,"blockbreak", !isEnabled(hopper)); } - + @Override public List getBlockedItems(Hopper hopper) { return null; } - + @Override public String getDescription() { - return EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.blockbreak", amount); + return plugin.getLocale().getMessage("interface.hopper.blockbreak", amount); + } + + public boolean isEnabled(Hopper hopper) { + Object isBlockBreaking = getData(hopper, "blockbreak"); + return isBlockBreaking != null && (boolean) isBlockBreaking; } } 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 98f14b2..96258c1 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 @@ -21,7 +21,7 @@ import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; -public class ModuleSuction implements Module { +public class ModuleSuction extends Module { private final int amount; @@ -30,16 +30,17 @@ public class ModuleSuction implements Module { private boolean wildStacker = Bukkit.getPluginManager().isPluginEnabled("WildStacker"); private boolean ultimateStacker = Bukkit.getPluginManager().isPluginEnabled("UltimateStacker"); - public ModuleSuction(int amount) { + public ModuleSuction(EpicHoppers plugin, int amount) { + super(plugin); this.amount = amount; } - + @Override public String getName() { return "Suction"; } - + @Override public void run(Hopper hopper, Inventory hopperInventory) { double radius = amount + .5; @@ -96,19 +97,20 @@ public class ModuleSuction implements Module { return blacklist.contains(uuid); } + @Override public ItemStack getGUIButton(Hopper hopper) { return null; } - public void runButtonPress(Player player, Hopper hopper) { - - } + @Override + public void runButtonPress(Player player, Hopper hopper) { } + @Override public List getBlockedItems(Hopper hopper) { return null; } - + @Override public String getDescription() { return EpicHoppers.getInstance().getLocale().getMessage("interface.hopper.suction", amount); } diff --git a/src/main/java/com/songoda/epichoppers/storage/Storage.java b/src/main/java/com/songoda/epichoppers/storage/Storage.java index b7f11da..01c0915 100644 --- a/src/main/java/com/songoda/epichoppers/storage/Storage.java +++ b/src/main/java/com/songoda/epichoppers/storage/Storage.java @@ -46,11 +46,9 @@ public abstract class Storage { new StorageItem("player", hopper.getLastPlayerOpened() == null ? null : hopper.getLastPlayerOpened().toString()), new StorageItem("teleporttrigger", hopper.getTeleportTrigger().toString()), - new StorageItem("autocrafting", hopper.getAutoCrafting() == null || hopper.getAutoCrafting().getType() == Material.AIR ? null : hopper.getAutoCrafting().getType().name() + (hopper.getAutoCrafting().getDurability() == 0 ? "" : ":" + hopper.getAutoCrafting().getDurability())), new StorageItem("whitelist", hopper.getFilter().getWhiteList()), + new StorageItem("whitelist", hopper.getFilter().getWhiteList()), new StorageItem("blacklist", hopper.getFilter().getBlackList()), new StorageItem("void", hopper.getFilter().getVoidList()), - new StorageItem("autobreak", hopper.isAutoBreaking()), - new StorageItem("autosell", hopper.getAutoSellTimer() != -9999), new StorageItem("black", hopper.getFilter().getEndPoint() == null ? null : Methods.serializeLocation(hopper.getFilter().getEndPoint()))); } diff --git a/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java b/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java index 88db9f2..b240200 100644 --- a/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java +++ b/src/main/java/com/songoda/epichoppers/utils/MySQLDatabase.java @@ -30,13 +30,10 @@ public class MySQLDatabase { "\t`placedby` TEXT NULL,\n" + "\t`player` TEXT NULL,\n" + "\t`teleporttrigger` TEXT NULL,\n" + - "\t`autocrafting` TEXT NULL,\n" + - "\t`autosell` TEXT NULL,\n" + "\t`whitelist` TEXT NULL,\n" + "\t`blacklist` TEXT NULL,\n" + "\t`void` TEXT NULL,\n" + "\t`black` TEXT NULL,\n" + - "\t`autobreak` TINYINT(1) NULL\n" + ")"); connection.createStatement().execute("CREATE TABLE IF NOT EXISTS `" + instance.getConfig().getString("Database.Prefix") + "boosts` (\n" +