diff --git a/Essentials/src/com/earth2me/essentials/MobData.java b/Essentials/src/com/earth2me/essentials/MobData.java index b2d1324c3..d44f8248a 100644 --- a/Essentials/src/com/earth2me/essentials/MobData.java +++ b/Essentials/src/com/earth2me/essentials/MobData.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.utils.StringUtil; import org.bukkit.DyeColor; import org.bukkit.Material; @@ -240,8 +241,8 @@ public enum MobData { ((Horse) spawned).getInventory().setArmor(new ItemStack((Material) this.value, 1)); } else if (this.type.equals(EntityType.ZOMBIE.getEntityClass()) || this.type.equals(EntityType.SKELETON)) { final EntityEquipment invent = ((LivingEntity) spawned).getEquipment(); - invent.setItemInMainHand(new ItemStack((Material) this.value, 1)); - invent.setItemInMainHandDropChance(0.1f); + InventoryWorkaround.setItemInMainHand(invent, new ItemStack((Material) this.value, 1)); + InventoryWorkaround.setItemInMainHandDropChance(invent, 0.1f); } } } diff --git a/Essentials/src/com/earth2me/essentials/SpawnMob.java b/Essentials/src/com/earth2me/essentials/SpawnMob.java index 038fd65b3..314d96add 100644 --- a/Essentials/src/com/earth2me/essentials/SpawnMob.java +++ b/Essentials/src/com/earth2me/essentials/SpawnMob.java @@ -1,6 +1,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.Mob.MobException; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.StringUtil; import net.ess3.api.IEssentials; @@ -224,8 +225,8 @@ public class SpawnMob { private static void defaultMobData(final EntityType type, final Entity spawned) { if (type == EntityType.SKELETON) { final EntityEquipment invent = ((LivingEntity) spawned).getEquipment(); - invent.setItemInMainHand(new ItemStack(Material.BOW, 1)); - invent.setItemInMainHandDropChance(0.1f); + InventoryWorkaround.setItemInMainHand(invent, new ItemStack(Material.BOW, 1)); + InventoryWorkaround.setItemInMainHandDropChance(invent, 0.1f); invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1)); invent.setBootsDropChance(0.0f); @@ -233,10 +234,11 @@ public class SpawnMob { if (type == EntityType.PIG_ZOMBIE) { final PigZombie zombie = ((PigZombie) spawned); + setVillager(zombie); final EntityEquipment invent = zombie.getEquipment(); - invent.setItemInMainHand(new ItemStack(Material.GOLD_SWORD, 1)); - invent.setItemInMainHandDropChance(0.1f); + InventoryWorkaround.setItemInMainHand(invent, new ItemStack(Material.GOLD_SWORD, 1)); + InventoryWorkaround.setItemInMainHandDropChance(invent, 0.1f); invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1)); invent.setBootsDropChance(0.0f); @@ -254,4 +256,12 @@ public class SpawnMob { ((Horse) spawned).setJumpStrength(1.2); } } + + @SuppressWarnings("deprecation") + private static void setVillager(Zombie zombie) { + try { + zombie.setVillager(false); + } catch (Exception ignored) { + } + } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbook.java b/Essentials/src/com/earth2me/essentials/commands/Commandbook.java index 079756d91..902f085fb 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandbook.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbook.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.User; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.inventory.ItemStack; @@ -42,7 +43,7 @@ public class Commandbook extends EssentialsCommand { if (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others")) { ItemStack newItem = new ItemStack(Material.BOOK_AND_QUILL, item.getAmount()); newItem.setItemMeta(bmeta); - user.getBase().getInventory().setItemInMainHand(newItem); + InventoryWorkaround.setItemInMainHand(user.getBase(), newItem); user.sendMessage(tl("editBookContents")); } else { throw new Exception(tl("denyBookEdit")); @@ -55,7 +56,7 @@ public class Commandbook extends EssentialsCommand { } ItemStack newItem = new ItemStack(Material.WRITTEN_BOOK, item.getAmount()); newItem.setItemMeta(bmeta); - user.getBase().getInventory().setItemInMainHand(newItem); + InventoryWorkaround.setItemInMainHand(user.getBase(), newItem); user.sendMessage(tl("bookLocked")); } else { throw new Exception(tl("holdBook")); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java index 7f81dc76c..7988da3eb 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.Enchantments; import com.earth2me.essentials.MetaItemStack; import com.earth2me.essentials.User; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import com.earth2me.essentials.utils.StringUtil; import org.bukkit.Material; import org.bukkit.Server; @@ -55,7 +56,7 @@ public class Commandenchant extends EssentialsCommand { final MetaItemStack metaStack = new MetaItemStack(stack); final Enchantment enchantment = metaStack.getEnchantment(user, args[0]); metaStack.addEnchantment(user.getSource(), allowUnsafe, enchantment, level); - user.getBase().getInventory().setItemInMainHand(metaStack.getItemStack()); + InventoryWorkaround.setItemInMainHand(user.getBase(), metaStack.getItemStack()); user.getBase().updateInventory(); final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java index ed4dec1a7..cb4fd6d88 100644 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java @@ -2,6 +2,8 @@ package com.earth2me.essentials.craftbukkit; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -177,4 +179,64 @@ public final class InventoryWorkaround { } return leftover; } + + // Hot-ish code so cache + private static Boolean hasMainHandSupport = null; + + @SuppressWarnings("deprecation") + public static void setItemInMainHand(Player p, ItemStack item) { + if (hasMainHandSupport == null) { + try { + p.getInventory().setItemInMainHand(item); + hasMainHandSupport = true; + } catch (Exception e) { + p.setItemInHand(item); + hasMainHandSupport = false; + } + } else { + if (hasMainHandSupport) { + p.getInventory().setItemInMainHand(item); + } else { + p.setItemInHand(item); + } + } + } + + @SuppressWarnings("deprecation") + public static void setItemInMainHand(EntityEquipment invent, ItemStack item) { + if (hasMainHandSupport == null) { + try { + invent.setItemInMainHand(item); + hasMainHandSupport = true; + } catch (Exception e) { + invent.setItemInHand(item); + hasMainHandSupport = false; + } + } else { + if (hasMainHandSupport) { + invent.setItemInMainHand(item); + } else { + invent.setItemInHand(item); + } + } + } + + @SuppressWarnings("deprecation") + public static void setItemInMainHandDropChance(EntityEquipment invent, float chance) { + if (hasMainHandSupport == null) { + try { + invent.setItemInMainHandDropChance(chance); + hasMainHandSupport = true; + } catch (Exception e) { + invent.setItemInHandDropChance(chance); + hasMainHandSupport = false; + } + } else { + if (hasMainHandSupport) { + invent.setItemInMainHandDropChance(chance); + } else { + invent.setItemInHandDropChance(chance); + } + } + } }