From 6dbf2e0d955261380de376e4d924822569b7c167 Mon Sep 17 00:00:00 2001 From: T00thpick1 Date: Thu, 16 May 2013 20:44:45 -0400 Subject: [PATCH] FishingTreasureEvent, FishingShakeEvent and MagicHunterEvent --- .../McMMOPlayerFishingTreasureEvent.java | 45 +++++++++++++ .../fishing/McMMOPlayerMagicHunterEvent.java | 22 +++++++ .../skills/fishing/McMMOPlayerShakeEvent.java | 36 +++++++++++ .../skills/fishing/FishingManager.java | 63 ++++++++++++++++--- 4 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingTreasureEvent.java create mode 100644 src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerMagicHunterEvent.java create mode 100644 src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerShakeEvent.java 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 new file mode 100644 index 000000000..a9eb58d7a --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerFishingTreasureEvent.java @@ -0,0 +1,45 @@ +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; + private ItemStack treasure; + private int xp; + + public McMMOPlayerFishingTreasureEvent(Player player, ItemStack treasure, int xp) { + super(player, SkillType.FISHING); + this.treasure = treasure; + this.xp = xp; + } + + public ItemStack getTreasure() { + return treasure; + } + + public void setTreasure(ItemStack item) { + this.treasure = item; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean newValue) { + this.cancelled = newValue; + } + + public int getXp() { + return xp; + } + + public void setXp(int xp) { + this.xp = 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 new file mode 100644 index 000000000..53a98a1fb --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerMagicHunterEvent.java @@ -0,0 +1,22 @@ +package com.gmail.nossr50.events.skills.fishing; + +import java.util.Map; + +import org.bukkit.enchantments.Enchantment; +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) { + super(player, treasure, xp); + this.enchants = enchants; + } + + public Map getEnchantments() { + return 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 new file mode 100644 index 000000000..b91a33063 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/skills/fishing/McMMOPlayerShakeEvent.java @@ -0,0 +1,36 @@ +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; + private ItemStack drop; + + public McMMOPlayerShakeEvent(Player player, ItemStack drop) { + super(player, SkillType.FISHING); + this.drop = drop; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean newValue) { + this.cancelled = newValue; + } + + public ItemStack getDrop() { + return drop; + } + + public void setDrop(ItemStack drop) { + this.drop = drop; + } + +} 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 c06f59d3f..5279adb4e 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java @@ -2,7 +2,9 @@ package com.gmail.nossr50.skills.fishing; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -37,6 +39,9 @@ import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.treasure.FishingTreasure; import com.gmail.nossr50.datatypes.treasure.ShakeTreasure; 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; @@ -269,13 +274,43 @@ public class FishingManager extends SkillManager { treasureXp = treasure.getXp(); ItemStack treasureDrop = treasure.getDrop().clone(); // Not cloning is bad, m'kay? - if (Permissions.magicHunter(player) && ItemUtils.isEnchantable(treasureDrop) && handleMagicHunter(treasureDrop)) { - player.sendMessage(LocaleLoader.getString("Fishing.MagicFound")); + McMMOPlayerFishingTreasureEvent event; + Map enchants = new HashMap(); + + if (Permissions.magicHunter(player) && ItemUtils.isEnchantable(treasureDrop)) { + enchants = handleMagicHunter(treasureDrop); + event = new McMMOPlayerMagicHunterEvent(player, treasureDrop, treasureXp, enchants); + } + 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; } // Drop the original catch at the feet of the player and set the treasure as the real catch - Misc.dropItem(player.getEyeLocation(), fishingCatch.getItemStack()); - fishingCatch.setItemStack(treasureDrop); + if (treasureDrop != null) { + boolean enchanted = false; + + if (!enchants.isEmpty()) { + treasureDrop.addUnsafeEnchantments(enchants); + enchanted = true; + } + + if (enchanted) { + player.sendMessage(LocaleLoader.getString("Fishing.MagicFound")); + } + + Misc.dropItem(player.getEyeLocation(), fishingCatch.getItemStack()); + fishingCatch.setItemStack(treasureDrop); + } } applyXpGain(Config.getInstance().getFishingBaseXP() + treasureXp); @@ -351,6 +386,14 @@ public class FishingManager extends SkillManager { break; } + McMMOPlayerShakeEvent event = new McMMOPlayerShakeEvent(getPlayer(), drop); + + drop = event.getDrop(); + + if (event.isCancelled() || drop == null) { + return; + } + Misc.dropItem(target.getLocation(), drop); CombatUtils.dealDamage(target, Math.max(target.getMaxHealth() / 4, 1)); // Make it so you can shake a mob no more than 4 times. } @@ -399,7 +442,7 @@ public class FishingManager extends SkillManager { * @param treasureDrop The {@link ItemStack} to enchant * @return true if the item has been enchanted */ - private boolean handleMagicHunter(ItemStack treasureDrop) { + private Map handleMagicHunter(ItemStack treasureDrop) { Player player = getPlayer(); int activationChance = this.activationChance; @@ -407,8 +450,10 @@ public class FishingManager extends SkillManager { activationChance *= Fishing.STORM_MODIFIER; } + Map enchants = new HashMap(); + if (Misc.getRandom().nextInt(activationChance) > getLootTier() * AdvancedConfig.getInstance().getFishingMagicMultiplier()) { - return false; + return enchants; } List possibleEnchantments = getPossibleEnchantments(treasureDrop); @@ -416,7 +461,6 @@ public class FishingManager extends SkillManager { // This make sure that the order isn't always the same, for example previously Unbreaking had a lot more chance to be used than any other enchant Collections.shuffle(possibleEnchantments, Misc.getRandom()); - boolean enchanted = false; int specificChance = 1; for (Enchantment possibleEnchantment : possibleEnchantments) { @@ -424,13 +468,12 @@ public class FishingManager extends SkillManager { continue; } - treasureDrop.addEnchantment(possibleEnchantment, Math.max(Misc.getRandom().nextInt(possibleEnchantment.getMaxLevel()) + 1, possibleEnchantment.getStartLevel())); + enchants.put(possibleEnchantment, Math.max(Misc.getRandom().nextInt(possibleEnchantment.getMaxLevel()) + 1, possibleEnchantment.getStartLevel())); specificChance++; - enchanted = true; } - return enchanted; + return enchants; } private List getPossibleEnchantments(ItemStack treasureDrop) {