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

View File

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