|
| 1 | +package ch.njol.skript.expressions; |
| 2 | + |
| 3 | +import ch.njol.skript.Skript; |
| 4 | +import ch.njol.skript.aliases.ItemType; |
| 5 | +import ch.njol.skript.doc.Description; |
| 6 | +import ch.njol.skript.doc.Examples; |
| 7 | +import ch.njol.skript.doc.Name; |
| 8 | +import ch.njol.skript.doc.Since; |
| 9 | +import ch.njol.skript.lang.Expression; |
| 10 | +import ch.njol.skript.lang.ExpressionType; |
| 11 | +import ch.njol.skript.lang.Literal; |
| 12 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 13 | +import ch.njol.skript.lang.util.SimpleExpression; |
| 14 | +import ch.njol.util.Kleenean; |
| 15 | +import org.bukkit.Bukkit; |
| 16 | +import org.bukkit.Material; |
| 17 | +import org.bukkit.NamespacedKey; |
| 18 | +import org.bukkit.Registry; |
| 19 | +import org.bukkit.block.banner.PatternType; |
| 20 | +import org.bukkit.event.Event; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
| 23 | +import java.lang.reflect.InvocationTargetException; |
| 24 | +import java.util.*; |
| 25 | + |
| 26 | +@Name("Banner Pattern Item") |
| 27 | +@Description({ |
| 28 | + "Gets the item from a banner pattern type.", |
| 29 | + "Note that not all banner pattern types have an item.", |
| 30 | +}) |
| 31 | +@Examples({ |
| 32 | + "set {_item} to creeper charged banner pattern item", |
| 33 | + "set {_item} to snout banner pattern item", |
| 34 | + "set {_item} to thing banner pattern item" |
| 35 | +}) |
| 36 | +@Since("INSERT VERSION") |
| 37 | +public class ExprBannerItem extends SimpleExpression<ItemType> { |
| 38 | + |
| 39 | + private static final Map<Object, Material> bannerMaterials = new HashMap<>(); |
| 40 | + private static boolean PATTERN_TYPE_IS_REGISTRY = false; |
| 41 | + |
| 42 | + static { |
| 43 | + Registry<PatternType> patternRegistry = Bukkit.getRegistry(PatternType.class); |
| 44 | + Object[] bannerPatterns; |
| 45 | + if (patternRegistry != null) { |
| 46 | + bannerPatterns = patternRegistry.stream().toArray(); |
| 47 | + PATTERN_TYPE_IS_REGISTRY = true; |
| 48 | + } else { |
| 49 | + try { |
| 50 | + Class<?> patternClass = Class.forName("org.bukkit.block.banner.PatternType"); |
| 51 | + if (patternClass.isEnum()) { |
| 52 | + //noinspection unchecked,rawtypes |
| 53 | + Class<? extends Enum> enumClass = (Class<? extends Enum>) patternClass; |
| 54 | + bannerPatterns = enumClass.getEnumConstants(); |
| 55 | + } else { |
| 56 | + throw new IllegalStateException("PatternType is neither an enum nor a valid registry."); |
| 57 | + } |
| 58 | + } catch (ClassNotFoundException e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + if (bannerPatterns != null) { |
| 63 | + for (Object object : bannerPatterns) { |
| 64 | + Material material = getMaterial(object); |
| 65 | + if (material != null) |
| 66 | + bannerMaterials.put(object, material); |
| 67 | + } |
| 68 | + Skript.registerExpression(ExprBannerItem.class, ItemType.class, ExpressionType.COMBINED, |
| 69 | + "[a[n]] %*bannerpatterntypes% item[s]"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private PatternType[] patternTypes; |
| 74 | + private Literal<PatternType> literalPattern; |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 78 | + //noinspection unchecked |
| 79 | + literalPattern = (Literal<PatternType>) exprs[0]; |
| 80 | + patternTypes = literalPattern.getArray(); |
| 81 | + for (PatternType type : patternTypes) { |
| 82 | + if (!bannerMaterials.containsKey(type)) { |
| 83 | + Skript.error("There is no item for the banner pattern type '" + type + "'."); |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected ItemType @Nullable [] get(Event event) { |
| 92 | + List<ItemType> itemTypes = new ArrayList<>(); |
| 93 | + for (PatternType type : patternTypes) { |
| 94 | + Material material = bannerMaterials.get(type); |
| 95 | + ItemType itemType = new ItemType(material); |
| 96 | + itemTypes.add(itemType); |
| 97 | + } |
| 98 | + return itemTypes.toArray(new ItemType[0]); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean isSingle() { |
| 103 | + return literalPattern.isSingle(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Class<ItemType> getReturnType() { |
| 108 | + return ItemType.class; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String toString(@Nullable Event event, boolean debug) { |
| 113 | + return literalPattern.toString(event, debug) + " items"; |
| 114 | + } |
| 115 | + |
| 116 | + private static @Nullable Material getMaterial(Object object) { |
| 117 | + if (!(object instanceof PatternType patternType)) |
| 118 | + return null; |
| 119 | + String key = null; |
| 120 | + try { |
| 121 | + if (PATTERN_TYPE_IS_REGISTRY) { |
| 122 | + NamespacedKey namespacedKey = (NamespacedKey) PatternType.class.getMethod("getKey").invoke(patternType); |
| 123 | + if (namespacedKey != null) |
| 124 | + key = namespacedKey.getKey().toUpperCase(Locale.ENGLISH); |
| 125 | + } else { |
| 126 | + key = (String) PatternType.class.getMethod("toString").invoke(patternType); |
| 127 | + } |
| 128 | + } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { |
| 129 | + throw new RuntimeException(e); |
| 130 | + } |
| 131 | + if (key == null) |
| 132 | + return null; |
| 133 | + Material material = Material.getMaterial(key + "_BANNER_PATTERN"); |
| 134 | + return material != null ? material : checkAlias(patternType); |
| 135 | + } |
| 136 | + |
| 137 | + private static @Nullable Material checkAlias(PatternType patternType) { |
| 138 | + if (patternType == PatternType.BRICKS && Material.getMaterial("FIELD_MASONED_BANNER_PATTERN") != null) { |
| 139 | + return Material.FIELD_MASONED_BANNER_PATTERN; |
| 140 | + } else if (patternType == PatternType.BORDER && Material.getMaterial("BORDURE_INDENTED_BANNER_PATTER") != null) { |
| 141 | + return Material.BORDURE_INDENTED_BANNER_PATTERN; |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments