From 42738e9306d5b94f3a9ef7bb842e9458922d9eaa Mon Sep 17 00:00:00 2001 From: Niels Vergucht Date: Mon, 3 Dec 2018 13:04:33 +0100 Subject: [PATCH] Shop data added --- .../epicbuckets/file/ConfigManager.java | 47 ++++++++++++++ .../epicbuckets/genbucket/GenbucketType.java | 22 +++++++ .../com/songoda/epicbuckets/shop/Shop.java | 55 +++++++++++++--- .../songoda/epicbuckets/shop/ShopManager.java | 65 ++++++++++++++++--- .../com/songoda/epicbuckets/shop/SubShop.java | 4 ++ .../songoda/epicbuckets/util/Debugger.java | 10 ++- .../songoda/epicbuckets/util/Validator.java | 18 +++++ 7 files changed, 202 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/songoda/epicbuckets/genbucket/GenbucketType.java diff --git a/src/main/java/com/songoda/epicbuckets/file/ConfigManager.java b/src/main/java/com/songoda/epicbuckets/file/ConfigManager.java index 635db40..41de4b2 100644 --- a/src/main/java/com/songoda/epicbuckets/file/ConfigManager.java +++ b/src/main/java/com/songoda/epicbuckets/file/ConfigManager.java @@ -1,7 +1,10 @@ package com.songoda.epicbuckets.file; import com.songoda.epicbuckets.EpicBuckets; +import com.songoda.epicbuckets.util.Validator; +import com.songoda.epicbuckets.util.XMaterial; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; import java.io.File; import java.util.HashMap; @@ -11,6 +14,14 @@ public class ConfigManager { private EpicBuckets epicBuckets; private HashMap configDatabase; + private String backButtonPath = "BACK-BUTTON"; + private String fillItemPath = "FILL-ITEM"; + private String bulkShopIncreasePath = "BULK-SHOP-INVENTORY.increase-item"; + private String bulkShopDecreasePath = "BULK-SHOP-INVENTORY.decrease-item"; + private String bulkShopPurchasePath = "BULK-SHOP-INVENTORY.purchase-item"; + + private ItemStack backButton; + public ConfigManager() { this.epicBuckets = EpicBuckets.getInstance(); setup(); @@ -19,6 +30,23 @@ public class ConfigManager { private void setup() { epicBuckets.saveDefaultConfig(); createConfig("shops", true); + + setupBackButton(); + setupFillItem(); + } + + private void setupFillItem() { + boolean m = Validator.getInstance().isMaterial(epicBuckets.getConfig().getString(getFillItemPath() + ".material")); + + backButton = ((!m) ? XMaterial.BLACK_STAINED_GLASS_PANE.parseItem() : XMaterial.valueOf(epicBuckets.getConfig().getString(getFillItemPath() + ".material")).parseItem()); + backButton.getItemMeta().setDisplayName(epicBuckets.getConfig().getString(getFillItemPath() + ".name")); + } + + private void setupBackButton() { + boolean m = Validator.getInstance().isMaterial(epicBuckets.getConfig().getString(getBackButtonPath() + ".material")); + + backButton = ((!m) ? XMaterial.BARRIER.parseItem() : XMaterial.valueOf(epicBuckets.getConfig().getString(getBackButtonPath() + ".material")).parseItem()); + backButton.getItemMeta().setDisplayName(epicBuckets.getConfig().getString(getBackButtonPath() + ".name")); } public void createConfig(String name, boolean resource) { @@ -36,4 +64,23 @@ public class ConfigManager { return configDatabase.get(name).getConfig(); } + public String getBackButtonPath() { + return backButtonPath; + } + + public String getFillItemPath() { + return fillItemPath; + } + + public String getBulkShopIncreasePath() { + return bulkShopIncreasePath; + } + + public String getBulkShopDecreasePath() { + return bulkShopDecreasePath; + } + + public String getBulkShopPurchasePath() { + return bulkShopPurchasePath; + } } diff --git a/src/main/java/com/songoda/epicbuckets/genbucket/GenbucketType.java b/src/main/java/com/songoda/epicbuckets/genbucket/GenbucketType.java new file mode 100644 index 0000000..24076e3 --- /dev/null +++ b/src/main/java/com/songoda/epicbuckets/genbucket/GenbucketType.java @@ -0,0 +1,22 @@ +package com.songoda.epicbuckets.genbucket; + +public enum GenbucketType { + + HORIZONTAL("HORIZONTAL"), + INFUSED("INFUSED"), + PSUEDO("PSUEDO"), + VERTICAL("VERTICAL"); + + public final String name; + + GenbucketType(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + +} + diff --git a/src/main/java/com/songoda/epicbuckets/shop/Shop.java b/src/main/java/com/songoda/epicbuckets/shop/Shop.java index 569e2b8..4ebf7cd 100644 --- a/src/main/java/com/songoda/epicbuckets/shop/Shop.java +++ b/src/main/java/com/songoda/epicbuckets/shop/Shop.java @@ -1,6 +1,7 @@ package com.songoda.epicbuckets.shop; import com.songoda.epicbuckets.EpicBuckets; +import com.songoda.epicbuckets.genbucket.GenbucketType; import com.songoda.epicbuckets.util.Validator; import com.songoda.epicbuckets.util.XMaterial; import org.bukkit.configuration.file.FileConfiguration; @@ -11,35 +12,67 @@ import java.util.HashMap; public class Shop { private EpicBuckets epicBuckets; + private FileConfiguration shops; private HashMap subShops; + private ItemStack shopItem; private String menuItem; private String shopName; - private String path; - private ItemStack shopItem; private int slot; + private String path; + private String shopPath; + + private int backButton; + private GenbucketType trait; + private int inventorySize; + private boolean fillInventory; + private String inventoryName; + private boolean enabled = true; public Shop(String menuItem, String name, String path) { this.epicBuckets = EpicBuckets.getInstance(); + this.shops = EpicBuckets.getInstance().getConfigManager().getConfig("shops");; this.subShops = new HashMap<>(); this.menuItem = menuItem; this.shopName = name; - this.path = path; + this.path = path; + this.shopPath = epicBuckets.getShopManager().getShopPath() + "." + shopName; + + loadData(); setupShopItem(); loadSubShops(); } - private void loadSubShops() { - FileConfiguration shops = EpicBuckets.getInstance().getConfigManager().getConfig("shops"); + private void loadData() { + trait = Validator.getInstance().genbucketType(shops.getString(shopPath + "trait")); + backButton = Validator.getInstance().slot(shops.getString(shopPath + "goBackButton")); + inventorySize = Validator.getInstance().inventorySize(shops.getString(shopPath + "size")); + fillInventory = shops.getBoolean(shopPath + "fill"); + inventoryName = shops.getString(shopPath + "inventory-name"); - for (String shop : shops.getConfigurationSection(epicBuckets.getShopManager().getShopPath() + "." + shopName).getKeys(false)) { - if (!shops.isConfigurationSection(epicBuckets.getShopManager().getShopPath() + "." + shopName + "." + shop)) { + if (trait == null) { + epicBuckets.getDebugger().invalidGenbucketType(shopPath + "trait"); + setEnabled(false); + } + if (backButton == -1) { + epicBuckets.getDebugger().invalidSlot(shopPath + "goBackButton"); + setEnabled(false); + } + if (inventorySize == -1) { + epicBuckets.getDebugger().invalidInventorySize(shopPath + "size"); + setEnabled(false); + } + } + + private void loadSubShops() { + for (String shop : shops.getConfigurationSection(shopPath).getKeys(false)) { + if (!shops.isConfigurationSection(shopPath + "." + shop)) { continue; } @@ -54,8 +87,8 @@ public class Shop { boolean m = Validator.getInstance().isMaterial(epicBuckets.getConfig().getString(itemPath + ".material")); if (slot == -1) { - epicBuckets.getDebugger().invalidSlot(epicBuckets.getShopManager().getPath() + "." + menuItem); - enabled = false; + epicBuckets.getDebugger().invalidSlot(itemPath); + setEnabled(false); } shopItem = ((!m) ? XMaterial.WATER_BUCKET.parseItem() : XMaterial.valueOf(epicBuckets.getConfig().getString(itemPath + ".material")).parseItem()); @@ -64,6 +97,10 @@ public class Shop { shopItem.getItemMeta().setLore(epicBuckets.getConfig().getStringList(itemPath + ".lore")); } + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + public boolean isEnabled() { return enabled; } diff --git a/src/main/java/com/songoda/epicbuckets/shop/ShopManager.java b/src/main/java/com/songoda/epicbuckets/shop/ShopManager.java index 6c01759..3b6efee 100644 --- a/src/main/java/com/songoda/epicbuckets/shop/ShopManager.java +++ b/src/main/java/com/songoda/epicbuckets/shop/ShopManager.java @@ -1,9 +1,16 @@ package com.songoda.epicbuckets.shop; import com.songoda.epicbuckets.EpicBuckets; +import com.songoda.epicbuckets.util.Validator; +import com.songoda.epicbuckets.util.XMaterial; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; public class ShopManager { @@ -11,29 +18,69 @@ public class ShopManager { private FileConfiguration shops; private EpicBuckets epicBuckets; - private String path = "MENU-ITEMS"; + private String configPath = "MENU-ITEMS"; private String shopPath = "shops"; + private ItemStack increaseItem; + private ItemStack decreaseItem; + private ItemStack purchaseItem; + + private List increaseSlots; + private List decreaseSlots; + private int purchaseSlot; + + private boolean useBackButtons; + public ShopManager() { epicBuckets = EpicBuckets.getInstance(); shopDatabase = new HashMap<>(); + increaseSlots = new ArrayList<>(); + decreaseSlots = new ArrayList<>(); shops = EpicBuckets.getInstance().getConfigManager().getConfig("shops"); + loadData(); loadShops(); + setupBulkShop(); } - private void loadShops() { - for (String key : epicBuckets.getConfig().getConfigurationSection(path).getKeys(false)) { - if (!epicBuckets.getConfig().isConfigurationSection(path + "." + key)) { - continue; - } + private void setupBulkShop() { + boolean i = Validator.getInstance().isMaterial(epicBuckets.getConfigManager().getBulkShopIncreasePath() + ".material"); + boolean d = Validator.getInstance().isMaterial(epicBuckets.getConfigManager().getBulkShopDecreasePath() + ".material"); + boolean p = Validator.getInstance().isMaterial(epicBuckets.getConfigManager().getBulkShopPurchasePath() + ".material"); + purchaseSlot = Validator.getInstance().slot(epicBuckets.getConfig().getString(epicBuckets.getConfigManager().getBulkShopPurchasePath() + ".slot")); - shopDatabase.put(epicBuckets.getConfig().getString(path + "." + key), new Shop(epicBuckets.getConfig().getString(path + "." + key), epicBuckets.getConfig().getString(path + "." + key + ".shop"), path + "." + key)); + if (purchaseSlot == -1) { + purchaseSlot = 40; + } + + increaseItem = ((!i) ? XMaterial.GREEN_STAINED_GLASS_PANE.parseItem() : XMaterial.valueOf(epicBuckets.getConfig().getString(epicBuckets.getConfigManager().getBulkShopIncreasePath() + ".material")).parseItem()); + decreaseItem = ((!i) ? XMaterial.RED_STAINED_GLASS_PANE.parseItem() : XMaterial.valueOf(epicBuckets.getConfig().getString(epicBuckets.getConfigManager().getBulkShopDecreasePath() + ".material")).parseItem()); + purchaseItem = ((!i) ? XMaterial.YELLOW_STAINED_GLASS.parseItem() : XMaterial.valueOf(epicBuckets.getConfig().getString(epicBuckets.getConfigManager().getBulkShopPurchasePath() + ".material")).parseItem()); + + for (String s : epicBuckets.getConfig().getString(epicBuckets.getConfigManager().getBulkShopIncreasePath() + ".slots").split(",")) { + increaseSlots.add(Integer.parseInt(s)); + } + for (String s : epicBuckets.getConfig().getString(epicBuckets.getConfigManager().getBulkShopDecreasePath() + ".slots").split(",")) { + increaseSlots.add(Integer.parseInt(s)); } } - public String getPath() { - return path; + private void loadData() { + useBackButtons = shops.getBoolean("use-back-buttons"); + } + + private void loadShops() { + for (String key : epicBuckets.getConfig().getConfigurationSection(configPath).getKeys(false)) { + if (!epicBuckets.getConfig().isConfigurationSection(configPath + "." + key)) { + continue; + } + + shopDatabase.put(epicBuckets.getConfig().getString(configPath + "." + key), new Shop(epicBuckets.getConfig().getString(configPath + "." + key), epicBuckets.getConfig().getString(configPath + "." + key + ".shop"), configPath + "." + key)); + } + } + + public String getConfigPath() { + return configPath; } public String getShopPath() { diff --git a/src/main/java/com/songoda/epicbuckets/shop/SubShop.java b/src/main/java/com/songoda/epicbuckets/shop/SubShop.java index 4fcd128..8e74ddb 100644 --- a/src/main/java/com/songoda/epicbuckets/shop/SubShop.java +++ b/src/main/java/com/songoda/epicbuckets/shop/SubShop.java @@ -74,6 +74,10 @@ public class SubShop { genItem = ((!t) ? XMaterial.WATER_BUCKET.parseItem() : XMaterial.valueOf(shops.getString(subShopPath + ".type")).parseItem()); } + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + public boolean isEnabled() { return enabled; } diff --git a/src/main/java/com/songoda/epicbuckets/util/Debugger.java b/src/main/java/com/songoda/epicbuckets/util/Debugger.java index a838f9c..85671fb 100644 --- a/src/main/java/com/songoda/epicbuckets/util/Debugger.java +++ b/src/main/java/com/songoda/epicbuckets/util/Debugger.java @@ -15,8 +15,16 @@ public class Debugger { epicBuckets.getLogger().info(ChatColor.translateAlternateColorCodes('&', message)); } + public void invalidInventorySize(String item) { + sendConsole(item + " has an invalid inventory size, disabling shop.."); + } + + public void invalidGenbucketType(String item) { + sendConsole(item + " has an invalid Genbucket type, disabling shop.."); + } + public void invalidSlot(String item) { - sendConsole(item + " has an invalid slot set, disabling shop.."); + sendConsole(item + " has an invalid slot, disabling shop.."); } public void invalidPrice(String item) { diff --git a/src/main/java/com/songoda/epicbuckets/util/Validator.java b/src/main/java/com/songoda/epicbuckets/util/Validator.java index c85b498..e62e5a5 100644 --- a/src/main/java/com/songoda/epicbuckets/util/Validator.java +++ b/src/main/java/com/songoda/epicbuckets/util/Validator.java @@ -1,6 +1,7 @@ package com.songoda.epicbuckets.util; import com.songoda.epicbuckets.EpicBuckets; +import com.songoda.epicbuckets.genbucket.GenbucketType; public class Validator { @@ -15,6 +16,23 @@ public class Validator { private Validator() { } + public int inventorySize(String s) { + int i; + if (isInt(s)) { + i = Integer.parseInt(s); + if (i%9 == 0 && i<=54) return i; + } + return -1; + } + + public GenbucketType genbucketType(String s) { + try { + return GenbucketType.valueOf(s); + } catch (Exception e) { + return null; + } + } + public boolean isDouble(String s) { try { Double.parseDouble(s);