From c713d0f0145997e7e20bd7212a519628183c4ac5 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 9 Aug 2016 14:41:41 +0200 Subject: [PATCH] Replace deprecated getItemInHand method --- .../de/epiceric/shopchest/ShopCommand.java | 11 ++-- .../de/epiceric/shopchest/utils/Utils.java | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/ShopCommand.java index b0da1e2..1c27b88 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/ShopCommand.java @@ -15,6 +15,7 @@ import de.epiceric.shopchest.utils.ClickType.EnumClickType; import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.UpdateChecker; import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult; +import de.epiceric.shopchest.utils.Utils; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -274,7 +275,7 @@ class ShopCommand extends BukkitCommand { plugin.debug(p.getName() + " has enabled buying, selling or both"); - if (p.getItemInHand().getType().equals(Material.AIR)) { + if (Utils.getPreferredItemInHand(p) == null) { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_ITEM_IN_HAND)); return; } @@ -291,7 +292,7 @@ class ShopCommand extends BukkitCommand { itemStack = new ItemStack(Material.getMaterial(item), 1); } - if (itemStack.getType().equals(p.getItemInHand().getType()) && itemStack.getDurability() == p.getItemInHand().getDurability()) { + if (itemStack.getType().equals(Utils.getPreferredItemInHand(p).getType()) && itemStack.getDurability() == Utils.getPreferredItemInHand(p).getDurability()) { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CANNOT_SELL_ITEM)); return; } @@ -310,7 +311,7 @@ class ShopCommand extends BukkitCommand { itemStack = new ItemStack(Material.getMaterial(key), 1); } - if (itemStack.getType().equals(p.getItemInHand().getType()) && itemStack.getDurability() == p.getItemInHand().getDurability()) { + if (itemStack.getType().equals(Utils.getPreferredItemInHand(p).getType()) && itemStack.getDurability() == Utils.getPreferredItemInHand(p).getDurability()) { if (buyEnabled) { if ((buyPrice <= amount * price) && (buyPrice > 0)) { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_PRICE_TOO_LOW, new LocalizedMessage.ReplacedRegex(Regex.MIN_PRICE, String.valueOf(amount * price)))); @@ -340,8 +341,8 @@ class ShopCommand extends BukkitCommand { plugin.debug(p.getName() + "'s buy price is high enough"); - ItemStack itemStack = new ItemStack(p.getItemInHand().getType(), amount, p.getItemInHand().getDurability()); - itemStack.setItemMeta(p.getItemInHand().getItemMeta()); + ItemStack itemStack = new ItemStack(Utils.getPreferredItemInHand(p).getType(), amount, Utils.getPreferredItemInHand(p).getDurability()); + itemStack.setItemMeta(Utils.getPreferredItemInHand(p).getItemMeta()); if (Enchantment.DURABILITY.canEnchantItem(itemStack)) { if (itemStack.getDurability() > 0 && !plugin.getShopChestConfig().allow_broken_items) { diff --git a/src/main/java/de/epiceric/shopchest/utils/Utils.java b/src/main/java/de/epiceric/shopchest/utils/Utils.java index eb790db..534a65a 100644 --- a/src/main/java/de/epiceric/shopchest/utils/Utils.java +++ b/src/main/java/de/epiceric/shopchest/utils/Utils.java @@ -2,6 +2,7 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -53,6 +54,55 @@ public class Utils { return amount; } + /** + * @param p Player whose item in his main hand should be returned + * @return {@link ItemStack} in his main hand, or {@code null} if he doesn't hold one + */ + public static ItemStack getItemInMainHand(Player p) { + if (getMajorVersion() < 9) { + if (p.getItemInHand().getType() == Material.AIR) + return null; + else + return p.getItemInHand(); + } else { + if (p.getInventory().getItemInMainHand().getType() == Material.AIR) + return null; + else + return p.getInventory().getItemInMainHand(); + } + } + + /** + * @param p Player whose item in his off hand should be returned + * @return {@link ItemStack} in his off hand, or {@code null} if he doesn't hold one or the server version is below 1.9 + */ + public static ItemStack getItemInOffHand(Player p) { + if (getMajorVersion() < 9) { + return null; + } else { + if (p.getInventory().getItemInOffHand().getType() == Material.AIR) + return null; + else + return p.getInventory().getItemInOffHand(); + } + } + + /** + * @param p Player whose item in his hand should be returned + * @return Item in his main hand, or the item in his off if he doesn't have one in this main hand, or {@code null} + * if he doesn't have one in both hands + */ + public static ItemStack getPreferredItemInHand(Player p) { + if (getMajorVersion() < 9) { + return getItemInMainHand(p); + } else { + if (getItemInMainHand(p) != null) + return getItemInMainHand(p); + else + return getItemInOffHand(p); + } + } + /** * @param className Name of the class * @return Class in {@code net.minecraft.server.[VERSION]} package with the specified name or {@code null} if the class was not found