Clean up the disguise runnable a bit for some more sanity, it's so ugly.

This commit is contained in:
libraryaddict 2020-06-13 21:52:52 +12:00
parent e12222ca26
commit 32bab36731
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
3 changed files with 177 additions and 177 deletions

View File

@ -171,6 +171,10 @@ public class LibsDisguises extends JavaPlugin {
new MetricsInitalizer(); new MetricsInitalizer();
} }
private void startTask() {
}
public void unregisterCommands(boolean force) { public void unregisterCommands(boolean force) {
CommandMap map = ReflectionManager.getCommandMap(); CommandMap map = ReflectionManager.getCommandMap();
Map<String, Command> commands = ReflectionManager.getCommands(map); Map<String, Command> commands = ReflectionManager.getCommands(map);

View File

@ -36,6 +36,7 @@ import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar; import org.bukkit.boss.BossBar;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -57,6 +58,7 @@ public abstract class Disguise {
private transient boolean disguiseInUse; private transient boolean disguiseInUse;
private DisguiseType disguiseType; private DisguiseType disguiseType;
private transient BukkitRunnable runnable;
private transient Entity entity; private transient Entity entity;
private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced(); private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced();
private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf(); private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf();
@ -66,8 +68,6 @@ public abstract class Disguise {
private boolean playerHiddenFromTab = DisguiseConfig.isHideDisguisedPlayers(); private boolean playerHiddenFromTab = DisguiseConfig.isHideDisguisedPlayers();
private boolean replaceSounds = DisguiseConfig.isSoundEnabled(); private boolean replaceSounds = DisguiseConfig.isSoundEnabled();
private boolean mobsIgnoreDisguise; private boolean mobsIgnoreDisguise;
private transient BukkitTask task;
private Runnable velocityRunnable;
private boolean velocitySent = DisguiseConfig.isVelocitySent(); private boolean velocitySent = DisguiseConfig.isVelocitySent();
private boolean viewSelfDisguise = DisguiseConfig.isViewDisguises(); private boolean viewSelfDisguise = DisguiseConfig.isViewDisguises();
@Getter @Getter
@ -350,7 +350,36 @@ public abstract class Disguise {
return this; return this;
} }
private void doActionBar() {
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,
new ComponentBuilder(LibsMsg.ACTION_BAR_MESSAGE.get(getDisguiseName())).create());
}
if (isDynamicName()) {
String name = getEntity().getCustomName();
if (name == null) {
name = "";
}
if (isPlayerDisguise()) {
if (!((PlayerDisguise) Disguise.this).getName().equals(name)) {
((PlayerDisguise) Disguise.this).setName(name);
}
} else {
getWatcher().setCustomName(name);
}
}
}
private void createRunnable() { private void createRunnable() {
if (runnable != null) {
runnable.cancel();
}
final boolean alwaysSendVelocity; final boolean alwaysSendVelocity;
switch (getType()) { switch (getType()) {
@ -382,39 +411,24 @@ public abstract class Disguise {
final TargetedDisguise disguise = (TargetedDisguise) this; final TargetedDisguise disguise = (TargetedDisguise) this;
// A scheduler to clean up any unused disguises. // A scheduler to clean up any unused disguises.
velocityRunnable = new Runnable() { runnable = new BukkitRunnable() {
private int blockX, blockY, blockZ, facing; private int blockX, blockY, blockZ, facing;
private int deadTicks = 0; private int deadTicks = 0;
private int refreshDisguise = 0;
private int actionBarTicks = -1; private int actionBarTicks = -1;
private long lastRefreshed;
@Override @Override
public void run() { public void run() {
if (!isDisguiseInUse() || getEntity() == null) {
runnable.cancel();
runnable = null;
return;
}
if (++actionBarTicks % 15 == 0) { if (++actionBarTicks % 15 == 0) {
actionBarTicks = 0; actionBarTicks = 0;
if (getNotifyBar() == DisguiseConfig.NotifyBar.ACTION_BAR && getEntity() instanceof Player && doActionBar();
!getEntity().hasPermission("libsdisguises.noactionbar") &&
DisguiseAPI.getDisguise(getEntity()) == Disguise.this) {
((Player) getEntity()).spigot().sendMessage(ChatMessageType.ACTION_BAR,
new ComponentBuilder(LibsMsg.ACTION_BAR_MESSAGE.get(getDisguiseName())).create());
}
if (isDynamicName()) {
String name = getEntity().getCustomName();
if (name == null) {
name = "";
}
if (isPlayerDisguise()) {
if (!((PlayerDisguise) Disguise.this).getName().equals(name)) {
((PlayerDisguise) Disguise.this).setName(name);
}
} else {
getWatcher().setCustomName(name);
}
}
} }
// If entity is no longer valid. Remove it. // If entity is no longer valid. Remove it.
@ -429,6 +443,8 @@ public abstract class Disguise {
if (getEntity() instanceof Player && expired.length() > 0) { if (getEntity() instanceof Player && expired.length() > 0) {
getEntity().sendMessage(expired); getEntity().sendMessage(expired);
} }
return;
} else if (!getEntity().isValid()) { } else if (!getEntity().isValid()) {
// If it has been dead for 30+ ticks // If it has been dead for 30+ ticks
// This is to ensure that this disguise isn't removed while clients think its the real entity // This is to ensure that this disguise isn't removed while clients think its the real entity
@ -436,43 +452,21 @@ public abstract class Disguise {
// death animation // death animation
// This is probably still a problem for wither and enderdragon deaths. // This is probably still a problem for wither and enderdragon deaths.
if (deadTicks++ > (getType() == DisguiseType.ENDER_DRAGON ? 200 : 20)) { if (deadTicks++ > (getType() == DisguiseType.ENDER_DRAGON ? 200 : 20)) {
deadTicks = 0;
if (isRemoveDisguiseOnDeath()) { if (isRemoveDisguiseOnDeath()) {
removeDisguise(); removeDisguise();
} }
} }
} else {
return;
}
deadTicks = 0; deadTicks = 0;
// 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) { if (getType() == DisguiseType.FIREWORK || getType() == DisguiseType.EVOKER_FANGS) {
refreshDisguise++; if (lastRefreshed < System.currentTimeMillis()) {
lastRefreshed =
if (refreshDisguise == 40) { System.currentTimeMillis() + ((getType() == DisguiseType.FIREWORK ? 40 : 23) * 50);
refreshDisguise = 0;
DisguiseUtilities.refreshTrackers(disguise);
}
} else if (getType() == DisguiseType.EVOKER_FANGS) {
refreshDisguise++;
if (refreshDisguise == 23) {
refreshDisguise = 0;
DisguiseUtilities.refreshTrackers(disguise);
}
} else if (getType() == DisguiseType.ITEM_FRAME) {
Location loc = getEntity().getLocation();
int newFacing = (((int) loc.getYaw() + 720 + 45) / 90) % 4;
if (loc.getBlockX() != blockX || loc.getBlockY() != blockY || loc.getBlockZ() != blockZ ||
newFacing != facing) {
blockX = loc.getBlockX();
blockY = loc.getBlockY();
blockZ = loc.getBlockZ();
facing = newFacing;
DisguiseUtilities.refreshTrackers(disguise); DisguiseUtilities.refreshTrackers(disguise);
} }
@ -486,6 +480,44 @@ public abstract class Disguise {
return; return;
} }
doVelocity(vectorY, alwaysSendVelocity);
if (getType() == DisguiseType.EXPERIENCE_ORB) {
PacketContainer packet = new PacketContainer(Server.REL_ENTITY_MOVE);
packet.getIntegers().write(0, getEntity().getEntityId());
try {
for (Player player : DisguiseUtilities.getPerverts(disguise)) {
if (getEntity() != player) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} else if (!isSelfDisguiseVisible()) {
continue;
}
PacketContainer selfPacket = packet.shallowClone();
selfPacket.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
try {
ProtocolLibrary.getProtocolManager()
.sendServerPacket((Player) getEntity(), selfPacket, false);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
};
runnable.runTaskTimer(LibsDisguises.getInstance(), 1, 1);
}
private void doVelocity(Double vectorY, boolean alwaysSendVelocity) {
// If the vectorY isn't 0. Cos if it is. Then it doesn't want to send any vectors. // If the vectorY isn't 0. Cos if it is. Then it doesn't want to send any vectors.
// If this disguise has velocity sending enabled and the entity is flying. // If this disguise has velocity sending enabled and the entity is flying.
if (isVelocitySent() && vectorY != null && (alwaysSendVelocity || !getEntity().isOnGround())) { if (isVelocitySent() && vectorY != null && (alwaysSendVelocity || !getEntity().isOnGround())) {
@ -495,8 +527,7 @@ public abstract class Disguise {
// head about the // head about the
// if statement. // if statement.
// But it doesn't seem to do anything wrong.. // But it doesn't seem to do anything wrong..
if (vector.getY() != 0 && if (vector.getY() != 0 && !(vector.getY() < 0 && alwaysSendVelocity && getEntity().isOnGround())) {
!(vector.getY() < 0 && alwaysSendVelocity && getEntity().isOnGround())) {
return; return;
} }
@ -504,8 +535,7 @@ public abstract class Disguise {
if (getType() != DisguiseType.EXPERIENCE_ORB || !getEntity().isOnGround()) { if (getType() != DisguiseType.EXPERIENCE_ORB || !getEntity().isOnGround()) {
PacketContainer lookPacket = null; PacketContainer lookPacket = null;
if (getType() == DisguiseType.WITHER_SKULL && if (getType() == DisguiseType.WITHER_SKULL && DisguiseConfig.isWitherSkullPacketsEnabled()) {
DisguiseConfig.isWitherSkullPacketsEnabled()) {
lookPacket = new PacketContainer(Server.ENTITY_LOOK); lookPacket = new PacketContainer(Server.ENTITY_LOOK);
StructureModifier<Object> mods = lookPacket.getModifier(); StructureModifier<Object> mods = lookPacket.getModifier();
@ -542,7 +572,7 @@ public abstract class Disguise {
mods.write(1, (int) (vector.getX() * 8000)); mods.write(1, (int) (vector.getX() * 8000));
mods.write(3, (int) (vector.getZ() * 8000)); mods.write(3, (int) (vector.getZ() * 8000));
for (Player player : DisguiseUtilities.getPerverts(disguise)) { for (Player player : DisguiseUtilities.getPerverts(this)) {
PacketContainer tempVelocityPacket = velocityPacket.shallowClone(); PacketContainer tempVelocityPacket = velocityPacket.shallowClone();
mods = tempVelocityPacket.getIntegers(); mods = tempVelocityPacket.getIntegers();
@ -557,16 +587,13 @@ public abstract class Disguise {
mods.write(0, DisguiseAPI.getSelfDisguiseId()); mods.write(0, DisguiseAPI.getSelfDisguiseId());
} }
mods.write(2, mods.write(2, (int) (8000D * (vectorY * ReflectionManager.getPing(player)) * 0.069D));
(int) (8000D * (vectorY * ReflectionManager.getPing(player)) * 0.069D));
if (lookPacket != null && player != getEntity()) { if (lookPacket != null && player != getEntity()) {
ProtocolLibrary.getProtocolManager() ProtocolLibrary.getProtocolManager().sendServerPacket(player, lookPacket, false);
.sendServerPacket(player, lookPacket, false);
} }
ProtocolLibrary.getProtocolManager() ProtocolLibrary.getProtocolManager().sendServerPacket(player, tempVelocityPacket, false);
.sendServerPacket(player, tempVelocityPacket, false);
} }
} }
catch (Exception e) { catch (Exception e) {
@ -577,36 +604,6 @@ public abstract class Disguise {
// sided to // sided to
// players. // players.
} }
if (getType() == DisguiseType.EXPERIENCE_ORB) {
PacketContainer packet = new PacketContainer(Server.REL_ENTITY_MOVE);
packet.getIntegers().write(0, getEntity().getEntityId());
try {
for (Player player : DisguiseUtilities.getPerverts(disguise)) {
if (getEntity() != player) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} else if (isSelfDisguiseVisible()) {
PacketContainer selfPacket = packet.shallowClone();
selfPacket.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
try {
ProtocolLibrary.getProtocolManager()
.sendServerPacket((Player) getEntity(), selfPacket, false);
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
};
} }
/** /**
@ -821,8 +818,8 @@ public abstract class Disguise {
* Internal use * Internal use
*/ */
public boolean isRemoveDisguiseOnDeath() { public boolean isRemoveDisguiseOnDeath() {
return getEntity() == null || return getEntity() == null || (getEntity() instanceof Player ? !isKeepDisguiseOnPlayerDeath() :
(getEntity() instanceof Player ? !isKeepDisguiseOnPlayerDeath() : getEntity().isDead()); getEntity().isDead() || !getEntity().isValid());
} }
@Deprecated @Deprecated
@ -886,9 +883,9 @@ public abstract class Disguise {
disguiseInUse = false; disguiseInUse = false;
if (task != null) { if (runnable != null) {
task.cancel(); runnable.cancel();
task = null; runnable = null;
} }
// If this disguise hasn't a entity set // If this disguise hasn't a entity set
@ -1122,13 +1119,8 @@ public abstract class Disguise {
disguiseInUse = true; disguiseInUse = true;
if (velocityRunnable == null) {
createRunnable();
}
if (!DisguiseUtilities.isInvalidFile()) { if (!DisguiseUtilities.isInvalidFile()) {
task = Bukkit.getScheduler(). createRunnable();
runTaskTimer(LibsDisguises.getInstance(), velocityRunnable, 1, 1);
} }
if (this instanceof PlayerDisguise) { if (this instanceof PlayerDisguise) {

View File

@ -1280,6 +1280,10 @@ public class DisguiseUtilities {
file.delete(); file.delete();
} }
public static void doDisguiseTick(Disguise disguise) {
}
public static void removeSelfDisguise(Disguise disguise) { public static void removeSelfDisguise(Disguise disguise) {
if (!Bukkit.isPrimaryThread()) if (!Bukkit.isPrimaryThread())
throw new IllegalStateException("Cannot modify disguises on an async thread"); throw new IllegalStateException("Cannot modify disguises on an async thread");