Prevent legacy material support from being initiated (#4697)

This commit is contained in:
Josh Roy 2022-02-13 15:33:51 -05:00 committed by GitHub
parent 19837f9309
commit 63cbf7e2da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 110 additions and 54 deletions

View File

@ -210,7 +210,7 @@ public class MetaItemStack {
final boolean value = split.length <= 1 || Boolean.parseBoolean(split[1]);
setUnbreakable(ess, stack, value);
} else if (split.length > 1 && (split[0].equalsIgnoreCase("player") || split[0].equalsIgnoreCase("owner")) && hasMetaPermission(sender, "head", false, true, ess)) {
if (MaterialUtil.isPlayerHead(stack.getType(), stack.getDurability())) {
if (MaterialUtil.isPlayerHead(stack)) {
final String owner = split[1];
setSkullOwner(ess, stack, owner);
} else {

View File

@ -3,6 +3,7 @@ package com.earth2me.essentials;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.craftbukkit.SetExpFix;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.IEssentials;
import net.ess3.api.IUser;
import net.ess3.api.MaxMoneyException;
@ -95,9 +96,11 @@ public class Trade {
} else {
if (charge.getItemStack() != null) {
sb.append(charge.getItemStack().getAmount()).append(",");
sb.append(charge.getItemStack().getType().toString()).append(",");
sb.append(charge.getItemStack().getType()).append(",");
if (VersionUtil.PRE_FLATTENING) {
sb.append(charge.getItemStack().getDurability());
}
}
if (charge.getMoney() != null) {
sb.append(charge.getMoney()).append(",");
sb.append("money").append(",");
@ -119,9 +122,11 @@ public class Trade {
} else {
if (pay.getItemStack() != null) {
sb.append(pay.getItemStack().getAmount()).append(",");
sb.append(pay.getItemStack().getType().toString()).append(",");
sb.append(pay.getItemStack().getType()).append(",");
if (VersionUtil.PRE_FLATTENING) {
sb.append(pay.getItemStack().getDurability());
}
}
if (pay.getMoney() != null) {
sb.append(pay.getMoney()).append(",");
sb.append("money").append(",");

View File

@ -319,7 +319,7 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
if (isAuthorized("essentials.itemspawn.item-all") || isAuthorized("essentials.itemspawn.item-" + name))
return true;
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01)) {
if (VersionUtil.PRE_FLATTENING) {
final int id = material.getId();
if (isAuthorized("essentials.itemspawn.item-" + id)) return true;
}

View File

@ -30,13 +30,15 @@ public class Worth implements IConf {
* @return The price from the config.
*/
public BigDecimal getPrice(final IEssentials ess, final ItemStack itemStack) {
BigDecimal result;
BigDecimal result = BigDecimal.ONE.negate();
final String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
if (VersionUtil.PRE_FLATTENING) {
// Check for matches with data value from stack
// Note that we always default to BigDecimal.ONE.negate(), equivalent to -1
result = config.getBigDecimal("worth." + itemname + "." + itemStack.getDurability(), BigDecimal.ONE.negate());
}
// Check for matches with data value 0
if (result.signum() < 0) {
@ -140,7 +142,7 @@ public class Worth implements IConf {
String path = "worth." + itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
// Spigot 1.13+ throws an exception if a 1.13+ plugin even *attempts* to do set data.
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01) && itemStack.getType().getData() == null) {
if (VersionUtil.PRE_FLATTENING && itemStack.getType().getData() == null) {
// Bukkit-bug: getDurability still contains the correct value, while getData().getData() is 0.
path = path + "." + itemStack.getDurability();
}

View File

@ -130,7 +130,7 @@ public class Commandclearinventory extends EssentialsCommand {
} else {
for (final Item item : items) {
final ItemStack stack = new ItemStack(item.getMaterial());
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01)) {
if (VersionUtil.PRE_FLATTENING) {
stack.setDurability(item.getData());
}
// amount -1 means all items will be cleared

View File

@ -4,6 +4,7 @@ import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.Trade.OverflowType;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.MaxMoneyException;
import org.bukkit.Material;
import org.bukkit.Server;
@ -161,8 +162,7 @@ public class Commandcondense extends EssentialsCommand {
iter.remove();
continue;
}
if (inputSlot.getDurability() == Short.MAX_VALUE) {
if (VersionUtil.PRE_FLATTENING && inputSlot.getDurability() == Short.MAX_VALUE) {
inputSlot.setDurability((short) 0);
}
if (!inputSlot.isSimilar(stack)) {

View File

@ -5,6 +5,7 @@ import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.VersionUtil;
import com.google.common.collect.Lists;
import org.bukkit.Material;
import org.bukkit.Server;
@ -37,7 +38,7 @@ public class Commandgive extends EssentialsLoopCommand {
}
try {
if (args.length > 3 && NumberUtil.isInt(args[2]) && NumberUtil.isInt(args[3])) {
if (args.length > 3 && VersionUtil.PRE_FLATTENING && NumberUtil.isInt(args[2]) && NumberUtil.isInt(args[3])) {
stack.setAmount(Integer.parseInt(args[2]));
stack.setDurability(Short.parseShort(args[3]));
} else if (args.length > 2 && Integer.parseInt(args[2]) > 0) {

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.StringUtil;
import com.earth2me.essentials.utils.VersionUtil;
import org.bukkit.Material;
@ -36,7 +37,7 @@ public class Commanditemdb extends EssentialsCommand {
String itemId = "none";
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01)) {
if (VersionUtil.PRE_FLATTENING) {
itemId = itemStack.getType().getId() + ":" + itemStack.getDurability();
}
@ -49,7 +50,7 @@ public class Commanditemdb extends EssentialsCommand {
if (itemHeld && itemStack.getType() != Material.AIR) {
final int maxuses = itemStack.getType().getMaxDurability();
final int durability = (maxuses + 1) - itemStack.getDurability();
final int durability = (maxuses + 1) - MaterialUtil.getDamage(itemStack);
if (maxuses != 0) {
sender.sendMessage(tl("durability", Integer.toString(durability)));
}

View File

@ -117,7 +117,7 @@ public class Commandrecipe extends EssentialsCommand {
if (item == null) {
continue;
}
if (item.getDurability() == Short.MAX_VALUE) {
if (VersionUtil.PRE_FLATTENING && item.getDurability() == Short.MAX_VALUE) {
item.setDurability((short) 0);
}
view.getTopInventory().setItem(j * 3 + k + 1, item);
@ -159,7 +159,7 @@ public class Commandrecipe extends EssentialsCommand {
final InventoryView view = user.getBase().openWorkbench(null, true);
for (int i = 0; i < ingredients.size(); i++) {
final ItemStack item = ingredients.get(i);
if (item.getDurability() == Short.MAX_VALUE) {
if (VersionUtil.PRE_FLATTENING && item.getDurability() == Short.MAX_VALUE) {
item.setDurability((short) 0);
}
view.setItem(i + 1, item);

View File

@ -3,6 +3,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.StringUtil;
import com.earth2me.essentials.utils.VersionUtil;
import com.google.common.collect.Lists;
@ -39,7 +40,7 @@ public class Commandrepair extends EssentialsCommand {
public void repairHand(final User user) throws Exception {
final ItemStack item = user.getItemInHand();
if (item == null || item.getType().isBlock() || item.getDurability() == 0) {
if (item == null || item.getType().isBlock() || MaterialUtil.getDamage(item) == 0) {
throw new Exception(tl("repairInvalidType"));
}
@ -81,16 +82,16 @@ public class Commandrepair extends EssentialsCommand {
throw new Exception(tl("repairInvalidType"));
}
if (item.getDurability() == 0) {
if (MaterialUtil.getDamage(item) == 0) {
throw new Exception(tl("repairAlreadyFixed"));
}
item.setDurability((short) 0);
MaterialUtil.setDamage(item, 0);
}
private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired) throws Exception {
private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired) {
for (final ItemStack item : items) {
if (item == null || item.getType().isBlock() || item.getDurability() == 0) {
if (item == null || item.getType().isBlock() || MaterialUtil.getDamage(item) == 0) {
continue;
}
@ -123,7 +124,7 @@ public class Commandrepair extends EssentialsCommand {
private Trade getCharge(final Material material) {
final String itemName = material.toString().toLowerCase(Locale.ENGLISH);
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01)) {
if (VersionUtil.PRE_FLATTENING) {
final int itemId = material.getId();
return new Trade("repair-" + itemName.replace('_', '-'), new Trade("repair-" + itemId, new Trade("repair-item", ess), ess), ess);
} else {

View File

@ -19,7 +19,7 @@ public class Commandrest extends EssentialsLoopCommand {
@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01)) {
if (VersionUtil.PRE_FLATTENING) {
sender.sendMessage(tl("unsupportedFeature"));
return;
}

View File

@ -42,7 +42,7 @@ public class Commandskull extends EssentialsCommand {
final SkullMeta metaSkull;
boolean spawn = false;
if (itemSkull != null && MaterialUtil.isPlayerHead(itemSkull.getType(), itemSkull.getDurability())) {
if (itemSkull != null && MaterialUtil.isPlayerHead(itemSkull)) {
metaSkull = (SkullMeta) itemSkull.getItemMeta();
} else if (user.isAuthorized("essentials.skull.spawn")) {
itemSkull = new ItemStack(SKULL_ITEM, 1, (byte) 3);

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.NumberUtil;
import com.google.common.collect.Lists;
import org.bukkit.Server;
@ -87,7 +88,7 @@ public class Commandworth extends EssentialsCommand {
}
final BigDecimal result = worth.multiply(BigDecimal.valueOf(amount));
sender.sendMessage(is.getDurability() != 0 ? tl("worthMeta", is.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), is.getDurability(), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)) : tl("worth", is.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)));
sender.sendMessage(MaterialUtil.getDamage(is) != 0 ? tl("worthMeta", is.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), MaterialUtil.getDamage(is), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)) : tl("worth", is.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)));
return result;
}

View File

@ -23,6 +23,7 @@ import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.Potion;
@ -279,12 +280,22 @@ public abstract class AbstractItemDb implements IConf, net.ess3.api.IItemDb {
serializeEffectMeta(sb, fireworkEffectMeta.getEffect());
}
} else if (MaterialUtil.isPotion(material)) {
final boolean splash;
final Collection<PotionEffect> effects;
if (VersionUtil.PRE_FLATTENING) {
final Potion potion = Potion.fromDamage(is.getDurability());
for (final PotionEffect e : potion.getEffects()) {
// long but needs to be effect:speed power:2 duration:120 in that order.
sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" ");
splash = potion.isSplash();
effects = potion.getEffects();
} else {
splash = is.getType() == Material.SPLASH_POTION;
effects = ((PotionMeta) is.getItemMeta()).getCustomEffects();
}
} else if (MaterialUtil.isPlayerHead(material, is.getData().getData())) {
for (final PotionEffect e : effects) {
// long but needs to be effect:speed power:2 duration:120 in that order.
sb.append("splash:").append(splash).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" ");
}
} else if (MaterialUtil.isPlayerHead(is)) {
// item stack with meta
final SkullMeta skullMeta = (SkullMeta) is.getItemMeta();
if (skullMeta != null && skullMeta.hasOwner()) {

View File

@ -5,6 +5,7 @@ import com.earth2me.essentials.Trade;
import com.earth2me.essentials.Trade.OverflowType;
import com.earth2me.essentials.Trade.TradeType;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.NumberUtil;
import net.ess3.api.IEssentials;
import net.ess3.api.MaxMoneyException;
@ -83,7 +84,7 @@ public class SignTrade extends EssentialsSign {
private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException {
final Trade trade = getTrade(sign, 2, AmountType.COST, false, true, ess);
if (trade.getItemStack() != null && player.getBase().getItemInHand() != null && trade.getItemStack().getType() == player.getBase().getItemInHand().getType() && trade.getItemStack().getDurability() == player.getBase().getItemInHand().getDurability() && trade.getItemStack().getEnchantments().equals(player.getBase().getItemInHand().getEnchantments())) {
if (trade.getItemStack() != null && player.getBase().getItemInHand() != null && trade.getItemStack().getType() == player.getBase().getItemInHand().getType() && MaterialUtil.getDamage(trade.getItemStack()) == MaterialUtil.getDamage(player.getBase().getItemInHand()) && trade.getItemStack().getEnchantments().equals(player.getBase().getItemInHand().getEnchantments())) {
final int amount = trade.getItemStack().getAmount();
if (player.getBase().getInventory().containsAtLeast(trade.getItemStack(), amount)) {
final ItemStack stack = player.getBase().getItemInHand().clone();

View File

@ -3,6 +3,8 @@ package com.earth2me.essentials.utils;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.material.MaterialData;
import java.lang.reflect.InvocationTargetException;
@ -11,7 +13,6 @@ import java.util.EnumSet;
import java.util.Set;
public final class MaterialUtil {
public static final Material SPAWNER = EnumUtil.getMaterial("MOB_SPAWNER", "SPAWNER");
private static final Set<Material> BEDS;
private static final Set<Material> BANNERS;
@ -129,20 +130,12 @@ public final class MaterialUtil {
return LEATHER_ARMOR.contains(material);
}
public static boolean isMobHead(final Material material, final int durability) {
if (MOB_HEADS.contains(material)) {
public static boolean isPlayerHead(final ItemStack stack) {
if (PLAYER_HEADS.contains(stack.getType())) {
return true;
}
return LEGACY_SKULLS.contains(material) && (durability != 3);
}
public static boolean isPlayerHead(final Material material, final int durability) {
if (PLAYER_HEADS.contains(material)) {
return true;
}
return LEGACY_SKULLS.contains(material) && durability == 3;
return VersionUtil.PRE_FLATTENING && LEGACY_SKULLS.contains(stack.getType()) && stack.getDurability() == 3;
}
public static boolean isPotion(final Material material) {
@ -166,13 +159,35 @@ public final class MaterialUtil {
}
public static boolean isSkull(final Material material) {
return isPlayerHead(material, -1) || isMobHead(material, -1);
return PLAYER_HEADS.contains(material) || LEGACY_SKULLS.contains(material);
}
public static boolean isAir(final Material material) {
return material == Material.AIR || (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_14_4_R01) && material.isAir());
}
public static int getDamage(final ItemStack stack) {
if (VersionUtil.PRE_FLATTENING) {
return stack.getDurability();
}
if (stack.getItemMeta() instanceof Damageable) {
return ((Damageable) stack.getItemMeta()).getDamage();
}
return 0;
}
public static void setDamage(final ItemStack stack, final int damage) {
if (VersionUtil.PRE_FLATTENING) {
stack.setDurability((short) damage);
} else {
if (stack.getItemMeta() instanceof Damageable) {
final Damageable damageable = (Damageable) stack.getItemMeta();
damageable.setDamage(damage);
stack.setItemMeta(damageable);
}
}
}
public static Material convertFromLegacy(final int id, final byte damage) {
for (final Material material : EnumSet.allOf(Material.class)) {
if (material.getId() == id) {

View File

@ -38,6 +38,8 @@ public final class VersionUtil {
private static final Set<BukkitVersion> supportedVersions = ImmutableSet.of(v1_8_8_R01, v1_9_4_R01, v1_10_2_R01, v1_11_2_R01, v1_12_2_R01, v1_13_2_R01, v1_14_4_R01, v1_15_2_R01, v1_16_5_R01, v1_17_1_R01, v1_18_1_R01);
public static final boolean PRE_FLATTENING = VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01);
private static final Map<String, SupportStatus> unsupportedServerClasses;
static {

View File

@ -69,8 +69,24 @@ public class EssentialsAntiBuildListener implements Listener {
}
return false;
}
if (VersionUtil.PRE_FLATTENING) {
return metaPermCheck(user, action, block.getType(), block.getData());
}
return metaPermCheck(user, action, block.getType());
}
private boolean metaPermCheck(final User user, final String action, final ItemStack item) {
if (item == null) {
if (ess.getSettings().isDebug()) {
logger.log(Level.INFO, "AntiBuild permission check failed, invalid item.");
}
return false;
}
if (VersionUtil.PRE_FLATTENING) {
return metaPermCheck(user, action, item.getType(), item.getDurability());
}
return metaPermCheck(user, action, item.getType());
}
public boolean metaPermCheck(final User user, final String action, final Material material) {
final String blockPerm = "essentials.build." + action + "." + material;
@ -81,7 +97,7 @@ public class EssentialsAntiBuildListener implements Listener {
final String blockPerm = "essentials.build." + action + "." + material;
final String dataPerm = blockPerm + ":" + data;
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_13_0_R01)) {
if (VersionUtil.PRE_FLATTENING) {
if (user.getBase().isPermissionSet(dataPerm)) {
return user.isAuthorized(dataPerm);
} else {
@ -322,7 +338,7 @@ public class EssentialsAntiBuildListener implements Listener {
}
if (prot.getSettingBool(AntiBuildConfig.disable_use) && !user.canBuild()) {
if (event.hasItem() && !metaPermCheck(user, "interact", item.getType(), item.getDurability())) {
if (event.hasItem() && !metaPermCheck(user, "interact", item)) {
event.setCancelled(true);
if (ess.getSettings().warnOnBuildDisallow()) {
user.sendMessage(tl("antiBuildUse", item.getType().toString()));
@ -347,7 +363,7 @@ public class EssentialsAntiBuildListener implements Listener {
final ItemStack item = event.getRecipe().getResult();
if (prot.getSettingBool(AntiBuildConfig.disable_use) && !user.canBuild()) {
if (!metaPermCheck(user, "craft", item.getType(), item.getDurability())) {
if (!metaPermCheck(user, "craft", item)) {
event.setCancelled(true);
if (ess.getSettings().warnOnBuildDisallow()) {
user.sendMessage(tl("antiBuildCraft", item.getType().toString()));
@ -364,7 +380,7 @@ public class EssentialsAntiBuildListener implements Listener {
final ItemStack item = event.getItemDrop().getItemStack();
if (prot.getSettingBool(AntiBuildConfig.disable_use) && !user.canBuild()) {
if (!metaPermCheck(user, "drop", item.getType(), item.getDurability())) {
if (!metaPermCheck(user, "drop", item)) {
event.setCancelled(true);
user.getBase().updateInventory();
if (ess.getSettings().warnOnBuildDisallow()) {
@ -391,7 +407,7 @@ public class EssentialsAntiBuildListener implements Listener {
final ItemStack item = event.getItem().getItemStack();
if (prot.getSettingBool(AntiBuildConfig.disable_use) && !user.canBuild()) {
if (!metaPermCheck(user, "pickup", item.getType(), item.getDurability())) {
if (!metaPermCheck(user, "pickup", item)) {
event.setCancelled(true);
}
}
@ -406,7 +422,7 @@ public class EssentialsAntiBuildListener implements Listener {
final ItemStack item = event.getItem().getItemStack();
if (prot.getSettingBool(AntiBuildConfig.disable_use) && !user.canBuild()) {
if (!metaPermCheck(user, "pickup", item.getType(), item.getDurability())) {
if (!metaPermCheck(user, "pickup", item)) {
event.setCancelled(true);
}
}