/
ProfileEditorImpl.java
179 lines (168 loc) · 8.48 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
package com.denizenscript.denizen.nms.v1_16.impl;
import com.denizenscript.denizen.nms.abstracts.ProfileEditor;
import com.denizenscript.denizen.nms.v1_16.helpers.PacketHelperImpl;
import com.denizenscript.denizen.nms.v1_16.impl.network.handlers.DenizenNetworkManagerImpl;
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.denizen.nms.util.ReflectionHelper;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.server.v1_16_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_16_R1.CraftServer;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
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 handleMirrorProfiles(PacketPlayOutPlayerInfo packet, DenizenNetworkManagerImpl manager) {
if (ProfileEditor.mirrorUUIDs.isEmpty()) {
return true;
}
PacketPlayOutPlayerInfo.EnumPlayerInfoAction action = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "a", packet);
if (action != PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER) {
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())) {
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())) {
PacketPlayOutPlayerInfo newPacket = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
List newPacketDataList = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "b", newPacket);
newPacketDataList.add(data);
manager.oldManager.sendPacket(newPacket);
}
else {
PacketPlayOutPlayerInfo newPacket = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER);
List newPacketDataList = ReflectionHelper.getFieldValue(PacketPlayOutPlayerInfo.class, "b", newPacket);
GameProfile patchedProfile = new GameProfile(gameProfile.getId(), gameProfile.getName());
patchedProfile.getProperties().putAll(ownProfile.getProperties());
Object newData = playerInfoData_construct.newInstance(newPacket, patchedProfile,
playerInfoData_latency.getInt(data), playerInfoData_gamemode.get(data), playerInfoData_displayName.get(data));
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.set(data, getGameProfile(fakeProfiles.get(gameProfile.getId())));
}
}
}
catch (Exception 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 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;
}
}