WIP: Logic cleanup.
This commit is contained in:
@ -38,6 +38,10 @@ public abstract class TotemItem extends Item {
|
||||
super(new Properties().stacksTo(1).rarity(rarity).durability(durability));
|
||||
}
|
||||
|
||||
public static boolean isTotem(ItemStack stack) {
|
||||
return (stack.getItem() instanceof TotemItem);
|
||||
}
|
||||
|
||||
public abstract boolean isCharge(ItemStack stack);
|
||||
|
||||
public static Optional<Integer> getTargetIndex(ItemStack stack) {
|
||||
@ -95,51 +99,64 @@ public abstract class TotemItem extends Item {
|
||||
stack.set(ModDataComponents.TOTEM_DATA.get(), data.get());
|
||||
}
|
||||
|
||||
public static int getTargetCost(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return -1; }
|
||||
return !isCostDynamic(stack) ? getConfig(stack).getChargeCost() :
|
||||
(int)(getTargetDeaths(stack) * getConfig(stack).getChargeCostMultiplier());
|
||||
public static Optional<Integer> getTargetCost(ItemStack stack) {
|
||||
Optional<Config.TotemConfig> config = getConfig(stack);
|
||||
Optional<Integer> targetDeaths = getTargetDeaths(stack);
|
||||
if (config.isEmpty() || targetDeaths.isEmpty()) { return Optional.empty(); }
|
||||
|
||||
return Optional.of(
|
||||
config.get().getChargeCost() != -1
|
||||
? config.get().getChargeCost()
|
||||
: (int)(targetDeaths.get() * config.get().getChargeCostMultiplier())
|
||||
);
|
||||
}
|
||||
|
||||
public static boolean isTotemFull(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return false; }
|
||||
return getCharge(stack) == getMaxCharge(stack);
|
||||
public static Optional<Integer> getMaxCharge(ItemStack stack) {
|
||||
return getConfig(stack).map(totemConfig -> totemConfig.getChargeCost() == -1
|
||||
? totemConfig.getChargeCostLimit()
|
||||
: totemConfig.getChargeCost()
|
||||
);
|
||||
}
|
||||
|
||||
public static boolean canTotemAffordTarget(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return false; }
|
||||
if (getCharge(stack) < getTargetCost(stack)) {
|
||||
if (!isTotemFull(stack)) {
|
||||
return false;
|
||||
}
|
||||
return isTotemFull(stack) && canReviveMoreExpensiveTargets(stack);
|
||||
}
|
||||
return true;
|
||||
public static Optional<Boolean> isTotemFull(ItemStack stack) {
|
||||
Optional<Config.TotemConfig> config = getConfig(stack);
|
||||
Optional<Integer> charge = getCharge(stack);
|
||||
Optional<Integer> maxCharge = getMaxCharge(stack);
|
||||
if (config.isEmpty() || charge.isEmpty() || maxCharge.isEmpty()) { return Optional.empty(); }
|
||||
|
||||
return Optional.of(charge.get().equals(maxCharge.get()));
|
||||
}
|
||||
|
||||
public static Optional<Boolean> canTotemAffordTarget(ItemStack stack) {
|
||||
Optional<Integer> charge = getCharge(stack);
|
||||
Optional<Integer> targetCost = getTargetCost(stack);
|
||||
Optional<Boolean> isTotemFull = isTotemFull(stack);
|
||||
Optional<Boolean> canReviveMoreExpensiveTargets = canReviveMoreExpensiveTargets(stack);
|
||||
if (charge.isEmpty() || targetCost.isEmpty() || isTotemFull.isEmpty() || canReviveMoreExpensiveTargets.isEmpty()) { return Optional.empty(); }
|
||||
|
||||
return Optional.of(charge.get() >= targetCost.get() || (isTotemFull.get() && canReviveMoreExpensiveTargets.get()));
|
||||
}
|
||||
|
||||
public static int getMaxCharge(ItemStack stack) {
|
||||
Config.TotemConfig config = getConfig(stack);
|
||||
if (config == null) { return -1; }
|
||||
|
||||
if (config.getChargeCost() == -1) {
|
||||
return config.getChargeCostLimit();
|
||||
}
|
||||
return config.getChargeCost();
|
||||
public static Optional<Boolean> canReviveAcrossDimensions(ItemStack stack) {
|
||||
return getConfig(stack).map(Config.TotemConfig::getCanReviveAcrossDimensions);
|
||||
}
|
||||
|
||||
public static Optional<Boolean> canReviveMoreExpensiveTargets(ItemStack stack) {
|
||||
return getConfig(stack).map(Config.TotemConfig::getCanReviveMoreExpensiveTargets);
|
||||
}
|
||||
|
||||
public static boolean isCostDynamic(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return false; }
|
||||
return getConfig(stack).getChargeCost() == -1;
|
||||
|
||||
private static Optional<TotemData> getTotemData(ItemStack stack) {
|
||||
return Optional.ofNullable(stack.getComponents().get(ModDataComponents.TOTEM_DATA.get()));
|
||||
}
|
||||
|
||||
public static boolean canReviveAcrossDimensions(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return false; }
|
||||
return getConfig(stack).getCanReviveAcrossDimensions();
|
||||
}
|
||||
|
||||
public static boolean canReviveMoreExpensiveTargets(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return false; }
|
||||
return getConfig(stack).getCanReviveMoreExpensiveTargets();
|
||||
|
||||
private static Optional<Config.TotemConfig> getConfig(ItemStack stack) {
|
||||
return switch (stack.getItem()) {
|
||||
case StrawTotemItem ignored -> Optional.of(Config.Server.getStrawTotemConfig());
|
||||
case IronTotemItem ignored -> Optional.of(Config.Server.getIronTotemConfig());
|
||||
case DiamondTotemItem ignored -> Optional.of(Config.Server.getDiamondTotemConfig());
|
||||
case NetheriteTotemItem ignored -> Optional.of(Config.Server.getNetheriteTotemConfig());
|
||||
default -> Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -229,21 +246,4 @@ public abstract class TotemItem extends Item {
|
||||
}
|
||||
return (isDamageable(stack)) ? super.getDamage(stack) : 0;
|
||||
}
|
||||
|
||||
private static boolean isTotem(ItemStack stack) {
|
||||
return (stack.getItem() instanceof TotemItem);
|
||||
}
|
||||
|
||||
private static Optional<TotemData> getTotemData(ItemStack stack) {
|
||||
return Optional.ofNullable(stack.getComponents().get(ModDataComponents.TOTEM_DATA.get()));
|
||||
}
|
||||
|
||||
private static Config.TotemConfig getConfig(ItemStack stack) {
|
||||
Item item = stack.getItem();
|
||||
if (item instanceof StrawTotemItem) { return Config.Server.getStrawTotemConfig(); }
|
||||
if (item instanceof IronTotemItem) { return Config.Server.getIronTotemConfig(); }
|
||||
if (item instanceof DiamondTotemItem) { return Config.Server.getDiamondTotemConfig(); }
|
||||
if (item instanceof NetheriteTotemItem) { return Config.Server.getNetheriteTotemConfig(); }
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user