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));
|
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 abstract boolean isCharge(ItemStack stack);
|
||||||
|
|
||||||
public static Optional<Integer> getTargetIndex(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());
|
stack.set(ModDataComponents.TOTEM_DATA.get(), data.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getTargetCost(ItemStack stack) {
|
public static Optional<Integer> getTargetCost(ItemStack stack) {
|
||||||
if (!isTotem(stack)) { return -1; }
|
Optional<Config.TotemConfig> config = getConfig(stack);
|
||||||
return !isCostDynamic(stack) ? getConfig(stack).getChargeCost() :
|
Optional<Integer> targetDeaths = getTargetDeaths(stack);
|
||||||
(int)(getTargetDeaths(stack) * getConfig(stack).getChargeCostMultiplier());
|
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) {
|
public static Optional<Integer> getMaxCharge(ItemStack stack) {
|
||||||
if (!isTotem(stack)) { return false; }
|
return getConfig(stack).map(totemConfig -> totemConfig.getChargeCost() == -1
|
||||||
return getCharge(stack) == getMaxCharge(stack);
|
? totemConfig.getChargeCostLimit()
|
||||||
|
: totemConfig.getChargeCost()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean canTotemAffordTarget(ItemStack stack) {
|
public static Optional<Boolean> isTotemFull(ItemStack stack) {
|
||||||
if (!isTotem(stack)) { return false; }
|
Optional<Config.TotemConfig> config = getConfig(stack);
|
||||||
if (getCharge(stack) < getTargetCost(stack)) {
|
Optional<Integer> charge = getCharge(stack);
|
||||||
if (!isTotemFull(stack)) {
|
Optional<Integer> maxCharge = getMaxCharge(stack);
|
||||||
return false;
|
if (config.isEmpty() || charge.isEmpty() || maxCharge.isEmpty()) { return Optional.empty(); }
|
||||||
}
|
|
||||||
return isTotemFull(stack) && canReviveMoreExpensiveTargets(stack);
|
return Optional.of(charge.get().equals(maxCharge.get()));
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
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) {
|
public static Optional<Boolean> canReviveAcrossDimensions(ItemStack stack) {
|
||||||
Config.TotemConfig config = getConfig(stack);
|
return getConfig(stack).map(Config.TotemConfig::getCanReviveAcrossDimensions);
|
||||||
if (config == null) { return -1; }
|
}
|
||||||
|
|
||||||
if (config.getChargeCost() == -1) {
|
public static Optional<Boolean> canReviveMoreExpensiveTargets(ItemStack stack) {
|
||||||
return config.getChargeCostLimit();
|
return getConfig(stack).map(Config.TotemConfig::getCanReviveMoreExpensiveTargets);
|
||||||
}
|
|
||||||
return config.getChargeCost();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isCostDynamic(ItemStack stack) {
|
private static Optional<TotemData> getTotemData(ItemStack stack) {
|
||||||
if (!isTotem(stack)) { return false; }
|
return Optional.ofNullable(stack.getComponents().get(ModDataComponents.TOTEM_DATA.get()));
|
||||||
return getConfig(stack).getChargeCost() == -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean canReviveAcrossDimensions(ItemStack stack) {
|
private static Optional<Config.TotemConfig> getConfig(ItemStack stack) {
|
||||||
if (!isTotem(stack)) { return false; }
|
return switch (stack.getItem()) {
|
||||||
return getConfig(stack).getCanReviveAcrossDimensions();
|
case StrawTotemItem ignored -> Optional.of(Config.Server.getStrawTotemConfig());
|
||||||
}
|
case IronTotemItem ignored -> Optional.of(Config.Server.getIronTotemConfig());
|
||||||
|
case DiamondTotemItem ignored -> Optional.of(Config.Server.getDiamondTotemConfig());
|
||||||
public static boolean canReviveMoreExpensiveTargets(ItemStack stack) {
|
case NetheriteTotemItem ignored -> Optional.of(Config.Server.getNetheriteTotemConfig());
|
||||||
if (!isTotem(stack)) { return false; }
|
default -> Optional.empty();
|
||||||
return getConfig(stack).getCanReviveMoreExpensiveTargets();
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -229,21 +246,4 @@ public abstract class TotemItem extends Item {
|
|||||||
}
|
}
|
||||||
return (isDamageable(stack)) ? super.getDamage(stack) : 0;
|
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