From 549b075939834784f4301f67c4a35c0cf9bb5fc4 Mon Sep 17 00:00:00 2001 From: Daniel Walsh Date: Thu, 21 Jan 2021 19:25:38 +0000 Subject: [PATCH] Make RechargeableHelper public --- .../core/attributes/Rechargeable.java | 23 ++++++++++--------- .../RechargeableHelper.java | 21 ++++++++++++----- 2 files changed, 27 insertions(+), 17 deletions(-) rename src/main/java/io/github/thebusybiscuit/slimefun4/{core/attributes => utils}/RechargeableHelper.java (78%) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/Rechargeable.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/Rechargeable.java index 5497ea637..17a3c9bc4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/Rechargeable.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/Rechargeable.java @@ -9,15 +9,16 @@ import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets.Jetpack; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets.MultiTool; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.ChargingBench; +import io.github.thebusybiscuit.slimefun4.utils.RechargeableHelper; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; /** * A {@link Rechargeable} {@link SlimefunItem} can hold energy and is able to * be recharged using a {@link ChargingBench}. * Any {@link SlimefunItem} which is supposed to be chargeable must implement this interface. - * + * * @author TheBusyBiscuit - * + * * @see ChargingBench * @see EnergyNet * @see Jetpack @@ -28,10 +29,10 @@ public interface Rechargeable extends ItemAttribute { /** * This method returns the maximum charge the given {@link ItemStack} is capable of holding. - * + * * @param item * The {@link ItemStack} for which to determine the maximum charge - * + * * @return The maximum energy charge for this {@link ItemStack} */ float getMaxItemCharge(ItemStack item); @@ -39,7 +40,7 @@ public interface Rechargeable extends ItemAttribute { /** * This method sets the stored energy charge for a given {@link ItemStack}. * The charge must be at least zero and at most {@link #getMaxItemCharge(ItemStack)}. - * + * * @param item * The {@link ItemStack} to charge * @param charge @@ -63,10 +64,10 @@ public interface Rechargeable extends ItemAttribute { /** * This method returns the currently stored energy charge on the provided {@link ItemStack}. - * + * * @param item * The {@link ItemStack} to get the charge from - * + * * @return The charge stored on this {@link ItemStack} */ default float getItemCharge(ItemStack item) { @@ -81,12 +82,12 @@ public interface Rechargeable extends ItemAttribute { * This method adds the given charge to the provided {@link ItemStack}. * The method will also return whether this operation was successful. * If the {@link ItemStack} is already at maximum charge, the method will return false. - * + * * @param item * The {@link ItemStack} to charge * @param charge * The amount of charge to add - * + * * @return Whether the given charge could be added successfully */ default boolean addItemCharge(ItemStack item, float charge) { @@ -116,12 +117,12 @@ public interface Rechargeable extends ItemAttribute { * This method removes the given charge to the provided {@link ItemStack}. * The method will also return whether this operation was successful. * If the {@link ItemStack} does not have enough charge, the method will return false. - * + * * @param item * The {@link ItemStack} to remove the charge from * @param charge * The amount of charge to remove - * + * * @return Whether the given charge could be removed successfully */ default boolean removeItemCharge(ItemStack item, float charge) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RechargeableHelper.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/RechargeableHelper.java similarity index 78% rename from src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RechargeableHelper.java rename to src/main/java/io/github/thebusybiscuit/slimefun4/utils/RechargeableHelper.java index 65ea56133..150273355 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/RechargeableHelper.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/RechargeableHelper.java @@ -1,4 +1,4 @@ -package io.github.thebusybiscuit.slimefun4.core.attributes; +package io.github.thebusybiscuit.slimefun4.utils; import java.math.BigDecimal; import java.math.RoundingMode; @@ -8,6 +8,8 @@ import java.util.regex.Pattern; import javax.annotation.Nonnull; +import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable; +import org.apache.commons.lang.Validate; import org.bukkit.NamespacedKey; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; @@ -20,20 +22,25 @@ import net.md_5.bungee.api.ChatColor; /** * This is just a simple helper class to provide static methods to the {@link Rechargeable} * interface. - * + * * @author TheBusyBiscuit - * + * @author WalshyDev + * * @see Rechargeable * */ -final class RechargeableHelper { +public final class RechargeableHelper { private static final String LORE_PREFIX = ChatColors.color("&8\u21E8 &e\u26A1 &7"); private static final Pattern REGEX = Pattern.compile(ChatColors.color("(&c&o)?" + LORE_PREFIX) + "[0-9.]+ / [0-9.]+ J"); private RechargeableHelper() {} - static void setCharge(@Nonnull ItemMeta meta, float charge, float capacity) { + public static void setCharge(@Nonnull ItemMeta meta, float charge, float capacity) { + Validate.notNull(meta, "Meta cannot be null!"); + Validate.isTrue(charge >= 0, "Charge has to be equal to or greater than 0!"); + Validate.isTrue(charge <= capacity, "Charge must be less than the capacity!"); + BigDecimal decimal = BigDecimal.valueOf(charge).setScale(2, RoundingMode.HALF_UP); float value = decimal.floatValue(); @@ -55,7 +62,9 @@ final class RechargeableHelper { meta.setLore(lore); } - static float getCharge(@Nonnull ItemMeta meta) { + public static float getCharge(@Nonnull ItemMeta meta) { + Validate.notNull(meta, "Meta cannot be null!"); + NamespacedKey key = SlimefunPlugin.getRegistry().getItemChargeDataKey(); Float value = meta.getPersistentDataContainer().get(key, PersistentDataType.FLOAT);