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

Overridable durability RNG

This commit is contained in:
RobotHanzo 2021-04-02 22:16:25 +08:00
parent 6d12d1f7ac
commit 11fded46db
2 changed files with 39 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package io.github.thebusybiscuit.slimefun4.core.attributes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import io.github.thebusybiscuit.slimefun4.utils.UnbreakingRNGUtils;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -49,7 +50,7 @@ public interface DamageableItem extends ItemAttribute {
if (isDamageable() && item != null && !item.getType().isAir() && item.getAmount() > 0) { if (isDamageable() && item != null && !item.getType().isAir() && item.getAmount() > 0) {
int unbreakingLevel = item.getEnchantmentLevel(Enchantment.DURABILITY); int unbreakingLevel = item.getEnchantmentLevel(Enchantment.DURABILITY);
if (unbreakingLevel > 0 && !(Math.random() < (1.0 / (unbreakingLevel + 1)))) { if (unbreakingLevel > 0 && durabilityRNG(unbreakingLevel)) {
return; return;
} }
@ -69,4 +70,16 @@ 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
*/
default boolean durabilityRNG(int unbreakingLevel) {
return new UnbreakingRNGUtils().getToolsRNG(unbreakingLevel);
}
} }

View File

@ -0,0 +1,25 @@
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)));
}
}