Replace deprecated getItemInHand method

This commit is contained in:
Eric 2016-08-09 14:41:41 +02:00
parent a95106a335
commit c713d0f014
2 changed files with 56 additions and 5 deletions

View File

@ -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) {

View File

@ -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