-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ProfileEditorImpl.java
197 lines (185 loc) · 10.1 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.denizenscript.denizen.nms.v1_17.impl;
import com.denizenscript.denizen.nms.abstracts.ProfileEditor;
import com.denizenscript.denizen.nms.v1_17.Handler;
import com.denizenscript.denizen.nms.v1_17.helpers.PacketHelperImpl;
import com.denizenscript.denizen.nms.v1_17.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.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.ChatColor;
import net.minecraft.network.chat.IChatBaseComponent;
import net.minecraft.network.protocol.game.PacketPlayOutEntityDestroy;
import net.minecraft.network.protocol.game.PacketPlayOutNamedEntitySpawn;
import net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo;
import net.minecraft.server.level.EntityPlayer;
import net.minecraft.server.level.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_17_R1.CraftServer;
import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;
public class ProfileEditorImpl extends ProfileEditor {
@Override
protected void updatePlayer(Player player, final boolean isSkinChanging) {
final EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
final UUID uuid = player.getUniqueId();
PacketPlayOutEntityDestroy destroyPacket = new PacketPlayOutEntityDestroy(entityPlayer.getId());
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (!p.getUniqueId().equals(uuid)) {
PacketHelperImpl.sendPacket(p, destroyPacket);
}
}
new BukkitRunnable() {
@Override
public void run() {
PacketPlayOutPlayerInfo playerInfo = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, entityPlayer);
PacketPlayOutNamedEntitySpawn spawnPacket = new PacketPlayOutNamedEntitySpawn(entityPlayer);
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
PacketHelperImpl.sendPacket(player, playerInfo);
if (!player.getUniqueId().equals(uuid)) {
PacketHelperImpl.sendPacket(player, spawnPacket);
}
else {
if (isSkinChanging) {
((CraftServer) Bukkit.getServer()).getHandle().moveToWorld(entityPlayer, (WorldServer) entityPlayer.world, true, player.getLocation(), false);
}
player.updateInventory();
}
}
}
}.runTaskLater(NMSHandler.getJavaPlugin(), 5);
}
public static boolean handleAlteredProfiles(PacketPlayOutPlayerInfo packet, DenizenNetworkManagerImpl manager) {
if (ProfileEditor.mirrorUUIDs.isEmpty() && !RenameCommand.hasAnyDynamicRenames()) {
return true;
}
PacketPlayOutPlayerInfo.EnumPlayerInfoAction action = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "a", packet);
if (action != PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER && action != PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_DISPLAY_NAME) {
return true;
}
List dataList = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "b", packet);
if (dataList == null) {
return true;
}
try {
boolean any = false;
for (Object data : dataList) {
GameProfile gameProfile = (GameProfile) playerInfoData_gameProfile.get(data);
if (ProfileEditor.mirrorUUIDs.contains(gameProfile.getId()) || RenameCommand.customNames.containsKey(gameProfile.getId())) {
any = true;
}
}
if (!any) {
return true;
}
GameProfile ownProfile = manager.player.getProfile();
for (Object data : dataList) {
GameProfile gameProfile = (GameProfile) playerInfoData_gameProfile.get(data);
if (!ProfileEditor.mirrorUUIDs.contains(gameProfile.getId()) && !RenameCommand.customNames.containsKey(gameProfile.getId())) {
PacketPlayOutPlayerInfo newPacket = new PacketPlayOutPlayerInfo(action);
List newPacketDataList = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "b", newPacket);
newPacketDataList.add(data);
manager.oldManager.sendPacket(newPacket);
}
else {
String rename = RenameCommand.getCustomNameFor(gameProfile.getId(), manager.player.getBukkitEntity(), false);
PacketPlayOutPlayerInfo newPacket = new PacketPlayOutPlayerInfo(action);
List newPacketDataList = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "b", newPacket);
GameProfile patchedProfile = new GameProfile(gameProfile.getId(), rename != null ? (rename.length() > 16 ? rename.substring(0, 16) : rename) : gameProfile.getName());
if (ProfileEditor.mirrorUUIDs.contains(gameProfile.getId())) {
patchedProfile.getProperties().putAll(ownProfile.getProperties());
}
else {
patchedProfile.getProperties().putAll(gameProfile.getProperties());
}
String listRename = RenameCommand.getCustomNameFor(gameProfile.getId(), manager.player.getBukkitEntity(), true);
IChatBaseComponent displayName = listRename != null ? Handler.componentToNMS(FormattedTextHelper.parse(listRename, ChatColor.WHITE)) : (IChatBaseComponent) playerInfoData_displayName.get(data);
Object newData = playerInfoData_construct.newInstance(newPacket, patchedProfile, playerInfoData_latency.getInt(data), playerInfoData_gamemode.get(data), displayName);
newPacketDataList.add(newData);
manager.oldManager.sendPacket(newPacket);
}
}
return false;
}
catch (Exception e) {
Debug.echoError(e);
return true;
}
}
public static void updatePlayerProfiles(PacketPlayOutPlayerInfo packet) {
PacketPlayOutPlayerInfo.EnumPlayerInfoAction action = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "a", packet);
if (action != PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER) {
return;
}
List<?> dataList = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "b", packet);
if (dataList != null) {
try {
for (Object data : dataList) {
GameProfile gameProfile = (GameProfile) playerInfoData_gameProfile.get(data);
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());
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
return gameProfile;
}
public static final Class playerInfoData;
public static final Field playerInfoData_latency,
playerInfoData_gamemode,
playerInfoData_gameProfile,
playerInfoData_displayName;
public static final MethodHandle playerInfoData_gameProfile_Setter;
public static final Constructor playerInfoData_construct;
static {
Class pid = null;
Field pidLatency = null, pidGamemode = null, pidGameProfile = null, pidDisplayName = null;
Constructor pidConstruct = null;
try {
for (Class clzz : PacketPlayOutPlayerInfo.class.getDeclaredClasses()) {
if (CoreUtilities.toLowerCase(clzz.getName()).contains("infodata")) { // PlayerInfoData.
pid = clzz;
pidLatency = clzz.getDeclaredField("b");
pidLatency.setAccessible(true);
pidGamemode = clzz.getDeclaredField("c");
pidGamemode.setAccessible(true);
pidGameProfile = clzz.getDeclaredField("d");
pidGameProfile.setAccessible(true);
pidDisplayName = clzz.getDeclaredField("e");
pidDisplayName.setAccessible(true);
pidConstruct = pid.getDeclaredConstructors()[0];
pidConstruct.setAccessible(true);
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
playerInfoData = pid;
playerInfoData_latency = pidLatency;
playerInfoData_gamemode = pidGamemode;
playerInfoData_gameProfile = pidGameProfile;
playerInfoData_displayName = pidDisplayName;
playerInfoData_construct = pidConstruct;
playerInfoData_gameProfile_Setter = ReflectionHelper.getFinalSetter(pid, "d");
}
}