Fixed setSkin not working properly

This commit is contained in:
libraryaddict 2016-12-01 14:34:34 +13:00
parent d58c47542e
commit 2c5055924c

View File

@ -1,360 +1,360 @@
package me.libraryaddict.disguise.disguisetypes; package me.libraryaddict.disguise.disguisetypes;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode; import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode;
import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction; import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction;
import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.PlayerInfoData;
import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsProfileLookup; import me.libraryaddict.disguise.utilities.LibsProfileLookup;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.ReflectionManager;
public class PlayerDisguise extends TargetedDisguise { public class PlayerDisguise extends TargetedDisguise {
private LibsProfileLookup currentLookup; private LibsProfileLookup currentLookup;
private WrappedGameProfile gameProfile; private WrappedGameProfile gameProfile;
private String playerName; private String playerName;
private String skinToUse; private String skinToUse;
private UUID uuid = UUID.randomUUID(); private UUID uuid = UUID.randomUUID();
private PlayerDisguise() { private PlayerDisguise() {
super(DisguiseType.PLAYER); super(DisguiseType.PLAYER);
} }
public PlayerDisguise(Player player) { public PlayerDisguise(Player player) {
this(ReflectionManager.getGameProfile(player)); this(ReflectionManager.getGameProfile(player));
} }
public PlayerDisguise(Player player, Player skinToUse) { public PlayerDisguise(Player player, Player skinToUse) {
this(ReflectionManager.getGameProfile(player), ReflectionManager.getGameProfile(skinToUse)); this(ReflectionManager.getGameProfile(player), ReflectionManager.getGameProfile(skinToUse));
} }
public PlayerDisguise(String name) { public PlayerDisguise(String name) {
this(); this();
setName(name); setName(name);
setSkin(name); setSkin(name);
createDisguise(); createDisguise();
} }
public PlayerDisguise(String name, String skinToUse) { public PlayerDisguise(String name, String skinToUse) {
this(); this();
setName(name); setName(name);
setSkin(skinToUse); setSkin(skinToUse);
createDisguise(); createDisguise();
} }
public PlayerDisguise(WrappedGameProfile gameProfile) { public PlayerDisguise(WrappedGameProfile gameProfile) {
this(); this();
setName(gameProfile.getName()); setName(gameProfile.getName());
this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, gameProfile.getName(), gameProfile); this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, gameProfile.getName(), gameProfile);
createDisguise(); createDisguise();
} }
public PlayerDisguise(WrappedGameProfile gameProfile, WrappedGameProfile skinToUse) { public PlayerDisguise(WrappedGameProfile gameProfile, WrappedGameProfile skinToUse) {
this(); this();
setName(gameProfile.getName()); setName(gameProfile.getName());
this.gameProfile = ReflectionManager.getGameProfile(uuid, gameProfile.getName()); this.gameProfile = ReflectionManager.getGameProfile(uuid, gameProfile.getName());
setSkin(skinToUse); setSkin(skinToUse);
createDisguise(); createDisguise();
} }
@Override @Override
public PlayerDisguise addPlayer(Player player) { public PlayerDisguise addPlayer(Player player) {
return (PlayerDisguise) super.addPlayer(player); return (PlayerDisguise) super.addPlayer(player);
} }
@Override @Override
public PlayerDisguise addPlayer(String playername) { public PlayerDisguise addPlayer(String playername) {
return (PlayerDisguise) super.addPlayer(playername); return (PlayerDisguise) super.addPlayer(playername);
} }
@Override @Override
public PlayerDisguise clone() { public PlayerDisguise clone() {
PlayerDisguise disguise = new PlayerDisguise(); PlayerDisguise disguise = new PlayerDisguise();
disguise.playerName = getName(); disguise.playerName = getName();
if (disguise.currentLookup == null && disguise.gameProfile != null) { if (disguise.currentLookup == null && disguise.gameProfile != null) {
disguise.skinToUse = getSkin(); disguise.skinToUse = getSkin();
disguise.gameProfile = ReflectionManager.getGameProfileWithThisSkin(disguise.uuid, getGameProfile().getName(), disguise.gameProfile = ReflectionManager.getGameProfileWithThisSkin(disguise.uuid, getGameProfile().getName(),
getGameProfile()); getGameProfile());
} }
else { else {
disguise.setSkin(getSkin()); disguise.setSkin(getSkin());
} }
disguise.setReplaceSounds(isSoundsReplaced()); disguise.setReplaceSounds(isSoundsReplaced());
disguise.setViewSelfDisguise(isSelfDisguiseVisible()); disguise.setViewSelfDisguise(isSelfDisguiseVisible());
disguise.setHearSelfDisguise(isSelfDisguiseSoundsReplaced()); disguise.setHearSelfDisguise(isSelfDisguiseSoundsReplaced());
disguise.setHideArmorFromSelf(isHidingArmorFromSelf()); disguise.setHideArmorFromSelf(isHidingArmorFromSelf());
disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf()); disguise.setHideHeldItemFromSelf(isHidingHeldItemFromSelf());
disguise.setVelocitySent(isVelocitySent()); disguise.setVelocitySent(isVelocitySent());
disguise.setModifyBoundingBox(isModifyBoundingBox()); disguise.setModifyBoundingBox(isModifyBoundingBox());
if (getWatcher() != null) { if (getWatcher() != null) {
disguise.setWatcher(getWatcher().clone(disguise)); disguise.setWatcher(getWatcher().clone(disguise));
} }
disguise.createDisguise(); disguise.createDisguise();
return disguise; return disguise;
} }
public WrappedGameProfile getGameProfile() { public WrappedGameProfile getGameProfile() {
if (gameProfile == null) { if (gameProfile == null) {
if (getSkin() != null) { if (getSkin() != null) {
gameProfile = ReflectionManager.getGameProfile(uuid, getName()); gameProfile = ReflectionManager.getGameProfile(uuid, getName());
} }
else { else {
gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getName(), gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getName(),
DisguiseUtilities.getProfileFromMojang(this)); DisguiseUtilities.getProfileFromMojang(this));
} }
} }
return gameProfile; return gameProfile;
} }
public String getName() { public String getName() {
return playerName; return playerName;
} }
public String getSkin() { public String getSkin() {
return skinToUse; return skinToUse;
} }
@Override @Override
public PlayerWatcher getWatcher() { public PlayerWatcher getWatcher() {
return (PlayerWatcher) super.getWatcher(); return (PlayerWatcher) super.getWatcher();
} }
public boolean isDisplayedInTab() { public boolean isDisplayedInTab() {
return getWatcher().isDisplayedInTab(); return getWatcher().isDisplayedInTab();
} }
@Override @Override
public boolean isPlayerDisguise() { public boolean isPlayerDisguise() {
return true; return true;
} }
@Override @Override
public PlayerDisguise removePlayer(Player player) { public PlayerDisguise removePlayer(Player player) {
return (PlayerDisguise) super.removePlayer(player); return (PlayerDisguise) super.removePlayer(player);
} }
@Override @Override
public PlayerDisguise removePlayer(String playername) { public PlayerDisguise removePlayer(String playername) {
return (PlayerDisguise) super.removePlayer(playername); return (PlayerDisguise) super.removePlayer(playername);
} }
@Override @Override
public PlayerDisguise setDisguiseTarget(TargetType newTargetType) { public PlayerDisguise setDisguiseTarget(TargetType newTargetType) {
return (PlayerDisguise) super.setDisguiseTarget(newTargetType); return (PlayerDisguise) super.setDisguiseTarget(newTargetType);
} }
public void setDisplayedInTab(boolean showPlayerInTab) { public void setDisplayedInTab(boolean showPlayerInTab) {
getWatcher().setDisplayedInTab(showPlayerInTab); getWatcher().setDisplayedInTab(showPlayerInTab);
} }
@Override @Override
public PlayerDisguise setEntity(Entity entity) { public PlayerDisguise setEntity(Entity entity) {
return (PlayerDisguise) super.setEntity(entity); return (PlayerDisguise) super.setEntity(entity);
} }
public void setGameProfile(WrappedGameProfile gameProfile) { public void setGameProfile(WrappedGameProfile gameProfile) {
this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, gameProfile.getName(), gameProfile); this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, gameProfile.getName(), gameProfile);
} }
@Override @Override
public PlayerDisguise setHearSelfDisguise(boolean hearSelfDisguise) { public PlayerDisguise setHearSelfDisguise(boolean hearSelfDisguise) {
return (PlayerDisguise) super.setHearSelfDisguise(hearSelfDisguise); return (PlayerDisguise) super.setHearSelfDisguise(hearSelfDisguise);
} }
@Override @Override
public PlayerDisguise setHideArmorFromSelf(boolean hideArmor) { public PlayerDisguise setHideArmorFromSelf(boolean hideArmor) {
return (PlayerDisguise) super.setHideArmorFromSelf(hideArmor); return (PlayerDisguise) super.setHideArmorFromSelf(hideArmor);
} }
@Override @Override
public PlayerDisguise setHideHeldItemFromSelf(boolean hideHeldItem) { public PlayerDisguise setHideHeldItemFromSelf(boolean hideHeldItem) {
return (PlayerDisguise) super.setHideHeldItemFromSelf(hideHeldItem); return (PlayerDisguise) super.setHideHeldItemFromSelf(hideHeldItem);
} }
@Override @Override
public PlayerDisguise setKeepDisguiseOnEntityDespawn(boolean keepDisguise) { public PlayerDisguise setKeepDisguiseOnEntityDespawn(boolean keepDisguise) {
return (PlayerDisguise) super.setKeepDisguiseOnEntityDespawn(keepDisguise); return (PlayerDisguise) super.setKeepDisguiseOnEntityDespawn(keepDisguise);
} }
@Override @Override
public PlayerDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) { public PlayerDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) {
return (PlayerDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise); return (PlayerDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise);
} }
@Override @Override
public PlayerDisguise setKeepDisguiseOnPlayerLogout(boolean keepDisguise) { public PlayerDisguise setKeepDisguiseOnPlayerLogout(boolean keepDisguise) {
return (PlayerDisguise) super.setKeepDisguiseOnPlayerLogout(keepDisguise); return (PlayerDisguise) super.setKeepDisguiseOnPlayerLogout(keepDisguise);
} }
@Override @Override
public PlayerDisguise setModifyBoundingBox(boolean modifyBox) { public PlayerDisguise setModifyBoundingBox(boolean modifyBox) {
return (PlayerDisguise) super.setModifyBoundingBox(modifyBox); return (PlayerDisguise) super.setModifyBoundingBox(modifyBox);
} }
private void setName(String name) { private void setName(String name) {
if (name.length() > 16) { if (name.length() > 16) {
name = name.substring(0, 16); name = name.substring(0, 16);
} }
playerName = name; playerName = name;
} }
@Override @Override
public PlayerDisguise setReplaceSounds(boolean areSoundsReplaced) { public PlayerDisguise setReplaceSounds(boolean areSoundsReplaced) {
return (PlayerDisguise) super.setReplaceSounds(areSoundsReplaced); return (PlayerDisguise) super.setReplaceSounds(areSoundsReplaced);
} }
public PlayerDisguise setSkin(String newSkin) { public PlayerDisguise setSkin(String newSkin) {
if (newSkin != null && newSkin.length() > 50) { if (newSkin != null && newSkin.length() > 50) {
try { try {
setSkin(ReflectionManager.parseGameProfile(newSkin)); return setSkin(ReflectionManager.parseGameProfile(newSkin));
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The skin is too long to be a playername, but cannot be parsed to a GameProfile!"); "The skin is too long to be a playername, but cannot be parsed to a GameProfile!");
} }
} }
skinToUse = newSkin; skinToUse = newSkin;
if (newSkin == null) { if (newSkin == null) {
currentLookup = null; currentLookup = null;
gameProfile = null; gameProfile = null;
} }
else { else {
if (newSkin.length() > 16) { if (newSkin.length() > 16) {
skinToUse = newSkin.substring(0, 16); skinToUse = newSkin.substring(0, 16);
} }
currentLookup = new LibsProfileLookup() { currentLookup = new LibsProfileLookup() {
@Override @Override
public void onLookup(WrappedGameProfile gameProfile) { public void onLookup(WrappedGameProfile gameProfile) {
if (currentLookup != this || gameProfile == null) if (currentLookup != this || gameProfile == null)
return; return;
setSkin(gameProfile); setSkin(gameProfile);
currentLookup = null; currentLookup = null;
} }
}; };
WrappedGameProfile gameProfile = DisguiseUtilities.getProfileFromMojang(this.skinToUse, currentLookup, WrappedGameProfile gameProfile = DisguiseUtilities.getProfileFromMojang(this.skinToUse, currentLookup,
LibsDisguises.getInstance().getConfig().getBoolean("ContactMojangServers", true)); LibsDisguises.getInstance().getConfig().getBoolean("ContactMojangServers", true));
if (gameProfile != null) { if (gameProfile != null) {
setSkin(gameProfile); setSkin(gameProfile);
} }
} }
return this; return this;
} }
/** /**
* Set the GameProfile, without tampering. * Set the GameProfile, without tampering.
* *
* @param gameProfile * @param gameProfile
* GameProfile * GameProfile
* @return * @return
*/ */
public PlayerDisguise setSkin(WrappedGameProfile gameProfile) { public PlayerDisguise setSkin(WrappedGameProfile gameProfile) {
if (gameProfile == null) { if (gameProfile == null) {
this.gameProfile = null; this.gameProfile = null;
this.skinToUse = null; this.skinToUse = null;
return this; return this;
} }
Validate.notEmpty(gameProfile.getName(), "Name must be set"); Validate.notEmpty(gameProfile.getName(), "Name must be set");
currentLookup = null; currentLookup = null;
this.skinToUse = gameProfile.getName(); this.skinToUse = gameProfile.getName();
this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getName(), gameProfile); this.gameProfile = ReflectionManager.getGameProfileWithThisSkin(uuid, getName(), gameProfile);
if (DisguiseUtilities.isDisguiseInUse(this)) { if (DisguiseUtilities.isDisguiseInUse(this)) {
if (isDisplayedInTab()) { if (isDisplayedInTab()) {
PacketContainer addTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); PacketContainer addTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
addTab.getPlayerInfoAction().write(0, PlayerInfoAction.ADD_PLAYER); addTab.getPlayerInfoAction().write(0, PlayerInfoAction.ADD_PLAYER);
addTab.getPlayerInfoDataLists().write(0, Arrays.asList(new PlayerInfoData(getGameProfile(), 0, addTab.getPlayerInfoDataLists().write(0, Arrays.asList(new PlayerInfoData(getGameProfile(), 0,
NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(getName())))); NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(getName()))));
PacketContainer deleteTab = addTab.shallowClone(); PacketContainer deleteTab = addTab.shallowClone();
deleteTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER); deleteTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER);
try { try {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
if (!((TargetedDisguise) this).canSee(player)) if (!((TargetedDisguise) this).canSee(player))
continue; continue;
ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab); ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab);
ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab); ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab);
} }
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
DisguiseUtilities.refreshTrackers(this); DisguiseUtilities.refreshTrackers(this);
} }
return this; return this;
} }
@Override @Override
public PlayerDisguise setVelocitySent(boolean sendVelocity) { public PlayerDisguise setVelocitySent(boolean sendVelocity) {
return (PlayerDisguise) super.setVelocitySent(sendVelocity); return (PlayerDisguise) super.setVelocitySent(sendVelocity);
} }
@Override @Override
public PlayerDisguise setViewSelfDisguise(boolean viewSelfDisguise) { public PlayerDisguise setViewSelfDisguise(boolean viewSelfDisguise) {
return (PlayerDisguise) super.setViewSelfDisguise(viewSelfDisguise); return (PlayerDisguise) super.setViewSelfDisguise(viewSelfDisguise);
} }
@Override @Override
public PlayerDisguise setWatcher(FlagWatcher newWatcher) { public PlayerDisguise setWatcher(FlagWatcher newWatcher) {
return (PlayerDisguise) super.setWatcher(newWatcher); return (PlayerDisguise) super.setWatcher(newWatcher);
} }
@Override @Override
public PlayerDisguise silentlyAddPlayer(String playername) { public PlayerDisguise silentlyAddPlayer(String playername) {
return (PlayerDisguise) super.silentlyAddPlayer(playername); return (PlayerDisguise) super.silentlyAddPlayer(playername);
} }
@Override @Override
public PlayerDisguise silentlyRemovePlayer(String playername) { public PlayerDisguise silentlyRemovePlayer(String playername) {
return (PlayerDisguise) super.silentlyRemovePlayer(playername); return (PlayerDisguise) super.silentlyRemovePlayer(playername);
} }
} }