-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ProfileEditorImpl.java
136 lines (128 loc) · 7.26 KB
/
ProfileEditorImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.denizenscript.denizen.nms.v1_19.impl;
import com.denizenscript.denizen.nms.abstracts.ProfileEditor;
import com.denizenscript.denizen.nms.v1_19.Handler;
import com.denizenscript.denizen.nms.v1_19.helpers.PacketHelperImpl;
import com.denizenscript.denizen.nms.v1_19.impl.network.handlers.DenizenNetworkManagerImpl;
import com.denizenscript.denizen.scripts.commands.entity.RenameCommand;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.ChatColor;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerLevel;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_19_R2.CraftServer;
import org.bukkit.craftbukkit.v1_19_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import java.lang.invoke.MethodHandle;
import java.util.List;
import java.util.UUID;
public class ProfileEditorImpl extends ProfileEditor {
@Override
protected void updatePlayer(final Player player, final boolean isSkinChanging) {
final ServerPlayer entityPlayer = ((CraftPlayer) player).getHandle();
final UUID uuid = player.getUniqueId();
ClientboundPlayerInfoUpdatePacket playerInfo = new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, entityPlayer);
for (Player otherPlayer : Bukkit.getServer().getOnlinePlayers()) {
PacketHelperImpl.send(otherPlayer, playerInfo);
}
for (Player otherPlayer : NMSHandler.entityHelper.getPlayersThatSee(player)) {
if (!otherPlayer.getUniqueId().equals(uuid)) {
PacketHelperImpl.forceRespawnPlayerEntity(player, otherPlayer);
}
}
if (isSkinChanging) {
((CraftServer) Bukkit.getServer()).getHandle().respawn(entityPlayer, (ServerLevel) entityPlayer.level, true, player.getLocation(), false);
}
player.updateInventory();
}
public static boolean handleAlteredProfiles(ClientboundPlayerInfoUpdatePacket packet, DenizenNetworkManagerImpl manager) {
if (ProfileEditor.mirrorUUIDs.isEmpty() && !RenameCommand.hasAnyDynamicRenames()) {
return true;
}
ClientboundPlayerInfoUpdatePacket.Action action = packet.actions();
if (action != ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER && action != ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME) {
return true;
}
List<ClientboundPlayerInfoUpdatePacket.Entry> dataList = packet.entries();
if (dataList == null) {
return true;
}
try {
boolean any = false;
for (ClientboundPlayerInfoUpdatePacket.Entry data : dataList) {
if (ProfileEditor.mirrorUUIDs.contains(data.profile().getId()) || RenameCommand.customNames.containsKey(data.profile().getId())) {
any = true;
}
}
if (!any) {
return true;
}
GameProfile ownProfile = manager.player.getGameProfile();
for (ClientboundPlayerInfoUpdatePacket.Entry data : dataList) {
if (!ProfileEditor.mirrorUUIDs.contains(data.profile().getId()) && !RenameCommand.customNames.containsKey(data.profile().getId())) {
ClientboundPlayerInfoUpdatePacket newPacket = new ClientboundPlayerInfoUpdatePacket(action);
List<ClientboundPlayerInfoUpdatePacket.Entry> newPacketDataList = newPacket.entries();
newPacketDataList.add(data);
manager.oldManager.send(newPacket);
}
else {
String rename = RenameCommand.getCustomNameFor(data.profile().getId(), manager.player.getBukkitEntity(), false);
ClientboundPlayerInfoUpdatePacket newPacket = new ClientboundPlayerInfoUpdatePacket(action);
List<ClientboundPlayerInfoUpdatePacket.Entry> newPacketDataList = newPacket.entries();
GameProfile patchedProfile = new GameProfile(data.profile().getId(), rename != null ? (rename.length() > 16 ? rename.substring(0, 16) : rename) : data.profile().getName());
if (ProfileEditor.mirrorUUIDs.contains(data.profile().getId())) {
patchedProfile.getProperties().putAll(ownProfile.getProperties());
}
else {
patchedProfile.getProperties().putAll(data.profile().getProperties());
}
String listRename = RenameCommand.getCustomNameFor(data.profile().getId(), manager.player.getBukkitEntity(), true);
Component displayName = listRename != null ? Handler.componentToNMS(FormattedTextHelper.parse(listRename, ChatColor.WHITE)) : data.displayName();
ClientboundPlayerInfoUpdatePacket.Entry newData = new ClientboundPlayerInfoUpdatePacket.Entry(data.profileId(), patchedProfile, data.listed(), data.latency(), data.gameMode(), displayName, data.chatSession());
newPacketDataList.add(newData);
manager.oldManager.send(newPacket);
}
}
return false;
}
catch (Exception e) {
Debug.echoError(e);
return true;
}
}
public static void updatePlayerProfiles(ClientboundPlayerInfoUpdatePacket packet) {
ClientboundPlayerInfoUpdatePacket.Action action = packet.actions();
if (action != ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER) {
return;
}
List<ClientboundPlayerInfoUpdatePacket.Entry> dataList = packet.entries();
if (dataList != null) {
try {
for (ClientboundPlayerInfoUpdatePacket.Entry data : dataList) {
GameProfile gameProfile = data.profile();
if (fakeProfiles.containsKey(gameProfile.getId())) {
playerInfoData_gameProfile_Setter.invoke(data, getGameProfile(fakeProfiles.get(gameProfile.getId())));
}
}
}
catch (Throwable e) {
Debug.echoError(e);
}
}
}
private static GameProfile getGameProfile(PlayerProfile playerProfile) {
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
if (playerProfile.hasTexture()) {
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
}
return gameProfile;
}
public static final MethodHandle playerInfoData_gameProfile_Setter = ReflectionHelper.getFinalSetterForFirstOfType(ClientboundPlayerInfoUpdatePacket.Entry.class, GameProfile.class);
}