Skip to content

Commit

Permalink
rename per_player: partial player-type entity support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 18, 2020
1 parent a3caf80 commit f54b7e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Expand Up @@ -278,6 +278,10 @@ public void forceSpectate(Player player, Entity entity) {
@Override
public void sendRename(Player player, Entity entity, String name) {
try {
if (entity.getType() == EntityType.PLAYER) {
// TODO: player rename somehow
return;
}
PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata();
ENTITY_METADATA_EID_SETTER.invoke(packet, entity.getEntityId());
List<DataWatcher.Item<?>> list = new ArrayList<>();
Expand Down
Expand Up @@ -3,6 +3,7 @@
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.denizenscript.denizen.scripts.commands.entity.RenameCommand;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.NMSHandler;
Expand All @@ -17,6 +18,7 @@
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;
Expand Down Expand Up @@ -46,8 +48,7 @@ public void run() {
}
else {
if (isSkinChanging) {
((CraftServer) Bukkit.getServer()).getHandle().moveToWorld(
entityPlayer, (WorldServer) entityPlayer.world, true, player.getLocation(), false);
((CraftServer) Bukkit.getServer()).getHandle().moveToWorld(entityPlayer, (WorldServer) entityPlayer.world, true, player.getLocation(), false);
}
player.updateInventory();
}
Expand All @@ -56,8 +57,8 @@ public void run() {
}.runTaskLater(NMSHandler.getJavaPlugin(), 5);
}

public static boolean handleMirrorProfiles(PacketPlayOutPlayerInfo packet, DenizenNetworkManagerImpl manager) {
if (ProfileEditor.mirrorUUIDs.isEmpty()) {
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);
Expand All @@ -72,7 +73,7 @@ public static boolean handleMirrorProfiles(PacketPlayOutPlayerInfo packet, Deniz
boolean any = false;
for (Object data : dataList) {
GameProfile gameProfile = (GameProfile) playerInfoData_gameProfile.get(data);
if (ProfileEditor.mirrorUUIDs.contains(gameProfile.getId())) {
if (ProfileEditor.mirrorUUIDs.contains(gameProfile.getId()) || RenameCommand.customNames.containsKey(gameProfile.getId())) {
any = true;
}
}
Expand All @@ -82,19 +83,24 @@ public static boolean handleMirrorProfiles(PacketPlayOutPlayerInfo packet, Deniz
GameProfile ownProfile = manager.player.getProfile();
for (Object data : dataList) {
GameProfile gameProfile = (GameProfile) playerInfoData_gameProfile.get(data);
if (!ProfileEditor.mirrorUUIDs.contains(gameProfile.getId())) {
if (!ProfileEditor.mirrorUUIDs.contains(gameProfile.getId()) && !RenameCommand.customNames.containsKey(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 {
String rename = RenameCommand.getCustomNameFor(gameProfile.getId(), manager.player.getBukkitEntity());
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));
GameProfile patchedProfile = new GameProfile(gameProfile.getId(), rename != null ? rename : gameProfile.getName());
if (ProfileEditor.mirrorUUIDs.contains(gameProfile.getId())) {
patchedProfile.getProperties().putAll(ownProfile.getProperties());
}
else {
patchedProfile.getProperties().putAll(gameProfile.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);
}
Expand All @@ -118,30 +124,33 @@ public static void updatePlayerProfiles(PacketPlayOutPlayerInfo packet) {
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())));
playerInfoData_gameProfile_Setter.invoke(data, getGameProfile(fakeProfiles.get(gameProfile.getId())));
}
}
}
catch (Exception e) {
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()));
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
return gameProfile;
}

public static Field PLAYER_INFO_PLAYERDATA_LIST = ReflectionHelper.getFields(PacketPlayOutPlayerInfo.class).get("b");

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 {
Expand Down Expand Up @@ -175,5 +184,6 @@ private static GameProfile getGameProfile(PlayerProfile playerProfile) {
playerInfoData_gameProfile = pidGameProfile;
playerInfoData_displayName = pidDisplayName;
playerInfoData_construct = pidConstruct;
playerInfoData_gameProfile_Setter = ReflectionHelper.getFinalSetter(pid, "d");
}
}
Expand Up @@ -174,7 +174,6 @@ public void sendPacket(Packet<?> packet, GenericFutureListener<? extends Future<
|| processShowFakeForPacket(packet, genericfuturelistener)) {
return;
}
processMirrorForPacket(packet);
processBlockLightForPacket(packet);
processCustomNameForPacket(packet);
oldManager.sendPacket(packet, genericfuturelistener);
Expand Down Expand Up @@ -423,7 +422,7 @@ public void processFakePlayerSpawn(Entity entity) {
public boolean processMirrorForPacket(Packet<?> packet) {
if (packet instanceof PacketPlayOutPlayerInfo) {
PacketPlayOutPlayerInfo playerInfo = (PacketPlayOutPlayerInfo) packet;
if (!ProfileEditorImpl.handleMirrorProfiles(playerInfo, this)) {
if (!ProfileEditorImpl.handleAlteredProfiles(playerInfo, this)) {
return true;
}
ProfileEditorImpl.updatePlayerProfiles(playerInfo);
Expand Down

0 comments on commit f54b7e8

Please sign in to comment.