Restore basic pre-1.11 compat

This commit is contained in:
vemacs 2016-11-28 17:13:36 -06:00
parent 4102b11f15
commit 96164f5851
5 changed files with 84 additions and 9 deletions

View File

@ -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);
}
}
}

View File

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

View File

@ -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"));

View File

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

View File

@ -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);
}
}
}
}