mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 03:35:51 +00:00
Move block energy code to EnergyNetComponent
This commit is contained in:
parent
736b48a799
commit
368d2f5798
@ -80,7 +80,10 @@ public class SlimefunRegistry {
|
||||
private final Map<String, BlockInfoConfig> chunks = new HashMap<>();
|
||||
private final Map<SlimefunGuideLayout, SlimefunGuideImplementation> layouts = new EnumMap<>(SlimefunGuideLayout.class);
|
||||
private final Map<EntityType, Set<ItemStack>> mobDrops = new EnumMap<>(EntityType.class);
|
||||
|
||||
@Deprecated
|
||||
private final Map<String, Integer> capacities = new HashMap<>();
|
||||
|
||||
private final Map<String, BlockMenuPreset> blockMenuPresets = new HashMap<>();
|
||||
private final Map<String, UniversalBlockMenu> universalInventories = new HashMap<>();
|
||||
private final Map<Class<? extends ItemHandler>, Set<ItemHandler>> globalItemHandlers = new HashMap<>();
|
||||
@ -234,6 +237,7 @@ public class SlimefunRegistry {
|
||||
return slimefunIds;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Map<String, Integer> getEnergyCapacities() {
|
||||
return capacities;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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");
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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()) {
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user