Use schedulers instead of BukkitRunnable. Supports re-disguising!

This commit is contained in:
libraryaddict 2014-04-05 03:40:17 +13:00
parent 1b914b9c7b
commit 31252e896e

@ -43,7 +43,8 @@ public abstract class Disguise {
private boolean keepDisguisePlayerLogout = DisguiseConfig.isKeepDisguiseOnPlayerLogout(); private boolean keepDisguisePlayerLogout = DisguiseConfig.isKeepDisguiseOnPlayerLogout();
private boolean modifyBoundingBox = DisguiseConfig.isModifyBoundingBox(); private boolean modifyBoundingBox = DisguiseConfig.isModifyBoundingBox();
private boolean replaceSounds = DisguiseConfig.isSoundEnabled(); private boolean replaceSounds = DisguiseConfig.isSoundEnabled();
private BukkitRunnable velocityRunnable; private Runnable velocityRunnable;
private int taskId = -1;
private boolean velocitySent = DisguiseConfig.isVelocitySent(); private boolean velocitySent = DisguiseConfig.isVelocitySent();
private boolean viewSelfDisguise = DisguiseConfig.isViewDisguises(); private boolean viewSelfDisguise = DisguiseConfig.isViewDisguises();
private FlagWatcher watcher; private FlagWatcher watcher;
@ -178,7 +179,7 @@ public abstract class Disguise {
final double vectorY = velocitySpeed; final double vectorY = velocitySpeed;
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 BukkitRunnable() { velocityRunnable = new Runnable() {
private int deadTicks = 0; private int deadTicks = 0;
private int refreshDisguise = 0; private int refreshDisguise = 0;
@ -196,7 +197,8 @@ public abstract class Disguise {
} else { } else {
entity = null; entity = null;
watcher = getWatcher().clone(disguise); watcher = getWatcher().clone(disguise);
cancel(); Bukkit.getScheduler().cancelTask(taskId);
taskId = -1;
} }
} }
} else { } else {
@ -399,35 +401,34 @@ public abstract class Disguise {
* Removes the disguise and undisguises the entity if its using this disguise. This doesn't fire a UndisguiseEvent * Removes the disguise and undisguises the entity if its using this disguise. This doesn't fire a UndisguiseEvent
*/ */
public void removeDisguise() { public void removeDisguise() {
// Why the hell can't I safely check if its running?!?! if (taskId != -1) {
try { Bukkit.getScheduler().cancelTask(taskId);
velocityRunnable.cancel(); taskId = -1;
} catch (Exception ex) { HashMap<UUID, HashSet<TargetedDisguise>> disguises = DisguiseUtilities.getDisguises();
} // If this disguise has a entity set
HashMap<UUID, HashSet<TargetedDisguise>> disguises = DisguiseUtilities.getDisguises(); if (getEntity() != null) {
// If this disguise has a entity set // If this disguise is active
if (getEntity() != null) { // Remove the disguise from the current disguises.
// If this disguise is active if (DisguiseUtilities.removeDisguise((TargetedDisguise) this)) {
// Remove the disguise from the current disguises. if (getEntity() instanceof Player) {
if (DisguiseUtilities.removeDisguise((TargetedDisguise) this)) { DisguiseUtilities.removeSelfDisguise((Player) getEntity());
if (getEntity() instanceof Player) { }
DisguiseUtilities.removeSelfDisguise((Player) getEntity());
}
// Better refresh the entity to undisguise it // Better refresh the entity to undisguise it
if (getEntity().isValid()) { if (getEntity().isValid()) {
DisguiseUtilities.refreshTrackers((TargetedDisguise) this); DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
} else { } else {
DisguiseUtilities.destroyEntity((TargetedDisguise) this); DisguiseUtilities.destroyEntity((TargetedDisguise) this);
}
} }
} } else {
} else { // 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. Iterator<UUID> itel = disguises.keySet().iterator();
Iterator<UUID> itel = disguises.keySet().iterator(); while (itel.hasNext()) {
while (itel.hasNext()) { UUID id = itel.next();
UUID id = itel.next(); if (disguises.get(id).remove(this) && disguises.get(id).isEmpty()) {
if (disguises.get(id).remove(this) && disguises.get(id).isEmpty()) { itel.remove();
itel.remove(); }
} }
} }
} }
@ -444,7 +445,7 @@ public abstract class Disguise {
} }
this.entity = entity; this.entity = entity;
setupWatcher(); setupWatcher();
velocityRunnable.runTaskTimer(plugin, 1, 1); taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, velocityRunnable, 1, 1);
} }
public void setHearSelfDisguise(boolean hearSelfDisguise) { public void setHearSelfDisguise(boolean hearSelfDisguise) {