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

View File

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