Beehive upgrades MVP

This commit is contained in:
Tomás F 2020-07-01 23:46:39 +01:00
parent 84ab5daf40
commit 5e9f3bc791
6 changed files with 184 additions and 6 deletions

View File

@ -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) {

View File

@ -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 <T, Z> ItemBuilder setPersistentData(NamespacedKey key, PersistentDataType<T, Z> type, Z value) {
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(key, type, value);
item.setItemMeta(meta);
return this;
}
public ItemStack build() {
return item;
}

View File

@ -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<Character, Material> getIngredients() {
Map<Character, Material> 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);
}
}
}
}

View File

@ -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,

View File

@ -23,4 +23,11 @@ beeprotectionsuit:
# removeanger: remove anger from bees when they are healed?
#
healing:
removeanger: true
removeanger: true
# Beehive upgrade settings
#
# maximumSize: maximum number of bees a beehive can be upgraded to
#
beehiveupgrade:
maximumpopulation: 9

View File

@ -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"
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!"