1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Now using enum

This commit is contained in:
RobotHanzo 2021-04-03 16:07:41 +08:00
parent 11fded46db
commit c56713ed7d
4 changed files with 50 additions and 33 deletions

View File

@ -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");
}
}
}

View File

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

View File

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

View File

@ -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.<br>
* 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)));
}
}