Skip to content

Commit cc33aa7

Browse files
Fix xp, health, and food level desyncing when setting cape
1 parent 1f6206a commit cc33aa7

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/main/java/xyz/eclipseisoffline/capecommand/CapeCommand.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
1414
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
1515
import net.minecraft.entity.Entity;
16-
import net.minecraft.entity.player.PlayerPosition;
1716
import net.minecraft.network.PacketByteBuf;
1817
import net.minecraft.network.codec.PacketCodec;
1918
import net.minecraft.network.packet.CustomPayload;
@@ -28,12 +27,12 @@
2827
import net.minecraft.server.world.ServerChunkLoadingManager;
2928
import net.minecraft.text.Text;
3029
import net.minecraft.util.Identifier;
31-
import net.minecraft.util.math.Vec3d;
3230
import org.slf4j.Logger;
3331
import org.slf4j.LoggerFactory;
3432
import xyz.eclipseisoffline.capecommand.mixin.EntityAccessor;
3533
import xyz.eclipseisoffline.capecommand.mixin.ServerChunkLoadingManagerAccessor;
3634
import xyz.eclipseisoffline.capecommand.mixin.ServerConfigurationNetworkHandlerAccessor;
35+
import xyz.eclipseisoffline.capecommand.mixin.ServerPlayerEntityAccessor;
3736

3837
import java.util.List;
3938

@@ -68,13 +67,19 @@ public void onInitialize() {
6867

6968
ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
7069
if (cape.requiresClient() && !CONFIG.hasCapeCommand(player)) {
71-
throw new SimpleCommandExceptionType(Text.of("This cape requires you to install the Cape Command mod locally.")).create();
70+
throw new SimpleCommandExceptionType(Text.of("This cape requires you to install the Cape Command mod locally")).create();
7271
}
7372

7473
CONFIG.setPlayerCape(context.getSource().getPlayerOrThrow().getGameProfile(), cape);
7574

7675
reloadPlayerSkin(context.getSource());
77-
context.getSource().sendFeedback(() -> Text.of("Cape saved. Note that this cape is only visible to you and other players that have Cape Command installed."), true);
76+
context.getSource().sendFeedback(() -> Text.of("Now wearing cape \"" + capeString.toLowerCase() + "\""), true);
77+
78+
if (CONFIG.isGeyserAvailable()) {
79+
context.getSource().sendFeedback(() -> Text.of("Note that this cape is only visible to you, bedrock players, and other Java players that have Cape Command installed"), false);
80+
} else {
81+
context.getSource().sendFeedback(() -> Text.of("Note that this cape is only visible to you and other players that have Cape Command installed"), false);
82+
}
7883

7984
return 0;
8085
}))
@@ -158,6 +163,9 @@ private void reloadPlayerSkin(ServerCommandSource source) throws CommandSyntaxEx
158163
// Client clears these when respawning
159164
source.getServer().getPlayerManager().sendPlayerStatus(player);
160165
source.getServer().getPlayerManager().sendStatusEffects(player);
166+
((ServerPlayerEntityAccessor) player).setSyncedExperience(-1);
167+
((ServerPlayerEntityAccessor) player).setSyncedHealth(-1.0F);
168+
((ServerPlayerEntityAccessor) player).setSyncedFoodLevel(-1);
161169

162170
continue;
163171
}

src/main/java/xyz/eclipseisoffline/capecommand/CapeConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public void unregisterCapeCommandPlayer(ServerPlayerEntity serverPlayerEntity) {
6262
capeCommandPlayers.remove(serverPlayerEntity.getGameProfile());
6363
}
6464

65+
public boolean isGeyserAvailable() {
66+
return geyserAvailable;
67+
}
68+
6569
private void writeToConfig() {
6670
JsonObject capesJson = new JsonObject();
6771
for (Entry<UUID, Cape> playerCape : playerCapes.entrySet()) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package xyz.eclipseisoffline.capecommand.mixin;
2+
3+
import net.minecraft.server.network.ServerPlayerEntity;
4+
import org.spongepowered.asm.mixin.Mixin;
5+
import org.spongepowered.asm.mixin.gen.Accessor;
6+
7+
@Mixin(ServerPlayerEntity.class)
8+
public interface ServerPlayerEntityAccessor {
9+
10+
@Accessor("syncedExperience")
11+
void setSyncedExperience(int syncedExperience);
12+
13+
@Accessor("syncedHealth")
14+
void setSyncedHealth(float syncedHealth);
15+
16+
@Accessor("syncedFoodLevel")
17+
void setSyncedFoodLevel(int syncedFoodLevel);
18+
}

src/main/resources/capecommand.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"ServerChunkLoadingManagerAccessor",
1010
"ServerConfigurationNetworkHandlerAccessor",
1111
"ServerPlayNetworkHandlerMixin",
12+
"ServerPlayerEntityAccessor",
1213
"TextureUrlCheckerMixin"
1314
],
1415
"client": [

0 commit comments

Comments
 (0)