Add support for colored hex player names, still has an issue with longer names and splitting not respecting the hex

This commit is contained in:
libraryaddict 2020-07-04 20:48:26 +12:00
parent ca520c819e
commit 1ce0a9a612
3 changed files with 70 additions and 1 deletions

View File

@ -29,7 +29,7 @@ public class PlayerDisguise extends TargetedDisguise {
* Has someone set name visible explicitly?
*/
private boolean explicitNameVisible = false;
private UUID uuid = UUID.randomUUID();
private final UUID uuid = UUID.randomUUID();
private volatile DisguiseUtilities.DScoreTeam scoreboardName;
private PlayerDisguise() {

View File

@ -10,6 +10,7 @@ import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.packets.packetlisteners.*;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -26,6 +27,7 @@ public class PacketsManager {
private static boolean viewDisguisesListenerEnabled;
private static PacketsHandler packetsHandler;
private static PacketListener destroyListener;
private static PacketListener scoreboardTeamListener;
public static void addPacketListeners() {
// Add a client listener to cancel them interacting with uninteractable disguised entitys.
@ -166,6 +168,11 @@ public class PacketsManager {
ProtocolLibrary.getProtocolManager().addPacketListener(mainListener);
ProtocolLibrary.getProtocolManager().addPacketListener(destroyListener);
if (NmsVersion.v1_16.isSupported()) {
scoreboardTeamListener = new PacketListenerScoreboardTeam();
ProtocolLibrary.getProtocolManager().addPacketListener(scoreboardTeamListener);
}
}
}

View File

@ -0,0 +1,62 @@
package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.plugin.Plugin;
import java.util.Set;
/**
* Created by libraryaddict on 4/07/2020.
*/
public class PacketListenerScoreboardTeam extends PacketAdapter {
public PacketListenerScoreboardTeam() {
super(LibsDisguises.getInstance(), PacketType.Play.Server.SCOREBOARD_TEAM);
}
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
String name = packet.getStrings().read(0);
if (!name.startsWith("LD_")) {
return;
}
DisguiseUtilities.DScoreTeam team = null;
loop:
for (Set<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) {
for (Disguise disguise : disguises) {
if (!disguise.isPlayerDisguise() || !((PlayerDisguise) disguise).hasScoreboardName()) {
continue;
}
team = ((PlayerDisguise) disguise).getScoreboardName();
break loop;
}
}
if (team == null) {
return;
}
StructureModifier<WrappedChatComponent> chats = packet.getChatComponents();
BaseComponent[] prefix = DisguiseUtilities.getColoredChat(team.getPrefix());
BaseComponent[] suffix = DisguiseUtilities.getColoredChat(team.getSuffix());
chats.write(1, WrappedChatComponent.fromJson(ComponentSerializer.toString(prefix)));
chats.write(2, WrappedChatComponent.fromJson(ComponentSerializer.toString(suffix)));
}
}