From c56713ed7da55f35ac40f05d0fcf1bfe414164ea Mon Sep 17 00:00:00 2001 From: RobotHanzo Date: Sat, 3 Apr 2021 16:07:41 +0800 Subject: [PATCH] Now using enum --- .../core/attributes/DamageableItem.java | 37 +++++++++++++++---- .../implementation/items/armor/ElytraCap.java | 6 +++ .../slimefun4/utils/UnbreakingAlgorithms.java | 15 ++++++++ .../slimefun4/utils/UnbreakingRNGUtils.java | 25 ------------- 4 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingAlgorithms.java delete mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingRNGUtils.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/DamageableItem.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/DamageableItem.java index fc0fb5eb3..de29ca615 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/DamageableItem.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/DamageableItem.java @@ -3,7 +3,7 @@ package io.github.thebusybiscuit.slimefun4.core.attributes; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import io.github.thebusybiscuit.slimefun4.utils.UnbreakingRNGUtils; +import io.github.thebusybiscuit.slimefun4.utils.UnbreakingAlgorithms; import org.bukkit.Sound; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; @@ -35,6 +35,17 @@ public interface DamageableItem extends ItemAttribute { */ boolean isDamageable(); + /** + * Change this to a {@link UnbreakingAlgorithms} value if needed to fit the type of the damageable item + * Defaults to return {@link UnbreakingAlgorithms#TOOLS}, + * which represents {@link DamageableItem} utilizes the tool algorithm to determine the chance of item damaging + * + * @return The {@link UnbreakingAlgorithms} of the itemType + */ + default UnbreakingAlgorithms itemType() { + return UnbreakingAlgorithms.TOOLS; + } + /** * This method will damage the given {@link ItemStack} once. * It also takes into account the {@link Enchantment} {@code Unbreaking}. @@ -42,15 +53,15 @@ public interface DamageableItem extends ItemAttribute { * It will only apply the damage if {@link #isDamageable()} returned true. * * @param p - * The {@link Player} to which the item belongs + * The {@link Player} to which the item belongs * @param item - * The {@link ItemStack} to damage + * The {@link ItemStack} to damage */ default void damageItem(@Nonnull Player p, @Nullable ItemStack item) { if (isDamageable() && item != null && !item.getType().isAir() && item.getAmount() > 0) { int unbreakingLevel = item.getEnchantmentLevel(Enchantment.DURABILITY); - if (unbreakingLevel > 0 && durabilityRNG(unbreakingLevel)) { + if (unbreakingLevel > 0 && evaluateUnbreakingEnchantment(unbreakingLevel)) { return; } @@ -73,13 +84,23 @@ public interface DamageableItem extends ItemAttribute { /** * This method will randomly decide if the item should be damaged or not * This does not damage the item, it is called by {@link #damageItem(Player, ItemStack)} to randomly generate a boolean - * This method can be overridden when trying to break a piece of armor, with the provided method {@link UnbreakingRNGUtils().getArmorRNG(Integer)} * * @param unbreakingLevel - * The {@link Integer} level of the unbreaking enchantment + * The {@link Integer} level of the unbreaking enchantment + * @throws IllegalArgumentException + * Thrown when {@link #itemType()} doesn't return a valid {@link UnbreakingAlgorithms} value + * @return Whether you should keep the item undamaged + * */ - default boolean durabilityRNG(int unbreakingLevel) { - return new UnbreakingRNGUtils().getToolsRNG(unbreakingLevel); + default boolean evaluateUnbreakingEnchantment(int unbreakingLevel) { + switch (itemType()){ + case TOOLS: + return !(Math.random() < (1.0 / (unbreakingLevel + 1))); + case ARMORS: + return !(Math.random() < 0.6 + (0.4 / (unbreakingLevel + 1))); + default: + throw new IllegalArgumentException("itemType must be either UnbreakingAlgorithms.TOOLS or UnbreakingAlgorithms.ARMORS"); + } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/ElytraCap.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/ElytraCap.java index d21422b40..934f41c60 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/ElytraCap.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/armor/ElytraCap.java @@ -4,6 +4,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.utils.UnbreakingAlgorithms; import org.bukkit.GameMode; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; @@ -29,6 +30,11 @@ public class ElytraCap extends SlimefunArmorPiece implements DamageableItem, Pro private final NamespacedKey key; + @Override + public UnbreakingAlgorithms itemType() { + return UnbreakingAlgorithms.ARMORS; + } + @ParametersAreNonnullByDefault public ElytraCap(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(category, item, recipeType, recipe, null); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingAlgorithms.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingAlgorithms.java new file mode 100644 index 000000000..4d96df3dc --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingAlgorithms.java @@ -0,0 +1,15 @@ +package io.github.thebusybiscuit.slimefun4.utils; + + +import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; + +/** + * This a enum indicating if a {@link DamageableItem} is a tool or an armor + * This effects the way they determine if an item should be damaged + * + * @author RobotHanzo + */ +public enum UnbreakingAlgorithms { + TOOLS, + ARMORS +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingRNGUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingRNGUtils.java deleted file mode 100644 index a4ea39877..000000000 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/UnbreakingRNGUtils.java +++ /dev/null @@ -1,25 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.utils; - - -import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; - -/** - * This class is created for a RNG generator to determine if the tool / armor should be damaged when unbreaking enchant is applied.
- * Mainly used in {@link DamageableItem} and their implementations - * - * @author RobotHanzo - * - */ -public class UnbreakingRNGUtils { - - public UnbreakingRNGUtils(){} - - public boolean getToolsRNG(int unbreakingLevel){ - return !(Math.random() < (1.0 / (unbreakingLevel + 1))); - } - - public boolean getArmorRNG(int unbreakingLevel){ - return !(Math.random() < 0.6 + (0.4 / (unbreakingLevel + 1))); - } - -}