Break all the plugins!

Changes all methods returning void in the Disguise objects to return the
Disguise itself.
The methods startDisguise and removeDisguise now return a boolean which
indicates the success of the method called.
Added stopDisguise as its rather obvious when compared to startDisguise
However that method merely calls removeDisguise
This commit is contained in:
libraryaddict 2014-07-11 17:07:47 +12:00
parent bb442fb3a5
commit 54d785b680
4 changed files with 241 additions and 102 deletions

@ -23,6 +23,7 @@ import me.libraryaddict.disguise.utilities.DisguiseValues;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Horse.Variant; import org.bukkit.entity.Horse.Variant;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -38,7 +39,7 @@ public abstract class Disguise {
private static JavaPlugin plugin; private static JavaPlugin plugin;
private boolean disguiseInUse; private boolean disguiseInUse;
private DisguiseType disguiseType; private DisguiseType disguiseType;
protected Entity entity; private Entity entity;
private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced(); private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced();
private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf(); private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf();
private boolean hideHeldItemFromSelf = DisguiseConfig.isHidingHeldItemFromSelf(); private boolean hideHeldItemFromSelf = DisguiseConfig.isHidingHeldItemFromSelf();
@ -411,101 +412,128 @@ public abstract class Disguise {
return velocitySent; return velocitySent;
} }
public boolean stopDisguise() {
return removeDisguise();
}
/** /**
* Removes the disguise and undisguises the entity if its using this disguise. * Removes the disguise and undisguises the entity if its using this disguise.
*
* @return
*/ */
public void removeDisguise() { public boolean removeDisguise() {
if (disguiseInUse) { if (disguiseInUse) {
UndisguiseEvent event = new UndisguiseEvent(entity, this); UndisguiseEvent event = new UndisguiseEvent(entity, this);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (!event.isCancelled()) {
return; disguiseInUse = false;
disguiseInUse = false; if (task != null) {
if (task != null) { task.cancel();
task.cancel(); task = null;
task = null;
}
HashMap<UUID, HashSet<TargetedDisguise>> disguises = DisguiseUtilities.getDisguises();
// If this disguise has a entity set
if (getEntity() != null) {
// If this disguise is active
// Remove the disguise from the current disguises.
if (DisguiseUtilities.removeDisguise((TargetedDisguise) this)) {
if (getEntity() instanceof Player) {
DisguiseUtilities.removeSelfDisguise((Player) getEntity());
}
// Better refresh the entity to undisguise it
if (getEntity().isValid()) {
DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
} else {
DisguiseUtilities.destroyEntity((TargetedDisguise) this);
}
} }
} else { HashMap<UUID, HashSet<TargetedDisguise>> disguises = DisguiseUtilities.getDisguises();
// Loop through the disguises because it could be used with a unknown entity id. // If this disguise has a entity set
HashMap<Integer, HashSet<TargetedDisguise>> future = DisguiseUtilities.getFutureDisguises(); if (getEntity() != null) {
Iterator<Integer> itel = DisguiseUtilities.getFutureDisguises().keySet().iterator(); // If this disguise is active
while (itel.hasNext()) { // Remove the disguise from the current disguises.
int id = itel.next(); if (DisguiseUtilities.removeDisguise((TargetedDisguise) this)) {
if (future.get(id).remove(this) && future.get(id).isEmpty()) { if (getEntity() instanceof Player) {
itel.remove(); DisguiseUtilities.removeSelfDisguise((Player) getEntity());
} }
}
}
if (isPlayerDisguise()) { // Better refresh the entity to undisguise it
String name = ((PlayerDisguise) this).getName(); if (getEntity().isValid()) {
if (!DisguiseUtilities.getAddedByPlugins().contains(name.toLowerCase())) { DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
for (HashSet<TargetedDisguise> disguise : disguises.values()) { } else {
for (Disguise d : disguise) { DisguiseUtilities.destroyEntity((TargetedDisguise) this);
if (d.isPlayerDisguise() && ((PlayerDisguise) d).getName().equals(name)) { }
return; }
} } else {
// Loop through the disguises because it could be used with a unknown entity id.
HashMap<Integer, HashSet<TargetedDisguise>> future = DisguiseUtilities.getFutureDisguises();
Iterator<Integer> itel = DisguiseUtilities.getFutureDisguises().keySet().iterator();
while (itel.hasNext()) {
int id = itel.next();
if (future.get(id).remove(this) && future.get(id).isEmpty()) {
itel.remove();
} }
} }
DisguiseUtilities.getGameProfiles().remove(name.toLowerCase());
} }
if (isPlayerDisguise()) {
String name = ((PlayerDisguise) this).getName();
if (!DisguiseUtilities.getAddedByPlugins().contains(name.toLowerCase())) {
for (HashSet<TargetedDisguise> disguise : disguises.values()) {
for (Disguise d : disguise) {
if (d.isPlayerDisguise() && ((PlayerDisguise) d).getName().equals(name)) {
return true;
}
}
}
DisguiseUtilities.getGameProfiles().remove(name.toLowerCase());
}
}
return true;
} }
} }
return false;
} }
/** /**
* Set the entity of the disguise. Only used for internal things. * Set the entity of the disguise. Only used for internal things.
*/ */
public abstract Disguise setEntity(Entity entity); public Disguise setEntity(Entity entity) {
if (this.getEntity() != null) {
public void setHearSelfDisguise(boolean hearSelfDisguise) { if (getEntity() == entity)
this.hearSelfDisguise = hearSelfDisguise; return this;
throw new RuntimeException("This disguise is already in use! Try .clone()");
}
if (isMiscDisguise() && !DisguiseConfig.isMiscDisguisesForLivingEnabled() && entity instanceof LivingEntity) {
throw new RuntimeException(
"Cannot disguise a living entity with a misc disguise. Renable MiscDisguisesForLiving in the config to do this");
}
this.entity = entity;
setupWatcher();
return this;
} }
public void setHideArmorFromSelf(boolean hideArmor) { public Disguise setHearSelfDisguise(boolean hearSelfDisguise) {
this.hearSelfDisguise = hearSelfDisguise;
return this;
}
public Disguise setHideArmorFromSelf(boolean hideArmor) {
this.hideArmorFromSelf = hideArmor; this.hideArmorFromSelf = hideArmor;
if (getEntity() instanceof Player) { if (getEntity() instanceof Player) {
((Player) getEntity()).updateInventory(); ((Player) getEntity()).updateInventory();
} }
return this;
} }
public void setHideHeldItemFromSelf(boolean hideHeldItem) { public Disguise setHideHeldItemFromSelf(boolean hideHeldItem) {
this.hideHeldItemFromSelf = hideHeldItem; this.hideHeldItemFromSelf = hideHeldItem;
if (getEntity() instanceof Player) { if (getEntity() instanceof Player) {
((Player) getEntity()).updateInventory(); ((Player) getEntity()).updateInventory();
} }
return this;
} }
public void setKeepDisguiseOnEntityDespawn(boolean keepDisguise) { public Disguise setKeepDisguiseOnEntityDespawn(boolean keepDisguise) {
this.keepDisguiseEntityDespawn = keepDisguise; this.keepDisguiseEntityDespawn = keepDisguise;
return this;
} }
public void setKeepDisguiseOnPlayerDeath(boolean keepDisguise) { public Disguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) {
this.keepDisguisePlayerDeath = keepDisguise; this.keepDisguisePlayerDeath = keepDisguise;
return this;
} }
public void setKeepDisguiseOnPlayerLogout(boolean keepDisguise) { public Disguise setKeepDisguiseOnPlayerLogout(boolean keepDisguise) {
this.keepDisguisePlayerLogout = keepDisguise; this.keepDisguisePlayerLogout = keepDisguise;
return this;
} }
public void setModifyBoundingBox(boolean modifyBox) { public Disguise setModifyBoundingBox(boolean modifyBox) {
if (((TargetedDisguise) this).getDisguiseTarget() != TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) { if (((TargetedDisguise) this).getDisguiseTarget() != TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS) {
throw new RuntimeException( throw new RuntimeException(
"Cannot modify the bounding box of a disguise which is not TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS"); "Cannot modify the bounding box of a disguise which is not TargetType.SHOW_TO_EVERYONE_BUT_THESE_PLAYERS");
@ -516,17 +544,19 @@ public abstract class Disguise {
DisguiseUtilities.doBoundingBox((TargetedDisguise) this); DisguiseUtilities.doBoundingBox((TargetedDisguise) this);
} }
} }
return this;
} }
public void setReplaceSounds(boolean areSoundsReplaced) { public Disguise setReplaceSounds(boolean areSoundsReplaced) {
replaceSounds = areSoundsReplaced; replaceSounds = areSoundsReplaced;
return this;
} }
/** /**
* Sets up the FlagWatcher with the entityclass, it creates all the data it needs to prevent conflicts when sending the * Sets up the FlagWatcher with the entityclass, it creates all the data it needs to prevent conflicts when sending the
* datawatcher. * datawatcher.
*/ */
protected void setupWatcher() { private void setupWatcher() {
HashMap<Integer, Object> disguiseValues = DisguiseValues.getMetaValues(getType()); HashMap<Integer, Object> disguiseValues = DisguiseValues.getMetaValues(getType());
HashMap<Integer, Object> entityValues = DisguiseValues.getMetaValues(DisguiseType.getType(getEntity().getType())); HashMap<Integer, Object> entityValues = DisguiseValues.getMetaValues(DisguiseType.getType(getEntity().getType()));
// Start from 2 as they ALL share 0 and 1 // Start from 2 as they ALL share 0 and 1
@ -624,14 +654,17 @@ public abstract class Disguise {
} }
} }
public void setVelocitySent(boolean sendVelocity) { public Disguise setVelocitySent(boolean sendVelocity) {
this.velocitySent = sendVelocity; this.velocitySent = sendVelocity;
return this;
} }
/** /**
* Can the disguised view himself as the disguise * Can the disguised view himself as the disguise
*
* @return
*/ */
public void setViewSelfDisguise(boolean viewSelfDisguise) { public Disguise setViewSelfDisguise(boolean viewSelfDisguise) {
if (isSelfDisguiseVisible() != viewSelfDisguise) { if (isSelfDisguiseVisible() != viewSelfDisguise) {
this.viewSelfDisguise = viewSelfDisguise; this.viewSelfDisguise = viewSelfDisguise;
if (getEntity() != null && getEntity() instanceof Player) { if (getEntity() != null && getEntity() instanceof Player) {
@ -643,9 +676,10 @@ public abstract class Disguise {
} }
} }
} }
return this;
} }
public void setWatcher(FlagWatcher newWatcher) { public Disguise setWatcher(FlagWatcher newWatcher) {
if (!getType().getWatcherClass().isInstance(newWatcher)) { if (!getType().getWatcherClass().isInstance(newWatcher)) {
throw new IllegalArgumentException(newWatcher.getClass().getSimpleName() + " is not a instance of " throw new IllegalArgumentException(newWatcher.getClass().getSimpleName() + " is not a instance of "
+ getType().getWatcherClass().getSimpleName() + " for DisguiseType " + getType().name()); + getType().getWatcherClass().getSimpleName() + " for DisguiseType " + getType().name());
@ -654,9 +688,10 @@ public abstract class Disguise {
if (getEntity() != null) { if (getEntity() != null) {
setupWatcher(); setupWatcher();
} }
return this;
} }
public void startDisguise() { public boolean startDisguise() {
if (!isDisguiseInUse()) { if (!isDisguiseInUse()) {
if (getEntity() == null) { if (getEntity() == null) {
throw new RuntimeException("No entity is assigned to this disguise!"); throw new RuntimeException("No entity is assigned to this disguise!");
@ -666,16 +701,18 @@ public abstract class Disguise {
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
// If they cancelled this disguise event. No idea why. // If they cancelled this disguise event. No idea why.
// Just return. // Just return.
if (event.isCancelled()) if (!event.isCancelled()) {
return; disguiseInUse = true;
disguiseInUse = true; task = Bukkit.getScheduler().runTaskTimer(plugin, velocityRunnable, 1, 1);
task = Bukkit.getScheduler().runTaskTimer(plugin, velocityRunnable, 1, 1); // Stick the disguise in the disguises bin
// Stick the disguise in the disguises bin DisguiseUtilities.addDisguise(entity.getUniqueId(), (TargetedDisguise) this);
DisguiseUtilities.addDisguise(entity.getUniqueId(), (TargetedDisguise) this); // Resend the disguised entity's packet
// Resend the disguised entity's packet DisguiseUtilities.refreshTrackers((TargetedDisguise) this);
DisguiseUtilities.refreshTrackers((TargetedDisguise) this); // If he is a player, then self disguise himself
// If he is a player, then self disguise himself DisguiseUtilities.setupFakeDisguise(this);
DisguiseUtilities.setupFakeDisguise(this); return true;
}
} }
return false;
} }
} }

@ -2,7 +2,6 @@ package me.libraryaddict.disguise.disguisetypes;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.watchers.DroppedItemWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.DroppedItemWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PaintingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PaintingWatcher;
@ -11,7 +10,6 @@ import me.libraryaddict.disguise.disguisetypes.watchers.SplashPotionWatcher;
import org.bukkit.Art; import org.bukkit.Art;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class MiscDisguise extends TargetedDisguise { public class MiscDisguise extends TargetedDisguise {
@ -152,18 +150,49 @@ public class MiscDisguise extends TargetedDisguise {
@Override @Override
public MiscDisguise setEntity(Entity entity) { public MiscDisguise setEntity(Entity entity) {
if (this.getEntity() != null) { return (MiscDisguise) super.setEntity(entity);
if (getEntity() == entity) } public MiscDisguise setHearSelfDisguise(boolean hearSelfDisguise) {
return this; return (MiscDisguise) super.setHearSelfDisguise(hearSelfDisguise);
throw new RuntimeException("This disguise is already in use! Try .clone()"); }
}
if (!DisguiseConfig.isMiscDisguisesForLivingEnabled() && entity instanceof LivingEntity) { public MiscDisguise setHideArmorFromSelf(boolean hideArmor) {
throw new RuntimeException( return (MiscDisguise) super.setHideArmorFromSelf(hideArmor);
"Cannot disguise a living entity with a misc disguise. Renable MiscDisguisesForLiving in the config to do this"); }
}
this.entity = entity; public MiscDisguise setHideHeldItemFromSelf(boolean hideHeldItem) {
setupWatcher(); return (MiscDisguise) super.setHideHeldItemFromSelf(hideHeldItem);
return this; }
public MiscDisguise setKeepDisguiseOnEntityDespawn(boolean keepDisguise) {
return (MiscDisguise) super.setKeepDisguiseOnEntityDespawn(keepDisguise);
}
public MiscDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) {
return (MiscDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise);
}
public MiscDisguise setKeepDisguiseOnPlayerLogout(boolean keepDisguise) {
return (MiscDisguise) super.setKeepDisguiseOnPlayerLogout(keepDisguise);
}
public MiscDisguise setModifyBoundingBox(boolean modifyBox) {
return (MiscDisguise) super.setModifyBoundingBox(modifyBox);
}
public MiscDisguise setReplaceSounds(boolean areSoundsReplaced) {
return (MiscDisguise) super.setReplaceSounds(areSoundsReplaced);
}
public MiscDisguise setVelocitySent(boolean sendVelocity) {
return (MiscDisguise) super.setVelocitySent(sendVelocity);
}
public MiscDisguise setViewSelfDisguise(boolean viewSelfDisguise) {
return (MiscDisguise) super.setViewSelfDisguise(viewSelfDisguise);
}
public MiscDisguise setWatcher(FlagWatcher newWatcher) {
return (MiscDisguise) super.setWatcher(newWatcher);
} }
} }

@ -84,13 +84,50 @@ public class MobDisguise extends TargetedDisguise {
@Override @Override
public MobDisguise setEntity(Entity entity) { public MobDisguise setEntity(Entity entity) {
if (this.getEntity() != null) { return (MobDisguise) super.setEntity(entity);
if (getEntity() == entity) }
return this;
throw new RuntimeException("This disguise is already in use! Try .clone()"); public MobDisguise setHearSelfDisguise(boolean hearSelfDisguise) {
} return (MobDisguise) super.setHearSelfDisguise(hearSelfDisguise);
this.entity = entity; }
setupWatcher();
return this; public MobDisguise setHideArmorFromSelf(boolean hideArmor) {
return (MobDisguise) super.setHideArmorFromSelf(hideArmor);
}
public MobDisguise setHideHeldItemFromSelf(boolean hideHeldItem) {
return (MobDisguise) super.setHideHeldItemFromSelf(hideHeldItem);
}
public MobDisguise setKeepDisguiseOnEntityDespawn(boolean keepDisguise) {
return (MobDisguise) super.setKeepDisguiseOnEntityDespawn(keepDisguise);
}
public MobDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) {
return (MobDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise);
}
public MobDisguise setKeepDisguiseOnPlayerLogout(boolean keepDisguise) {
return (MobDisguise) super.setKeepDisguiseOnPlayerLogout(keepDisguise);
}
public MobDisguise setModifyBoundingBox(boolean modifyBox) {
return (MobDisguise) super.setModifyBoundingBox(modifyBox);
}
public MobDisguise setReplaceSounds(boolean areSoundsReplaced) {
return (MobDisguise) super.setReplaceSounds(areSoundsReplaced);
}
public MobDisguise setVelocitySent(boolean sendVelocity) {
return (MobDisguise) super.setVelocitySent(sendVelocity);
}
public MobDisguise setViewSelfDisguise(boolean viewSelfDisguise) {
return (MobDisguise) super.setViewSelfDisguise(viewSelfDisguise);
}
public MobDisguise setWatcher(FlagWatcher newWatcher) {
return (MobDisguise) super.setWatcher(newWatcher);
} }
} }

@ -84,14 +84,39 @@ public class PlayerDisguise extends TargetedDisguise {
@Override @Override
public PlayerDisguise setEntity(Entity entity) { public PlayerDisguise setEntity(Entity entity) {
if (this.getEntity() != null) { return (PlayerDisguise) super.setEntity(entity);
if (getEntity() == entity) }
return this;
throw new RuntimeException("This disguise is already in use! Try .clone()"); public PlayerDisguise setHearSelfDisguise(boolean hearSelfDisguise) {
} return (PlayerDisguise) super.setHearSelfDisguise(hearSelfDisguise);
this.entity = entity; }
setupWatcher();
return this; public PlayerDisguise setHideArmorFromSelf(boolean hideArmor) {
return (PlayerDisguise) super.setHideArmorFromSelf(hideArmor);
}
public PlayerDisguise setHideHeldItemFromSelf(boolean hideHeldItem) {
return (PlayerDisguise) super.setHideHeldItemFromSelf(hideHeldItem);
}
public PlayerDisguise setKeepDisguiseOnEntityDespawn(boolean keepDisguise) {
return (PlayerDisguise) super.setKeepDisguiseOnEntityDespawn(keepDisguise);
}
public PlayerDisguise setKeepDisguiseOnPlayerDeath(boolean keepDisguise) {
return (PlayerDisguise) super.setKeepDisguiseOnPlayerDeath(keepDisguise);
}
public PlayerDisguise setKeepDisguiseOnPlayerLogout(boolean keepDisguise) {
return (PlayerDisguise) super.setKeepDisguiseOnPlayerLogout(keepDisguise);
}
public PlayerDisguise setModifyBoundingBox(boolean modifyBox) {
return (PlayerDisguise) super.setModifyBoundingBox(modifyBox);
}
public PlayerDisguise setReplaceSounds(boolean areSoundsReplaced) {
return (PlayerDisguise) super.setReplaceSounds(areSoundsReplaced);
} }
@Deprecated @Deprecated
@ -131,7 +156,7 @@ public class PlayerDisguise extends TargetedDisguise {
* *
* @param gameProfile * @param gameProfile
* GameProfile * GameProfile
* @return * @return
*/ */
@Deprecated @Deprecated
public PlayerDisguise setSkin(WrappedGameProfile gameProfile) { public PlayerDisguise setSkin(WrappedGameProfile gameProfile) {
@ -148,4 +173,15 @@ public class PlayerDisguise extends TargetedDisguise {
return this; return this;
} }
public PlayerDisguise setVelocitySent(boolean sendVelocity) {
return (PlayerDisguise) super.setVelocitySent(sendVelocity);
}
public PlayerDisguise setViewSelfDisguise(boolean viewSelfDisguise) {
return (PlayerDisguise) super.setViewSelfDisguise(viewSelfDisguise);
}
public PlayerDisguise setWatcher(FlagWatcher newWatcher) {
return (PlayerDisguise) super.setWatcher(newWatcher);
}
} }