WIP /username not showing in player list fix

current problem is display name, gamemode and latency not working but it shows in the player list now at least
This commit is contained in:
Chayapak 2023-07-04 18:59:30 +07:00
parent 49327ce22e
commit 143a47162d
5 changed files with 77 additions and 8 deletions

View file

@ -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;

View file

@ -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<ClientDynamicRegistryType> combinedDynamicRegistries;
@Inject(method = "<init>", 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(); }
}

View file

@ -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);
}

View file

@ -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);

View file

@ -24,7 +24,8 @@
"DecoratedPotBlockEntitySherdsMixin",
"TextMixin",
"ClientConnectionInvoker",
"ClientConnectionAccessor"
"ClientConnectionAccessor",
"PlayerListEntryAccessor"
],
"injectors": {
"defaultRequire": 1