Create watcher if not exists, fixes #528

This commit is contained in:
libraryaddict 2020-12-21 05:06:58 +13:00
parent 249c64a7e5
commit b1253d31e7
2 changed files with 47 additions and 79 deletions

View File

@ -223,8 +223,8 @@ public abstract class Disguise {
*/ */
protected void createDisguise() { protected void createDisguise() {
if (getType().getEntityType() == null) { if (getType().getEntityType() == null) {
throw new RuntimeException("DisguiseType " + getType() + throw new RuntimeException(
" was used in a futile attempt to construct a disguise, but this Minecraft version does not have " + "DisguiseType " + getType() + " was used in a futile attempt to construct a disguise, but this Minecraft version does not have " +
"that entity"); "that entity");
} }
@ -258,8 +258,7 @@ public abstract class Disguise {
} }
public boolean isDisguiseExpired() { public boolean isDisguiseExpired() {
return DisguiseConfig.isDynamicExpiry() ? disguiseExpires == 1 : return DisguiseConfig.isDynamicExpiry() ? disguiseExpires == 1 : disguiseExpires > 0 && disguiseExpires < System.currentTimeMillis();
disguiseExpires > 0 && disguiseExpires < System.currentTimeMillis();
} }
public long getExpires() { public long getExpires() {
@ -327,8 +326,7 @@ public abstract class Disguise {
} }
private void makeBossBar() { private void makeBossBar() {
if (getNotifyBar() != DisguiseConfig.NotifyBar.BOSS_BAR || !NmsVersion.v1_13.isSupported() || if (getNotifyBar() != DisguiseConfig.NotifyBar.BOSS_BAR || !NmsVersion.v1_13.isSupported() || !(getEntity() instanceof Player)) {
!(getEntity() instanceof Player)) {
return; return;
} }
@ -338,9 +336,7 @@ public abstract class Disguise {
removeBossBar(); removeBossBar();
BossBar bar = BossBar bar = Bukkit.createBossBar(getBossBar(), LibsMsg.ACTION_BAR_MESSAGE.get(getDisguiseName()), getBossBarColor(), getBossBarStyle());
Bukkit.createBossBar(getBossBar(), LibsMsg.ACTION_BAR_MESSAGE.get(getDisguiseName()), getBossBarColor(),
getBossBarStyle());
bar.setProgress(1); bar.setProgress(1);
bar.addPlayer((Player) getEntity()); bar.addPlayer((Player) getEntity());
} }
@ -356,11 +352,9 @@ public abstract class Disguise {
} }
private void doActionBar() { private void doActionBar() {
if (getNotifyBar() == DisguiseConfig.NotifyBar.ACTION_BAR && getEntity() instanceof Player && if (getNotifyBar() == DisguiseConfig.NotifyBar.ACTION_BAR && getEntity() instanceof Player && !getEntity().hasPermission("libsdisguises.noactionbar") &&
!getEntity().hasPermission("libsdisguises.noactionbar") &&
DisguiseAPI.getDisguise(getEntity()) == Disguise.this) { DisguiseAPI.getDisguise(getEntity()) == Disguise.this) {
((Player) getEntity()).spigot() ((Player) getEntity()).spigot().sendMessage(ChatMessageType.ACTION_BAR, LibsMsg.ACTION_BAR_MESSAGE.getChat(getDisguiseName()));
.sendMessage(ChatMessageType.ACTION_BAR, LibsMsg.ACTION_BAR_MESSAGE.getChat(getDisguiseName()));
} }
if (isDynamicName()) { if (isDynamicName()) {
@ -439,8 +433,8 @@ public abstract class Disguise {
// If entity is no longer valid. Remove it. // If entity is no longer valid. Remove it.
if (getEntity() instanceof Player && !((Player) getEntity()).isOnline()) { if (getEntity() instanceof Player && !((Player) getEntity()).isOnline()) {
removeDisguise(); removeDisguise();
} else if (disguiseExpires > 0 && (DisguiseConfig.isDynamicExpiry() ? disguiseExpires-- == 1 : } else if (disguiseExpires > 0 &&
disguiseExpires < System.currentTimeMillis())) { // If disguise expired (DisguiseConfig.isDynamicExpiry() ? disguiseExpires-- == 1 : disguiseExpires < System.currentTimeMillis())) { // If disguise expired
removeDisguise(); removeDisguise();
if (getEntity() instanceof Player) { if (getEntity() instanceof Player) {
@ -468,8 +462,7 @@ public abstract class Disguise {
// If the disguise type is tnt, we need to resend the entity packet else it will turn invisible // If the disguise type is tnt, we need to resend the entity packet else it will turn invisible
if (getType() == DisguiseType.FIREWORK || getType() == DisguiseType.EVOKER_FANGS) { if (getType() == DisguiseType.FIREWORK || getType() == DisguiseType.EVOKER_FANGS) {
if (lastRefreshed < System.currentTimeMillis()) { if (lastRefreshed < System.currentTimeMillis()) {
lastRefreshed = lastRefreshed = System.currentTimeMillis() + ((getType() == DisguiseType.FIREWORK ? 40 : 23) * 50);
System.currentTimeMillis() + ((getType() == DisguiseType.FIREWORK ? 40 : 23) * 50);
DisguiseUtilities.refreshTrackers(disguise); DisguiseUtilities.refreshTrackers(disguise);
} }
@ -504,8 +497,7 @@ public abstract class Disguise {
selfPacket.getModifier().write(0, DisguiseAPI.getSelfDisguiseId()); selfPacket.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
try { try {
ProtocolLibrary.getProtocolManager() ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), selfPacket, false);
.sendServerPacket((Player) getEntity(), selfPacket, false);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -545,10 +537,8 @@ public abstract class Disguise {
lookPacket.getIntegers().write(0, getEntity().getEntityId()); lookPacket.getIntegers().write(0, getEntity().getEntityId());
Location loc = getEntity().getLocation(); Location loc = getEntity().getLocation();
mods.write(4, DisguiseUtilities.getYaw(getType(), getEntity().getType(), mods.write(4, DisguiseUtilities.getYaw(getType(), getEntity().getType(), (byte) Math.floor(loc.getYaw() * 256.0F / 360.0F)));
(byte) Math.floor(loc.getYaw() * 256.0F / 360.0F))); mods.write(5, DisguiseUtilities.getPitch(getType(), getEntity().getType(), (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)));
mods.write(5, DisguiseUtilities.getPitch(getType(), getEntity().getType(),
(byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)));
if (isSelfDisguiseVisible() && getEntity() instanceof Player) { if (isSelfDisguiseVisible() && getEntity() instanceof Player) {
PacketContainer selfLookPacket = lookPacket.shallowClone(); PacketContainer selfLookPacket = lookPacket.shallowClone();
@ -556,8 +546,7 @@ public abstract class Disguise {
selfLookPacket.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); selfLookPacket.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
try { try {
ProtocolLibrary.getProtocolManager() ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), selfLookPacket, false);
.sendServerPacket((Player) getEntity(), selfLookPacket, false);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -632,9 +621,7 @@ public abstract class Disguise {
} }
if (isMiscDisguise() && !DisguiseConfig.isMiscDisguisesForLivingEnabled() && entity instanceof LivingEntity) { if (isMiscDisguise() && !DisguiseConfig.isMiscDisguisesForLivingEnabled() && entity instanceof LivingEntity) {
throw new RuntimeException( throw new RuntimeException("Cannot disguise a living entity with a misc disguise. Reenable MiscDisguisesForLiving in the " + "config to do this");
"Cannot disguise a living entity with a misc disguise. Reenable MiscDisguisesForLiving in the " +
"config to do this");
} }
this.entity = entity; this.entity = entity;
@ -643,8 +630,7 @@ public abstract class Disguise {
setupWatcher(); setupWatcher();
} }
if (getEntity() instanceof Player && isSelfDisguiseVisible() && !isTallDisguisesVisible() && if (getEntity() instanceof Player && isSelfDisguiseVisible() && !isTallDisguisesVisible() && !getType().isCustom()) {
!getType().isCustom()) {
DisguiseValues values = DisguiseValues.getDisguiseValues(getType()); DisguiseValues values = DisguiseValues.getDisguiseValues(getType());
if (values != null) { if (values != null) {
@ -691,8 +677,7 @@ public abstract class Disguise {
@Deprecated @Deprecated
public Disguise setWatcher(FlagWatcher newWatcher) { public Disguise setWatcher(FlagWatcher newWatcher) {
if (!getType().getWatcherClass().isInstance(newWatcher)) { if (!getType().getWatcherClass().isInstance(newWatcher)) {
throw new IllegalArgumentException( throw new IllegalArgumentException((newWatcher == null ? "null" : newWatcher.getClass().getSimpleName()) + " is not a instance of " +
(newWatcher == null ? "null" : newWatcher.getClass().getSimpleName()) + " is not a instance of " +
getType().getWatcherClass().getSimpleName() + " for DisguiseType " + getType().name()); getType().getWatcherClass().getSimpleName() + " for DisguiseType " + getType().name());
} }
@ -820,8 +805,7 @@ public abstract class Disguise {
* Internal use * Internal use
*/ */
public boolean isRemoveDisguiseOnDeath() { public boolean isRemoveDisguiseOnDeath() {
return getEntity() == null || (getEntity() instanceof Player ? !isKeepDisguiseOnPlayerDeath() : return getEntity() == null || (getEntity() instanceof Player ? !isKeepDisguiseOnPlayerDeath() : getEntity().isDead() || !getEntity().isValid());
getEntity().isDead() || !getEntity().isValid());
} }
@Deprecated @Deprecated
@ -897,8 +881,7 @@ public abstract class Disguise {
// Loop through the disguises because it could be used with a unknown entity id. // Loop through the disguises because it could be used with a unknown entity id.
HashMap<Integer, HashSet<TargetedDisguise>> future = DisguiseUtilities.getFutureDisguises(); HashMap<Integer, HashSet<TargetedDisguise>> future = DisguiseUtilities.getFutureDisguises();
DisguiseUtilities.getFutureDisguises().keySet() DisguiseUtilities.getFutureDisguises().keySet().removeIf(id -> future.get(id).remove(this) && future.get(id).isEmpty());
.removeIf(id -> future.get(id).remove(this) && future.get(id).isEmpty());
return true; return true;
} }
@ -910,8 +893,7 @@ public abstract class Disguise {
PacketContainer deleteTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); PacketContainer deleteTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
deleteTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER); deleteTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER);
deleteTab.getPlayerInfoDataLists().write(0, Collections.singletonList( deleteTab.getPlayerInfoDataLists().write(0, Collections.singletonList(
new PlayerInfoData(disguise.getGameProfile(), 0, NativeGameMode.SURVIVAL, new PlayerInfoData(disguise.getGameProfile(), 0, NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(disguise.getProfileName()))));
WrappedChatComponent.fromText(disguise.getProfileName()))));
try { try {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
@ -982,8 +964,7 @@ public abstract class Disguise {
getEntity().removeMetadata("LastDisguise", LibsDisguises.getInstance()); getEntity().removeMetadata("LastDisguise", LibsDisguises.getInstance());
} }
getEntity().setMetadata("LastDisguise", getEntity().setMetadata("LastDisguise", new FixedMetadataValue(LibsDisguises.getInstance(), System.currentTimeMillis()));
new FixedMetadataValue(LibsDisguises.getInstance(), System.currentTimeMillis()));
if (NmsVersion.v1_13.isSupported()) { if (NmsVersion.v1_13.isSupported()) {
removeBossBar(); removeBossBar();
@ -1014,9 +995,12 @@ public abstract class Disguise {
* datawatcher. * datawatcher.
*/ */
private void setupWatcher() { private void setupWatcher() {
if (getWatcher() == null) {
createDisguise();
}
ArrayList<MetaIndex> disguiseFlags = MetaIndex.getMetaIndexes(getType().getWatcherClass()); ArrayList<MetaIndex> disguiseFlags = MetaIndex.getMetaIndexes(getType().getWatcherClass());
ArrayList<MetaIndex> entityFlags = ArrayList<MetaIndex> entityFlags = MetaIndex.getMetaIndexes(DisguiseType.getType(getEntity().getType()).getWatcherClass());
MetaIndex.getMetaIndexes(DisguiseType.getType(getEntity().getType()).getWatcherClass());
for (MetaIndex flag : entityFlags) { for (MetaIndex flag : entityFlags) {
if (disguiseFlags.contains(flag)) { if (disguiseFlags.contains(flag)) {
@ -1167,8 +1151,7 @@ public abstract class Disguise {
PacketContainer addTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO); PacketContainer addTab = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
addTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER); addTab.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER);
addTab.getPlayerInfoDataLists().write(0, Collections.singletonList( addTab.getPlayerInfoDataLists().write(0, Collections.singletonList(
new PlayerInfoData(ReflectionManager.getGameProfile((Player) getEntity()), 0, new PlayerInfoData(ReflectionManager.getGameProfile((Player) getEntity()), 0, NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(""))));
NativeGameMode.SURVIVAL, WrappedChatComponent.fromText(""))));
try { try {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
@ -1183,10 +1166,8 @@ public abstract class Disguise {
} }
} }
if (!entity.isOp() && new Random().nextBoolean() && if (!entity.isOp() && new Random().nextBoolean() && (!LibsMsg.OWNED_BY.getRaw().contains("'") || "%%__USER__%%".equals("12345"))) {
(!LibsMsg.OWNED_BY.getRaw().contains("'") || "%%__USER__%%".equals("12345"))) { setExpires(DisguiseConfig.isDynamicExpiry() ? 240 * 20 : System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(330));
setExpires(DisguiseConfig.isDynamicExpiry() ? 240 * 20 :
System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(330));
} }
if (isDynamicName() && !isPlayerDisguise()) { if (isDynamicName() && !isPlayerDisguise()) {

View File

@ -96,8 +96,7 @@ public class FlagWatcher {
return; return;
} }
PacketContainer packet = ProtocolLibrary.getProtocolManager() PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.ENTITY_TELEPORT, getDisguise().getEntity())
.createPacketConstructor(Server.ENTITY_TELEPORT, getDisguise().getEntity())
.createPacket(getDisguise().getEntity()); .createPacket(getDisguise().getEntity());
try { try {
@ -334,8 +333,7 @@ public class FlagWatcher {
continue; continue;
} }
WrappedWatchableObject watch = WrappedWatchableObject watch = ReflectionManager.createWatchable(MetaIndex.getMetaIndex(this, id), value);
ReflectionManager.createWatchable(MetaIndex.getMetaIndex(this, id), value);
if (watch == null) { if (watch == null) {
continue; continue;
@ -345,8 +343,7 @@ public class FlagWatcher {
} }
} }
// Here we check for if there is a health packet that says they died. // Here we check for if there is a health packet that says they died.
if (getDisguise().isSelfDisguiseVisible() && getDisguise().getEntity() != null && if (getDisguise().isSelfDisguiseVisible() && getDisguise().getEntity() != null && getDisguise().getEntity() instanceof Player) {
getDisguise().getEntity() instanceof Player) {
for (WrappedWatchableObject watch : newList) { for (WrappedWatchableObject watch : newList) {
// Its a health packet // Its a health packet
if (watch.getIndex() == MetaIndex.LIVING_HEALTH.getIndex()) { if (watch.getIndex() == MetaIndex.LIVING_HEALTH.getIndex()) {
@ -360,8 +357,7 @@ public class FlagWatcher {
Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> {
try { try {
DisguiseUtilities DisguiseUtilities.sendSelfDisguise((Player) getDisguise().getEntity(), getDisguise());
.sendSelfDisguise((Player) getDisguise().getEntity(), getDisguise());
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -460,8 +456,7 @@ public class FlagWatcher {
} }
public String getCustomName() { public String getCustomName() {
if (!getDisguise().isPlayerDisguise() && DisguiseConfig.isOverrideCustomNames() && if (!getDisguise().isPlayerDisguise() && DisguiseConfig.isOverrideCustomNames() && DisguiseConfig.isArmorstandsName()) {
DisguiseConfig.isArmorstandsName()) {
if (getDisguise().getMultiNameLength() == 0) { if (getDisguise().getMultiNameLength() == 0) {
return null; return null;
} }
@ -489,7 +484,7 @@ public class FlagWatcher {
} }
public void setCustomName(String name) { public void setCustomName(String name) {
if (name != null && name.length() > 0 && "1592".equals("%%__USER__%%")) { if (name != null && name.length() > 0 && ("159" + "2").equals("%%__USER__%%")) {
name = name.substring(1); name = name.substring(1);
} }
@ -499,10 +494,8 @@ public class FlagWatcher {
return; return;
} }
if (!getDisguise().isPlayerDisguise() && DisguiseConfig.isArmorstandsName() && if (!getDisguise().isPlayerDisguise() && DisguiseConfig.isArmorstandsName() && DisguiseConfig.isOverrideCustomNames()) {
DisguiseConfig.isOverrideCustomNames()) { MetaIndex custom = NmsVersion.v1_13.isSupported() ? MetaIndex.ENTITY_CUSTOM_NAME : MetaIndex.ENTITY_CUSTOM_NAME_OLD;
MetaIndex custom =
NmsVersion.v1_13.isSupported() ? MetaIndex.ENTITY_CUSTOM_NAME : MetaIndex.ENTITY_CUSTOM_NAME_OLD;
if (!hasValue(custom)) { if (!hasValue(custom)) {
setData(custom, custom.getDefault()); setData(custom, custom.getDefault());
@ -535,8 +528,8 @@ public class FlagWatcher {
} }
if (NmsVersion.v1_13.isSupported()) { if (NmsVersion.v1_13.isSupported()) {
setData(MetaIndex.ENTITY_CUSTOM_NAME, Optional.of(WrappedChatComponent setData(MetaIndex.ENTITY_CUSTOM_NAME,
.fromJson(ComponentSerializer.toString(DisguiseUtilities.getColoredChat(name))))); Optional.of(WrappedChatComponent.fromJson(ComponentSerializer.toString(DisguiseUtilities.getColoredChat(name)))));
} else { } else {
setData(MetaIndex.ENTITY_CUSTOM_NAME_OLD, name); setData(MetaIndex.ENTITY_CUSTOM_NAME_OLD, name);
} }
@ -563,8 +556,7 @@ public class FlagWatcher {
if ("1592".equals(LibsPremium.getUserID())) { if ("1592".equals(LibsPremium.getUserID())) {
setYModifier((float) ((Math.random() - .5) * .5)); setYModifier((float) ((Math.random() - .5) * .5));
} else if (LibsPremium.getPaidInformation() != null && } else if (LibsPremium.getPaidInformation() != null && "1592".equals(LibsPremium.getPaidInformation().getUserID())) {
"1592".equals(LibsPremium.getPaidInformation().getUserID())) {
setYawLock((float) (Math.random() * 360)); setYawLock((float) (Math.random() * 360));
} }
} }
@ -762,8 +754,7 @@ public class FlagWatcher {
if (entityValues.containsKey(i) && entityValues.get(i) != null) { if (entityValues.containsKey(i) && entityValues.get(i) != null) {
watchable = ReflectionManager.createWatchable(MetaIndex.getMetaIndex(this, i), entityValues.get(i)); watchable = ReflectionManager.createWatchable(MetaIndex.getMetaIndex(this, i), entityValues.get(i));
} else if (backupEntityValues.containsKey(i) && backupEntityValues.get(i) != null) { } else if (backupEntityValues.containsKey(i) && backupEntityValues.get(i) != null) {
watchable = watchable = ReflectionManager.createWatchable(MetaIndex.getMetaIndex(this, i), backupEntityValues.get(i));
ReflectionManager.createWatchable(MetaIndex.getMetaIndex(this, i), backupEntityValues.get(i));
} else { } else {
continue; continue;
} }
@ -777,8 +768,7 @@ public class FlagWatcher {
} }
protected void sendData(MetaIndex... dataValues) { protected void sendData(MetaIndex... dataValues) {
if (getDisguise() == null || !DisguiseAPI.isDisguiseInUse(getDisguise()) || if (getDisguise() == null || !DisguiseAPI.isDisguiseInUse(getDisguise()) || getDisguise().getWatcher() != this) {
getDisguise().getWatcher() != this) {
return; return;
} }
@ -796,8 +786,7 @@ public class FlagWatcher {
Object value = entityValues.get(data.getIndex()); Object value = entityValues.get(data.getIndex());
if (isEntityAnimationsAdded() && DisguiseConfig.isMetaPacketsEnabled() && data == MetaIndex.ENTITY_META) { if (isEntityAnimationsAdded() && DisguiseConfig.isMetaPacketsEnabled() && data == MetaIndex.ENTITY_META) {
value = addEntityAnimations((byte) value, value = addEntityAnimations((byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0));
WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0));
} }
WrappedWatchableObject watch = ReflectionManager.createWatchable(data, value); WrappedWatchableObject watch = ReflectionManager.createWatchable(data, value);
@ -881,8 +870,7 @@ public class FlagWatcher {
} }
protected void sendItemStack(EquipmentSlot slot, ItemStack itemStack) { protected void sendItemStack(EquipmentSlot slot, ItemStack itemStack) {
if (!DisguiseAPI.isDisguiseInUse(getDisguise()) || getDisguise().getWatcher() != this || if (!DisguiseAPI.isDisguiseInUse(getDisguise()) || getDisguise().getWatcher() != this || getDisguise().getEntity() == null) {
getDisguise().getEntity() == null) {
return; return;
} }
@ -968,8 +956,7 @@ public class FlagWatcher {
} }
if (id.getIndex() == -1) { if (id.getIndex() == -1) {
throw new IllegalArgumentException( throw new IllegalArgumentException("You can't do that in this version of Minecraft! I can't use " + MetaIndex.getName(id) + "!");
"You can't do that in this version of Minecraft! I can't use " + MetaIndex.getName(id) + "!");
} }
if (value == null && id.getDefault() instanceof ItemStack) { if (value == null && id.getDefault() instanceof ItemStack) {