@@ -36,43 +36,49 @@ import net.minecraft.nbt.Tag;
3636 <#if w.hasVariablesOfScope("PLAYER_LIFETIME") || w.hasVariablesOfScope("PLAYER_PERSISTENT") >
3737 @SubscribeEvent public static void onPlayerLoggedInSyncPlayerVariables(PlayerEvent.PlayerLoggedInEvent event) {
3838 if (event.getEntity() instanceof ServerPlayer player)
39- ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(player.getCapability(PLAYER_VARIABLES).orElseGet(() -> null )));
39+ player.getCapability(PLAYER_VARIABLES).ifPresent(capability -> ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(capability )));
4040 }
4141
4242 @SubscribeEvent public static void onPlayerRespawnedSyncPlayerVariables(PlayerEvent.PlayerRespawnEvent event) {
4343 if (event.getEntity() instanceof ServerPlayer player)
44- ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(player.getCapability(PLAYER_VARIABLES).orElseGet(() -> null )));
44+ player.getCapability(PLAYER_VARIABLES).ifPresent(capability -> ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(capability )));
4545 }
4646
4747 @SubscribeEvent public static void onPlayerChangedDimensionSyncPlayerVariables(PlayerEvent.PlayerChangedDimensionEvent event) {
4848 if (event.getEntity() instanceof ServerPlayer player)
49- ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(player.getCapability(PLAYER_VARIABLES).orElseGet(() -> null )));
49+ player.getCapability(PLAYER_VARIABLES).ifPresent(capability -> ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(capability )));
5050 }
5151
5252 @SubscribeEvent public static void onPlayerTickUpdateSyncPlayerVariables(TickEvent.PlayerTickEvent event) {
53- if (event.phase == TickEvent.Phase.END && event.player instanceof ServerPlayer player && player.getCapability(PLAYER_VARIABLES).orElseGet(PlayerVariables::new)._syncDirty) {
54- ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(player.getCapability(PLAYER_VARIABLES).orElseGet(() -> null)));
55- player.getCapability(PLAYER_VARIABLES).ifPresent(capability -> capability._syncDirty = false);
53+ if (event.phase == TickEvent.Phase.END && event.player instanceof ServerPlayer player) {
54+ player.getCapability(PLAYER_VARIABLES).ifPresent(capability -> {
55+ if (capability._syncDirty) {
56+ ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerVariablesSyncMessage(capability));
57+ capability._syncDirty = false;
58+ }
59+ });
5660 }
5761 }
5862
5963 @SubscribeEvent public static void clonePlayer(PlayerEvent.Clone event) {
60- event.getOriginal().revive();
61-
62- PlayerVariables original = event.getOriginal().getCapability(PLAYER_VARIABLES).orElseGet(PlayerVariables::new);
63- PlayerVariables clone = event.getEntity().getCapability(PLAYER_VARIABLES).orElseGet(PlayerVariables::new);
64- <#list variables as var >
65- <#if var.getScope().name() == "PLAYER_PERSISTENT" >
66- clone.${var.getName()} = original.${var.getName()} ;
67- </#if >
68- </#list >
69- if(!event.isWasDeath()) {
70- <#list variables as var >
71- <#if var.getScope().name() == "PLAYER_LIFETIME" >
72- clone.${var.getName()} = original.${var.getName()} ;
73- </#if >
74- </#list >
75- }
64+ event.getOriginal().revive();
65+
66+ event.getOriginal().getCapability(PLAYER_VARIABLES).ifPresent(original -> {
67+ event.getEntity().getCapability(PLAYER_VARIABLES).ifPresent(clone -> {
68+ <#list variables as var >
69+ <#if var.getScope().name() == "PLAYER_PERSISTENT" >
70+ clone.${var.getName()} = original.${var.getName()} ;
71+ </#if >
72+ </#list >
73+ if(!event.isWasDeath()) {
74+ <#list variables as var >
75+ <#if var.getScope().name() == "PLAYER_LIFETIME" >
76+ clone.${var.getName()} = original.${var.getName()} ;
77+ </#if >
78+ </#list >
79+ }
80+ });
81+ });
7682 }
7783 </#if >
7884
@@ -90,7 +96,7 @@ import net.minecraft.nbt.Tag;
9096
9197 @SubscribeEvent public static void onPlayerChangedDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
9298 if (event.getEntity() instanceof ServerPlayer player) {
93- SavedData worlddata = WorldVariables.get(event.getEntity() .level());
99+ SavedData worlddata = WorldVariables.get(player .level());
94100 if(worlddata != null)
95101 ${JavaModName} .PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new SavedDataSyncMessage(1, worlddata));
96102 }
0 commit comments