Fixed totem data not saving or synchronizing on a dedicated server.

This commit is contained in:
2026-01-12 03:40:41 +01:00
parent 4bb8e9dca0
commit b03aa3e0ed
2 changed files with 34 additions and 39 deletions

View File

@ -29,11 +29,11 @@ public class TotemData {
TotemData::new TotemData::new
); );
private int targetIndex; private final int targetIndex;
private String targetUUID; private final String targetUUID;
private String targetName; private final String targetName;
private int targetDeaths; private final int targetDeaths;
private int charge; private final int charge;
public TotemData(int targetIndex, String targetUUID, String targetName, int targetDeaths, int charge) { public TotemData(int targetIndex, String targetUUID, String targetName, int targetDeaths, int charge) {
this.targetIndex = targetIndex; this.targetIndex = targetIndex;
@ -46,16 +46,10 @@ public class TotemData {
public int getTargetIndex() { public int getTargetIndex() {
return targetIndex; return targetIndex;
} }
public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}
public String getTargetStringUUID() { public String getTargetStringUUID() {
return targetUUID; return targetUUID;
} }
public void setTargetStringUUID(String targetUUID) {
this.targetUUID = targetUUID;
}
public Optional<UUID> getTargetUUID() { public Optional<UUID> getTargetUUID() {
Optional<UUID> targetUUID = Optional.empty(); Optional<UUID> targetUUID = Optional.empty();
@ -64,30 +58,18 @@ public class TotemData {
} catch (IllegalArgumentException ignored) {} } catch (IllegalArgumentException ignored) {}
return targetUUID; return targetUUID;
} }
public void setTargetUUID(UUID targetUUID) {
this.targetUUID = targetUUID.toString();
}
public String getTargetName() { public String getTargetName() {
return targetName; return targetName;
} }
public void setTargetName(String targetName) {
this.targetName = targetName;
}
public int getTargetDeaths() { public int getTargetDeaths() {
return targetDeaths; return targetDeaths;
} }
public void setTargetDeaths(int targetDeaths) {
this.targetDeaths = targetDeaths;
}
public int getCharge() { public int getCharge() {
return charge; return charge;
} }
public void setCharge(int charge) {
this.charge = charge;
}
@Override @Override
public int hashCode() { public int hashCode() {

View File

@ -86,26 +86,25 @@ public abstract class TotemItem extends Item {
return (stack.getItem() instanceof TotemItem); return (stack.getItem() instanceof TotemItem);
} }
@OnlyIn(Dist.DEDICATED_SERVER)
public static Optional<TotemData> cycleTarget(ItemStack itemStack, PlayerList playerList) { public static Optional<TotemData> cycleTarget(ItemStack itemStack, PlayerList playerList) {
if (!isTotem(itemStack)) return Optional.empty(); if (!isTotem(itemStack)) return Optional.empty();
TotemData totemData = getTotemData(itemStack); TotemData totemData = getTotemData(itemStack);
int targetIndex = totemData.getTargetIndex() + 1; int targetIndex = totemData.getTargetIndex() + 1;
targetIndex = targetIndex > playerList.getPlayerCount() ? 0 : targetIndex; targetIndex = targetIndex >= playerList.getPlayerCount() ? 0 : targetIndex;
ServerPlayer target = playerList.getPlayers().get(targetIndex); ServerPlayer target = playerList.getPlayers().get(targetIndex);
totemData.setTargetIndex(targetIndex); setTotemData(itemStack, new TotemData(
totemData.setTargetUUID(target.getUUID()); targetIndex,
totemData.setTargetName(target.getScoreboardName()); target.getStringUUID(),
totemData.setTargetDeaths(target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS))); target.getScoreboardName(),
target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS)),
setTotemData(itemStack, totemData); totemData.getCharge()
));
return Optional.of(totemData); return Optional.of(totemData);
} }
@OnlyIn(Dist.DEDICATED_SERVER)
public static boolean chargeTotem(ItemStack totemStack, ItemStack chargeStack) { public static boolean chargeTotem(ItemStack totemStack, ItemStack chargeStack) {
if (!isTotem(totemStack)) return false; if (!isTotem(totemStack)) return false;
TotemData totemData = getTotemData(totemStack); TotemData totemData = getTotemData(totemStack);
@ -114,14 +113,17 @@ public abstract class TotemItem extends Item {
if (!totemItem.isCharge(chargeStack)) return false; if (!totemItem.isCharge(chargeStack)) return false;
if (totemItem.isChargeFull(totemData)) return false; if (totemItem.isChargeFull(totemData)) return false;
totemData.setCharge(totemData.getCharge() + 1); setTotemData(totemStack, new TotemData(
totemData.getTargetIndex(),
setTotemData(totemStack, totemData); totemData.getTargetStringUUID(),
totemData.getTargetName(),
totemData.getTargetDeaths(),
totemData.getCharge() + 1
));
chargeStack.setCount(chargeStack.getCount() - 1); chargeStack.setCount(chargeStack.getCount() - 1);
return true; return true;
} }
@OnlyIn(Dist.DEDICATED_SERVER)
public static Component reviveTarget(EquipmentSlot slot, ItemStack itemStack, ServerPlayer user, PlayerList playerList) { public static Component reviveTarget(EquipmentSlot slot, ItemStack itemStack, ServerPlayer user, PlayerList playerList) {
if (!isTotem(itemStack)) return Component.empty(); if (!isTotem(itemStack)) return Component.empty();
TotemData totemData = getTotemData(itemStack); TotemData totemData = getTotemData(itemStack);
@ -138,8 +140,13 @@ public abstract class TotemItem extends Item {
return Component.literal(ChatFormatting.WHITE + "Unable to find player!"); return Component.literal(ChatFormatting.WHITE + "Unable to find player!");
} }
totemData.setTargetDeaths(target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS))); setTotemData(itemStack, new TotemData(
setTotemData(itemStack, totemData); totemData.getTargetIndex(),
totemData.getTargetStringUUID(),
totemData.getTargetName(),
target.getStats().getValue(Stats.CUSTOM.get(Stats.DEATHS)),
totemData.getCharge()
));
if (!target.isSpectator()) { if (!target.isSpectator()) {
return Component.literal(ChatFormatting.GRAY + target.getDisplayName().getString() + ChatFormatting.WHITE + " is not dead!"); return Component.literal(ChatFormatting.GRAY + target.getDisplayName().getString() + ChatFormatting.WHITE + " is not dead!");
@ -164,7 +171,13 @@ public abstract class TotemItem extends Item {
return Component.literal(ChatFormatting.WHITE + "Unable to get your level!"); return Component.literal(ChatFormatting.WHITE + "Unable to get your level!");
} }
totemData.setCharge(totemData.getCharge() - totemItem.getTargetCost(totemData)); setTotemData(itemStack, new TotemData(
totemData.getTargetIndex(),
totemData.getTargetStringUUID(),
totemData.getTargetName(),
totemData.getTargetDeaths(),
totemData.getCharge() - totemItem.getTargetCost(totemData)
));
setTotemData(itemStack, totemData); setTotemData(itemStack, totemData);
itemStack.hurtAndBreak(1, user, slot); itemStack.hurtAndBreak(1, user, slot);