diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java index acb74c0..6608a03 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java @@ -2,6 +2,7 @@ package land.chipmunk.chipmunkmod.mixin; import net.minecraft.client.network.PlayerListEntry; import net.minecraft.network.ClientConnection; +import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import net.minecraft.text.Text; diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java index 9cd2470..7debdfd 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java @@ -1,11 +1,19 @@ package land.chipmunk.chipmunkmod.mixin; +import com.mojang.authlib.GameProfile; import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.command.CommandManager; import land.chipmunk.chipmunkmod.modules.*; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.network.ClientDynamicRegistryType; +import net.minecraft.client.network.PlayerListEntry; +import net.minecraft.client.network.ServerInfo; +import net.minecraft.client.util.telemetry.WorldSession; import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.network.ClientConnection; import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; +import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket; import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket; import net.minecraft.registry.CombinedDynamicRegistries; import net.minecraft.resource.featuretoggle.FeatureSet; @@ -20,6 +28,11 @@ public class ClientPlayNetworkHandlerMixin { @Shadow private FeatureSet enabledFeatures; @Shadow private CombinedDynamicRegistries combinedDynamicRegistries; + @Inject(method = "", at = @At("TAIL")) + private void init (MinecraftClient client, Screen screen, ClientConnection connection, ServerInfo serverInfo, GameProfile profile, WorldSession worldSession, CallbackInfo ci) { + + } + @Inject(method = "onGameJoin", at = @At("TAIL")) private void onGameJoin (GameJoinS2CPacket packet, CallbackInfo ci) { final CommandRegistryAccess commandRegistryAccess = CommandRegistryAccess.of(this.combinedDynamicRegistries.getCombinedRegistryManager(), this.enabledFeatures); @@ -33,7 +46,5 @@ public class ClientPlayNetworkHandlerMixin { } @Inject(method = "onPlayerRemove", at = @At("HEAD"), cancellable = true) - private void onPlayerRemove (PlayerRemoveS2CPacket packet, CallbackInfo ci) { - ci.cancel(); - } + private void onPlayerRemove (PlayerRemoveS2CPacket packet, CallbackInfo ci) { ci.cancel(); } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/PlayerListEntryAccessor.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/PlayerListEntryAccessor.java new file mode 100644 index 0000000..39faf9a --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/PlayerListEntryAccessor.java @@ -0,0 +1,16 @@ +package land.chipmunk.chipmunkmod.mixin; + +import net.minecraft.client.network.PlayerListEntry; +import net.minecraft.world.GameMode; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(PlayerListEntry.class) +public interface PlayerListEntryAccessor { + @Accessor("gameMode") + void setGameMode (GameMode gameMode); + + @Accessor("latency") + void setLatency (int latency); +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java b/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java index e162342..1e21707 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java @@ -3,11 +3,11 @@ package land.chipmunk.chipmunkmod.modules; import com.mojang.brigadier.Message; import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; -import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.data.MutablePlayerListEntry; import land.chipmunk.chipmunkmod.listeners.Listener; import land.chipmunk.chipmunkmod.listeners.ListenerManager; import land.chipmunk.chipmunkmod.mixin.ClientPlayNetworkHandlerAccessor; +import land.chipmunk.chipmunkmod.mixin.PlayerListEntryAccessor; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.PlayerListEntry; import net.minecraft.network.packet.Packet; @@ -118,7 +118,9 @@ public class Players extends Listener { list.remove(duplicate); } - list.add(new MutablePlayerListEntry(newEntry)); + final MutablePlayerListEntry entry = new MutablePlayerListEntry(newEntry); + + list.add(entry); } catch (Exception e) { e.printStackTrace(); } @@ -130,6 +132,14 @@ public class Players extends Listener { if (target == null) return; target.gamemode = newEntry.gameMode(); + + final ClientPlayNetworkHandlerAccessor accessor = ((ClientPlayNetworkHandlerAccessor) MinecraftClient.getInstance().getNetworkHandler()); + + if (accessor == null) return; + + final PlayerListEntryAccessor entryAccessor = (PlayerListEntryAccessor) accessor.playerListEntries().get(newEntry.profile().getId()); + + entryAccessor.setGameMode(newEntry.gameMode()); } catch (Exception e) { e.printStackTrace(); } @@ -140,6 +150,14 @@ public class Players extends Listener { if (target == null) return; target.latency = newEntry.latency(); + + final ClientPlayNetworkHandlerAccessor accessor = ((ClientPlayNetworkHandlerAccessor) MinecraftClient.getInstance().getNetworkHandler()); + + if (accessor == null) return; + + final PlayerListEntryAccessor entryAccessor = (PlayerListEntryAccessor) accessor.playerListEntries().get(newEntry.profile().getId()); + + entryAccessor.setLatency(newEntry.latency()); } private void updateDisplayName (PlayerListS2CPacket.Entry newEntry) { @@ -147,6 +165,12 @@ public class Players extends Listener { if (target == null) return; target.displayName = newEntry.displayName(); + + final ClientPlayNetworkHandlerAccessor accessor = ((ClientPlayNetworkHandlerAccessor) MinecraftClient.getInstance().getNetworkHandler()); + + if (accessor == null) return; + + accessor.playerListEntries().get(newEntry.profile().getId()).setDisplayName(newEntry.displayName()); } private void removePlayer (UUID uuid) { @@ -155,7 +179,6 @@ public class Players extends Listener { if (target == null) return; if (!serverHasCommand("scoreboard")) { - ChipmunkMod.LOGGER.warn("Server doesn't have /scoreboard, so not showing vanished players."); removeFromPlayerList(uuid); return; } @@ -179,9 +202,14 @@ public class Players extends Listener { list.remove(target); - // TODO: fix players using /username gone from the player list, the cause is exactly at the next line and it's because uuid i guess removeFromPlayerList(uuid); + for (MutablePlayerListEntry entry : list) { + if (!entry.profile.getId().equals(uuid)) continue; + + addToPlayerList(new PlayerListEntry(entry.profile, false)); + } + return packet; }); } catch (Exception e) { @@ -189,6 +217,18 @@ public class Players extends Listener { } } + public void addToPlayerList (PlayerListEntry entry) { + client.getSocialInteractionsManager().setPlayerOnline(entry); + + final ClientPlayNetworkHandlerAccessor accessor = ((ClientPlayNetworkHandlerAccessor) MinecraftClient.getInstance().getNetworkHandler()); + + if (accessor == null) return; + + accessor.playerListEntries().put(entry.getProfile().getId(), entry); + + accessor.listedPlayerListEntries().add(entry); + } + private void removeFromPlayerList (UUID uuid) { client.getSocialInteractionsManager().setPlayerOffline(uuid); diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index d73c2c6..9dd6e9e 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -24,7 +24,8 @@ "DecoratedPotBlockEntitySherdsMixin", "TextMixin", "ClientConnectionInvoker", - "ClientConnectionAccessor" + "ClientConnectionAccessor", + "PlayerListEntryAccessor" ], "injectors": { "defaultRequire": 1