From 512b5c71e8468fca063b375261de80bb8eecae8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 18:23:51 +0100 Subject: [PATCH 01/20] Use appropriated isSedated method --- .../java/com/tomff/beesplus/gui/BeeHiveInfo.java | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java b/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java index ff1dfb9..be0d3a1 100644 --- a/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java +++ b/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java @@ -8,8 +8,6 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Beehive; -import org.bukkit.block.Block; -import org.bukkit.block.data.type.Campfire; import org.bukkit.inventory.ItemStack; public class BeeHiveInfo extends Gui { @@ -54,18 +52,6 @@ public class BeeHiveInfo extends Gui { } } - private boolean isSedated(Location location) { - for (int i = 1; i <= 5; i++) { - Block block = location.subtract(0, 1, 0).getBlock(); - - if (block.getType() == Material.CAMPFIRE && ((Campfire) block.getBlockData()).isLit()) { - return true; - } - } - - return false; - } - private int getBeehivePopulation(Beehive beehive) { return beehive.getEntityCount(); } @@ -87,7 +73,7 @@ public class BeeHiveInfo extends Gui { Icon honeyLevelIcon = new Icon(honeyLevel, null); setIcon(honeyLevelIcon, 10); - String isSedated = isSedated(beehive.getLocation()) ? Localization.get(Localization.BEEHIVE_INFO_GUI_SEDATED) : + String isSedated = beehive.isSedated() ? Localization.get(Localization.BEEHIVE_INFO_GUI_SEDATED) : Localization.get(Localization.BEEHIVE_INFO_GUI_NOT_SEDATED); ItemStack beeCapacity = new ItemBuilder(Material.BEE_NEST) From b85876c8b3ed11c905c93f3ebe4ccde17046d04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 19:42:39 +0100 Subject: [PATCH 02/20] Custom items refractoring --- src/main/java/com/tomff/beesplus/BeesPlus.java | 2 -- .../com/tomff/beesplus/core/items/CustomItem.java | 9 +-------- .../beesplus/core/items/CustomItemManager.java | 14 +++++--------- .../com/tomff/beesplus/handlers/DamageHandler.java | 8 ++++---- .../tomff/beesplus/items/BeeProtectionBoots.java | 13 ++++++------- .../beesplus/items/BeeProtectionChestplate.java | 13 ++++++------- .../tomff/beesplus/items/BeeProtectionHelmet.java | 13 ++++++------- .../beesplus/items/BeeProtectionLeggings.java | 13 ++++++------- 8 files changed, 34 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index 283e0c5..d10a46f 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -73,8 +73,6 @@ public class BeesPlus extends JavaPlugin { customItemManager.registerCustomItem("protection_chestplate", new BeeProtectionChestplate()); customItemManager.registerCustomItem("protection_helmet", new BeeProtectionHelmet()); - customItemManager.registerRecipes(); - getServer().getPluginManager().registerEvents(new DamageHandler(this), this); } diff --git a/src/main/java/com/tomff/beesplus/core/items/CustomItem.java b/src/main/java/com/tomff/beesplus/core/items/CustomItem.java index f1eebf4..dbb04e2 100644 --- a/src/main/java/com/tomff/beesplus/core/items/CustomItem.java +++ b/src/main/java/com/tomff/beesplus/core/items/CustomItem.java @@ -9,13 +9,6 @@ public abstract class CustomItem { public abstract String[] getRecipe(); public abstract Map getIngredients(); - public abstract String getName(); - public abstract Material getMaterial(); - - public ItemStack getItem() { - return new ItemBuilder(getMaterial()) - .setName(getName()) - .build(); - } + public abstract ItemStack getResult(); } diff --git a/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java b/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java index 6b33370..d1a8bec 100644 --- a/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java +++ b/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java @@ -24,16 +24,12 @@ public class CustomItemManager { public void registerCustomItem(String id, CustomItem customItem) { customItems.put(id, customItem); - } - public void registerRecipes() { - customItems.forEach((id, customItem) -> { - NamespacedKey namespacedKey = new NamespacedKey(beesPlus, id); - ShapedRecipe recipe = new ShapedRecipe(namespacedKey, customItem.getItem()); - recipe.shape(customItem.getRecipe()); - customItem.getIngredients().forEach(recipe::setIngredient); + NamespacedKey namespacedKey = new NamespacedKey(beesPlus, id); + ShapedRecipe recipe = new ShapedRecipe(namespacedKey, customItem.getResult()); + recipe.shape(customItem.getRecipe()); + customItem.getIngredients().forEach(recipe::setIngredient); - Bukkit.addRecipe(recipe); - }); + Bukkit.addRecipe(recipe); } } diff --git a/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java b/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java index f8f9995..0d6517a 100644 --- a/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java +++ b/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java @@ -42,10 +42,10 @@ public class DamageHandler implements Listener { PlayerInventory playerInventory = player.getInventory(); if (Stream.of(playerInventory.getArmorContents()).allMatch(Objects::nonNull)) { - if (playerInventory.getHelmet().isSimilar(helmet.getItem()) && - playerInventory.getChestplate().isSimilar(chestplate.getItem()) && - playerInventory.getLeggings().isSimilar(leggings.getItem()) && - playerInventory.getBoots().isSimilar(boots.getItem())) { + if (playerInventory.getHelmet().isSimilar(helmet.getResult()) && + playerInventory.getChestplate().isSimilar(chestplate.getResult()) && + playerInventory.getLeggings().isSimilar(leggings.getResult()) && + playerInventory.getBoots().isSimilar(boots.getResult())) { event.setDamage(reduction * event.getDamage()); } diff --git a/src/main/java/com/tomff/beesplus/items/BeeProtectionBoots.java b/src/main/java/com/tomff/beesplus/items/BeeProtectionBoots.java index 8d9fb92..e77af44 100644 --- a/src/main/java/com/tomff/beesplus/items/BeeProtectionBoots.java +++ b/src/main/java/com/tomff/beesplus/items/BeeProtectionBoots.java @@ -1,8 +1,10 @@ package com.tomff.beesplus.items; import com.tomff.beesplus.core.items.CustomItem; +import com.tomff.beesplus.core.items.ItemBuilder; import com.tomff.beesplus.localization.Localization; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; @@ -28,12 +30,9 @@ public class BeeProtectionBoots extends CustomItem { } @Override - public String getName() { - return Localization.get(Localization.BEE_PROTECTION_BOOTS); - } - - @Override - public Material getMaterial() { - return Material.CHAINMAIL_BOOTS; + public ItemStack getResult() { + return new ItemBuilder(Material.CHAINMAIL_BOOTS) + .setName(Localization.get(Localization.BEE_PROTECTION_BOOTS)) + .build(); } } diff --git a/src/main/java/com/tomff/beesplus/items/BeeProtectionChestplate.java b/src/main/java/com/tomff/beesplus/items/BeeProtectionChestplate.java index dfa106d..b88b90c 100644 --- a/src/main/java/com/tomff/beesplus/items/BeeProtectionChestplate.java +++ b/src/main/java/com/tomff/beesplus/items/BeeProtectionChestplate.java @@ -1,8 +1,10 @@ package com.tomff.beesplus.items; import com.tomff.beesplus.core.items.CustomItem; +import com.tomff.beesplus.core.items.ItemBuilder; import com.tomff.beesplus.localization.Localization; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; @@ -28,12 +30,9 @@ public class BeeProtectionChestplate extends CustomItem { } @Override - public String getName() { - return Localization.get(Localization.BEE_PROTECTION_CHESTPLATE); - } - - @Override - public Material getMaterial() { - return Material.CHAINMAIL_CHESTPLATE; + public ItemStack getResult() { + return new ItemBuilder(Material.CHAINMAIL_CHESTPLATE) + .setName(Localization.get(Localization.BEE_PROTECTION_CHESTPLATE)) + .build(); } } diff --git a/src/main/java/com/tomff/beesplus/items/BeeProtectionHelmet.java b/src/main/java/com/tomff/beesplus/items/BeeProtectionHelmet.java index cd33229..9d1f446 100644 --- a/src/main/java/com/tomff/beesplus/items/BeeProtectionHelmet.java +++ b/src/main/java/com/tomff/beesplus/items/BeeProtectionHelmet.java @@ -1,8 +1,10 @@ package com.tomff.beesplus.items; import com.tomff.beesplus.core.items.CustomItem; +import com.tomff.beesplus.core.items.ItemBuilder; import com.tomff.beesplus.localization.Localization; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; @@ -28,12 +30,9 @@ public class BeeProtectionHelmet extends CustomItem { } @Override - public String getName() { - return Localization.get(Localization.BEE_PROTECTION_HELMET); - } - - @Override - public Material getMaterial() { - return Material.CHAINMAIL_HELMET; + public ItemStack getResult() { + return new ItemBuilder(Material.CHAINMAIL_HELMET) + .setName(Localization.get(Localization.BEE_PROTECTION_HELMET)) + .build(); } } diff --git a/src/main/java/com/tomff/beesplus/items/BeeProtectionLeggings.java b/src/main/java/com/tomff/beesplus/items/BeeProtectionLeggings.java index 897d44d..dc5d439 100644 --- a/src/main/java/com/tomff/beesplus/items/BeeProtectionLeggings.java +++ b/src/main/java/com/tomff/beesplus/items/BeeProtectionLeggings.java @@ -1,8 +1,10 @@ package com.tomff.beesplus.items; import com.tomff.beesplus.core.items.CustomItem; +import com.tomff.beesplus.core.items.ItemBuilder; import com.tomff.beesplus.localization.Localization; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; @@ -28,12 +30,9 @@ public class BeeProtectionLeggings extends CustomItem { } @Override - public String getName() { - return Localization.get(Localization.BEE_PROTECTION_LEGGINGS); - } - - @Override - public Material getMaterial() { - return Material.CHAINMAIL_LEGGINGS; + public ItemStack getResult() { + return new ItemBuilder(Material.CHAINMAIL_LEGGINGS) + .setName(Localization.get(Localization.BEE_PROTECTION_LEGGINGS)) + .build(); } } From 1f079051fab87c738614b5825dbec04dd2028785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 19:58:17 +0100 Subject: [PATCH 03/20] Split the enable method into other functions --- .../java/com/tomff/beesplus/BeesPlus.java | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index d10a46f..e9b9ed9 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -31,51 +31,19 @@ public class BeesPlus extends JavaPlugin { public void onEnable() { saveDefaultConfig(); - String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()); - - localizationWrapper = new LocalizationWrapper(this, "locale"); - - try { - YamlConfiguration localeYamlFile = localizationWrapper.getLocale(locale); - Localization.load(localeYamlFile); - } catch (IOException e) { - getLogger().severe("Invalid locale! Please choose a valid locale."); - disablePlugin(); - - return; - } catch (InvalidConfigurationException e) { - getLogger().severe(e.getMessage()); - getLogger().severe("Locale file corrupted or malformed! Please check your locale file."); - disablePlugin(); - - return; - } catch (IllegalArgumentException e) { - getLogger().severe(e.getMessage()); - getLogger().severe("Error in the locale file! Please check your locale file."); - getLogger().severe("Maybe caused by a typo"); - disablePlugin(); - - return; - } - guiManager = new GuiManager(); + customItemManager = new CustomItemManager(this); getServer().getPluginManager().registerEvents(new GuiHandler(this), this); getServer().getPluginManager().registerEvents(new RightClickHandler(this), this); - boolean isProtectionSuitEnabled = getConfig().getBoolean("beeprotectionsuit.enabled", true); - - if (isProtectionSuitEnabled) { - customItemManager = new CustomItemManager(this); - - customItemManager.registerCustomItem("protection_boots", new BeeProtectionBoots()); - customItemManager.registerCustomItem("protection_leggings", new BeeProtectionLeggings()); - customItemManager.registerCustomItem("protection_chestplate", new BeeProtectionChestplate()); - customItemManager.registerCustomItem("protection_helmet", new BeeProtectionHelmet()); - - getServer().getPluginManager().registerEvents(new DamageHandler(this), this); + if (!loadLocale()) { + getServer().getPluginManager().disablePlugin(this); + return; } + registerItems(); + Metrics metrics = new Metrics(this, 7065); new UpdateChecker(this, 77224).getVersion(version -> { @@ -85,8 +53,38 @@ public class BeesPlus extends JavaPlugin { }); } - private void disablePlugin() { - getServer().getPluginManager().disablePlugin(this); + private boolean loadLocale() { + String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()); + localizationWrapper = new LocalizationWrapper(this, "locale"); + + try { + YamlConfiguration localeYamlFile = localizationWrapper.getLocale(locale); + Localization.load(localeYamlFile); + } catch (IOException e) { + getLogger().severe("Invalid locale! Please choose a valid locale."); + + return false; + } catch (InvalidConfigurationException | IllegalArgumentException e) { + getLogger().severe(e.getMessage()); + + getLogger().severe("Locale file corrupted or malformed! Please check your locale file."); + return false; + } + + return true; + } + + private void registerItems() { + boolean isProtectionSuitEnabled = getConfig().getBoolean("beeprotectionsuit.enabled", true); + + if(isProtectionSuitEnabled) { + customItemManager.registerCustomItem("protection_boots", new BeeProtectionBoots()); + customItemManager.registerCustomItem("protection_leggings", new BeeProtectionLeggings()); + customItemManager.registerCustomItem("protection_chestplate", new BeeProtectionChestplate()); + customItemManager.registerCustomItem("protection_helmet", new BeeProtectionHelmet()); + + getServer().getPluginManager().registerEvents(new DamageHandler(this), this); + } } public GuiManager getGuiManager() { From c73fcba252d79387a0428f5065b78600a703393f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 20:08:15 +0100 Subject: [PATCH 04/20] Use guard clause instead of nested if statememt --- .../tomff/beesplus/handlers/DamageHandler.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java b/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java index 0d6517a..6be39b9 100644 --- a/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java +++ b/src/main/java/com/tomff/beesplus/handlers/DamageHandler.java @@ -41,14 +41,16 @@ public class DamageHandler implements Listener { Player player = (Player) event.getEntity(); PlayerInventory playerInventory = player.getInventory(); - if (Stream.of(playerInventory.getArmorContents()).allMatch(Objects::nonNull)) { - if (playerInventory.getHelmet().isSimilar(helmet.getResult()) && - playerInventory.getChestplate().isSimilar(chestplate.getResult()) && - playerInventory.getLeggings().isSimilar(leggings.getResult()) && - playerInventory.getBoots().isSimilar(boots.getResult())) { + if (!Stream.of(playerInventory.getArmorContents()).allMatch(Objects::nonNull)) { + return; + } - event.setDamage(reduction * event.getDamage()); - } + if (playerInventory.getHelmet().isSimilar(helmet.getResult()) && + playerInventory.getChestplate().isSimilar(chestplate.getResult()) && + playerInventory.getLeggings().isSimilar(leggings.getResult()) && + playerInventory.getBoots().isSimilar(boots.getResult())) { + + event.setDamage(reduction * event.getDamage()); } } } From 3ba2c17c6a8fb6390d5ad91bfabea5038d9db5ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 21:15:18 +0100 Subject: [PATCH 05/20] Add support to hives with variable capacity --- .../java/com/tomff/beesplus/gui/BeeHiveInfo.java | 13 +++---------- .../tomff/beesplus/gui/HoneyLevelIndicators.java | 15 ++++++++++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java b/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java index be0d3a1..dcf915e 100644 --- a/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java +++ b/src/main/java/com/tomff/beesplus/gui/BeeHiveInfo.java @@ -52,14 +52,6 @@ public class BeeHiveInfo extends Gui { } } - private int getBeehivePopulation(Beehive beehive) { - return beehive.getEntityCount(); - } - - private int getBeehiveMaxPopulation(Beehive beehive) { - return beehive.getMaxEntities(); - } - @Override public void buildIcons() { org.bukkit.block.data.type.Beehive beehiveData = (org.bukkit.block.data.type.Beehive) beehive.getBlockData(); @@ -78,7 +70,7 @@ public class BeeHiveInfo extends Gui { ItemStack beeCapacity = new ItemBuilder(Material.BEE_NEST) .setName(Localization.get(Localization.BEEHIVE_INFO_GUI_BEE_CAPACITY)) - .setLore(Localization.get(Localization.BEEHIVE_INFO_GUI_BEE_CAPACITY_DESC, getBeehivePopulation(beehive), getBeehiveMaxPopulation(beehive)), + .setLore(Localization.get(Localization.BEEHIVE_INFO_GUI_BEE_CAPACITY_DESC, beehive.getEntityCount(), beehive.getMaxEntities()), isSedated) .build(); @@ -100,7 +92,8 @@ public class BeeHiveInfo extends Gui { Icon flowerIcon = new Icon(flower, null); setIcon(flowerIcon, 37); - HoneyLevelIndicators honeyLevelIndicator = HoneyLevelIndicators.getFromLevel(beehiveData.getHoneyLevel()); + HoneyLevelIndicators honeyLevelIndicator = HoneyLevelIndicators.getFromLevel(beehiveData.getHoneyLevel(), + beehiveData.getMaximumHoneyLevel()); setHoneyLevelSlots(honeyLevelIndicator); ItemStack filler = new ItemBuilder(Material.WHITE_STAINED_GLASS_PANE) diff --git a/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java b/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java index a221d48..43eca14 100644 --- a/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java +++ b/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java @@ -28,11 +28,16 @@ public enum HoneyLevelIndicators { this.slots = slots; } - public static HoneyLevelIndicators getFromLevel(int level) { - return Arrays.stream(values()) - .filter((levelIndicator) -> levelIndicator.level == level) - .findFirst() - .orElse(VERY_HIGH); + public static HoneyLevelIndicators getFromLevel(int currentHoneyLvl, int maxHoneyLvl) { + float ratio = (float) currentHoneyLvl / (float) maxHoneyLvl; + + if (ratio == 0) return EMPTY; + if (ratio <= 0.25) return LOW; + if (ratio <= 0.50) return MEDIUM; + if (ratio <= 0.75) return HIGH; + if (ratio <= 1) return VERY_HIGH; + + return EMPTY; } public int getLevel() { From 84ab5daf404005489293c50df4eb935481a5f3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 21:16:34 +0100 Subject: [PATCH 06/20] Remove unused level function --- .../java/com/tomff/beesplus/gui/HoneyLevelIndicators.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java b/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java index 43eca14..ce69bee 100644 --- a/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java +++ b/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java @@ -40,10 +40,6 @@ public enum HoneyLevelIndicators { return EMPTY; } - public int getLevel() { - return level; - } - public ChatColor getColor() { return color; } From 5e9f3bc7913e218d53ec23caf38f24282fcd5a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Wed, 1 Jul 2020 23:46:39 +0100 Subject: [PATCH 07/20] Beehive upgrades MVP --- .../java/com/tomff/beesplus/BeesPlus.java | 10 +- .../beesplus/core/items/ItemBuilder.java | 28 ++++ .../tomff/beesplus/items/BeeHiveUpgrade.java | 126 ++++++++++++++++++ .../beesplus/localization/Localization.java | 4 + src/main/resources/config.yml | 9 +- src/main/resources/locale/en.yml | 13 +- 6 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index e9b9ed9..11887aa 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -2,10 +2,7 @@ package com.tomff.beesplus; import com.tomff.beesplus.handlers.DamageHandler; import com.tomff.beesplus.handlers.RightClickHandler; -import com.tomff.beesplus.items.BeeProtectionBoots; -import com.tomff.beesplus.items.BeeProtectionChestplate; -import com.tomff.beesplus.items.BeeProtectionHelmet; -import com.tomff.beesplus.items.BeeProtectionLeggings; +import com.tomff.beesplus.items.*; import com.tomff.beesplus.core.UpdateChecker; import com.tomff.beesplus.core.gui.GuiHandler; import com.tomff.beesplus.core.gui.GuiManager; @@ -75,6 +72,11 @@ public class BeesPlus extends JavaPlugin { } private void registerItems() { + BeeHiveUpgrade beeHiveUpgrade = new BeeHiveUpgrade(this); + + customItemManager.registerCustomItem("honey_upgrade", beeHiveUpgrade); + getServer().getPluginManager().registerEvents(beeHiveUpgrade, this); + boolean isProtectionSuitEnabled = getConfig().getBoolean("beeprotectionsuit.enabled", true); if(isProtectionSuitEnabled) { diff --git a/src/main/java/com/tomff/beesplus/core/items/ItemBuilder.java b/src/main/java/com/tomff/beesplus/core/items/ItemBuilder.java index 2896e29..a6fbfe3 100644 --- a/src/main/java/com/tomff/beesplus/core/items/ItemBuilder.java +++ b/src/main/java/com/tomff/beesplus/core/items/ItemBuilder.java @@ -1,8 +1,12 @@ package com.tomff.beesplus.core.items; import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; import java.util.Arrays; @@ -33,6 +37,30 @@ public class ItemBuilder { return this; } + public ItemBuilder enchant(Enchantment enchantment, int level) { + item.addUnsafeEnchantment(enchantment, level); + + return this; + } + + public ItemBuilder hideEnchantments() { + ItemMeta meta = item.getItemMeta(); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + + item.setItemMeta(meta); + + return this; + } + + public ItemBuilder setPersistentData(NamespacedKey key, PersistentDataType type, Z value) { + ItemMeta meta = item.getItemMeta(); + meta.getPersistentDataContainer().set(key, type, value); + + item.setItemMeta(meta); + + return this; + } + public ItemStack build() { return item; } diff --git a/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java b/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java new file mode 100644 index 0000000..0993ecc --- /dev/null +++ b/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java @@ -0,0 +1,126 @@ +package com.tomff.beesplus.items; + +import com.tomff.beesplus.BeesPlus; +import com.tomff.beesplus.core.items.CustomItem; +import com.tomff.beesplus.core.items.ItemBuilder; +import com.tomff.beesplus.gui.BeeHiveInfo; +import com.tomff.beesplus.localization.Localization; +import org.bukkit.*; +import org.bukkit.block.Beehive; +import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.HashMap; +import java.util.Map; + +public class BeeHiveUpgrade extends CustomItem implements Listener { + + private NamespacedKey upgradeKey; + private int maxPopulation; + + public BeeHiveUpgrade(BeesPlus beesPlus) { + upgradeKey = new NamespacedKey(beesPlus, "upgrade"); + maxPopulation = beesPlus.getConfig().getInt("beehiveupgrade.maximumpopulation", 9); + } + + @Override + public String[] getRecipe() { + return new String[] { + "CCC", + "CHC", + "CCC" + }; + } + + @Override + public Map getIngredients() { + Map ingredients = new HashMap<>(); + + ingredients.put('C', Material.HONEYCOMB); + ingredients.put('H', Material.BEEHIVE); + + return ingredients; + } + + @Override + public ItemStack getResult() { + return new ItemBuilder(Material.HONEYCOMB) + .setName(Localization.get(Localization.BEEHIVE_UPGRADE_ITEM_NAME)) + .setLore(Localization.get(Localization.BEEHIVE_UPGRADE_ITEM_LORE).split("\\|\\|")) + .setPersistentData(upgradeKey, PersistentDataType.STRING, "beehive") + .enchant(Enchantment.DURABILITY, 1) + .hideEnchantments() + .build(); + } + + @EventHandler + public void onHiveClick(PlayerInteractEvent event) { + Player player = event.getPlayer(); + Action action = event.getAction(); + Block clickedBlock = event.getClickedBlock(); + + if (event.getHand() != EquipmentSlot.HAND) { + return; + } + + if (clickedBlock == null) { + return; + } + + if (event.getItem() == null) { + return; + } + + if (action == Action.RIGHT_CLICK_BLOCK && clickedBlock.getType().equals(Material.BEEHIVE)) { + ItemStack handItem = event.getItem(); + ItemMeta handItemMeta = handItem.getItemMeta(); + + if (handItemMeta == null) { + return; + } + + PersistentDataContainer container = handItemMeta.getPersistentDataContainer(); + + if (!container.has(upgradeKey, PersistentDataType.STRING)) { + return; + } + + String upgradeType = container.get(upgradeKey, PersistentDataType.STRING); + + if (upgradeType.equals("beehive")) { + event.setCancelled(true); + + Beehive beehive = (Beehive) clickedBlock.getState(); + + if (beehive.getMaxEntities() >= maxPopulation) { + Localization.sendMessage(player, Localization.BEEHIVE_UPGRADE_MAX); + player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); + + return; + } + + beehive.setMaxEntities(beehive.getMaxEntities() + 3); + beehive.update(); + + Localization.sendMessage(player, Localization.BEEHIVE_UPGRADE_SUCCESS, beehive.getMaxEntities()); + player.playSound(player.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 2, 2); + + ItemStack upgradeAmountRemove = handItem.clone(); + upgradeAmountRemove.setAmount(1); + + player.getInventory().removeItem(upgradeAmountRemove); + } + } + } +} diff --git a/src/main/java/com/tomff/beesplus/localization/Localization.java b/src/main/java/com/tomff/beesplus/localization/Localization.java index 629151c..f6a4142 100644 --- a/src/main/java/com/tomff/beesplus/localization/Localization.java +++ b/src/main/java/com/tomff/beesplus/localization/Localization.java @@ -43,6 +43,10 @@ public enum Localization { BEE_PROTECTION_CHESTPLATE, BEE_PROTECTION_LEGGINGS, BEE_PROTECTION_BOOTS, + BEEHIVE_UPGRADE_ITEM_NAME, + BEEHIVE_UPGRADE_ITEM_LORE, + BEEHIVE_UPGRADE_SUCCESS("beesno"), + BEEHIVE_UPGRADE_MAX, HONEY_LOW, HONEY_MEDIUM, HONEY_HIGH, diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e6dd67c..bea81be 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,4 +23,11 @@ beeprotectionsuit: # removeanger: remove anger from bees when they are healed? # healing: - removeanger: true \ No newline at end of file + removeanger: true + +# Beehive upgrade settings +# +# maximumSize: maximum number of bees a beehive can be upgraded to +# +beehiveupgrade: + maximumpopulation: 9 \ No newline at end of file diff --git a/src/main/resources/locale/en.yml b/src/main/resources/locale/en.yml index c6c9151..49eb2ff 100644 --- a/src/main/resources/locale/en.yml +++ b/src/main/resources/locale/en.yml @@ -76,4 +76,15 @@ ride_bee_subtitle: "&6a bee &8%name%&6!" bee_protection_helmet: "&6Bee Protection Helmet" bee_protection_chestplate: "&6Bee Protection Chestplate" bee_protection_leggings: "&6Bee Protection Leggings" -bee_protection_boots: "&6Bee Protection Boots" \ No newline at end of file +bee_protection_boots: "&6Bee Protection Boots" + +################### +# Beehive upgrade item +################### +beehive_upgrade_item_name: "&6Beehive Upgrade" + +# Use || to a create a new line +beehive_upgrade_item_lore: "&7Bee capacity: &a+3||&8(Right click to use)" + +beehive_upgrade_success: "&aBeehive upgraded! New population: &7%beesno%&a bees" +beehive_upgrade_max: "&cError: This beehive has reached the maximum population allowed!" \ No newline at end of file From 416167e5c1100b39c245474d307045599da194d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Thu, 2 Jul 2020 00:14:26 +0100 Subject: [PATCH 08/20] Improved GUI robustness and fixed critical bug --- .../java/com/tomff/beesplus/BeesPlus.java | 15 +++-- .../java/com/tomff/beesplus/core/gui/Gui.java | 4 ++ .../tomff/beesplus/core/gui/GuiHandler.java | 63 ++++++++++++++----- .../tomff/beesplus/core/gui/GuiManager.java | 26 -------- .../beesplus/core/gui/GuiViewTracker.java | 39 ++++++++++++ .../com/tomff/beesplus/core/gui/View.java | 22 +++++++ .../beesplus/handlers/RightClickHandler.java | 10 +-- 7 files changed, 126 insertions(+), 53 deletions(-) delete mode 100644 src/main/java/com/tomff/beesplus/core/gui/GuiManager.java create mode 100644 src/main/java/com/tomff/beesplus/core/gui/GuiViewTracker.java create mode 100644 src/main/java/com/tomff/beesplus/core/gui/View.java diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index 11887aa..785003e 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -5,7 +5,7 @@ import com.tomff.beesplus.handlers.RightClickHandler; import com.tomff.beesplus.items.*; import com.tomff.beesplus.core.UpdateChecker; import com.tomff.beesplus.core.gui.GuiHandler; -import com.tomff.beesplus.core.gui.GuiManager; +import com.tomff.beesplus.core.gui.GuiViewTracker; import com.tomff.beesplus.core.items.CustomItemManager; import com.tomff.beesplus.localization.Localization; import com.tomff.beesplus.localization.LocalizationWrapper; @@ -19,7 +19,7 @@ import java.util.Locale; public class BeesPlus extends JavaPlugin { - private GuiManager guiManager; + private GuiViewTracker guiViewTracker; private CustomItemManager customItemManager; private LocalizationWrapper localizationWrapper; @@ -28,7 +28,7 @@ public class BeesPlus extends JavaPlugin { public void onEnable() { saveDefaultConfig(); - guiManager = new GuiManager(); + guiViewTracker = new GuiViewTracker(); customItemManager = new CustomItemManager(this); getServer().getPluginManager().registerEvents(new GuiHandler(this), this); @@ -50,6 +50,11 @@ public class BeesPlus extends JavaPlugin { }); } + @Override + public void onDisable() { + guiViewTracker.clearViews(); + } + private boolean loadLocale() { String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()); localizationWrapper = new LocalizationWrapper(this, "locale"); @@ -89,8 +94,8 @@ public class BeesPlus extends JavaPlugin { } } - public GuiManager getGuiManager() { - return guiManager; + public GuiViewTracker getGuiViewTracker() { + return guiViewTracker; } public CustomItemManager getCustomItemManager() { diff --git a/src/main/java/com/tomff/beesplus/core/gui/Gui.java b/src/main/java/com/tomff/beesplus/core/gui/Gui.java index a82d10d..7b56257 100644 --- a/src/main/java/com/tomff/beesplus/core/gui/Gui.java +++ b/src/main/java/com/tomff/beesplus/core/gui/Gui.java @@ -30,6 +30,10 @@ public abstract class Gui { } } + public boolean hasIcon(int slot) { + return icons.containsKey(slot); + } + public Icon getIcon(int slot) { return icons.get(slot); } diff --git a/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java b/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java index 0383d0b..9382d2f 100644 --- a/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java +++ b/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java @@ -6,6 +6,8 @@ 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.InventoryCloseEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -15,42 +17,69 @@ import java.util.function.Consumer; public class GuiHandler implements Listener { private final BeesPlus beesPlus; - private final GuiManager guiManager; + private final GuiViewTracker guiViewTracker; public GuiHandler(BeesPlus beesPlus) { this.beesPlus = beesPlus; - this.guiManager = beesPlus.getGuiManager(); + this.guiViewTracker = beesPlus.getGuiViewTracker(); } @EventHandler public void onInventoryClick(InventoryClickEvent event) { - if (!(event.getWhoClicked() instanceof Player)) { - return; - } + if (!(event.getWhoClicked() instanceof Player)) return; - Inventory clickedInventory = event.getInventory(); Player player = (Player) event.getWhoClicked(); UUID uuid = player.getUniqueId(); - if (guiManager.getOpenedGuis().containsKey(uuid) && - guiManager.getOpenedGuis().get(uuid).getInventory().equals(clickedInventory)) { + if (!guiViewTracker.isViewingGui(uuid)) { + return; + } - event.setCancelled(true); + if (guiViewTracker.getView(uuid).getInventoryView() != event.getView()) { + return; + } - ItemStack clickedItem = event.getCurrentItem(); + event.setCancelled(true); - if (clickedItem == null || clickedItem.getType() == Material.AIR) return; + ItemStack clickedItem = event.getCurrentItem(); - Gui gui = guiManager.getOpenedGuis().get(uuid); + if (clickedItem == null) return; + if (clickedItem.getType() == Material.AIR) return; - Icon icon = gui.getIcon(event.getRawSlot()); - if (icon == null) return; + int slot = event.getRawSlot(); - Consumer callback = icon.getCallback(); - if(callback == null) return; + View view = guiViewTracker.getView(uuid); + Gui gui = view.getGui(); - icon.getCallback().accept(player); + if (!gui.hasIcon(slot)) return; + + Icon icon = gui.getIcon(event.getRawSlot()); + Consumer callback = icon.getCallback(); + + if(callback == null) return; + + icon.getCallback().accept(player); + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) { + if (!(event.getPlayer() instanceof Player)) return; + + Player player = (Player) event.getPlayer(); + UUID uuid = player.getUniqueId(); + + if (!guiViewTracker.isViewingGui(uuid)) { + return; + } + + if (guiViewTracker.getView(uuid).getInventoryView() == event.getView()) { + guiViewTracker.removeView(uuid); } } + @EventHandler + public void onLeave(PlayerQuitEvent event) { + guiViewTracker.removeView(event.getPlayer().getUniqueId()); + } + } diff --git a/src/main/java/com/tomff/beesplus/core/gui/GuiManager.java b/src/main/java/com/tomff/beesplus/core/gui/GuiManager.java deleted file mode 100644 index 8e74f47..0000000 --- a/src/main/java/com/tomff/beesplus/core/gui/GuiManager.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.tomff.beesplus.core.gui; - -import org.bukkit.entity.Player; - -import java.util.HashMap; -import java.util.UUID; - -public class GuiManager { - - private HashMap openedGuis; - - public GuiManager() { - openedGuis = new HashMap<>(); - } - - public void openGui(Player player, Gui gui) { - openedGuis.put(player.getUniqueId(), gui); - - gui.buildIcons(); - player.openInventory(gui.getInventory()); - } - - public HashMap getOpenedGuis() { - return openedGuis; - } -} diff --git a/src/main/java/com/tomff/beesplus/core/gui/GuiViewTracker.java b/src/main/java/com/tomff/beesplus/core/gui/GuiViewTracker.java new file mode 100644 index 0000000..25c7bfa --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/gui/GuiViewTracker.java @@ -0,0 +1,39 @@ +package com.tomff.beesplus.core.gui; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryView; + +import java.util.HashMap; +import java.util.UUID; + +public class GuiViewTracker { + + private HashMap views; + + public GuiViewTracker() { + views = new HashMap<>(); + } + + public boolean isViewingGui(UUID uuid) { + return views.containsKey(uuid); + } + + public View getView(UUID uuid) { + return views.get(uuid); + } + + public void removeView(UUID uuid) { + views.remove(uuid); + } + + public void clearViews() { + views.forEach((uuid, view) -> view.getInventoryView().close()); + } + + public void openGui(Player player, Gui gui) { + gui.buildIcons(); + InventoryView view = player.openInventory(gui.getInventory()); + + views.put(player.getUniqueId(), new View(view, gui)); + } +} diff --git a/src/main/java/com/tomff/beesplus/core/gui/View.java b/src/main/java/com/tomff/beesplus/core/gui/View.java new file mode 100644 index 0000000..6bf8ae7 --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/gui/View.java @@ -0,0 +1,22 @@ +package com.tomff.beesplus.core.gui; + +import org.bukkit.inventory.InventoryView; + +public class View { + + private final InventoryView inventoryView; + private final Gui gui; + + public View(InventoryView inventoryView, Gui gui) { + this.inventoryView = inventoryView; + this.gui = gui; + } + + public InventoryView getInventoryView() { + return inventoryView; + } + + public Gui getGui() { + return gui; + } +} diff --git a/src/main/java/com/tomff/beesplus/handlers/RightClickHandler.java b/src/main/java/com/tomff/beesplus/handlers/RightClickHandler.java index 8dc109f..6db8506 100644 --- a/src/main/java/com/tomff/beesplus/handlers/RightClickHandler.java +++ b/src/main/java/com/tomff/beesplus/handlers/RightClickHandler.java @@ -3,7 +3,7 @@ package com.tomff.beesplus.handlers; import com.tomff.beesplus.BeesPlus; import com.tomff.beesplus.gui.BeeHiveInfo; import com.tomff.beesplus.gui.BeeInfo; -import com.tomff.beesplus.core.gui.GuiManager; +import com.tomff.beesplus.core.gui.GuiViewTracker; import org.bukkit.EntityEffect; import org.bukkit.Material; import org.bukkit.block.Beehive; @@ -24,7 +24,7 @@ import java.util.Arrays; public class RightClickHandler implements Listener { private final BeesPlus beesPlus; - private final GuiManager guiManager; + private final GuiViewTracker guiViewTracker; private final boolean removeAnger; @@ -40,7 +40,7 @@ public class RightClickHandler implements Listener { public RightClickHandler(BeesPlus beesPlus) { this.beesPlus = beesPlus; - this.guiManager = beesPlus.getGuiManager(); + this.guiViewTracker = beesPlus.getGuiViewTracker(); removeAnger = beesPlus.getConfig().getBoolean("healing.removeanger", true); } @@ -64,7 +64,7 @@ public class RightClickHandler implements Listener { if (player.isSneaking() && player.hasPermission("beesplus.bee.view")) { event.setCancelled(true); - guiManager.openGui(player, new BeeInfo(bee)); + guiViewTracker.openGui(player, new BeeInfo(bee)); return; } @@ -99,7 +99,7 @@ public class RightClickHandler implements Listener { event.setCancelled(true); Beehive beehive = (Beehive) clickedBlock.getState(); - guiManager.openGui(player, new BeeHiveInfo(beehive)); + guiViewTracker.openGui(player, new BeeHiveInfo(beehive)); } } } From 7b349d4bc3bd7139cbac04de68ce1937c7e483af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Thu, 2 Jul 2020 18:55:08 +0100 Subject: [PATCH 09/20] Implement configuration files migration --- .../java/com/tomff/beesplus/BeesPlus.java | 20 ++- .../beesplus/core/migrations/AddField.java | 35 +++++ .../beesplus/core/migrations/Migration.java | 21 +++ .../core/migrations/MigrationsExecutor.java | 121 ++++++++++++++++++ .../beesplus/core/migrations/Operation.java | 13 ++ 5 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/tomff/beesplus/core/migrations/AddField.java create mode 100644 src/main/java/com/tomff/beesplus/core/migrations/Migration.java create mode 100644 src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java create mode 100644 src/main/java/com/tomff/beesplus/core/migrations/Operation.java diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index 785003e..d78f1e4 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -1,12 +1,15 @@ package com.tomff.beesplus; -import com.tomff.beesplus.handlers.DamageHandler; -import com.tomff.beesplus.handlers.RightClickHandler; -import com.tomff.beesplus.items.*; import com.tomff.beesplus.core.UpdateChecker; import com.tomff.beesplus.core.gui.GuiHandler; import com.tomff.beesplus.core.gui.GuiViewTracker; import com.tomff.beesplus.core.items.CustomItemManager; +import com.tomff.beesplus.core.migrations.AddField; +import com.tomff.beesplus.core.migrations.Migration; +import com.tomff.beesplus.core.migrations.MigrationsExecutor; +import com.tomff.beesplus.handlers.DamageHandler; +import com.tomff.beesplus.handlers.RightClickHandler; +import com.tomff.beesplus.items.*; import com.tomff.beesplus.localization.Localization; import com.tomff.beesplus.localization.LocalizationWrapper; import org.bstats.bukkit.Metrics; @@ -26,6 +29,7 @@ public class BeesPlus extends JavaPlugin { @Override public void onEnable() { + performMigrations(); saveDefaultConfig(); guiViewTracker = new GuiViewTracker(); @@ -76,6 +80,16 @@ public class BeesPlus extends JavaPlugin { return true; } + private void performMigrations() { + MigrationsExecutor migrationsExecutor = new MigrationsExecutor(this); + + migrationsExecutor.addMigration(1, new Migration() + .add(new AddField("config.yml", "beehiveupgrade.maximumpopulation", 9)) + ); + + migrationsExecutor.migrate(); + } + private void registerItems() { BeeHiveUpgrade beeHiveUpgrade = new BeeHiveUpgrade(this); diff --git a/src/main/java/com/tomff/beesplus/core/migrations/AddField.java b/src/main/java/com/tomff/beesplus/core/migrations/AddField.java new file mode 100644 index 0000000..6d9820e --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/migrations/AddField.java @@ -0,0 +1,35 @@ +package com.tomff.beesplus.core.migrations; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; + +public class AddField implements Operation { + private File file; + + private String fieldPath; + private Object fieldValue; + + public AddField(String filePath, String fieldPath, Object fieldValue) { + file = new File(BASE_PATH, filePath); + + this.fieldPath = fieldPath; + this.fieldValue = fieldValue; + } + + @Override + public void execute() throws IOException { + YamlConfiguration config = new YamlConfiguration(); + + try { + config.load(file); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + + config.set(fieldPath, fieldValue); + config.save(file); + } +} diff --git a/src/main/java/com/tomff/beesplus/core/migrations/Migration.java b/src/main/java/com/tomff/beesplus/core/migrations/Migration.java new file mode 100644 index 0000000..9da2996 --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/migrations/Migration.java @@ -0,0 +1,21 @@ +package com.tomff.beesplus.core.migrations; + +import java.util.ArrayList; +import java.util.List; + +public class Migration { + private List operations; + + public Migration() { + this.operations = new ArrayList<>(); + } + + public Migration add(Operation operation) { + operations.add(operation); + return this; + } + + public List getOperations() { + return operations; + } +} diff --git a/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java b/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java new file mode 100644 index 0000000..3b3e4e9 --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java @@ -0,0 +1,121 @@ +package com.tomff.beesplus.core.migrations; + +import com.tomff.beesplus.BeesPlus; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class MigrationsExecutor { + private int latestVersion; + + private Map migrations; + private File migrationFileRegistry; + private YamlConfiguration yaml; + + private BeesPlus beesPlus; + + public MigrationsExecutor(BeesPlus beesPlus) { + this.beesPlus = beesPlus; + this.migrations = new HashMap<>(); + this.yaml = new YamlConfiguration(); + this.migrationFileRegistry = new File(beesPlus.getDataFolder(), "migration.yml"); + this.latestVersion = 0; + } + + public void addMigration(int version, Migration migration) { + migrations.put(version, migration); + + if (version > latestVersion) { + this.latestVersion = version; + } + } + + public void migrate() { + if (hasConfig() && !hasMigrationRegistry()) { + loadRegistry(); + + migrateFrom(0); + return; + } + + if (!loadRegistry()) return; + + int version = yaml.getInt("migration", 0); + if (version == latestVersion) return; + + migrateFrom(version); + } + + private void migrateFrom(int version) { + beesPlus.getLogger().info("Old files detected. Migrating files to migration v" + latestVersion); + + for (int nextVersion = version + 1; nextVersion <= latestVersion; nextVersion++) { + performMigration(migrations.get(nextVersion)); + } + + setCurrentMigration(latestVersion); + } + + public void setCurrentMigration(int version) { + yaml.set("migration", version); + try { + yaml.save(migrationFileRegistry); + } catch (IOException e) { + beesPlus.getLogger().severe("An error occurred while trying to update the migration version"); + e.printStackTrace(); + } + } + + private void createIfNotExist() { + if (!hasMigrationRegistry()) { + yaml.set("migration", latestVersion); + + try { + yaml.save(migrationFileRegistry); + } catch (IOException e) { + e.printStackTrace(); + beesPlus.getLogger().severe("An error occurred while trying to create the migration registry file!"); + } + } + } + + private boolean loadRegistry() { + createIfNotExist(); + + try { + yaml.load(migrationFileRegistry); + } catch (IOException | InvalidConfigurationException e) { + beesPlus.getLogger().severe("An error occurred while opening the migration registry file."); + beesPlus.getLogger().severe("Suggested action: please delete the BeesPlus plugin folder and restart."); + e.printStackTrace(); + + return false; + } + + return true; + } + + private void performMigration(Migration migration) { + for(Operation operation : migration.getOperations()) { + try { + operation.execute(); + } catch (IOException e) { + e.printStackTrace(); + beesPlus.getLogger().severe("An error occurred while migrating a file"); + } + } + } + + private boolean hasMigrationRegistry() { + return migrationFileRegistry.exists(); + } + + private boolean hasConfig() { + return new File(beesPlus.getDataFolder(), "config.yml").exists(); + } + +} diff --git a/src/main/java/com/tomff/beesplus/core/migrations/Operation.java b/src/main/java/com/tomff/beesplus/core/migrations/Operation.java new file mode 100644 index 0000000..3976688 --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/migrations/Operation.java @@ -0,0 +1,13 @@ +package com.tomff.beesplus.core.migrations; + +import com.tomff.beesplus.BeesPlus; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; + +public interface Operation { + File BASE_PATH = JavaPlugin.getPlugin(BeesPlus.class).getDataFolder(); + + void execute() throws IOException; +} From 3e74124af2eafc3ce9f60da59ffa1dd1c484d6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Thu, 2 Jul 2020 18:55:16 +0100 Subject: [PATCH 10/20] Remove unused import --- src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java b/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java index ce69bee..c5edd05 100644 --- a/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java +++ b/src/main/java/com/tomff/beesplus/gui/HoneyLevelIndicators.java @@ -4,8 +4,6 @@ import com.tomff.beesplus.localization.Localization; import org.bukkit.ChatColor; import org.bukkit.Material; -import java.util.Arrays; - public enum HoneyLevelIndicators { EMPTY(0, Material.AIR, null, null, 0), From 3d7562061f2ae456b4c883ea7c06d16c591262b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 15:23:40 +0100 Subject: [PATCH 11/20] Add persistance to beehive population --- .../java/com/tomff/beesplus/BeesPlus.java | 2 + .../beesplus/handlers/BeehiveHandler.java | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/com/tomff/beesplus/handlers/BeehiveHandler.java diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index d78f1e4..b260bc0 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -7,6 +7,7 @@ import com.tomff.beesplus.core.items.CustomItemManager; import com.tomff.beesplus.core.migrations.AddField; import com.tomff.beesplus.core.migrations.Migration; import com.tomff.beesplus.core.migrations.MigrationsExecutor; +import com.tomff.beesplus.handlers.BeehiveHandler; import com.tomff.beesplus.handlers.DamageHandler; import com.tomff.beesplus.handlers.RightClickHandler; import com.tomff.beesplus.items.*; @@ -36,6 +37,7 @@ public class BeesPlus extends JavaPlugin { customItemManager = new CustomItemManager(this); getServer().getPluginManager().registerEvents(new GuiHandler(this), this); + getServer().getPluginManager().registerEvents(new BeehiveHandler(), this); getServer().getPluginManager().registerEvents(new RightClickHandler(this), this); if (!loadLocale()) { diff --git a/src/main/java/com/tomff/beesplus/handlers/BeehiveHandler.java b/src/main/java/com/tomff/beesplus/handlers/BeehiveHandler.java new file mode 100644 index 0000000..58aaec1 --- /dev/null +++ b/src/main/java/com/tomff/beesplus/handlers/BeehiveHandler.java @@ -0,0 +1,48 @@ +package com.tomff.beesplus.handlers; + +import org.bukkit.Material; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Item; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockDropItemEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BlockStateMeta; + +public class BeehiveHandler implements Listener { + + @EventHandler + public void onBeeHiveBreak(BlockDropItemEvent event) { + BlockState blockState = event.getBlockState(); + Material blockType = blockState.getType(); + + if (blockType != Material.BEEHIVE) { + return; + } + + if (event.getItems().size() == 0) { + return; + } + + event.getItems() + .stream() + .filter(this::validBeehive) + .findFirst() + .ifPresent(item -> saveState(blockState, item)); + } + + private boolean validBeehive(Item item) { + ItemStack itemStack = item.getItemStack(); + + return (!itemStack.hasItemMeta() && itemStack.getType() == Material.BEEHIVE); + } + + private void saveState(BlockState state, Item item) { + ItemStack itemStack = item.getItemStack(); + BlockStateMeta itemMeta = (BlockStateMeta) itemStack.getItemMeta(); + + itemMeta.setBlockState(state); + itemStack.setItemMeta(itemMeta); + item.setItemStack(itemStack); + } +} From 17312c4fc400472c2ea33211b9953e92bdca0b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 15:26:53 +0100 Subject: [PATCH 12/20] Code cleanup --- src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java | 4 ---- src/main/java/com/tomff/beesplus/core/gui/Icon.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java b/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java index 9382d2f..3810835 100644 --- a/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java +++ b/src/main/java/com/tomff/beesplus/core/gui/GuiHandler.java @@ -8,19 +8,15 @@ import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.util.UUID; import java.util.function.Consumer; public class GuiHandler implements Listener { - - private final BeesPlus beesPlus; private final GuiViewTracker guiViewTracker; public GuiHandler(BeesPlus beesPlus) { - this.beesPlus = beesPlus; this.guiViewTracker = beesPlus.getGuiViewTracker(); } diff --git a/src/main/java/com/tomff/beesplus/core/gui/Icon.java b/src/main/java/com/tomff/beesplus/core/gui/Icon.java index f185336..c3c2a93 100644 --- a/src/main/java/com/tomff/beesplus/core/gui/Icon.java +++ b/src/main/java/com/tomff/beesplus/core/gui/Icon.java @@ -15,7 +15,7 @@ public class Icon { this.callback = callback; } - public Consumer getCallback() { + public Consumer getCallback() { return callback; } From f0facfee7cbc54a1b3ba855351ffa433fd35c924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 18:31:40 +0100 Subject: [PATCH 13/20] Updated portuguese translation --- src/main/resources/locale/pt.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/resources/locale/pt.yml b/src/main/resources/locale/pt.yml index 6f9e770..00fc38a 100644 --- a/src/main/resources/locale/pt.yml +++ b/src/main/resources/locale/pt.yml @@ -76,4 +76,15 @@ ride_bee_subtitle: "&6uma abelha &8%name%&6!" bee_protection_helmet: "&6Proteção contra abelhas" bee_protection_chestplate: "&6Proteção contra abelhas" bee_protection_leggings: "&6Proteção contra abelhas" -bee_protection_boots: "&6Proteção contra abelhas" \ No newline at end of file +bee_protection_boots: "&6Proteção contra abelhas" + +################### +# Beehive upgrade item +################### +beehive_upgrade_item_name: "&6Melhorar Colmeia" + +# Use || to a create a new line +beehive_upgrade_item_lore: "&7População de abelhas: &a+3||&8(Clique direito para user)" + +beehive_upgrade_success: "&aColmeia melhorada! Nova população: &7%beesno%&a abelhas" +beehive_upgrade_max: "&cErro: Esta colmeia atingiu a população máxima permitida!" \ No newline at end of file From f89db04f71d12f22cbb13100f070d6c5f2b3f287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 18:37:04 +0100 Subject: [PATCH 14/20] Fix typo in translation --- src/main/resources/locale/pt.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/locale/pt.yml b/src/main/resources/locale/pt.yml index 00fc38a..ce32c4e 100644 --- a/src/main/resources/locale/pt.yml +++ b/src/main/resources/locale/pt.yml @@ -84,7 +84,7 @@ bee_protection_boots: "&6Proteção contra abelhas" beehive_upgrade_item_name: "&6Melhorar Colmeia" # Use || to a create a new line -beehive_upgrade_item_lore: "&7População de abelhas: &a+3||&8(Clique direito para user)" +beehive_upgrade_item_lore: "&7População de abelhas: &a+3||&8(Clique direito para usar)" beehive_upgrade_success: "&aColmeia melhorada! Nova população: &7%beesno%&a abelhas" beehive_upgrade_max: "&cErro: Esta colmeia atingiu a população máxima permitida!" \ No newline at end of file From 4c3906f7734ddf5a68483fbca427efc27cb88c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 18:45:40 +0100 Subject: [PATCH 15/20] Update other translations --- src/main/resources/locale/fr.yml | 13 ++++++++++++- src/main/resources/locale/hu.yml | 13 ++++++++++++- src/main/resources/locale/zh_cn.yml | 13 ++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/resources/locale/fr.yml b/src/main/resources/locale/fr.yml index 8ecfd16..44894ba 100644 --- a/src/main/resources/locale/fr.yml +++ b/src/main/resources/locale/fr.yml @@ -82,4 +82,15 @@ ride_bee_subtitle: "&6a abeille &8%name%&6!" bee_protection_helmet: "&6Casque de Protection des Abeilles" bee_protection_chestplate: "&6Plastron de Protection des Abeilles" bee_protection_leggings: "&6Leggings de Protection des Abeilles" -bee_protection_boots: "&6Bottes de protection des abeilles" \ No newline at end of file +bee_protection_boots: "&6Bottes de protection des abeilles" + +################### +# Beehive upgrade item +################### +beehive_upgrade_item_name: "&6Beehive Upgrade" + +# Use || to a create a new line +beehive_upgrade_item_lore: "&7Bee capacity: &a+3||&8(Right click to use)" + +beehive_upgrade_success: "&aBeehive upgraded! New population: &7%beesno%&a bees" +beehive_upgrade_max: "&cError: This beehive has reached the maximum population allowed!" \ No newline at end of file diff --git a/src/main/resources/locale/hu.yml b/src/main/resources/locale/hu.yml index d1e5f07..ab93893 100644 --- a/src/main/resources/locale/hu.yml +++ b/src/main/resources/locale/hu.yml @@ -82,4 +82,15 @@ ride_bee_subtitle: "&6méhecskén &8%name%&6!" bee_protection_helmet: "&6Méh Védelmi Sisak" bee_protection_chestplate: "&6Méh Védelmi Mellvért" bee_protection_leggings: "&6Méh Védelmi Lábszárvédő" -bee_protection_boots: "&6Méh Védelmi Csizma" \ No newline at end of file +bee_protection_boots: "&6Méh Védelmi Csizma" + +################### +# Méhkas fejlesztő eszköz +################### +beehive_upgrade_item_name: "&6Méhkas fejlesztés" + +# Használd a || jelzést, hogy elkészíts egy új sort. +beehive_upgrade_item_lore: "&7Méh kapacitás: &a+3||&8(Jobb klikk, hogy használd)" + +beehive_upgrade_success: "&aMéhkas felfejlesztve! Új populáció: &7%beesno%&a méh" +beehive_upgrade_max: "&cHiba: Ez a méhkas elérte a megengedett maximum populációt!" \ No newline at end of file diff --git a/src/main/resources/locale/zh_cn.yml b/src/main/resources/locale/zh_cn.yml index 3665a2d..8d54194 100644 --- a/src/main/resources/locale/zh_cn.yml +++ b/src/main/resources/locale/zh_cn.yml @@ -82,4 +82,15 @@ ride_bee_subtitle: "&6一只蜜蜂 &8%name%&6!" bee_protection_helmet: "&6防蜂头盔" bee_protection_chestplate: "&6蜜蜂保护胸甲" bee_protection_leggings: "&6蜜蜂护腿" -bee_protection_boots: "&6蜜蜂保护靴" \ No newline at end of file +bee_protection_boots: "&6蜜蜂保护靴" + +################### +# Beehive upgrade item +################### +beehive_upgrade_item_name: "&6Beehive Upgrade" + +# Use || to a create a new line +beehive_upgrade_item_lore: "&7Bee capacity: &a+3||&8(Right click to use)" + +beehive_upgrade_success: "&aBeehive upgraded! New population: &7%beesno%&a bees" +beehive_upgrade_max: "&cError: This beehive has reached the maximum population allowed!" \ No newline at end of file From 791f69a19dfcc39e475087111c2fc62a6727734a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 20:16:02 +0100 Subject: [PATCH 16/20] Add migration v1 --- .../java/com/tomff/beesplus/BeesPlus.java | 33 ++++++++++++++----- .../{AddField.java => AddFields.java} | 21 +++++++----- .../tomff/beesplus/core/migrations/Field.java | 19 +++++++++++ .../beesplus/core/migrations/Migration.java | 21 ------------ .../core/migrations/MigrationsExecutor.java | 11 ++++--- .../tomff/beesplus/items/BeeHiveUpgrade.java | 6 ++-- 6 files changed, 65 insertions(+), 46 deletions(-) rename src/main/java/com/tomff/beesplus/core/migrations/{AddField.java => AddFields.java} (65%) create mode 100644 src/main/java/com/tomff/beesplus/core/migrations/Field.java delete mode 100644 src/main/java/com/tomff/beesplus/core/migrations/Migration.java diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index b260bc0..73981e2 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -4,8 +4,8 @@ import com.tomff.beesplus.core.UpdateChecker; import com.tomff.beesplus.core.gui.GuiHandler; import com.tomff.beesplus.core.gui.GuiViewTracker; import com.tomff.beesplus.core.items.CustomItemManager; -import com.tomff.beesplus.core.migrations.AddField; -import com.tomff.beesplus.core.migrations.Migration; +import com.tomff.beesplus.core.migrations.AddFields; +import com.tomff.beesplus.core.migrations.Field; import com.tomff.beesplus.core.migrations.MigrationsExecutor; import com.tomff.beesplus.handlers.BeehiveHandler; import com.tomff.beesplus.handlers.DamageHandler; @@ -26,8 +26,6 @@ public class BeesPlus extends JavaPlugin { private GuiViewTracker guiViewTracker; private CustomItemManager customItemManager; - private LocalizationWrapper localizationWrapper; - @Override public void onEnable() { performMigrations(); @@ -63,7 +61,7 @@ public class BeesPlus extends JavaPlugin { private boolean loadLocale() { String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()); - localizationWrapper = new LocalizationWrapper(this, "locale"); + LocalizationWrapper localizationWrapper = new LocalizationWrapper(this, "locale"); try { YamlConfiguration localeYamlFile = localizationWrapper.getLocale(locale); @@ -85,8 +83,27 @@ public class BeesPlus extends JavaPlugin { private void performMigrations() { MigrationsExecutor migrationsExecutor = new MigrationsExecutor(this); - migrationsExecutor.addMigration(1, new Migration() - .add(new AddField("config.yml", "beehiveupgrade.maximumpopulation", 9)) + Field[] beehiveUpgradeTranslation = new Field[] { + new Field("beehive_upgrade_item_name", "&6Beehive Upgrade"), + new Field("beehive_upgrade_item_lore", "&7Bee capacity: &a+3||&8(Right click to use)"), + new Field("beehive_upgrade_success", "&aBeehive upgraded! New population: &7%beesno%&a bees"), + new Field("beehive_upgrade_max", "&cError: This beehive has reached the maximum population allowed!") + }; + + migrationsExecutor.addMigration(1, + new AddFields("config.yml", new Field[] { + new Field("beehiveupgrade.maximumpopulation", 9) + }), + new AddFields("locale/en.yml", beehiveUpgradeTranslation), + new AddFields("locale/fr.yml", beehiveUpgradeTranslation), + new AddFields("locale/hu.yml", beehiveUpgradeTranslation), + new AddFields("locale/zh_cn.yml", beehiveUpgradeTranslation), + new AddFields("locale/pt.yml", new Field[] { + new Field("beehive_upgrade_item_name", "&6Melhorar Colmeia"), + new Field("beehive_upgrade_item_lore", "&7População de abelhas: &a+3||&8(Clique direito para usar)"), + new Field("beehive_upgrade_success", "&aColmeia melhorada! Nova população: &7%beesno%&a abelhas"), + new Field("beehive_upgrade_max", "&cErro: Esta colmeia atingiu a população máxima permitida!") + }) ); migrationsExecutor.migrate(); @@ -117,4 +134,4 @@ public class BeesPlus extends JavaPlugin { public CustomItemManager getCustomItemManager() { return customItemManager; } -} +} \ No newline at end of file diff --git a/src/main/java/com/tomff/beesplus/core/migrations/AddField.java b/src/main/java/com/tomff/beesplus/core/migrations/AddFields.java similarity index 65% rename from src/main/java/com/tomff/beesplus/core/migrations/AddField.java rename to src/main/java/com/tomff/beesplus/core/migrations/AddFields.java index 6d9820e..d3ba35a 100644 --- a/src/main/java/com/tomff/beesplus/core/migrations/AddField.java +++ b/src/main/java/com/tomff/beesplus/core/migrations/AddFields.java @@ -6,21 +6,21 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; import java.io.IOException; -public class AddField implements Operation { +public class AddFields implements Operation { private File file; + private Field[] fields; - private String fieldPath; - private Object fieldValue; - - public AddField(String filePath, String fieldPath, Object fieldValue) { + public AddFields(String filePath, Field[] fields) { file = new File(BASE_PATH, filePath); - - this.fieldPath = fieldPath; - this.fieldValue = fieldValue; + this.fields = fields; } @Override public void execute() throws IOException { + if (!file.exists()) { + return; + } + YamlConfiguration config = new YamlConfiguration(); try { @@ -29,7 +29,10 @@ public class AddField implements Operation { e.printStackTrace(); } - config.set(fieldPath, fieldValue); + for (Field field : fields) { + config.set(field.getPath(), field.getValue()); + } + config.save(file); } } diff --git a/src/main/java/com/tomff/beesplus/core/migrations/Field.java b/src/main/java/com/tomff/beesplus/core/migrations/Field.java new file mode 100644 index 0000000..8305396 --- /dev/null +++ b/src/main/java/com/tomff/beesplus/core/migrations/Field.java @@ -0,0 +1,19 @@ +package com.tomff.beesplus.core.migrations; + +public class Field { + private final String path; + private final Object value; + + public Field(String path, Object value) { + this.path = path; + this.value = value; + } + + public String getPath() { + return path; + } + + public Object getValue() { + return value; + } +} diff --git a/src/main/java/com/tomff/beesplus/core/migrations/Migration.java b/src/main/java/com/tomff/beesplus/core/migrations/Migration.java deleted file mode 100644 index 9da2996..0000000 --- a/src/main/java/com/tomff/beesplus/core/migrations/Migration.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.tomff.beesplus.core.migrations; - -import java.util.ArrayList; -import java.util.List; - -public class Migration { - private List operations; - - public Migration() { - this.operations = new ArrayList<>(); - } - - public Migration add(Operation operation) { - operations.add(operation); - return this; - } - - public List getOperations() { - return operations; - } -} diff --git a/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java b/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java index 3b3e4e9..b4c9209 100644 --- a/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java +++ b/src/main/java/com/tomff/beesplus/core/migrations/MigrationsExecutor.java @@ -12,7 +12,7 @@ import java.util.Map; public class MigrationsExecutor { private int latestVersion; - private Map migrations; + private Map migrations; private File migrationFileRegistry; private YamlConfiguration yaml; @@ -26,8 +26,8 @@ public class MigrationsExecutor { this.latestVersion = 0; } - public void addMigration(int version, Migration migration) { - migrations.put(version, migration); + public void addMigration(int version, Operation... operations) { + migrations.put(version, operations); if (version > latestVersion) { this.latestVersion = version; @@ -62,6 +62,7 @@ public class MigrationsExecutor { public void setCurrentMigration(int version) { yaml.set("migration", version); + try { yaml.save(migrationFileRegistry); } catch (IOException e) { @@ -99,8 +100,8 @@ public class MigrationsExecutor { return true; } - private void performMigration(Migration migration) { - for(Operation operation : migration.getOperations()) { + private void performMigration(Operation[] operations) { + for(Operation operation : operations) { try { operation.execute(); } catch (IOException e) { diff --git a/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java b/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java index 0993ecc..38ec5a6 100644 --- a/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java +++ b/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java @@ -3,9 +3,10 @@ package com.tomff.beesplus.items; import com.tomff.beesplus.BeesPlus; import com.tomff.beesplus.core.items.CustomItem; import com.tomff.beesplus.core.items.ItemBuilder; -import com.tomff.beesplus.gui.BeeHiveInfo; import com.tomff.beesplus.localization.Localization; -import org.bukkit.*; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Sound; import org.bukkit.block.Beehive; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; @@ -19,7 +20,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; -import org.bukkit.plugin.java.JavaPlugin; import java.util.HashMap; import java.util.Map; From edc0b479823498d441bb7d797619fa5926718611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 20:23:28 +0100 Subject: [PATCH 17/20] Added language metric --- src/main/java/com/tomff/beesplus/BeesPlus.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index 73981e2..ae8bceb 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -20,6 +20,7 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.IOException; import java.util.Locale; +import java.util.concurrent.Callable; public class BeesPlus extends JavaPlugin { @@ -44,8 +45,7 @@ public class BeesPlus extends JavaPlugin { } registerItems(); - - Metrics metrics = new Metrics(this, 7065); + setupMetrics(); new UpdateChecker(this, 77224).getVersion(version -> { if (!this.getDescription().getVersion().equalsIgnoreCase(version)) { @@ -54,6 +54,13 @@ public class BeesPlus extends JavaPlugin { }); } + private void setupMetrics() { + Metrics metrics = new Metrics(this, 7065); + + metrics.addCustomChart(new Metrics.SimplePie("language_used", + () -> getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()))); + } + @Override public void onDisable() { guiViewTracker.clearViews(); From 635d4bed7780e95133b08f22c3c21b03e3eb6101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 20:24:26 +0100 Subject: [PATCH 18/20] Remove unused import --- src/main/java/com/tomff/beesplus/BeesPlus.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index ae8bceb..5d4d514 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -20,7 +20,6 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.IOException; import java.util.Locale; -import java.util.concurrent.Callable; public class BeesPlus extends JavaPlugin { From 8e78219e2d8e021eba9c7a44079a4d9cbabd06be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 20:39:10 +0100 Subject: [PATCH 19/20] Add permission to use upgrades --- src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java | 4 ++++ src/main/resources/plugin.yml | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java b/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java index 38ec5a6..224bcae 100644 --- a/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java +++ b/src/main/java/com/tomff/beesplus/items/BeeHiveUpgrade.java @@ -103,6 +103,10 @@ public class BeeHiveUpgrade extends CustomItem implements Listener { Beehive beehive = (Beehive) clickedBlock.getState(); + if (!player.hasPermission("beesplus.beehive.upgrade")) { + return; + } + if (beehive.getMaxEntities() >= maxPopulation) { Localization.sendMessage(player, Localization.BEEHIVE_UPGRADE_MAX); player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index df53186..1df202f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -12,4 +12,6 @@ permissions: default: op beesplus.beehive.view: default: op + beesplus.beehive.upgrade: + default: op From a8199f41646b84e29f1fcfe969bdf5445f3d075f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Fri, 3 Jul 2020 20:41:17 +0100 Subject: [PATCH 20/20] Bump version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index caba4f1..726fe71 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.tomff.beesplus BeesPlus - 1.4.5-SNAPSHOT + 1.5 UTF-8