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