Skip to content

Commit 0f250c0

Browse files
Merge branch 'dev/patch' into patch/archive-docs
2 parents 76ddb64 + 2f629f1 commit 0f250c0

23 files changed

+309
-102
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies {
2929
shadow group: 'org.bstats', name: 'bstats-bukkit', version: '3.0.2'
3030
shadow group: 'net.kyori', name: 'adventure-text-serializer-bungeecord', version: '4.3.2'
3131

32-
implementation group: 'io.papermc.paper', name: 'paper-api', version: '1.20.6-R0.1-SNAPSHOT'
32+
implementation group: 'io.papermc.paper', name: 'paper-api', version: '1.21-R0.1-SNAPSHOT'
3333
implementation group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '2.2.700'
3434
implementation group: 'com.google.code.findbugs', name: 'findbugs', version: '3.0.1'
3535
implementation group: 'com.sk89q.worldguard', name: 'worldguard-legacy', version: '7.0.0-SNAPSHOT'
@@ -235,7 +235,7 @@ def java21 = 21
235235
def java17 = 17
236236
def java8 = 8
237237

238-
def latestEnv = 'java21/paper-1.20.6.json'
238+
def latestEnv = 'java21/paper-1.21.0.json'
239239
def latestJava = java21
240240
def oldestJava = java8
241241

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ groupid=ch.njol
77
name=skript
88
version=2.8.7
99
jarName=Skript.jar
10-
testEnv=java21/paper-1.20.6
10+
testEnv=java21/paper-1.21.0
1111
testEnvJavaVersion=21

src/main/java/ch/njol/skript/aliases/AliasesProvider.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,19 @@ public void addAlias(AliasName name, String id, @Nullable Map<String, Object> ta
304304
}
305305

306306
// Apply (NBT) tags to item stack
307-
ItemStack stack = new ItemStack(material);
307+
ItemStack stack = null;
308308
int itemFlags = 0;
309-
if (tags != null) {
310-
itemFlags = applyTags(stack, new HashMap<>(tags));
309+
if (material.isItem()) {
310+
stack = new ItemStack(material);
311+
if (tags != null) {
312+
itemFlags = applyTags(stack, new HashMap<>(tags));
313+
}
311314
}
312315

313316
// Parse block state to block values
314317
BlockValues blockValues = BlockCompat.INSTANCE.createBlockValues(material, blockStates, stack, itemFlags);
315318

316-
ItemData data = new ItemData(stack, blockValues);
319+
ItemData data = stack != null ? new ItemData(stack, blockValues) : new ItemData(material, blockValues);
317320
data.isAlias = true;
318321
data.itemFlags = itemFlags;
319322

src/main/java/ch/njol/skript/aliases/ItemData.java

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.bukkit.inventory.ItemFlag;
3838
import org.bukkit.inventory.ItemStack;
3939
import org.bukkit.inventory.meta.ItemMeta;
40-
import org.eclipse.jdt.annotation.Nullable;
40+
import org.jetbrains.annotations.Nullable;
4141

4242
import java.io.IOException;
4343
import java.io.NotSerializableException;
@@ -91,11 +91,6 @@ public static class OldItemData {
9191
@Deprecated
9292
public static final boolean itemDataValues = false;
9393

94-
/**
95-
* ItemStack, which is used for everything but serialization.
96-
*/
97-
transient ItemStack stack;
98-
9994
/**
10095
* Type of the item as Bukkit material. Serialized manually.
10196
*/
@@ -105,14 +100,18 @@ public static class OldItemData {
105100
* If this represents all possible items.
106101
*/
107102
boolean isAnything;
108-
103+
104+
/**
105+
* ItemStack, which is used for everything but serialization.
106+
*/
107+
transient @Nullable ItemStack stack;
108+
109109
/**
110110
* When this ItemData represents a block, this contains information to
111111
* allow comparing it against other blocks.
112112
*/
113-
@Nullable
114-
BlockValues blockValues;
115-
113+
@Nullable BlockValues blockValues;
114+
116115
/**
117116
* Whether this represents an item (that definitely cannot have
118117
* block states) or a block, which might have them.
@@ -140,32 +139,39 @@ public static class OldItemData {
140139

141140
public ItemData(Material type, @Nullable String tags) {
142141
this.type = type;
143-
144-
this.stack = new ItemStack(type);
145-
this.blockValues = BlockCompat.INSTANCE.getBlockValues(stack);
142+
143+
if (type.isItem())
144+
this.stack = new ItemStack(type);
145+
this.blockValues = BlockCompat.INSTANCE.getBlockValues(type);
146146
if (tags != null) {
147147
applyTags(tags);
148148
}
149149
}
150150

151151
public ItemData(Material type, int amount) {
152152
this.type = type;
153-
this.stack = new ItemStack(type, Math.abs(amount));
154-
this.blockValues = BlockCompat.INSTANCE.getBlockValues(stack);
153+
if (type.isItem())
154+
this.stack = new ItemStack(type, Math.abs(amount));
155+
this.blockValues = BlockCompat.INSTANCE.getBlockValues(type);
155156
}
156157

157158
public ItemData(Material type) {
158159
this(type, 1);
159160
}
160161

161162
public ItemData(ItemData data) {
162-
this.stack = data.stack.clone();
163+
this.stack = data.stack != null ? data.stack.clone() : null;
163164
this.type = data.type;
164165
this.blockValues = data.blockValues;
165166
this.isAlias = data.isAlias;
166167
this.plain = data.plain;
167168
this.itemFlags = data.itemFlags;
168169
}
170+
171+
public ItemData(Material material, @Nullable BlockValues values) {
172+
this.type = material;
173+
this.blockValues = values;
174+
}
169175

170176
public ItemData(ItemStack stack, @Nullable BlockValues values) {
171177
this.stack = stack;
@@ -200,7 +206,8 @@ public ItemData(BlockState blockState) {
200206

201207
public ItemData(BlockData blockData) {
202208
this.type = blockData.getMaterial();
203-
this.stack = new ItemStack(type);
209+
if (type.isItem())
210+
this.stack = new ItemStack(type);
204211
this.blockValues = BlockCompat.INSTANCE.getBlockValues(blockData);
205212
}
206213

@@ -227,13 +234,12 @@ public boolean isOfType(@Nullable ItemStack item) {
227234
if (type != item.getType())
228235
return false; // Obvious mismatch
229236

230-
if (itemFlags != 0) { // Either stack has tags (or durability)
237+
if (stack != null && itemFlags != 0) { // Either stack has tags (or durability)
231238
if (ItemUtils.getDamage(stack) != ItemUtils.getDamage(item))
232239
return false; // On 1.12 and below, damage is not in meta
233240
if (stack.hasItemMeta() == item.hasItemMeta()) // Compare ItemMeta as in isSimilar() of ItemStack
234-
return stack.hasItemMeta() ? itemFactory.equals(stack.getItemMeta(), item.getItemMeta()) : true;
235-
else
236-
return false;
241+
return !stack.hasItemMeta() || itemFactory.equals(stack.getItemMeta(), item.getItemMeta());
242+
return false;
237243
}
238244
return true;
239245
}
@@ -249,7 +255,7 @@ public String toString() {
249255

250256
public String toString(final boolean debug, final boolean plural) {
251257
StringBuilder builder = new StringBuilder(Aliases.getMaterialName(this, plural));
252-
ItemMeta meta = stack.getItemMeta();
258+
ItemMeta meta = stack != null ? stack.getItemMeta() : null;
253259
if (meta != null && meta.hasDisplayName()) {
254260
builder.append(" ").append(m_named).append(" ");
255261
builder.append(meta.getDisplayName());
@@ -282,7 +288,7 @@ public boolean equals(final @Nullable Object obj) {
282288
@Override
283289
public int hashCode() {
284290
int hash = type.hashCode(); // Has collisions, but probably not too many of them
285-
if (blockValues == null || (blockValues != null && blockValues.isDefault())) {
291+
if (blockValues == null || blockValues.isDefault()) {
286292
hash = hash * 37 + 1;
287293
}
288294
return hash;
@@ -351,7 +357,7 @@ public MatchQuality matchAlias(ItemData item) {
351357
}
352358

353359
// See if we need to compare item metas (excluding durability)
354-
if (quality.isAtLeast(MatchQuality.SAME_ITEM) && stack.hasItemMeta() || item.stack.hasItemMeta()) { // Item meta checks could lower this
360+
if (quality.isAtLeast(MatchQuality.SAME_ITEM) && this.hasItemMeta() || item.hasItemMeta()) { // Item meta checks could lower this
355361
MatchQuality metaQuality = compareItemMetas(getItemMeta(), item.getItemMeta());
356362

357363
// If given item doesn't care about meta, promote to SAME_ITEM
@@ -489,9 +495,13 @@ public ItemData intersection(final ItemData other) {
489495
* It is not a copy, so please be careful.
490496
* @return Item stack.
491497
*/
492-
public ItemStack getStack() {
498+
public @Nullable ItemStack getStack() {
493499
return stack;
494500
}
501+
502+
private boolean hasItemMeta() {
503+
return stack != null && stack.hasItemMeta();
504+
}
495505

496506
@Override
497507
public ItemData clone() {
@@ -508,7 +518,7 @@ public BlockValues getBlockValues() {
508518
}
509519

510520
public ItemMeta getItemMeta() {
511-
ItemMeta meta = stack.getItemMeta();
521+
ItemMeta meta = stack != null ? stack.getItemMeta() : null;
512522
if (meta == null) { // AIR has null item meta!
513523
meta = itemFactory.getItemMeta(Material.STONE);
514524
}
@@ -517,17 +527,23 @@ public ItemMeta getItemMeta() {
517527
}
518528

519529
public void setItemMeta(ItemMeta meta) {
530+
if (stack == null)
531+
return;
520532
stack.setItemMeta(meta);
521533
isAlias = false; // This is no longer exact alias
522534
plain = false; // This is no longer a plain item
523535
itemFlags |= ItemFlags.CHANGED_TAGS;
524536
}
525537

526538
public int getDurability() {
539+
if (stack == null)
540+
return 0; // no damage?
527541
return ItemUtils.getDamage(stack);
528542
}
529543

530544
public void setDurability(int durability) {
545+
if (stack == null)
546+
return;
531547
ItemUtils.setDamage(stack, durability);
532548
isAlias = false; // Change happened
533549
plain = false; // This is no longer a plain item
@@ -567,7 +583,7 @@ public boolean matchPlain(ItemData other) {
567583
public Fields serialize() throws NotSerializableException {
568584
Fields fields = new Fields(this); // ItemStack is transient, will be ignored
569585
fields.putPrimitive("id", type.ordinal());
570-
fields.putObject("meta", stack.getItemMeta());
586+
fields.putObject("meta", stack != null ? stack.getItemMeta() : null);
571587
return fields;
572588
}
573589

@@ -579,8 +595,10 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria
579595
ItemMeta meta = fields.getAndRemoveObject("meta", ItemMeta.class);
580596

581597
// Initialize ItemStack
582-
this.stack = new ItemStack(type);
583-
stack.setItemMeta(meta); // Just set meta to it
598+
if (meta != null && type.isItem()) {
599+
this.stack = new ItemStack(type);
600+
stack.setItemMeta(meta); // Just set meta to it
601+
}
584602

585603
fields.setFields(this); // Everything but ItemStack and Material
586604
}
@@ -598,17 +616,17 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria
598616
*/
599617
public ItemData aliasCopy() {
600618
ItemData data = new ItemData();
601-
data.stack = new ItemStack(type, 1);
602-
603-
if (stack.hasItemMeta()) {
604-
ItemMeta meta = stack.getItemMeta(); // Creates a copy
605-
meta.setDisplayName(null); // Clear display name
606-
if (!itemFactory.getItemMeta(type).equals(meta)) // there may be different tags (e.g. potions)
607-
data.itemFlags |= ItemFlags.CHANGED_TAGS;
608-
data.stack.setItemMeta(meta);
619+
if (stack != null) {
620+
data.stack = new ItemStack(type, 1);
621+
if (stack.hasItemMeta()) {
622+
ItemMeta meta = stack.getItemMeta(); // Creates a copy
623+
meta.setDisplayName(null); // Clear display name
624+
if (!itemFactory.getItemMeta(type).equals(meta)) // there may be different tags (e.g. potions)
625+
data.itemFlags |= ItemFlags.CHANGED_TAGS;
626+
data.stack.setItemMeta(meta);
627+
}
628+
ItemUtils.setDamage(data.stack, 0); // Set to undamaged
609629
}
610-
ItemUtils.setDamage(data.stack, 0); // Set to undamaged
611-
612630
data.type = type;
613631
data.blockValues = blockValues;
614632
data.itemForm = itemForm;
@@ -620,6 +638,8 @@ public ItemData aliasCopy() {
620638
* @param tags Tags in Mojang's JSON format.
621639
*/
622640
public void applyTags(String tags) {
641+
if (stack == null)
642+
return;
623643
BukkitUnsafe.modifyItemStack(stack, tags);
624644
itemFlags |= ItemFlags.CHANGED_TAGS;
625645
}

src/main/java/ch/njol/skript/aliases/ItemType.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.util.Random;
6969
import java.util.RandomAccess;
7070
import java.util.Set;
71+
import java.util.stream.Collectors;
7172

7273
@ContainerType(ItemStack.class)
7374
public class ItemType implements Unit, Iterable<ItemData>, Container<ItemStack>, YggdrasilExtendedSerializable {
@@ -315,7 +316,7 @@ public boolean isOfType(Material id, @Nullable String tags) {
315316

316317
public boolean isOfType(Material id) {
317318
// TODO avoid object creation
318-
return isOfType(new ItemData(id, null));
319+
return isOfType(new ItemData(id, (String) null));
319320
}
320321

321322
/**
@@ -343,7 +344,7 @@ public ItemType getBlock() {
343344
*/
344345
public boolean hasItem() {
345346
for (ItemData d : types) {
346-
if (!d.type.isBlock())
347+
if (d.type.isItem())
347348
return true;
348349
}
349350
return false;
@@ -487,9 +488,13 @@ public boolean hasNext() {
487488

488489
@Override
489490
public ItemStack next() {
490-
if (!hasNext())
491-
throw new NoSuchElementException();
492-
ItemStack is = iter.next().getStack().clone();
491+
ItemStack is = null;
492+
while (is == null) {
493+
if (!hasNext())
494+
throw new NoSuchElementException();
495+
is = iter.next().getStack();
496+
}
497+
is = is.clone();
493498
is.setAmount(getAmount());
494499
return is;
495500
}
@@ -588,10 +593,17 @@ public ItemType clone() {
588593
* @see #removeFrom(ItemStack)
589594
* @see #removeFrom(List...)
590595
*/
591-
public ItemStack getRandom() {
592-
int numItems = types.size();
596+
public @Nullable ItemStack getRandom() {
597+
List<ItemData> datas = types.stream()
598+
.filter(data -> data.stack != null)
599+
.collect(Collectors.toList());
600+
if (datas.isEmpty())
601+
return null;
602+
int numItems = datas.size();
593603
int index = random.nextInt(numItems);
594-
ItemStack is = types.get(index).getStack().clone();
604+
ItemStack is = datas.get(index).getStack();
605+
assert is != null; // verified above
606+
is = is.clone();
595607
is.setAmount(getAmount());
596608
return is;
597609
}
@@ -869,7 +881,9 @@ public final boolean removeFrom(boolean replaceWithNull, List<ItemStack>... list
869881
*/
870882
public void addTo(final List<ItemStack> list) {
871883
if (!isAll()) {
872-
list.add(getItem().getRandom());
884+
ItemStack random = getItem().getRandom();
885+
if (random != null)
886+
list.add(getItem().getRandom());
873887
return;
874888
}
875889
for (final ItemStack is : getItem().getAll())
@@ -936,7 +950,9 @@ private static boolean addTo(@Nullable ItemStack is, ItemStack[] buf) {
936950

937951
public boolean addTo(final ItemStack[] buf) {
938952
if (!isAll()) {
939-
return addTo(getItem().getRandom(), buf);
953+
ItemStack random = getItem().getRandom();
954+
if (random != null)
955+
return addTo(getItem().getRandom(), buf);
940956
}
941957
boolean ok = true;
942958
for (ItemStack is : getItem().getAll()) {

0 commit comments

Comments
 (0)