diff --git a/src/com/projectkorra/projectkorra/GeneralMethods.java b/src/com/projectkorra/projectkorra/GeneralMethods.java index 89fbe296..b7ae1bf8 100644 --- a/src/com/projectkorra/projectkorra/GeneralMethods.java +++ b/src/com/projectkorra/projectkorra/GeneralMethods.java @@ -93,7 +93,7 @@ import com.projectkorra.projectkorra.command.Commands; import com.projectkorra.projectkorra.configuration.ConfigManager; import com.projectkorra.projectkorra.earthbending.EarthBlast; import com.projectkorra.projectkorra.earthbending.EarthPassive; -import com.projectkorra.projectkorra.event.BendingDamageEvent; +import com.projectkorra.projectkorra.event.AbilityDamageEntityEvent; import com.projectkorra.projectkorra.event.BendingReloadEvent; import com.projectkorra.projectkorra.event.BindChangeEvent; import com.projectkorra.projectkorra.event.EntityBendingDeathEvent; @@ -359,25 +359,26 @@ public class GeneralMethods { * @param ability The ability that is used to damage the entity */ public static void damageEntity(Player player, Entity entity, double damage, String ability) { + AbilityDamageEntityEvent damageEvent = new AbilityDamageEntityEvent(entity, CoreAbility.getAbility(player, CoreAbility.getAbility(ability).getClass()), damage); + Bukkit.getServer().getPluginManager().callEvent(damageEvent); if (entity instanceof LivingEntity) { - if (entity instanceof Player) { - if (Commands.invincible.contains(entity.getName())) { - return; + if (entity instanceof Player && Commands.invincible.contains(entity.getName())) { + damageEvent.setCancelled(true); + } + if (!damageEvent.isCancelled()) { + damage = damageEvent.getDamage(); + if (Bukkit.getPluginManager().isPluginEnabled("NoCheatPlus")) { + NCPExemptionManager.exemptPermanently(player, CheckType.FIGHT_REACH); + } + if (((LivingEntity) entity).getHealth() - damage <= 0 && !entity.isDead()) { + EntityBendingDeathEvent deathEvent = new EntityBendingDeathEvent(entity, player, damage, ability); + Bukkit.getServer().getPluginManager().callEvent(deathEvent); + } + ((LivingEntity) entity).damage(damage, player); + entity.setLastDamageCause(new EntityDamageByEntityEvent(player, entity, DamageCause.CUSTOM, damage)); + if (Bukkit.getPluginManager().isPluginEnabled("NoCheatPlus")) { + NCPExemptionManager.unexempt(player); } - } - if (Bukkit.getPluginManager().isPluginEnabled("NoCheatPlus")) { - NCPExemptionManager.exemptPermanently(player, CheckType.FIGHT_REACH); - } - if (((LivingEntity) entity).getHealth() - damage <= 0 && !entity.isDead()) { - EntityBendingDeathEvent event = new EntityBendingDeathEvent(entity, player, damage, ability); - Bukkit.getServer().getPluginManager().callEvent(event); - } - BendingDamageEvent event = new BendingDamageEvent(); - Bukkit.getServer().getPluginManager().callEvent(event); - ((LivingEntity) entity).damage(damage, player); - entity.setLastDamageCause(new EntityDamageByEntityEvent(player, entity, DamageCause.CUSTOM, damage)); - if (Bukkit.getPluginManager().isPluginEnabled("NoCheatPlus")) { - NCPExemptionManager.unexempt(player); } } } diff --git a/src/com/projectkorra/projectkorra/ability/CoreAbility.java b/src/com/projectkorra/projectkorra/ability/CoreAbility.java index dac3482f..36e59042 100644 --- a/src/com/projectkorra/projectkorra/ability/CoreAbility.java +++ b/src/com/projectkorra/projectkorra/ability/CoreAbility.java @@ -32,6 +32,8 @@ import com.projectkorra.projectkorra.ability.util.ComboManager; import com.projectkorra.projectkorra.ability.util.MultiAbilityManager; import com.projectkorra.projectkorra.ability.util.MultiAbilityManager.MultiAbilityInfo; import com.projectkorra.projectkorra.configuration.ConfigManager; +import com.projectkorra.projectkorra.event.AbilityEndEvent; +import com.projectkorra.projectkorra.event.AbilityStartEvent; import sun.reflect.ReflectionFactory; @@ -114,7 +116,12 @@ public abstract class CoreAbility implements Ability { if (player == null) { return; } - + AbilityStartEvent event = new AbilityStartEvent(this); + Bukkit.getServer().getPluginManager().callEvent(event); + if(event.isCancelled()) { + remove(); + return; + } this.started = true; this.startTime = System.currentTimeMillis(); Class clazz = getClass(); @@ -148,6 +155,7 @@ public abstract class CoreAbility implements Ability { return; } + Bukkit.getServer().getPluginManager().callEvent(new AbilityEndEvent(this)); removed = true; ConcurrentHashMap> classMap = INSTANCES.get(getClass()); @@ -346,6 +354,7 @@ public abstract class CoreAbility implements Ability { * @see #getAbility(String) */ public static void registerPluginAbilities(JavaPlugin plugin, String packagePrefix) { + List disabled = new ArrayList(); //this way multiple classes with the same name only show once if (plugin == null) { return; } @@ -373,8 +382,9 @@ public abstract class CoreAbility implements Ability { if (ability == null || ability.getName() == null) { continue; - } else if (!ability.isEnabled()) { + } else if (!ability.isEnabled() && !disabled.contains(ability.getName())) { plugin.getLogger().info(ability.getName() + " is disabled"); + disabled.add(ability.getName()); continue; } diff --git a/src/com/projectkorra/projectkorra/command/WhoCommand.java b/src/com/projectkorra/projectkorra/command/WhoCommand.java index 4ed633ab..52afe3b2 100644 --- a/src/com/projectkorra/projectkorra/command/WhoCommand.java +++ b/src/com/projectkorra/projectkorra/command/WhoCommand.java @@ -149,7 +149,7 @@ public class WhoCommand extends PKCommand { //Player player = Bukkit.getPlayer(playerName); @SuppressWarnings("deprecation") final OfflinePlayer player = Bukkit.getOfflinePlayer(playerName); - if (player == null || !player.hasPlayedBefore()) { + if (player == null || !player.hasPlayedBefore() && !player.isOnline()) { sender.sendMessage(ChatColor.RED + "Player not found!"); return; } diff --git a/src/com/projectkorra/projectkorra/event/AbilityDamageEntityEvent.java b/src/com/projectkorra/projectkorra/event/AbilityDamageEntityEvent.java new file mode 100644 index 00000000..8d514587 --- /dev/null +++ b/src/com/projectkorra/projectkorra/event/AbilityDamageEntityEvent.java @@ -0,0 +1,85 @@ +package com.projectkorra.projectkorra.event; + +import org.bukkit.entity.Entity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.projectkorra.projectkorra.ability.Ability; + +/** + * Called when an ability damages an {@link Entity} + * @author kingbirdy + * + */ +public class AbilityDamageEntityEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private boolean cancelled = false; + private Entity entity; + private Ability ability; + double damage; + + /** + * Create a new AbilityDamageEntityEvent + * @param entity The entity that was damaged + * @param ability The damaging ability + * @param damage The amount of damage done + */ + public AbilityDamageEntityEvent(Entity entity, Ability ability, double damage) { + this.entity = entity; + this.ability = ability; + this.damage = damage; + } + + /** + * Returns the damage dealt to the entity + * @return the amount of damage done + */ + public double getDamage() { + return damage; + } + + /** + * Sets the damage dealt to the entity + * @param damage the amount of damage done + */ + public void setDamage(double damage) { + this.damage = damage; + } + + /** + * Gets the entity that was damaged + * @return the damaged entity + */ + public Entity getEntity() { + return entity; + } + + /** + * + * @return + */ + public Ability getAbility() { + return ability; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +} diff --git a/src/com/projectkorra/projectkorra/event/AbilityEndEvent.java b/src/com/projectkorra/projectkorra/event/AbilityEndEvent.java new file mode 100644 index 00000000..5d8c17dc --- /dev/null +++ b/src/com/projectkorra/projectkorra/event/AbilityEndEvent.java @@ -0,0 +1,22 @@ +package com.projectkorra.projectkorra.event; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.projectkorra.projectkorra.ability.Ability; + +public class AbilityEndEvent extends Event { + private final HandlerList handlers = new HandlerList(); + + Ability ability; + + public AbilityEndEvent(Ability ability) { + this.ability = ability; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/com/projectkorra/projectkorra/event/AbilityStartEvent.java b/src/com/projectkorra/projectkorra/event/AbilityStartEvent.java new file mode 100644 index 00000000..a42794f8 --- /dev/null +++ b/src/com/projectkorra/projectkorra/event/AbilityStartEvent.java @@ -0,0 +1,47 @@ +package com.projectkorra.projectkorra.event; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.projectkorra.projectkorra.ability.Ability; + +/** + * Called when an ability starts + * @author Philip + * + */ +public class AbilityStartEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + boolean cancelled = false; + Ability ability; + + public AbilityStartEvent(Ability ability) { + this.ability = ability; + } + + public Ability getAbility() { + return ability; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + +} diff --git a/src/com/projectkorra/projectkorra/event/BendingDamageEvent.java b/src/com/projectkorra/projectkorra/event/BendingDamageEvent.java deleted file mode 100644 index 8c4adf72..00000000 --- a/src/com/projectkorra/projectkorra/event/BendingDamageEvent.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.projectkorra.projectkorra.event; - -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class BendingDamageEvent extends Event implements Cancellable { - - private static final HandlerList handlers = new HandlerList(); - private boolean cancelled; - - public BendingDamageEvent() { - this.cancelled = false; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } - -}