Skip to content

Commit

Permalink
Improve 1.21.2+ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Dec 16, 2024
1 parent 666ef8a commit 7d455cc
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/main/java/cn/dreeam/surf/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import cn.dreeam.surf.Surf;
import cn.dreeam.surf.config.Config;
import com.cryptomorin.xseries.XMaterial;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.jetbrains.annotations.NotNull;
import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
Expand Down Expand Up @@ -36,6 +37,9 @@ public class ItemUtil {
public static final List<String> illegalEnchants = initIllegalEnchants();
public static final Map<String, Integer> illegalEnchantsMap = initIllegalEnchantsMap();

private static Method attributeValueOf;
private static Method attributeValues;

/*
public static boolean isContainer(ItemStack i) {
switch (i.getType()) {
Expand Down Expand Up @@ -157,7 +161,7 @@ public static boolean hasIllegalItemFlag(ItemStack i) {
public static boolean hasIllegalAttributes(ItemStack i) {
if (i.hasItemMeta()) {
for (String attribute : Config.antiIllegalIllegalAttributeModifierList) {
if (i.getItemMeta().getAttributeModifiers(Attribute.valueOf(attribute)) != null) {
if (i.getItemMeta().getAttributeModifiers(getAttributeByName(attribute)) != null) {
return true;
}
}
Expand Down Expand Up @@ -272,8 +276,8 @@ private static ItemStack cleanIllegals(ItemStack i) {

// Clean illegal AttributeModifier
for (String attribute : Config.antiIllegalIllegalAttributeModifierList) {
if (meta.getAttributeModifiers(Attribute.valueOf(attribute)) != null) {
meta.removeAttributeModifier(Attribute.valueOf(attribute));
if (meta.getAttributeModifiers(getAttributeByName(attribute)) != null) {
meta.removeAttributeModifier(getAttributeByName(attribute));
}
}

Expand Down Expand Up @@ -305,6 +309,8 @@ private static List<String> initIllegalBlocks() {
"REINFORCED_DEEPSLATE"
));

// <= 1.12.2
if (Util.isNewerAndEqual(12, 0)) {
return list;
}

Expand All @@ -321,7 +327,7 @@ private static List<String> initIllegalItemFlags() {
private static List<String> initIllegalAttribute() {
List<String> list = new ArrayList<>();

for (Attribute attribute : Attribute.values()) {
for (Attribute attribute : getAttributes()) {
list.add(attribute.toString());
}

Expand Down Expand Up @@ -390,4 +396,38 @@ private static Map<String, Integer> initIllegalEnchantsMap() {
public static String getItemDisplayName(ItemStack itemStack) {
return PlainTextComponentSerializer.plainText().serialize(itemStack.displayName());
}

private static Attribute getAttributeByName(String name) {
if (Util.isNewerThan(21, 1)) {
return Registry.ATTRIBUTE.get(Key.key(name));
}

try {
if (attributeValueOf == null) {
attributeValueOf = Attribute.class.getMethod("valueOf", String.class);
}

return (Attribute) attributeValueOf.invoke(null, name);
} catch (ReflectiveOperationException e) {
Surf.LOGGER.error(e);
return null;
}
}

private static Attribute[] getAttributes() {
if (Util.isNewerThan(21, 1)) {
return Registry.ATTRIBUTE.stream().toArray(Attribute[]::new);
}

try {
if (attributeValues == null) {
attributeValues = Attribute.class.getMethod("values");
}

return (Attribute[]) attributeValues.invoke(null);
} catch (ReflectiveOperationException e) {
Surf.LOGGER.error(e);
return null;
}
}
}

0 comments on commit 7d455cc

Please sign in to comment.