From 6c6ba248b7534ab8154e1df1bbd86e35da8941a6 Mon Sep 17 00:00:00 2001 From: 00Creamy <81294829+00Creamy@users.noreply.github.com> Date: Thu, 26 Aug 2021 12:04:46 +0800 Subject: [PATCH 1/5] Update CustomItem.java --- .../java/com/strangeone101/holoitemsapi/CustomItem.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java index 8fc214d..e9f37a1 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java +++ b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java @@ -297,11 +297,8 @@ public void damageItem(ItemStack stack, int amount, Player player) { return; } - meta.getPersistentDataContainer().set(HoloItemsAPI.getKeys().CUSTOM_ITEM_DURABILITY, PersistentDataType.INTEGER, damage); - if (meta instanceof Damageable) { - ((Damageable) meta).setDamage((damage / getMaxDurability()) * stack.getType().getMaxDurability()); - } - stack.setItemMeta(meta); + setDurability(stack, damage); + updateStack(stack, player); } } @@ -311,7 +308,7 @@ public void damageItem(ItemStack stack, int amount, Player player) { * @param maxDurability The max durability * @return The string */ - public static String getDurabilityString(int durability, int maxDurability) { + public static String getDurabilityString(double durability, double maxDurability) { if (maxDurability == 0) return ""; //No durability double percentage = durability / maxDurability; double bigPercentage = percentage * 100; From d4201660f399b6d67b7eec61328a5b5d6a061f6c Mon Sep 17 00:00:00 2001 From: 00Creamy <81294829+00Creamy@users.noreply.github.com> Date: Thu, 26 Aug 2021 14:47:30 +0800 Subject: [PATCH 2/5] Update CustomItem.java --- src/main/java/com/strangeone101/holoitemsapi/CustomItem.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java index e9f37a1..515f5d8 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java +++ b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java @@ -308,9 +308,9 @@ public void damageItem(ItemStack stack, int amount, Player player) { * @param maxDurability The max durability * @return The string */ - public static String getDurabilityString(double durability, double maxDurability) { + public static String getDurabilityString(int durability, int maxDurability) { if (maxDurability == 0) return ""; //No durability - double percentage = durability / maxDurability; + double percentage = ((double) durability) / ((double) maxDurability); double bigPercentage = percentage * 100; ChatColor color = ChatColor.DARK_RED; if (bigPercentage >= 90) color = ChatColor.DARK_GREEN; From f4206e4d9fca24f08fe8e0dfa025a787701d8f93 Mon Sep 17 00:00:00 2001 From: 00Creamy <81294829+00Creamy@users.noreply.github.com> Date: Fri, 27 Aug 2021 07:15:22 +0800 Subject: [PATCH 3/5] Fixed attribute stuff and some durability stuff too --- .../holoitemsapi/CustomItem.java | 279 ++++++++++++++---- .../holoitemsapi/itemevent/EventCache.java | 16 +- .../holoitemsapi/util/ItemUtils.java | 2 +- 3 files changed, 240 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java index 515f5d8..1b949eb 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java +++ b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java @@ -19,6 +19,7 @@ import org.bukkit.attribute.AttributeModifier; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; @@ -39,7 +40,6 @@ import java.util.Set; import java.util.UUID; import java.util.function.BiConsumer; -import java.util.function.BiFunction; import java.util.function.Function; /** @@ -67,7 +67,7 @@ public class CustomItem { private BiConsumer onBuild; private BiConsumer onUpdate; - private Map> attributes = new HashMap<>(); + private Map>> attributes = new HashMap<>(); private Map> variables = new HashMap<>(); private Map nbt = new HashMap<>(); @@ -158,15 +158,19 @@ public ItemStack buildStack(Player player) { if (flags != null && flags.length > 0) meta.addItemFlags(flags); - stack.setItemMeta(meta); - //Add all attributes to the item for (Attribute attr : getAttributes().keySet()) { - Pair pair = getAttributes().get(attr); - ItemUtils.setAttriute(pair.getRight(), attr, pair.getLeft(), stack); + Map> map = getAttributes().get(attr); + for (AttributeModifier.Operation operation: map.keySet()) { + if (map.get(operation).getRight() == null) { + meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation)); + } else { + meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation, map.get(operation).getRight())); + } + } } - + stack.setItemMeta(meta); if (this.jsonLore) { ReflectionUtils.setTrueLore(stack, lore); @@ -254,14 +258,27 @@ public ItemStack updateStack(ItemStack stack, Player player) { if (flags != null && flags.length > 0) meta.addItemFlags(flags); - stack.setItemMeta(meta); + //Remove all current attribute + if (meta.getAttributeModifiers() != null) { + for (Attribute attribute : meta.getAttributeModifiers().keySet()) { + meta.removeAttributeModifier(attribute); + } + } //Add all attributes to the item for (Attribute attr : getAttributes().keySet()) { - Pair pair = getAttributes().get(attr); - ItemUtils.setAttriute(pair.getRight(), attr, pair.getLeft(), stack); + Map> map = getAttributes().get(attr); + for (AttributeModifier.Operation operation: map.keySet()) { + if (map.get(operation).getRight() == null) { + meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation)); + } else { + meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation, map.get(operation).getRight())); + } + } } + stack.setItemMeta(meta); + if (this.jsonLore) { ReflectionUtils.setTrueLore(stack, lore); } @@ -375,7 +392,7 @@ public void setDurability(ItemStack stack, int durability) { meta.getPersistentDataContainer().set(HoloItemsAPI.getKeys().CUSTOM_ITEM_DURABILITY, PersistentDataType.INTEGER, durability); if (meta instanceof Damageable) { - ((Damageable) meta).setDamage((durability / getMaxDurability()) * stack.getType().getMaxDurability()); + ((Damageable) meta).setDamage((int)((((double) durability) / ((double)getMaxDurability())) * stack.getType().getMaxDurability())); } stack.setItemMeta(meta); //Update item @@ -661,84 +678,236 @@ public int hashCode() { return name.hashCode(); } - public CustomItem setDamage(double damage) { - getAttributes().put(Attribute.GENERIC_ATTACK_DAMAGE, - new ImmutablePair<>(AttributeModifier.Operation.ADD_NUMBER, damage)); + public CustomItem setAttribute(Attribute attribute, double amount, boolean percentage, EquipmentSlot equipmentSlot) { + Pair pair = new ImmutablePair<>(amount, equipmentSlot); + Map> map = new HashMap<>(); + if (percentage) { + map.put(AttributeModifier.Operation.ADD_SCALAR, pair); + } else { + map.put(AttributeModifier.Operation.ADD_NUMBER, pair); + } + getAttributes().put(attribute, map); return this; } - public CustomItem setDamagePercentage(double percentage) { - getAttributes().put(Attribute.GENERIC_ATTACK_DAMAGE, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, percentage)); + public CustomItem setArmor(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_ARMOR, amount, percentage, equipmentSlot); + } - return this; + public CustomItem setArmor(double amount, boolean percentage) { + return setArmor(amount, percentage, null); } - public CustomItem setArmor(int armor) { - getAttributes().put(Attribute.GENERIC_ARMOR, - new ImmutablePair<>(AttributeModifier.Operation.ADD_NUMBER, (double)armor)); + public CustomItem setArmor(double amount, EquipmentSlot equipmentSlot) { + return setArmor(amount, false, equipmentSlot); + } - return this; + public CustomItem setArmor(double amount) { + return setArmor(amount, false); } - public CustomItem setArmorToughness(int armor) { - getAttributes().put(Attribute.GENERIC_ARMOR_TOUGHNESS, - new ImmutablePair<>(AttributeModifier.Operation.ADD_NUMBER, (double)armor)); + public CustomItem setArmorPercentage(double amount, EquipmentSlot equipmentSlot) { + return setArmor(amount, true, equipmentSlot); + } - return this; + public CustomItem setArmorPercentage(double amount) { + return setArmor(amount, true); } - public CustomItem setHealth(int health) { - getAttributes().put(Attribute.GENERIC_MAX_HEALTH, - new ImmutablePair<>(AttributeModifier.Operation.ADD_NUMBER, (double)health)); + public CustomItem setArmorToughness(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_ARMOR_TOUGHNESS, amount, percentage, equipmentSlot); + } - return this; + public CustomItem setArmorToughness(double amount, boolean percentage) { + return setArmorToughness(amount, percentage, null); } - public CustomItem setHealthPercentage(double percentage) { - getAttributes().put(Attribute.GENERIC_MAX_HEALTH, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, (double)percentage)); + public CustomItem setArmorToughness(double amount, EquipmentSlot equipmentSlot) { + return setArmorToughness(amount, false, equipmentSlot); + } - return this; + public CustomItem setArmorToughness(double amount) { + return setArmorToughness(amount, false); + } + + public CustomItem setArmorToughnessPercentage(double amount, EquipmentSlot equipmentSlot) { + return setArmorToughness(amount, true, equipmentSlot); } - public CustomItem setSpeed(double percentage) { - getAttributes().put(Attribute.GENERIC_MOVEMENT_SPEED, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, percentage)); + public CustomItem setArmorToughnessPercentage(double amount) { + return setArmorToughness(amount, true); + } - return this; + public CustomItem setDamage(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_ATTACK_DAMAGE, amount, percentage, equipmentSlot); } - public CustomItem setKnockbackResistance(double percentage) { - getAttributes().put(Attribute.GENERIC_KNOCKBACK_RESISTANCE, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, percentage)); + public CustomItem setDamage(double amount, boolean percentage) { + return setDamage(amount, percentage, null); + } - return this; + public CustomItem setDamage(double amount, EquipmentSlot equipmentSlot) { + return setDamage(amount, false, equipmentSlot); } - public CustomItem setAttackSpeed(double percentage) { - getAttributes().put(Attribute.GENERIC_ATTACK_SPEED, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, percentage)); + public CustomItem setDamage(double amount) { + return setDamage(amount, false); + } - return this; + public CustomItem setDamagePercentage(double amount, EquipmentSlot equipmentSlot) { + return setDamage(amount, true, equipmentSlot); } - public CustomItem setAttackKnockback(double percentage) { - getAttributes().put(Attribute.GENERIC_ATTACK_KNOCKBACK, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, percentage)); + public CustomItem setDamagePercentage(double amount) { + return setDamage(amount, true); + } - return this; + public CustomItem setKnockback(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK, amount, percentage, equipmentSlot); + } + + public CustomItem setKnockback(double amount, boolean percentage) { + return setKnockback(amount, percentage, null); } - public CustomItem setLuck(double percentage) { - getAttributes().put(Attribute.GENERIC_LUCK, - new ImmutablePair<>(AttributeModifier.Operation.ADD_SCALAR, percentage)); + public CustomItem setKnockback(double amount, EquipmentSlot equipmentSlot) { + return setKnockback(amount, false, equipmentSlot); + } - return this; + public CustomItem setKnockback(double amount) { + return setKnockback(amount, false); + } + + public CustomItem setKnockbackPercentage(double amount, EquipmentSlot equipmentSlot) { + return setKnockback(amount, true, equipmentSlot); + } + + public CustomItem setKnockbackPercentage(double amount) { + return setKnockback(amount, true); + } + + public CustomItem setAttackSpeed(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_ATTACK_SPEED, amount, percentage, equipmentSlot); + } + + public CustomItem setAttackSpeed(double amount, boolean percentage) { + return setAttackSpeed(amount, percentage, null); + } + + public CustomItem setAttackSpeed(double amount, EquipmentSlot equipmentSlot) { + return setAttackSpeed(amount, false, equipmentSlot); + } + + public CustomItem setAttackSpeed(double amount) { + return setAttackSpeed(amount, false); + } + + public CustomItem setAttackSpeedPercentage(double amount, EquipmentSlot equipmentSlot) { + return setAttackSpeed(amount, true, equipmentSlot); + } + + public CustomItem setAttackSpeedPercentage(double amount) { + return setAttackSpeed(amount, true); + } + + public CustomItem setKnockbackResistance(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE, amount, percentage, equipmentSlot); + } + + public CustomItem setKnockbackResistance(double amount, boolean percentage) { + return setKnockbackResistance(amount, percentage, null); + } + + public CustomItem setKnockbackResistance(double amount, EquipmentSlot equipmentSlot) { + return setKnockbackResistance(amount, false, equipmentSlot); + } + + public CustomItem setKnockbackResistance(double amount) { + return setKnockbackResistance(amount, false); + } + + public CustomItem setKnockbackResistancePercentage(double amount, EquipmentSlot equipmentSlot) { + return setKnockbackResistance(amount, true, equipmentSlot); + } + + public CustomItem setKnockbackResistancePercentage(double amount) { + return setKnockbackResistance(amount, true); + } + + public CustomItem setLuck(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_LUCK, amount, percentage, equipmentSlot); + } + + public CustomItem setLuck(double amount, boolean percentage) { + return setLuck(amount, percentage, null); + } + + public CustomItem setLuck(double amount, EquipmentSlot equipmentSlot) { + return setLuck(amount, false, equipmentSlot); + } + + public CustomItem setLuck(double amount) { + return setLuck(amount, false); + } + + public CustomItem setLuckPercentage(double amount, EquipmentSlot equipmentSlot) { + return setLuck(amount, true, equipmentSlot); + } + + public CustomItem setLuckPercentage(double amount) { + return setLuck(amount, true); + } + + public CustomItem setHealth(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_MAX_HEALTH, amount, percentage, equipmentSlot); + } + + public CustomItem setHealth(double amount, boolean percentage) { + return setHealth(amount, percentage, null); + } + + public CustomItem setHealth(double amount, EquipmentSlot equipmentSlot) { + return setHealth(amount, false, equipmentSlot); + } + + public CustomItem setHealth(double amount) { + return setHealth(amount, false); + } + + public CustomItem setHealthPercentage(double amount, EquipmentSlot equipmentSlot) { + return setHealth(amount, true, equipmentSlot); + } + + public CustomItem setHealthPercentage(double amount) { + return setHealth(amount, true); + } + + public CustomItem setMovementSpeed(double amount, boolean percentage, EquipmentSlot equipmentSlot) { + return setAttribute(Attribute.GENERIC_MOVEMENT_SPEED, amount, percentage, equipmentSlot); + } + + public CustomItem setMovementSpeed(double amount, boolean percentage) { + return setMovementSpeed(amount, percentage, null); + } + + public CustomItem setMovementSpeed(double amount, EquipmentSlot equipmentSlot) { + return setMovementSpeed(amount, false, equipmentSlot); + } + + public CustomItem setMovementSpeed(double amount) { + return setMovementSpeed(amount, false); + } + + public CustomItem setMovementSpeedPercentage(double amount, EquipmentSlot equipmentSlot) { + return setMovementSpeed(amount, true, equipmentSlot); + } + + public CustomItem setMovementSpeedPercentage(double amount) { + return setMovementSpeed(amount, true); } - public Map> getAttributes() { + public Map>> getAttributes() { return attributes; } diff --git a/src/main/java/com/strangeone101/holoitemsapi/itemevent/EventCache.java b/src/main/java/com/strangeone101/holoitemsapi/itemevent/EventCache.java index b04eb0d..415c7d5 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/itemevent/EventCache.java +++ b/src/main/java/com/strangeone101/holoitemsapi/itemevent/EventCache.java @@ -398,7 +398,21 @@ static void triggerItemEvents(Event event) { e.printStackTrace(); } } - + } + } + } else if (event instanceof EntityEvent && ((EntityEvent) event).getEntity() instanceof Player) { + Player player = ((Player) ((EntityEvent) event).getEntity()); + if (POSITIONS_BY_ITEM.get(item).containsKey(player)) { + for (int slot : POSITIONS_BY_ITEM.get(item).get(player).keySet()) { + Pair pair = POSITIONS_BY_ITEM.get(item).get(player).get(slot); + if (t.getRight().matches(pair.getRight())) { //If activeConditions match Position + EventContext context = new EventContext(player, item, pair.getLeft(), pair.getRight()); + try { + m.invoke(item, context, event); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } } } } diff --git a/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java b/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java index 042abc1..0d8714d 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java +++ b/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java @@ -81,7 +81,7 @@ public static SkullMeta setSkinFromURL(SkullMeta meta, String skin) { * @param operation The operation * @param stack The itemstack */ - public static void setAttriute(double number, Attribute attribute, AttributeModifier.Operation operation, ItemStack stack) { + public static void setAttribute(double number, Attribute attribute, AttributeModifier.Operation operation, ItemStack stack) { if (ATTRIBUTE_MAP.isEmpty()) { Random random = new Random("unique_seed".hashCode()); From 77cb742be9dba74b1e83188974433f57e9250f9c Mon Sep 17 00:00:00 2001 From: 00Creamy <81294829+00Creamy@users.noreply.github.com> Date: Fri, 27 Aug 2021 23:51:09 +0800 Subject: [PATCH 4/5] Another fix --- .../holoitemsapi/CustomItem.java | 266 +++++------------- .../holoitemsapi/util/ItemUtils.java | 57 ++-- 2 files changed, 103 insertions(+), 220 deletions(-) diff --git a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java index 1b949eb..70bfb6f 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java +++ b/src/main/java/com/strangeone101/holoitemsapi/CustomItem.java @@ -19,7 +19,6 @@ import org.bukkit.attribute.AttributeModifier; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; -import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; @@ -28,6 +27,7 @@ import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import org.w3c.dom.Attr; import java.text.DecimalFormat; import java.util.ArrayList; @@ -67,7 +67,7 @@ public class CustomItem { private BiConsumer onBuild; private BiConsumer onUpdate; - private Map>> attributes = new HashMap<>(); + private Map> attributes = new HashMap<>(); private Map> variables = new HashMap<>(); private Map nbt = new HashMap<>(); @@ -158,20 +158,15 @@ public ItemStack buildStack(Player player) { if (flags != null && flags.length > 0) meta.addItemFlags(flags); + stack.setItemMeta(meta); + //Add all attributes to the item for (Attribute attr : getAttributes().keySet()) { - Map> map = getAttributes().get(attr); - for (AttributeModifier.Operation operation: map.keySet()) { - if (map.get(operation).getRight() == null) { - meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation)); - } else { - meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation, map.get(operation).getRight())); - } + for (AttributeModifier.Operation operation: getAttributes().get(attr).keySet()) { + ItemUtils.setAttribute(getAttributes().get(attr).get(operation), attr, operation, stack, this); } } - stack.setItemMeta(meta); - if (this.jsonLore) { ReflectionUtils.setTrueLore(stack, lore); } @@ -258,27 +253,15 @@ public ItemStack updateStack(ItemStack stack, Player player) { if (flags != null && flags.length > 0) meta.addItemFlags(flags); - //Remove all current attribute - if (meta.getAttributeModifiers() != null) { - for (Attribute attribute : meta.getAttributeModifiers().keySet()) { - meta.removeAttributeModifier(attribute); - } - } + stack.setItemMeta(meta); //Add all attributes to the item for (Attribute attr : getAttributes().keySet()) { - Map> map = getAttributes().get(attr); - for (AttributeModifier.Operation operation: map.keySet()) { - if (map.get(operation).getRight() == null) { - meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation)); - } else { - meta.addAttributeModifier(attr, new AttributeModifier(UUID.randomUUID(), (attr.name() + operation.toString()), map.get(operation).getLeft(), operation, map.get(operation).getRight())); - } + for (AttributeModifier.Operation operation: getAttributes().get(attr).keySet()) { + ItemUtils.setAttribute(getAttributes().get(attr).get(operation), attr, operation, stack, this); } } - stack.setItemMeta(meta); - if (this.jsonLore) { ReflectionUtils.setTrueLore(stack, lore); } @@ -392,7 +375,7 @@ public void setDurability(ItemStack stack, int durability) { meta.getPersistentDataContainer().set(HoloItemsAPI.getKeys().CUSTOM_ITEM_DURABILITY, PersistentDataType.INTEGER, durability); if (meta instanceof Damageable) { - ((Damageable) meta).setDamage((int)((((double) durability) / ((double)getMaxDurability())) * stack.getType().getMaxDurability())); + ((Damageable) meta).setDamage((int)(((double)durability / (double)getMaxDurability()) * stack.getType().getMaxDurability())); } stack.setItemMeta(meta); //Update item @@ -678,236 +661,127 @@ public int hashCode() { return name.hashCode(); } - public CustomItem setAttribute(Attribute attribute, double amount, boolean percentage, EquipmentSlot equipmentSlot) { - Pair pair = new ImmutablePair<>(amount, equipmentSlot); - Map> map = new HashMap<>(); + public CustomItem setAttribute(Attribute attribute, double amount, boolean percentage) { + AttributeModifier.Operation operation = AttributeModifier.Operation.ADD_NUMBER; if (percentage) { - map.put(AttributeModifier.Operation.ADD_SCALAR, pair); - } else { - map.put(AttributeModifier.Operation.ADD_NUMBER, pair); + operation = AttributeModifier.Operation.ADD_SCALAR; } + HashMap map = new HashMap<>(); + map.put(operation, amount); getAttributes().put(attribute, map); return this; } - public CustomItem setArmor(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_ARMOR, amount, percentage, equipmentSlot); - } - - public CustomItem setArmor(double amount, boolean percentage) { - return setArmor(amount, percentage, null); - } - - public CustomItem setArmor(double amount, EquipmentSlot equipmentSlot) { - return setArmor(amount, false, equipmentSlot); - } - - public CustomItem setArmor(double amount) { - return setArmor(amount, false); - } - - public CustomItem setArmorPercentage(double amount, EquipmentSlot equipmentSlot) { - return setArmor(amount, true, equipmentSlot); - } - - public CustomItem setArmorPercentage(double amount) { - return setArmor(amount, true); - } - - public CustomItem setArmorToughness(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_ARMOR_TOUGHNESS, amount, percentage, equipmentSlot); - } - - public CustomItem setArmorToughness(double amount, boolean percentage) { - return setArmorToughness(amount, percentage, null); - } - - public CustomItem setArmorToughness(double amount, EquipmentSlot equipmentSlot) { - return setArmorToughness(amount, false, equipmentSlot); - } - - public CustomItem setArmorToughness(double amount) { - return setArmorToughness(amount, false); - } - - public CustomItem setArmorToughnessPercentage(double amount, EquipmentSlot equipmentSlot) { - return setArmorToughness(amount, true, equipmentSlot); - } - - public CustomItem setArmorToughnessPercentage(double amount) { - return setArmorToughness(amount, true); - } - - public CustomItem setDamage(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_ATTACK_DAMAGE, amount, percentage, equipmentSlot); - } - - public CustomItem setDamage(double amount, boolean percentage) { - return setDamage(amount, percentage, null); - } - - public CustomItem setDamage(double amount, EquipmentSlot equipmentSlot) { - return setDamage(amount, false, equipmentSlot); - } - - public CustomItem setDamage(double amount) { - return setDamage(amount, false); - } - - public CustomItem setDamagePercentage(double amount, EquipmentSlot equipmentSlot) { - return setDamage(amount, true, equipmentSlot); - } - - public CustomItem setDamagePercentage(double amount) { - return setDamage(amount, true); - } - - public CustomItem setKnockback(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK, amount, percentage, equipmentSlot); - } - - public CustomItem setKnockback(double amount, boolean percentage) { - return setKnockback(amount, percentage, null); - } - - public CustomItem setKnockback(double amount, EquipmentSlot equipmentSlot) { - return setKnockback(amount, false, equipmentSlot); - } - - public CustomItem setKnockback(double amount) { - return setKnockback(amount, false); - } - - public CustomItem setKnockbackPercentage(double amount, EquipmentSlot equipmentSlot) { - return setKnockback(amount, true, equipmentSlot); - } - - public CustomItem setKnockbackPercentage(double amount) { - return setKnockback(amount, true); - } - - public CustomItem setAttackSpeed(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_ATTACK_SPEED, amount, percentage, equipmentSlot); - } - - public CustomItem setAttackSpeed(double amount, boolean percentage) { - return setAttackSpeed(amount, percentage, null); - } - - public CustomItem setAttackSpeed(double amount, EquipmentSlot equipmentSlot) { - return setAttackSpeed(amount, false, equipmentSlot); - } - - public CustomItem setAttackSpeed(double amount) { - return setAttackSpeed(amount, false); + public CustomItem setArmor(double armor, boolean percentage) { + return setAttribute(Attribute.GENERIC_ARMOR, armor, percentage); } - public CustomItem setAttackSpeedPercentage(double amount, EquipmentSlot equipmentSlot) { - return setAttackSpeed(amount, true, equipmentSlot); + public CustomItem setArmor(double armor) { + return setArmor(armor, false); } - public CustomItem setAttackSpeedPercentage(double amount) { - return setAttackSpeed(amount, true); + public CustomItem setArmorPercentage(double armorPercentage) { + return setArmor(armorPercentage, true); } - public CustomItem setKnockbackResistance(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE, amount, percentage, equipmentSlot); + public CustomItem setArmorToughness(double armorToughness, boolean percentage) { + return setAttribute(Attribute.GENERIC_ARMOR_TOUGHNESS, armorToughness, percentage); } - public CustomItem setKnockbackResistance(double amount, boolean percentage) { - return setKnockbackResistance(amount, percentage, null); + public CustomItem setArmorToughness(double armorToughness) { + return setArmorToughness(armorToughness, false); } - public CustomItem setKnockbackResistance(double amount, EquipmentSlot equipmentSlot) { - return setKnockbackResistance(amount, false, equipmentSlot); + public CustomItem setArmorToughnessPercentage(double armorToughnessPercentage) { + return setArmorToughness(armorToughnessPercentage, true); } - public CustomItem setKnockbackResistance(double amount) { - return setKnockbackResistance(amount, false); + public CustomItem setDamage(double damage, boolean percentage) { + return setAttribute(Attribute.GENERIC_ATTACK_DAMAGE, damage, percentage); } - public CustomItem setKnockbackResistancePercentage(double amount, EquipmentSlot equipmentSlot) { - return setKnockbackResistance(amount, true, equipmentSlot); + public CustomItem setDamage(double damage) { + return setDamage(damage, false); } - public CustomItem setKnockbackResistancePercentage(double amount) { - return setKnockbackResistance(amount, true); + public CustomItem setDamagePercentage(double damagePercentage) { + return setDamage(damagePercentage, true); } - public CustomItem setLuck(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_LUCK, amount, percentage, equipmentSlot); + public CustomItem setAttackKnockback(double knockback, boolean percentage) { + return setAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK, knockback, percentage); } - public CustomItem setLuck(double amount, boolean percentage) { - return setLuck(amount, percentage, null); + public CustomItem setAttackKnockback(double knockback) { + return setAttackKnockback(knockback, false); } - public CustomItem setLuck(double amount, EquipmentSlot equipmentSlot) { - return setLuck(amount, false, equipmentSlot); + public CustomItem setAttackKnockbackPercentage(double knockbackPercentage) { + return setAttackKnockback(knockbackPercentage, true); } - public CustomItem setLuck(double amount) { - return setLuck(amount, false); + public CustomItem setAttackSpeed(double speed, boolean percentage) { + return setAttribute(Attribute.GENERIC_ATTACK_SPEED, speed, percentage); } - public CustomItem setLuckPercentage(double amount, EquipmentSlot equipmentSlot) { - return setLuck(amount, true, equipmentSlot); + public CustomItem setAttackSpeed(double speed) { + return setAttackSpeed(speed, false); } - public CustomItem setLuckPercentage(double amount) { - return setLuck(amount, true); + public CustomItem setAttackSpeedPercentage(double speedPercentage) { + return setAttackSpeed(speedPercentage, true); } - public CustomItem setHealth(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_MAX_HEALTH, amount, percentage, equipmentSlot); + public CustomItem setKnockbackResistance(double resistance, boolean percentage) { + return setAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE, resistance, percentage); } - public CustomItem setHealth(double amount, boolean percentage) { - return setHealth(amount, percentage, null); + public CustomItem setKnockbackResistance(double resistance) { + return setKnockbackResistance(resistance, false); } - public CustomItem setHealth(double amount, EquipmentSlot equipmentSlot) { - return setHealth(amount, false, equipmentSlot); + public CustomItem setKnockbackResistancePercentage(double resistancePercentage) { + return setKnockbackResistance(resistancePercentage, true); } - public CustomItem setHealth(double amount) { - return setHealth(amount, false); + public CustomItem setLuck(double luck, boolean percentage) { + return setAttribute(Attribute.GENERIC_LUCK, luck, percentage); } - public CustomItem setHealthPercentage(double amount, EquipmentSlot equipmentSlot) { - return setHealth(amount, true, equipmentSlot); + public CustomItem setLuck(double luck) { + return setLuck(luck, false); } - public CustomItem setHealthPercentage(double amount) { - return setHealth(amount, true); + public CustomItem setLuckPercentage(double luckPercentage) { + return setLuck(luckPercentage, true); } - public CustomItem setMovementSpeed(double amount, boolean percentage, EquipmentSlot equipmentSlot) { - return setAttribute(Attribute.GENERIC_MOVEMENT_SPEED, amount, percentage, equipmentSlot); + public CustomItem setHealth(double health, boolean percentage) { + return setAttribute(Attribute.GENERIC_MAX_HEALTH, health, percentage); } - public CustomItem setMovementSpeed(double amount, boolean percentage) { - return setMovementSpeed(amount, percentage, null); + public CustomItem setHealth(int health) { + return setHealth(health, false); } - public CustomItem setMovementSpeed(double amount, EquipmentSlot equipmentSlot) { - return setMovementSpeed(amount, false, equipmentSlot); + public CustomItem setHealthPercentage(double healthPercentage) { + return setHealth(healthPercentage, true); } - public CustomItem setMovementSpeed(double amount) { - return setMovementSpeed(amount, false); + public CustomItem setSpeed(double speed, boolean percentage) { + return setAttribute(Attribute.GENERIC_MOVEMENT_SPEED, speed, percentage); } - public CustomItem setMovementSpeedPercentage(double amount, EquipmentSlot equipmentSlot) { - return setMovementSpeed(amount, true, equipmentSlot); + public CustomItem setSpeed(double speed) { + return setSpeed(speed, false); } - public CustomItem setMovementSpeedPercentage(double amount) { - return setMovementSpeed(amount, true); + public CustomItem setSpeedPercentage(double speedPercentage) { + return setSpeed(speedPercentage, true); } - public Map>> getAttributes() { + public Map> getAttributes() { return attributes; } diff --git a/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java b/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java index 0d8714d..dfc3942 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java +++ b/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java @@ -2,6 +2,7 @@ import com.mojang.authlib.GameProfile; import com.mojang.authlib.properties.Property; +import com.strangeone101.holoitemsapi.CustomItem; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.attribute.Attribute; @@ -24,7 +25,7 @@ public class ItemUtils { - public static final Map ATTRIBUTE_MAP = new HashMap<>(); + public static final Map> ATTRIBUTE_MAP = new HashMap<>(); /** * Sets the skin of a Skull to the skin provided. Can be a UUID, name, texture ID or URL @@ -81,34 +82,42 @@ public static SkullMeta setSkinFromURL(SkullMeta meta, String skin) { * @param operation The operation * @param stack The itemstack */ - public static void setAttribute(double number, Attribute attribute, AttributeModifier.Operation operation, ItemStack stack) { - if (ATTRIBUTE_MAP.isEmpty()) { - Random random = new Random("unique_seed".hashCode()); + public static void setAttribute(double number, Attribute attribute, AttributeModifier.Operation operation, ItemStack stack, CustomItem ci) { + if (!(ATTRIBUTE_MAP.containsKey(ci))) { + ATTRIBUTE_MAP.put(ci, new HashMap<>()); + } + Map ciMap = ATTRIBUTE_MAP.get(ci); + if (ATTRIBUTE_MAP.get(ci).isEmpty()) { + Random random = new Random(ci.getInternalName().hashCode()); //Due to the set random seed above, the UUIDs bellow should always be the same. - ATTRIBUTE_MAP.put(Attribute.GENERIC_ATTACK_DAMAGE, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_ATTACK_SPEED, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_ATTACK_KNOCKBACK, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_MAX_HEALTH, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_ARMOR, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_ARMOR_TOUGHNESS, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_KNOCKBACK_RESISTANCE, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_MOVEMENT_SPEED, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_FLYING_SPEED, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_FOLLOW_RANGE, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.GENERIC_LUCK, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.HORSE_JUMP_STRENGTH, new UUID(random.nextLong(), random.nextLong())); - ATTRIBUTE_MAP.put(Attribute.ZOMBIE_SPAWN_REINFORCEMENTS, new UUID(random.nextLong(), random.nextLong())); - - if (!ATTRIBUTE_MAP.containsKey(attribute)) { //Bodge for attributes added in future - ATTRIBUTE_MAP.put(attribute, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_ATTACK_DAMAGE, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_ATTACK_SPEED, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_ATTACK_KNOCKBACK, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_MAX_HEALTH, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_ARMOR, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_ARMOR_TOUGHNESS, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_KNOCKBACK_RESISTANCE, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_MOVEMENT_SPEED, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_FLYING_SPEED, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_FOLLOW_RANGE, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.GENERIC_LUCK, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.HORSE_JUMP_STRENGTH, new UUID(random.nextLong(), random.nextLong())); + ciMap.put(Attribute.ZOMBIE_SPAWN_REINFORCEMENTS, new UUID(random.nextLong(), random.nextLong())); + + if (!ciMap.containsKey(attribute)) { //Bodge for attributes added in future + ciMap.put(attribute, new UUID(random.nextLong(), random.nextLong())); } } - AttributeModifier mod = new AttributeModifier(ATTRIBUTE_MAP.get(attribute), attribute.name(), number, operation, getSlotForItem(stack.getType())); - ItemMeta meta = stack.getItemMeta(); - meta.addAttributeModifier(attribute, mod); - stack.setItemMeta(meta); + try { + AttributeModifier mod = new AttributeModifier(ciMap.get(attribute), attribute.name(), number, operation, getSlotForItem(stack.getType())); + ItemMeta meta = stack.getItemMeta(); + meta.addAttributeModifier(attribute, mod); + stack.setItemMeta(meta); + } catch (IllegalArgumentException e) { + + } } /** From 76ecb371ee1842285bbd0b52f85887b74a58f472 Mon Sep 17 00:00:00 2001 From: 00Creamy <81294829+00Creamy@users.noreply.github.com> Date: Sun, 29 Aug 2021 05:43:25 +0800 Subject: [PATCH 5/5] Update ItemUtils.java --- .../holoitemsapi/util/ItemUtils.java | 43 ++++++------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java b/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java index dfc3942..d050a6b 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java +++ b/src/main/java/com/strangeone101/holoitemsapi/util/ItemUtils.java @@ -25,8 +25,6 @@ public class ItemUtils { - public static final Map> ATTRIBUTE_MAP = new HashMap<>(); - /** * Sets the skin of a Skull to the skin provided. Can be a UUID, name, texture ID or URL * @param meta The skull meta @@ -81,37 +79,13 @@ public static SkullMeta setSkinFromURL(SkullMeta meta, String skin) { * @param attribute The attribute * @param operation The operation * @param stack The itemstack + * @param ci The custom item */ public static void setAttribute(double number, Attribute attribute, AttributeModifier.Operation operation, ItemStack stack, CustomItem ci) { - if (!(ATTRIBUTE_MAP.containsKey(ci))) { - ATTRIBUTE_MAP.put(ci, new HashMap<>()); - } - Map ciMap = ATTRIBUTE_MAP.get(ci); - if (ATTRIBUTE_MAP.get(ci).isEmpty()) { - Random random = new Random(ci.getInternalName().hashCode()); - - //Due to the set random seed above, the UUIDs bellow should always be the same. - ciMap.put(Attribute.GENERIC_ATTACK_DAMAGE, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_ATTACK_SPEED, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_ATTACK_KNOCKBACK, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_MAX_HEALTH, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_ARMOR, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_ARMOR_TOUGHNESS, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_KNOCKBACK_RESISTANCE, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_MOVEMENT_SPEED, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_FLYING_SPEED, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_FOLLOW_RANGE, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.GENERIC_LUCK, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.HORSE_JUMP_STRENGTH, new UUID(random.nextLong(), random.nextLong())); - ciMap.put(Attribute.ZOMBIE_SPAWN_REINFORCEMENTS, new UUID(random.nextLong(), random.nextLong())); - - if (!ciMap.containsKey(attribute)) { //Bodge for attributes added in future - ciMap.put(attribute, new UUID(random.nextLong(), random.nextLong())); - } - } + Random random = new Random(cantorFunction(ci.getInternalID(), cantorFunction(attribute.ordinal(), operation.ordinal()))); try { - AttributeModifier mod = new AttributeModifier(ciMap.get(attribute), attribute.name(), number, operation, getSlotForItem(stack.getType())); + AttributeModifier mod = new AttributeModifier(new UUID(random.nextLong(), random.nextLong()), attribute.name(), number, operation, getSlotForItem(stack.getType())); ItemMeta meta = stack.getItemMeta(); meta.addAttributeModifier(attribute, mod); stack.setItemMeta(meta); @@ -238,4 +212,15 @@ public static boolean isLeatherArmor(Material material) { return false; } } + + /** + * Create one unique number from two different number + * Note that cantorFunction(1,0) is different from cantorFunction(0,1) + * @param x The first number + * @param y The second number + * @return A unique number + */ + public static int cantorFunction(int x, int y) { + return (((x * x) + (3 * x) + (2 * x * y) + y + (y * y)) / 2); + } }