Duplicate code is bad, m'kay?

This commit is contained in:
GJ 2013-02-14 19:02:50 -05:00
parent d39ff036c7
commit caa1b0d14c
3 changed files with 30 additions and 75 deletions

View File

@ -1,11 +1,8 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
import java.util.List;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.Furnace; import org.bukkit.block.Furnace;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -19,11 +16,11 @@ import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.FurnaceInventory; import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.skills.smelting.SmeltingManager; import com.gmail.nossr50.skills.smelting.SmeltingManager;
import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Users; import com.gmail.nossr50.util.Users;
@ -137,27 +134,6 @@ public class InventoryListener implements Listener{
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryClickEvent(InventoryClickEvent event) { public void onInventoryClickEvent(InventoryClickEvent event) {
ItemStack item = event.getCurrentItem(); ItemStack item = event.getCurrentItem();
SkillTools.removeAbilityBuff(item); //Remove enchants if they try to move them in an inventory
if (item != null && item.containsEnchantment(Enchantment.DIG_SPEED)) {
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta.hasLore()) {
int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
List<String> itemLore = itemMeta.getLore();
if (itemLore.remove("mcMMO Ability Tool")) {
if (efficiencyLevel <= 5) {
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
}
else {
itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true);
}
itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta);
return;
}
}
}
} }
} }

View File

@ -1,10 +1,7 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
import java.util.List;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
@ -24,7 +21,6 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.chat.ChatManager; import com.gmail.nossr50.chat.ChatManager;
@ -49,8 +45,8 @@ import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.BlockChecks; import com.gmail.nossr50.util.BlockChecks;
import com.gmail.nossr50.util.Hardcore; import com.gmail.nossr50.util.Hardcore;
import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Motd;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Motd;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.Users; import com.gmail.nossr50.util.Users;
@ -79,31 +75,7 @@ public class PlayerListener implements Listener {
if (playerProfile.getAbilityMode(AbilityType.GIGA_DRILL_BREAKER) || playerProfile.getAbilityMode(AbilityType.SUPER_BREAKER)) { if (playerProfile.getAbilityMode(AbilityType.GIGA_DRILL_BREAKER) || playerProfile.getAbilityMode(AbilityType.SUPER_BREAKER)) {
for (ItemStack item : event.getDrops()) { for (ItemStack item : event.getDrops()) {
if (item == null || item.getType() == Material.AIR ) { SkillTools.removeAbilityBuff(item);
continue;
}
if (item.containsEnchantment(Enchantment.DIG_SPEED)) {
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta.hasLore()) {
int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
List<String> itemLore = itemMeta.getLore();
if (itemLore.remove("mcMMO Ability Tool")) {
if (efficiencyLevel <= 5) {
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
}
else {
itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true);
}
itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta);
return;
}
}
}
} }
} }
} }

View File

@ -545,19 +545,26 @@ public class SkillTools {
public static void handleAbilitySpeedDecrease(Player player) { public static void handleAbilitySpeedDecrease(Player player) {
PlayerInventory playerInventory = player.getInventory(); PlayerInventory playerInventory = player.getInventory();
for (ItemStack item : playerInventory.getContents()) { for (int i = 0; i < playerInventory.getContents().length; i++) {
ItemStack item = playerInventory.getItem(i);
playerInventory.setItem(i, removeAbilityBuff(item));
}
}
public static ItemStack removeAbilityBuff(ItemStack item) {
if (item == null || item.getType() == Material.AIR ) { if (item == null || item.getType() == Material.AIR ) {
continue; return item;
} }
if (item.containsEnchantment(Enchantment.DIG_SPEED)) { if (item.containsEnchantment(Enchantment.DIG_SPEED)) {
ItemMeta itemMeta = item.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
if (itemMeta.hasLore()) { if (itemMeta.hasLore()) {
int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
List<String> itemLore = itemMeta.getLore(); List<String> itemLore = itemMeta.getLore();
if (itemLore.remove("mcMMO Ability Tool")) { if (itemLore.remove("mcMMO Ability Tool")) {
int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
if (efficiencyLevel <= 5) { if (efficiencyLevel <= 5) {
itemMeta.removeEnchant(Enchantment.DIG_SPEED); itemMeta.removeEnchant(Enchantment.DIG_SPEED);
} }
@ -567,10 +574,10 @@ public class SkillTools {
itemMeta.setLore(itemLore); itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta); item.setItemMeta(itemMeta);
return;
}
} }
} }
} }
return item;
} }
} }