diff --git a/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java index 60ced389b..087b5e31c 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java @@ -2,10 +2,9 @@ package com.gmail.nossr50.commands.experience; import org.bukkit.command.CommandSender; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Permissions; public class AddlevelsCommand extends ExperienceCommand { @@ -21,11 +20,14 @@ public class AddlevelsCommand extends ExperienceCommand { @Override protected void handleCommand(SkillType skill) { + float xpRemoved = profile.getSkillXpLevelRaw(skill); profile.addLevels(skill, value); - if (player != null) { - mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerLevelUpEvent(player, skill, value)); + if (player == null) { + return; } + + EventUtils.handleLevelChangeEvent(player, skill, value, xpRemoved, true); } @Override diff --git a/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java index 6a36a0da6..3d990e308 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java @@ -2,11 +2,9 @@ package com.gmail.nossr50.commands.experience; import org.bukkit.command.CommandSender; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent; -import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Permissions; public class MmoeditCommand extends ExperienceCommand { @@ -22,20 +20,16 @@ public class MmoeditCommand extends ExperienceCommand { @Override protected void handleCommand(SkillType skill) { + int skillLevel = profile.getSkillLevel(skill); + float xpRemoved = profile.getSkillXpLevelRaw(skill); + profile.modifySkill(skill, value); - if (player == null) { + if (player == null || value == skillLevel) { return; } - int skillLevel = profile.getSkillLevel(skill); - - if (value > skillLevel) { - mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerLevelUpEvent(player, skill, value - skillLevel)); - } - else if (value < skillLevel) { - mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerLevelDownEvent(player, skill, skillLevel - value)); - } + EventUtils.handleLevelChangeEvent(player, skill, value, xpRemoved, value > skillLevel); } @Override diff --git a/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java index 0ec4c6a4a..a5f43fea7 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java @@ -6,8 +6,8 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.commands.CommandUtils; @@ -102,11 +102,16 @@ public class SkillresetCommand extends ExperienceCommand { return; } + int levelsRemoved = profile.getSkillLevel(skill); + float xpRemoved = profile.getSkillXpLevelRaw(skill); + profile.modifySkill(skill, 0); - if (player != null) { - mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerLevelDownEvent(player, skill, profile.getSkillLevel(skill))); + if (player == null) { + return; } + + EventUtils.handleLevelChangeEvent(player, skill, levelsRemoved, xpRemoved, false); } @Override diff --git a/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java b/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java index d3191bb16..b2dc25627 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java @@ -14,10 +14,10 @@ import org.bukkit.util.StringUtil; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.player.McMMOPlayer; -import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.runnables.items.TeleportationWarmup; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.commands.CommandUtils; @@ -177,25 +177,7 @@ public class PtpCommand implements TabExecutor { new TeleportationWarmup(mcMMOPlayer, mcMMOTarget).runTaskLater(mcMMO.p, 20 * warmup); } else { - handlePartyTeleportEvent(teleportingPlayer, targetPlayer); + EventUtils.handlePartyTeleportEvent(teleportingPlayer, targetPlayer); } } - - public static void handlePartyTeleportEvent(Player teleportingPlayer, Player targetPlayer) { - McMMOPlayer mcMMOPlayer = UserManager.getPlayer(teleportingPlayer); - McMMOPartyTeleportEvent event = new McMMOPartyTeleportEvent(teleportingPlayer, targetPlayer, mcMMOPlayer.getParty().getName()); - - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return; - } - - teleportingPlayer.teleport(targetPlayer); - - teleportingPlayer.sendMessage(LocaleLoader.getString("Party.Teleport.Player", targetPlayer.getName())); - targetPlayer.sendMessage(LocaleLoader.getString("Party.Teleport.Target", teleportingPlayer.getName())); - - mcMMOPlayer.actualizePtpLastUse(); - } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 25ad0b6be..61106d797 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -22,7 +22,6 @@ import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.ToolType; -import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.ShareHandler; @@ -593,15 +592,10 @@ public class McMMOPlayer { return; } - McMMOPlayerXpGainEvent event = new McMMOPlayerXpGainEvent(player, skillType, xp); - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { + if (!EventUtils.handleXpGainEvent(player, skillType, xp)) { return; } - profile.setSkillXpLevel(skillType, profile.getSkillXpLevelRaw(skillType) + event.getRawXpGained()); - isUsingUnarmed = (skillType == SkillType.UNARMED); checkXp(skillType); } @@ -626,13 +620,11 @@ public class McMMOPlayer { profile.skillUp(skillType, 1); } else { - profile.addLevels(skillType, 0); + profile.addLevels(skillType, 0); // This seems kinda pointless... why do we have this again? } } - if (EventUtils.callLevelUpEvent(player, skillType, levelsGained).isCancelled()) { - profile.modifySkill(skillType, profile.getSkillLevel(skillType) - levelsGained); - profile.setSkillXpLevel(skillType, profile.getSkillXpLevelRaw(skillType) + xpRemoved); + if (!EventUtils.handleLevelChangeEvent(player, skillType, levelsGained, xpRemoved, true)) { return; } diff --git a/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityActivateEvent.java b/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityActivateEvent.java index 31d8dfff7..f0b371fd3 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityActivateEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityActivateEvent.java @@ -3,28 +3,16 @@ package com.gmail.nossr50.events.skills.abilities; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; -import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; -public class McMMOPlayerAbilityActivateEvent extends McMMOPlayerSkillEvent implements Cancellable { - - private AbilityType abilityType; +public class McMMOPlayerAbilityActivateEvent extends McMMOPlayerAbilityEvent implements Cancellable { private boolean cancelled; public McMMOPlayerAbilityActivateEvent(Player player, SkillType skill) { super(player, skill); - abilityType = skill.getAbility(); cancelled = false; } - /** - * @return The ability type involved in this event - */ - public AbilityType getAbilityType() { - return abilityType; - } - public boolean isCancelled() { return cancelled; } diff --git a/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityDeactivateEvent.java b/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityDeactivateEvent.java index 77b4fed92..6eeb7f479 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityDeactivateEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityDeactivateEvent.java @@ -2,21 +2,10 @@ package com.gmail.nossr50.events.skills.abilities; import org.bukkit.entity.Player; -import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; - -public class McMMOPlayerAbilityDeactivateEvent extends McMMOPlayerSkillEvent { - - private AbilityType ability; +public class McMMOPlayerAbilityDeactivateEvent extends McMMOPlayerAbilityEvent { public McMMOPlayerAbilityDeactivateEvent(Player player, SkillType skill) { super(player, skill); - this.ability = skill.getAbility(); } - - public AbilityType getAbility() { - return ability; - } - } diff --git a/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityEvent.java b/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityEvent.java new file mode 100644 index 000000000..a65752c55 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/skills/abilities/McMMOPlayerAbilityEvent.java @@ -0,0 +1,20 @@ +package com.gmail.nossr50.events.skills.abilities; + +import org.bukkit.entity.Player; + +import com.gmail.nossr50.datatypes.skills.AbilityType; +import com.gmail.nossr50.datatypes.skills.SkillType; +import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; + +public class McMMOPlayerAbilityEvent extends McMMOPlayerSkillEvent { + private AbilityType ability; + + protected McMMOPlayerAbilityEvent(Player player, SkillType skill) { + super(player, skill); + ability = skill.getAbility(); + } + + public AbilityType getAbility() { + return ability; + } +} diff --git a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingEvent.java b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingEvent.java new file mode 100644 index 000000000..09d8c0842 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingEvent.java @@ -0,0 +1,24 @@ +package com.gmail.nossr50.events.skills.fishing; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +import com.gmail.nossr50.datatypes.skills.SkillType; +import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; + +public class McMMOPlayerFishingEvent extends McMMOPlayerSkillEvent implements Cancellable { + private boolean cancelled; + + protected McMMOPlayerFishingEvent(Player player) { + super(player, SkillType.FISHING); + cancelled = false; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean newValue) { + this.cancelled = newValue; + } +} diff --git a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingTreasureEvent.java b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingTreasureEvent.java index e17851924..dba01b751 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingTreasureEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingTreasureEvent.java @@ -1,20 +1,14 @@ package com.gmail.nossr50.events.skills.fishing; import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; import org.bukkit.inventory.ItemStack; -import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; - -public class McMMOPlayerFishingTreasureEvent extends McMMOPlayerSkillEvent implements Cancellable { - - private boolean cancelled = false; +public class McMMOPlayerFishingTreasureEvent extends McMMOPlayerFishingEvent { private ItemStack treasure; private int xp; public McMMOPlayerFishingTreasureEvent(Player player, ItemStack treasure, int xp) { - super(player, SkillType.FISHING); + super(player); this.treasure = treasure; this.xp = xp; } @@ -27,14 +21,6 @@ public class McMMOPlayerFishingTreasureEvent extends McMMOPlayerSkillEvent imple this.treasure = item; } - public boolean isCancelled() { - return cancelled; - } - - public void setCancelled(boolean newValue) { - this.cancelled = newValue; - } - public int getXp() { return xp; } diff --git a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerMagicHunterEvent.java b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerMagicHunterEvent.java index 828a34220..c481a371e 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerMagicHunterEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerMagicHunterEvent.java @@ -7,7 +7,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public class McMMOPlayerMagicHunterEvent extends McMMOPlayerFishingTreasureEvent { - private Map enchants; public McMMOPlayerMagicHunterEvent(Player player, ItemStack treasure, int xp, Map enchants) { diff --git a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerShakeEvent.java b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerShakeEvent.java index e81dcf368..0953569e4 100644 --- a/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerShakeEvent.java +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerShakeEvent.java @@ -1,30 +1,16 @@ package com.gmail.nossr50.events.skills.fishing; import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; import org.bukkit.inventory.ItemStack; -import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; - -public class McMMOPlayerShakeEvent extends McMMOPlayerSkillEvent implements Cancellable { - - private boolean cancelled = false; +public class McMMOPlayerShakeEvent extends McMMOPlayerFishingEvent { private ItemStack drop; public McMMOPlayerShakeEvent(Player player, ItemStack drop) { - super(player, SkillType.FISHING); + super(player); this.drop = drop; } - public boolean isCancelled() { - return cancelled; - } - - public void setCancelled(boolean newValue) { - this.cancelled = newValue; - } - public ItemStack getDrop() { return drop; } @@ -32,5 +18,4 @@ public class McMMOPlayerShakeEvent extends McMMOPlayerSkillEvent implements Canc public void setDrop(ItemStack drop) { this.drop = drop; } - } diff --git a/src/main/java/com/gmail/nossr50/runnables/items/TeleportationWarmup.java b/src/main/java/com/gmail/nossr50/runnables/items/TeleportationWarmup.java index 374306c80..e22363e1b 100644 --- a/src/main/java/com/gmail/nossr50/runnables/items/TeleportationWarmup.java +++ b/src/main/java/com/gmail/nossr50/runnables/items/TeleportationWarmup.java @@ -4,11 +4,11 @@ import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; -import com.gmail.nossr50.commands.party.teleport.PtpCommand; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.skills.SkillUtils; @@ -58,6 +58,6 @@ public class TeleportationWarmup extends BukkitRunnable { } } - PtpCommand.handlePartyTeleportEvent(teleportingPlayer, targetPlayer); + EventUtils.handlePartyTeleportEvent(teleportingPlayer, targetPlayer); } } diff --git a/src/main/java/com/gmail/nossr50/runnables/skills/AbilityDisableTask.java b/src/main/java/com/gmail/nossr50/runnables/skills/AbilityDisableTask.java index 2c65ede0f..5049d69d8 100644 --- a/src/main/java/com/gmail/nossr50/runnables/skills/AbilityDisableTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/skills/AbilityDisableTask.java @@ -7,8 +7,7 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.HiddenConfig; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.AbilityType; -import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityDeactivateEvent; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.skills.ParticleEffectUtils; import com.gmail.nossr50.util.skills.PerksUtils; @@ -47,8 +46,7 @@ public class AbilityDisableTask extends BukkitRunnable { break; } - McMMOPlayerAbilityDeactivateEvent event = new McMMOPlayerAbilityDeactivateEvent(player, SkillType.byAbility(ability)); - mcMMO.p.getServer().getPluginManager().callEvent(event); + EventUtils.callAbilityDeactivateEvent(player, ability); mcMMOPlayer.setAbilityMode(ability, false); mcMMOPlayer.setAbilityInformed(ability, false); diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java index ec8b9616a..ec29087e1 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java @@ -28,7 +28,6 @@ import org.bukkit.entity.Skeleton; import org.bukkit.entity.Skeleton.SkeletonType; import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.ThrownPotion; -import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.inventory.ItemStack; import org.bukkit.material.Wool; @@ -47,16 +46,14 @@ import com.gmail.nossr50.datatypes.treasure.EnchantmentTreasure; import com.gmail.nossr50.datatypes.treasure.FishingTreasure; import com.gmail.nossr50.datatypes.treasure.Rarity; import com.gmail.nossr50.datatypes.treasure.ShakeTreasure; -import com.gmail.nossr50.events.fake.FakeBlockBreakEvent; -import com.gmail.nossr50.events.fake.FakePlayerFishEvent; import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent; -import com.gmail.nossr50.events.skills.fishing.McMMOPlayerMagicHunterEvent; import com.gmail.nossr50.events.skills.fishing.McMMOPlayerShakeEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.runnables.skills.KrakenAttackTask; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.fishing.Fishing.Tier; import com.gmail.nossr50.util.BlockUtils; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; @@ -211,10 +208,7 @@ public class FishingManager extends SkillManager { return false; } - FakeBlockBreakEvent blockBreakEvent = new FakeBlockBreakEvent(block, player); - mcMMO.p.getServer().getPluginManager().callEvent(blockBreakEvent); - - return !blockBreakEvent.isCancelled(); + return EventUtils.simulateBlockBreak(block, player, false); } /** @@ -278,7 +272,7 @@ public class FishingManager extends SkillManager { } // Recast in the new spot - mcMMO.p.getServer().getPluginManager().callEvent(new FakePlayerFishEvent(getPlayer(), null, hook, PlayerFishEvent.State.FISHING)); + EventUtils.callFakeFishEvent(getPlayer(), hook); } public void masterAngler(Fish hook) { @@ -316,26 +310,20 @@ public class FishingManager extends SkillManager { if (treasure != null) { player.sendMessage(LocaleLoader.getString("Fishing.Ability.TH.ItemFound")); - treasureXp = treasure.getXp(); ItemStack treasureDrop = treasure.getDrop().clone(); // Not cloning is bad, m'kay? - - McMMOPlayerFishingTreasureEvent event; Map enchants = new HashMap(); if (Permissions.magicHunter(player) && ItemUtils.isEnchantable(treasureDrop)) { enchants = handleMagicHunter(treasureDrop); - event = new McMMOPlayerMagicHunterEvent(player, treasureDrop, treasureXp, enchants); + } + + McMMOPlayerFishingTreasureEvent event = EventUtils.callFishingTreasureEvent(player, treasureDrop, treasure.getXp(), enchants); + + if (!event.isCancelled()) { + treasureDrop = event.getTreasure(); + treasureXp = event.getXp(); } else { - event = new McMMOPlayerFishingTreasureEvent(player, treasureDrop, treasureXp); - } - - mcMMO.p.getServer().getPluginManager().callEvent(event); - - treasureDrop = event.getTreasure(); - treasureXp = event.getXp(); - - if (event.isCancelled()) { treasureDrop = null; treasureXp = 0; } @@ -353,7 +341,6 @@ public class FishingManager extends SkillManager { player.sendMessage(LocaleLoader.getString("Fishing.Ability.TH.MagicFound")); } -// Misc.dropItem(player.getEyeLocation(), fishingCatch.getItemStack()); fishingCatch.setItemStack(treasureDrop); } } diff --git a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java index ea667a48a..45768c98b 100644 --- a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java +++ b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java @@ -16,7 +16,6 @@ import org.bukkit.material.CocoaPlant.CocoaPlantSize; import org.bukkit.material.Crops; import org.bukkit.material.NetherWarts; -import com.gmail.nossr50.events.fake.FakeBlockBreakEvent; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.experience.ExperienceConfig; @@ -31,6 +30,7 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.runnables.skills.HerbalismBlockUpdaterTask; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.util.BlockUtils; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.ModUtils; import com.gmail.nossr50.util.Permissions; @@ -229,21 +229,16 @@ public class HerbalismManager extends SkillManager { return false; } - if (treasures.isEmpty()) { - return false; - } + Player player = getPlayer(); - FakeBlockBreakEvent blockBreakEvent = new FakeBlockBreakEvent(blockState.getBlock(), getPlayer()); - mcMMO.p.getServer().getPluginManager().callEvent(blockBreakEvent); - - if (blockBreakEvent.isCancelled()) { + if (treasures.isEmpty() || EventUtils.simulateBlockBreak(blockState.getBlock(), player, false)) { return false; } blockState.setType(Material.AIR); Misc.dropItem(blockState.getLocation(), treasures.get(Misc.getRandom().nextInt(treasures.size())).getDrop()); - getPlayer().sendMessage(LocaleLoader.getString("Herbalism.HylianLuck")); + player.sendMessage(LocaleLoader.getString("Herbalism.HylianLuck")); return true; } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java index 2b54224ab..d1db4d746 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java @@ -17,11 +17,11 @@ import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.experience.ExperienceConfig; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.skills.repair.McMMOPlayerRepairCheckEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.repair.ArcaneForging.Tier; import com.gmail.nossr50.skills.repair.repairables.Repairable; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.StringUtils; @@ -116,10 +116,7 @@ public class RepairManager extends SkillManager { short newDurability = repairCalculate(startDurability, baseRepairAmount); // When I asked for sons? // Call event - McMMOPlayerRepairCheckEvent event = new McMMOPlayerRepairCheckEvent(player, (short) (startDurability - newDurability), toRemove, item); - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { + if (EventUtils.callRepairCheckEvent(player, (short) (startDurability - newDurability), toRemove, item).isCancelled()) { return; } diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java index 8b58780e5..627819c2c 100644 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java +++ b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java @@ -8,14 +8,13 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; import org.bukkit.material.SmoothBrick; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.ToolType; -import com.gmail.nossr50.events.skills.unarmed.McMMOPlayerDisarmEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.SkillManager; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.skills.CombatUtils; @@ -83,15 +82,14 @@ public class UnarmedManager extends SkillManager { */ public void disarmCheck(Player defender) { if (SkillUtils.activationSuccessful(getSkillLevel(), getActivationChance(), Unarmed.disarmMaxChance, Unarmed.disarmMaxBonusLevel) && !hasIronGrip(defender)) { - McMMOPlayerDisarmEvent disarmEvent = new McMMOPlayerDisarmEvent(defender); - mcMMO.p.getServer().getPluginManager().callEvent(disarmEvent); - - if (!disarmEvent.isCancelled()) { - Misc.dropItem(defender.getLocation(), defender.getItemInHand()); - - defender.setItemInHand(new ItemStack(Material.AIR)); - defender.sendMessage(LocaleLoader.getString("Skills.Disarmed")); + if (EventUtils.callDisarmEvent(defender).isCancelled()) { + return; } + + Misc.dropItem(defender.getLocation(), defender.getItemInHand()); + + defender.setItemInHand(new ItemStack(Material.AIR)); + defender.sendMessage(LocaleLoader.getString("Skills.Disarmed")); } } diff --git a/src/main/java/com/gmail/nossr50/util/EventUtils.java b/src/main/java/com/gmail/nossr50/util/EventUtils.java index 324637b67..b4cac0a5b 100644 --- a/src/main/java/com/gmail/nossr50/util/EventUtils.java +++ b/src/main/java/com/gmail/nossr50/util/EventUtils.java @@ -1,16 +1,38 @@ package com.gmail.nossr50.util; +import java.util.Map; + import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Fish; import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerFishEvent; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.PluginManager; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.datatypes.player.McMMOPlayer; +import com.gmail.nossr50.datatypes.player.PlayerProfile; +import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; +import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent; +import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent; import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; +import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent; import com.gmail.nossr50.events.fake.FakeBlockBreakEvent; import com.gmail.nossr50.events.fake.FakeBlockDamageEvent; import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; +import com.gmail.nossr50.events.fake.FakePlayerFishEvent; +import com.gmail.nossr50.events.hardcore.McMMOPlayerDeathPenaltyEvent; +import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent; import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityActivateEvent; +import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityDeactivateEvent; +import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent; +import com.gmail.nossr50.events.skills.fishing.McMMOPlayerMagicHunterEvent; +import com.gmail.nossr50.events.skills.repair.McMMOPlayerRepairCheckEvent; +import com.gmail.nossr50.events.skills.unarmed.McMMOPlayerDisarmEvent; +import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.player.UserManager; public class EventUtils { public static McMMOPlayerAbilityActivateEvent callPlayerAbilityActivateEvent(Player player, SkillType skill) { @@ -27,11 +49,20 @@ public class EventUtils { return event; } - public static McMMOPlayerLevelUpEvent callLevelUpEvent(Player player, SkillType skill, int levelsGained) { - McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained); + public static boolean handleLevelChangeEvent(Player player, SkillType skill, int levelsChanged, float xpRemoved, boolean isLevelUp) { + McMMOPlayerLevelChangeEvent event = isLevelUp ? new McMMOPlayerLevelUpEvent(player, skill, levelsChanged) : new McMMOPlayerLevelDownEvent(player, skill, levelsChanged); mcMMO.p.getServer().getPluginManager().callEvent(event); - return event; + boolean isCancelled = event.isCancelled(); + + if (isCancelled) { + PlayerProfile profile = UserManager.getPlayer(player).getProfile(); + + profile.modifySkill(skill, profile.getSkillLevel(skill) - (isLevelUp ? levelsChanged : - levelsChanged)); + profile.setSkillXpLevel(skill, profile.getSkillXpLevelRaw(skill) + xpRemoved); + } + + return !isCancelled; } /** @@ -58,5 +89,78 @@ public class EventUtils { return !damageEvent.isCancelled() && !breakEvent.isCancelled(); } -} + public static void handlePartyTeleportEvent(Player teleportingPlayer, Player targetPlayer) { + McMMOPlayer mcMMOPlayer = UserManager.getPlayer(teleportingPlayer); + + McMMOPartyTeleportEvent event = new McMMOPartyTeleportEvent(teleportingPlayer, targetPlayer, mcMMOPlayer.getParty().getName()); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return; + } + + teleportingPlayer.teleport(targetPlayer); + + teleportingPlayer.sendMessage(LocaleLoader.getString("Party.Teleport.Player", targetPlayer.getName())); + targetPlayer.sendMessage(LocaleLoader.getString("Party.Teleport.Target", teleportingPlayer.getName())); + + mcMMOPlayer.actualizePtpLastUse(); + } + + public static boolean handleXpGainEvent(Player player, SkillType skill, float xpGained) { + McMMOPlayerXpGainEvent event = new McMMOPlayerXpGainEvent(player, skill, xpGained); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + boolean isCancelled = event.isCancelled(); + + if (!isCancelled) { + PlayerProfile profile = UserManager.getPlayer(player).getProfile(); + profile.setSkillXpLevel(skill, profile.getSkillXpLevelRaw(skill) + event.getRawXpGained()); + } + + return isCancelled; + } + + public static McMMOPlayerAbilityDeactivateEvent callAbilityDeactivateEvent(Player player, AbilityType ability) { + McMMOPlayerAbilityDeactivateEvent event = new McMMOPlayerAbilityDeactivateEvent(player, SkillType.byAbility(ability)); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return event; + } + + public static McMMOPlayerFishingTreasureEvent callFishingTreasureEvent(Player player, ItemStack treasureDrop, int treasureXp, Map enchants) { + McMMOPlayerFishingTreasureEvent event = enchants.isEmpty() ? new McMMOPlayerFishingTreasureEvent(player, treasureDrop, treasureXp) : new McMMOPlayerMagicHunterEvent(player, treasureDrop, treasureXp, enchants); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return event; + } + + public static FakePlayerFishEvent callFakeFishEvent(Player player, Fish hook) { + FakePlayerFishEvent event = new FakePlayerFishEvent(player, null, hook, PlayerFishEvent.State.FISHING); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return event; + } + + public static McMMOPlayerRepairCheckEvent callRepairCheckEvent(Player player, short durability, ItemStack repairMaterial, ItemStack repairedObject) { + McMMOPlayerRepairCheckEvent event = new McMMOPlayerRepairCheckEvent(player, durability, repairMaterial, repairedObject); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return event; + } + + public static McMMOPlayerDeathPenaltyEvent callDeathPenaltyEvent(Player player) { + McMMOPlayerDeathPenaltyEvent event = new McMMOPlayerDeathPenaltyEvent(player); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return event; + } + + public static McMMOPlayerDisarmEvent callDisarmEvent(Player defender) { + McMMOPlayerDisarmEvent event = new McMMOPlayerDisarmEvent(defender); + mcMMO.p.getServer().getPluginManager().callEvent(event); + + return event; + } +} diff --git a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java index 841ebb322..af54beafa 100644 --- a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java +++ b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java @@ -2,11 +2,9 @@ package com.gmail.nossr50.util; import org.bukkit.entity.Player; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.events.hardcore.McMMOPlayerDeathPenaltyEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.util.player.UserManager; @@ -16,10 +14,7 @@ public final class HardcoreManager { public static void invokeStatPenalty(Player player) { double statLossPercentage = Config.getInstance().getHardcoreDeathStatPenaltyPercentage(); - McMMOPlayerDeathPenaltyEvent eventToFire = new McMMOPlayerDeathPenaltyEvent(player); - mcMMO.p.getServer().getPluginManager().callEvent(eventToFire); - - if (eventToFire.isCancelled()) { + if (EventUtils.callDeathPenaltyEvent(player).isCancelled()) { return; } @@ -45,10 +40,7 @@ public final class HardcoreManager { public static void invokeVampirism(Player killer, Player victim) { double vampirismStatLeechPercentage = Config.getInstance().getHardcoreVampirismStatLeechPercentage(); - McMMOPlayerDeathPenaltyEvent eventToFire = new McMMOPlayerDeathPenaltyEvent(victim); - mcMMO.p.getServer().getPluginManager().callEvent(eventToFire); - - if (eventToFire.isCancelled()) { + if (EventUtils.callDeathPenaltyEvent(victim).isCancelled()) { return; } diff --git a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java index 5195fade4..5c1466a2c 100644 --- a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java +++ b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java @@ -24,7 +24,6 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent; import com.gmail.nossr50.events.fake.FakeEntityDamageEvent; -import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.runnables.skills.AwardCombatXpTask; @@ -36,6 +35,7 @@ import com.gmail.nossr50.skills.swords.Swords; import com.gmail.nossr50.skills.swords.SwordsManager; import com.gmail.nossr50.skills.taming.TamingManager; import com.gmail.nossr50.skills.unarmed.UnarmedManager; +import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.MobHealthbarUtils; @@ -415,7 +415,7 @@ public final class CombatUtils { } LivingEntity livingEntity = (LivingEntity) entity; - mcMMO.p.getServer().getPluginManager().callEvent(new FakePlayerAnimationEvent(attacker)); + EventUtils.callFakeArmSwingEvent(attacker); switch (type) { case SWORDS: @@ -543,7 +543,7 @@ public final class CombatUtils { * @param entity The defending Entity * @return true if the Entity should be damaged, false otherwise. */ - public static boolean shouldBeAffected(Player player, Entity entity) { + private static boolean shouldBeAffected(Player player, Entity entity) { if (entity instanceof Player) { Player defender = (Player) entity; @@ -556,10 +556,7 @@ public final class CombatUtils { } // It may seem a bit redundant but we need a check here to prevent bleed from being applied in applyAbilityAoE() - EntityDamageEvent ede = new FakeEntityDamageByEntityEvent(player, entity, EntityDamageEvent.DamageCause.ENTITY_ATTACK, 1); - mcMMO.p.getServer().getPluginManager().callEvent(ede); - - if (ede.isCancelled()) { + if (callFakeDamageEvent(player, entity, 1.0) == 0) { return false; } } @@ -617,11 +614,11 @@ public final class CombatUtils { return (target instanceof Player || (target instanceof Tameable && ((Tameable) target).isTamed())) ? skill.getPVPEnabled() : skill.getPVEEnabled(); } - public static double callFakeDamageEvent(LivingEntity attacker, LivingEntity target, double damage) { + public static double callFakeDamageEvent(Entity attacker, Entity target, double damage) { return callFakeDamageEvent(attacker, target, DamageCause.ENTITY_ATTACK, damage); } - public static double callFakeDamageEvent(Entity attacker, LivingEntity target, DamageCause cause, double damage) { + public static double callFakeDamageEvent(Entity attacker, Entity target, DamageCause cause, double damage) { if (Config.getInstance().getEventCallbackEnabled()) { EntityDamageEvent damageEvent = attacker == null ? new FakeEntityDamageEvent(target, cause, damage) : new FakeEntityDamageByEntityEvent(attacker, target, cause, damage); mcMMO.p.getServer().getPluginManager().callEvent(damageEvent);