2023-02-28 17:44:14 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
public interface ChargeableItem {
|
|
|
|
|
|
|
|
int getMaxCharge();
|
|
|
|
|
|
|
|
default int getDefaultCharge() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
default boolean isChargable() {
|
|
|
|
return getMaxCharge() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
default boolean hasCharge(ItemStack stack) {
|
|
|
|
return getEnergy(stack) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
default ItemStack recharge(ItemStack stack) {
|
|
|
|
return setEnergy(stack, getMaxCharge());
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:28:26 +01:00
|
|
|
default ItemStack recharge(ItemStack stack, float amount) {
|
|
|
|
return setEnergy(stack, getEnergy(stack) + amount);
|
|
|
|
}
|
|
|
|
|
2023-02-28 17:44:14 +01:00
|
|
|
default boolean canCharge(ItemStack stack) {
|
|
|
|
return isChargable() && getEnergy(stack) < getMaxCharge();
|
|
|
|
}
|
|
|
|
|
|
|
|
default float getChargeRemainder(ItemStack stack) {
|
|
|
|
return Math.max(0, getMaxCharge() - getEnergy(stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
default void onDischarge(ItemStack stack) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void consumeEnergy(ItemStack stack, float amount) {
|
|
|
|
setEnergy(stack, getEnergy(stack) - amount);
|
|
|
|
if (stack.getItem() instanceof ChargeableItem c) {
|
|
|
|
c.onDischarge(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:28:26 +01:00
|
|
|
static int getDefaultCharge(ItemStack stack) {
|
|
|
|
return stack.getItem() instanceof ChargeableItem c ? c.getDefaultCharge() : 0;
|
|
|
|
}
|
|
|
|
|
2023-02-28 17:44:14 +01:00
|
|
|
static float getEnergy(ItemStack stack) {
|
2023-08-31 00:46:23 +02:00
|
|
|
return stack.hasNbt() && stack.getNbt().contains("energy") ? Math.max(0, stack.getNbt().getFloat("energy")) : getDefaultCharge(stack);
|
2023-02-28 17:44:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ItemStack setEnergy(ItemStack stack, float energy) {
|
2023-03-05 02:28:26 +01:00
|
|
|
if (energy <= 0 && getDefaultCharge(stack) <= 0) {
|
2023-02-28 17:44:14 +01:00
|
|
|
stack.removeSubNbt("energy");
|
|
|
|
} else {
|
|
|
|
stack.getOrCreateNbt().putFloat("energy", energy);
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|