From 368d2f5798f8daf9f541296518e977afaa357361 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Tue, 4 Aug 2020 13:29:45 +0200 Subject: [PATCH] Move block energy code to EnergyNetComponent --- .../slimefun4/core/SlimefunRegistry.java | 4 + .../core/attributes/EnergyNetComponent.java | 107 ++++++++++++++++++ .../core/networks/energy/EnergyNet.java | 2 +- .../slimefun4/utils/NumberUtils.java | 23 ++++ .../slimefun4/utils/SlimefunUtils.java | 27 +++++ .../abstractItems/AContainer.java | 2 +- .../abstractItems/AGenerator.java | 3 +- .../Slimefun/api/energy/ChargableBlock.java | 39 ++----- 8 files changed, 175 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java index b6bc8f953..f6bf728ab 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -80,7 +80,10 @@ public class SlimefunRegistry { private final Map chunks = new HashMap<>(); private final Map layouts = new EnumMap<>(SlimefunGuideLayout.class); private final Map> mobDrops = new EnumMap<>(EntityType.class); + + @Deprecated private final Map capacities = new HashMap<>(); + private final Map blockMenuPresets = new HashMap<>(); private final Map universalInventories = new HashMap<>(); private final Map, Set> globalItemHandlers = new HashMap<>(); @@ -234,6 +237,7 @@ public class SlimefunRegistry { return slimefunIds; } + @Deprecated public Map getEnergyCapacities() { return capacities; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/EnergyNetComponent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/EnergyNetComponent.java index 919df1866..9d1ae90d7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/EnergyNetComponent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/attributes/EnergyNetComponent.java @@ -1,8 +1,15 @@ package io.github.thebusybiscuit.slimefun4.core.attributes; +import org.apache.commons.lang.Validate; +import org.bukkit.Location; + import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.Capacitor; +import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; +import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.BlockStorage; /** * This Interface, when attached to a class that inherits from {@link SlimefunItem}, marks @@ -36,4 +43,104 @@ public interface EnergyNetComponent extends ItemAttribute { */ int getCapacity(); + /** + * This returns whether this {@link EnergyNetComponent} can hold energy charges. + * It returns true if {@link #getCapacity()} returns a number greater than zero. + * + * @return Whether this {@link EnergyNetComponent} can store energy. + */ + default boolean isChargeable() { + return getCapacity() > 0; + } + + /** + * This returns the currently stored charge at a given {@link Location}. + * + * @param l + * The target {@link Location} + * + * @return The charge stored at that {@link Location} + */ + default int getCharge(Location l) { + String charge = BlockStorage.getLocationInfo(l, "energy-charge"); + + if (charge != null) { + return Integer.parseInt(charge); + } + else { + return 0; + } + } + + /** + * This method sets the charge which is stored at a given {@link Location} + * If this {@link EnergyNetComponent} is of type {@code EnergyNetComponentType.CAPACITOR}, then + * this method will automatically update the texture of this {@link Capacitor} as well. + * + * @param l + * The target {@link Location} + * @param charge + * The new charge + */ + default void setCharge(Location l, int charge) { + int capacity = getCapacity(); + + // This method only makes sense if we can actually store energy + if (capacity > 0) { + charge = NumberUtils.clamp(0, charge, capacity); + + // Do we even need to update the value? + if (charge != getCharge(l)) { + BlockStorage.addBlockInfo(l, "energy-charge", String.valueOf(charge), false); + + // Update the capacitor texture + if (getEnergyComponentType() == EnergyNetComponentType.CAPACITOR) { + SlimefunUtils.updateCapacitorTexture(l, charge, capacity); + } + } + } + } + + default void addCharge(Location l, int charge) { + Validate.isTrue(charge > 0, "You can only add a positive charge!"); + int capacity = getCapacity(); + + // This method only makes sense if we can actually store energy + if (capacity > 0) { + int currentCharge = getCharge(l); + + // Check if there is even space for new energy + if (currentCharge < capacity) { + int newCharge = Math.min(capacity, currentCharge + charge); + BlockStorage.addBlockInfo(l, "energy-charge", String.valueOf(newCharge), false); + + // Update the capacitor texture + if (getEnergyComponentType() == EnergyNetComponentType.CAPACITOR) { + SlimefunUtils.updateCapacitorTexture(l, charge, capacity); + } + } + } + } + + default void removeCharge(Location l, int charge) { + Validate.isTrue(charge > 0, "The charge to remove must be greater than zero!"); + int capacity = getCapacity(); + + // This method only makes sense if we can actually store energy + if (capacity > 0) { + int currentCharge = getCharge(l); + + // Check if there is even energy stored + if (currentCharge > 0) { + int newCharge = Math.max(0, currentCharge - charge); + BlockStorage.addBlockInfo(l, "energy-charge", String.valueOf(newCharge), false); + + // Update the capacitor texture + if (getEnergyComponentType() == EnergyNetComponentType.CAPACITOR) { + SlimefunUtils.updateCapacitorTexture(l, charge, capacity); + } + } + } + } + } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java index cb450cabc..3f8440eda 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java @@ -226,7 +226,7 @@ public class EnergyNet extends Network { EnergyNetProvider provider = (EnergyNetProvider) item; int energy = provider.getGeneratedOutput(source, config); - if (provider.getCapacity() > 0) { + if (provider.isChargeable()) { generatorsWithCapacity.put(source, provider.getCapacity()); String charge = config.getString("energy-charge"); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java index 6bbb7837e..35eb149dc 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/NumberUtils.java @@ -99,4 +99,27 @@ public final class NumberUtils { public static float getFloat(Float value, float defaultValue) { return value == null ? defaultValue : value; } + + /** + * This method is a combination of Math.min and Math.max, it clamps the given value + * between a minimum and a maximum. + * + * @param min + * The minimum value + * @param value + * The value to clamp + * @param max + * The maximum value + */ + public static int clamp(int min, int value, int max) { + if (value < min) { + return min; + } + else if (value > max) { + return max; + } + else { + return value; + } + } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java index 260c6b971..15f527add 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java @@ -7,8 +7,10 @@ import java.util.List; import java.util.Optional; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.block.Block; import org.bukkit.entity.Item; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -18,6 +20,7 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; import io.github.thebusybiscuit.cscorelib2.item.ImmutableItemMeta; +import io.github.thebusybiscuit.cscorelib2.skull.SkullBlock; import io.github.thebusybiscuit.cscorelib2.skull.SkullItem; import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.api.exceptions.PrematureCodeException; @@ -29,6 +32,7 @@ import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper; import me.mrCookieSlime.EmeraldEnchants.EmeraldEnchants; import me.mrCookieSlime.EmeraldEnchants.ItemEnchantment; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.Slimefun; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; /** @@ -364,4 +368,27 @@ public final class SlimefunUtils { return string1.toString().equals(string2.toString()); } + public static void updateCapacitorTexture(Location l, int charge, int capacity) { + Slimefun.runSync(() -> { + Block b = l.getBlock(); + + if (b.getType() == Material.PLAYER_HEAD || b.getType() == Material.PLAYER_WALL_HEAD) { + double level = (double) charge / capacity; + + if (level <= 0.25) { + SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_25.getTexture()); + } + else if (level <= 0.5) { + SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_50.getTexture()); + } + else if (level <= 0.75) { + SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_75.getTexture()); + } + else { + SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_100.getTexture()); + } + } + }); + } + } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java index 7d135c987..d54662972 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AContainer.java @@ -233,7 +233,7 @@ public abstract class AContainer extends SlimefunItem implements InventoryBlock, if (timeleft > 0) { ChestMenuUtils.updateProgressbar(inv, 22, timeleft, processing.get(b).getTicks(), getProgressBar()); - if (getCapacity() > 0) { + if (isChargeable()) { if (ChargableBlock.getCharge(b) < getEnergyConsumption()) { return; } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java index 2a94bce84..ce1c3cb74 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/abstractItems/AGenerator.java @@ -133,7 +133,6 @@ public abstract class AGenerator extends AbstractEnergyProvider { @Override public int getGeneratedOutput(Location l, Config data) { BlockMenu inv = BlockStorage.getInventory(l); - boolean chargeable = getCapacity() > 0; if (isProcessing(l)) { int timeleft = progress.get(l); @@ -141,7 +140,7 @@ public abstract class AGenerator extends AbstractEnergyProvider { if (timeleft > 0) { ChestMenuUtils.updateProgressbar(inv, 22, timeleft, processing.get(l).getTicks(), getProgressBar()); - if (chargeable) { + if (isChargeable()) { int charge = ChargableBlock.getCharge(l); if (getCapacity() - charge >= getEnergyProduction()) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ChargableBlock.java b/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ChargableBlock.java index 3a93118c0..a7d7acc41 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ChargableBlock.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/energy/ChargableBlock.java @@ -1,17 +1,21 @@ package me.mrCookieSlime.Slimefun.api.energy; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.block.Block; -import io.github.thebusybiscuit.cscorelib2.skull.SkullBlock; +import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.Capacitor; -import io.github.thebusybiscuit.slimefun4.utils.HeadTexture; +import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.api.BlockStorage; -import me.mrCookieSlime.Slimefun.api.Slimefun; +/** + * + * @deprecated Use the methods provided by {@link EnergyNetComponent} instead. + * + */ +@Deprecated public final class ChargableBlock { private static final String KEY = "energy-charge"; @@ -70,7 +74,7 @@ public final class ChargableBlock { BlockStorage.addBlockInfo(l, KEY, String.valueOf(charge), false); if (updateTexture) { - updateCapacitor(l, charge, getMaxCharge(l)); + SlimefunUtils.updateCapacitorTexture(l, charge, getMaxCharge(l)); } } } @@ -106,7 +110,7 @@ public final class ChargableBlock { setCharge(l, charge); if (SlimefunItem.getByID(id) instanceof Capacitor) { - updateCapacitor(l, charge, capacity); + SlimefunUtils.updateCapacitorTexture(l, charge, capacity); } } else if (addedCharge < 0 && charge >= -addedCharge) { @@ -114,34 +118,13 @@ public final class ChargableBlock { setCharge(l, charge); if (SlimefunItem.getByID(id) instanceof Capacitor) { - updateCapacitor(l, charge, capacity); + SlimefunUtils.updateCapacitorTexture(l, charge, capacity); } } return rest; } - private static void updateCapacitor(Location l, int charge, int capacity) { - Slimefun.runSync(() -> { - Block b = l.getBlock(); - - if (b.getType() == Material.PLAYER_HEAD || b.getType() == Material.PLAYER_WALL_HEAD) { - if (charge < (int) (capacity * 0.25)) { - SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_25.getTexture()); - } - else if (charge < (int) (capacity * 0.5)) { - SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_50.getTexture()); - } - else if (charge < (int) (capacity * 0.75)) { - SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_75.getTexture()); - } - else { - SkullBlock.setFromHash(b, HeadTexture.CAPACITOR_100.getTexture()); - } - } - }); - } - public static int getMaxCharge(Block b) { return getMaxCharge(b.getLocation()); }