WIP: Included target deaths in data component.

This commit is contained in:
2026-01-09 05:26:42 +01:00
parent 91f3f9dbdd
commit 56f274a81b
2 changed files with 27 additions and 10 deletions

View File

@ -13,23 +13,28 @@ public class TotemData {
instance.group( instance.group(
Codec.INT.fieldOf("targetIndex").forGetter(TotemData::getTargetIndex), Codec.INT.fieldOf("targetIndex").forGetter(TotemData::getTargetIndex),
Codec.STRING.fieldOf("targetUUID").forGetter(TotemData::getTargetUUID), Codec.STRING.fieldOf("targetUUID").forGetter(TotemData::getTargetUUID),
Codec.STRING.fieldOf("targetName").forGetter(TotemData::getTargetName) Codec.STRING.fieldOf("targetName").forGetter(TotemData::getTargetName),
Codec.INT.fieldOf("targetDeaths").forGetter(TotemData::getTargetDeaths)
).apply(instance, TotemData::new) ).apply(instance, TotemData::new)
); );
public static final StreamCodec<ByteBuf, TotemData> STREAM_CODEC = StreamCodec.composite( public static final StreamCodec<ByteBuf, TotemData> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, TotemData::getTargetIndex, ByteBufCodecs.INT, TotemData::getTargetIndex,
ByteBufCodecs.STRING_UTF8, TotemData::getTargetUUID, ByteBufCodecs.STRING_UTF8, TotemData::getTargetUUID,
ByteBufCodecs.STRING_UTF8, TotemData::getTargetName, ByteBufCodecs.STRING_UTF8, TotemData::getTargetName,
ByteBufCodecs.INT, TotemData::getTargetDeaths,
TotemData::new TotemData::new
); );
private int targetIndex; private int targetIndex;
private String targetUUID; private String targetUUID;
private String targetName; private String targetName;
private int targetDeaths;
public TotemData(int targetIndex, String targetUUID, String targetName) { public TotemData(int targetIndex, String targetUUID, String targetName, int targetDeaths) {
this.targetIndex = targetIndex; this.targetIndex = targetIndex;
this.targetUUID = targetUUID; this.targetUUID = targetUUID;
this.targetName = targetName;
this.targetDeaths = targetDeaths;
} }
public int getTargetIndex() { public int getTargetIndex() {
@ -53,9 +58,16 @@ public class TotemData {
this.targetName = targetName; this.targetName = targetName;
} }
public int getTargetDeaths() {
return targetDeaths;
}
public void setTargetDeaths(int targetDeaths) {
this.targetDeaths = targetDeaths;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(this.targetIndex, this.targetUUID); return Objects.hash(this.targetIndex, this.targetUUID, this.targetName, this.targetDeaths);
} }
@Override @Override
@ -65,7 +77,9 @@ public class TotemData {
} else { } else {
return obj instanceof TotemData td return obj instanceof TotemData td
&& this.targetIndex == td.targetIndex && this.targetIndex == td.targetIndex
&& Objects.equals(this.targetUUID, td.targetUUID); && Objects.equals(this.targetUUID, td.targetUUID)
&& Objects.equals(this.targetName, td.targetName)
&& this.targetDeaths == td.targetDeaths;
} }
} }
} }

View File

@ -91,14 +91,17 @@ public abstract class TotemItem extends Item {
return !isCostDynamic(stack) ? getConfig(stack).getChargeCost() : return !isCostDynamic(stack) ? getConfig(stack).getChargeCost() :
(int)(getTargetDeaths(stack) * getConfig(stack).getChargeCostMultiplier()); (int)(getTargetDeaths(stack) * getConfig(stack).getChargeCostMultiplier());
} }
public static int getTargetDeaths(ItemStack stack) { @Nullable
if (!isTotem(stack)) { return -1; } public static Integer getTargetDeaths(ItemStack stack) {
return stack.getOrCreateTag().getInt(TAG_TARGET_DEATHS); return getTotemData(stack).map(TotemData::getTargetDeaths).orElse(null);
} }
public static void setTargetDeaths(ItemStack stack, ServerPlayer target) { public static void setTargetDeaths(ItemStack stack, ServerPlayer target) {
if (!isTotem(stack)) { return; } Optional<TotemData> data = getTotemData(stack);
stack.getOrCreateTag().putInt(TAG_TARGET_DEATHS, target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS))); if (data.isEmpty()) { return; }
data.get().setTargetDeaths(target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS)));
stack.set(ModDataComponents.TOTEM_DATA.get(), data.get());
} }
public static boolean isTotemFull(ItemStack stack) { public static boolean isTotemFull(ItemStack stack) {