Skip to content

Commit 704f883

Browse files
authored
Merge branch 'dev/feature' into shane/fix-exprsets
2 parents 20ae339 + 48d0bbe commit 704f883

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1273
-612
lines changed

src/main/java/ch/njol/skript/classes/data/BukkitClasses.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.bukkit.block.Block;
4646
import org.bukkit.block.BlockState;
4747
import org.bukkit.block.DoubleChest;
48+
import org.bukkit.block.banner.PatternType;
4849
import org.bukkit.block.data.BlockData;
4950
import org.bukkit.command.CommandSender;
5051
import org.bukkit.enchantments.Enchantment;
@@ -1568,6 +1569,39 @@ public String toVariableNameString(EnchantmentOffer eo) {
15681569
);
15691570
}
15701571

1572+
Classes.registerClass(new ClassInfo<>(org.bukkit.block.banner.Pattern.class, "bannerpattern")
1573+
.user("banner ?patterns?")
1574+
.name("Banner Pattern")
1575+
.description("Represents a banner pattern.")
1576+
.since("INSERT VERSION")
1577+
);
1578+
1579+
ClassInfo<?> patternTypeInfo;
1580+
Registry<PatternType> patternRegistry = Bukkit.getRegistry(PatternType.class);
1581+
if (patternRegistry != null) {
1582+
patternTypeInfo = new RegistryClassInfo<>(PatternType.class, patternRegistry, "bannerpatterntype", "banner pattern types");
1583+
} else {
1584+
try {
1585+
Class<?> patternClass = Class.forName("org.bukkit.block.banner.PatternType");
1586+
if (patternClass.isEnum()) {
1587+
//noinspection unchecked,rawtypes
1588+
Class<? extends Enum> enumClass = (Class<? extends Enum>) patternClass;
1589+
//noinspection rawtypes,unchecked
1590+
patternTypeInfo = new EnumClassInfo<>(enumClass, "bannerpatterntype", "banner pattern types");
1591+
} else {
1592+
throw new IllegalStateException("PatternType is neither an enum nor a valid registry.");
1593+
}
1594+
} catch (ClassNotFoundException e) {
1595+
throw new RuntimeException(e);
1596+
}
1597+
}
1598+
Classes.registerClass(patternTypeInfo
1599+
.user("banner ?pattern ?types?")
1600+
.name("Banner Pattern Type")
1601+
.description("Represents the various banner patterns that can be applied to a banner.")
1602+
.since("INSERT VERSION")
1603+
);
1604+
15711605
}
15721606

15731607
}

src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public Player get(final BlockIgniteEvent e) {
244244
@Override
245245
@Nullable
246246
public Block get(final BlockIgniteEvent e) {
247-
return e.getIgnitingBlock();
247+
return e.getBlock();
248248
}
249249
}, 0);
250250
// BlockDispenseEvent

src/main/java/ch/njol/skript/doc/DocumentationId.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@Retention(RetentionPolicy.RUNTIME)
1515
@Documented
1616
public @interface DocumentationId {
17-
18-
public String value();
17+
18+
public String value();
19+
1920
}

src/main/java/ch/njol/skript/doc/RequiredPlugins.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
* Provides a list of plugins other than Skript that the annotated
1111
* element requires to be used. Non-Spigot server software can be considered
1212
* to be plugins.
13-
*
13+
*
1414
*/
1515
@Target(ElementType.TYPE)
1616
@Retention(RetentionPolicy.RUNTIME)
1717
@Documented
1818
public @interface RequiredPlugins {
19-
20-
String[] value();
19+
20+
String[] value();
21+
2122
}

src/main/java/ch/njol/skript/entity/BoatData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public BoatData(@Nullable Boat.Type type){
5050
private BoatData(int type) {
5151
matchedPattern = type;
5252
}
53-
53+
5454
@Override
5555
protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) {
5656
return true;
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)