Skip to content

Commit

Permalink
1.21 Support (#6798)
Browse files Browse the repository at this point in the history
* Update Paper API to 1.21

* Patch up aliases

* Update 5491-xp orb merge overwrite.sk

---------

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
APickledWalrus and sovdeeth authored Jul 1, 2024
1 parent 333b5fb commit 585e8d1
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 57 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
shadow group: 'org.bstats', name: 'bstats-bukkit', version: '3.0.2'
shadow group: 'net.kyori', name: 'adventure-text-serializer-bungeecord', version: '4.3.2'

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

def latestEnv = 'java21/paper-1.20.6.json'
def latestEnv = 'java21/paper-1.21.0.json'
def latestJava = java21
def oldestJava = java8

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ groupid=ch.njol
name=skript
version=2.8.7
jarName=Skript.jar
testEnv=java21/paper-1.20.6
testEnv=java21/paper-1.21.0
testEnvJavaVersion=21
11 changes: 7 additions & 4 deletions src/main/java/ch/njol/skript/aliases/AliasesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,19 @@ public void addAlias(AliasName name, String id, @Nullable Map<String, Object> ta
}

// Apply (NBT) tags to item stack
ItemStack stack = new ItemStack(material);
ItemStack stack = null;
int itemFlags = 0;
if (tags != null) {
itemFlags = applyTags(stack, new HashMap<>(tags));
if (material.isItem()) {
stack = new ItemStack(material);
if (tags != null) {
itemFlags = applyTags(stack, new HashMap<>(tags));
}
}

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

ItemData data = new ItemData(stack, blockValues);
ItemData data = stack != null ? new ItemData(stack, blockValues) : new ItemData(material, blockValues);
data.isAlias = true;
data.itemFlags = itemFlags;

Expand Down
98 changes: 59 additions & 39 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.NotSerializableException;
Expand Down Expand Up @@ -91,11 +91,6 @@ public static class OldItemData {
@Deprecated
public static final boolean itemDataValues = false;

/**
* ItemStack, which is used for everything but serialization.
*/
transient ItemStack stack;

/**
* Type of the item as Bukkit material. Serialized manually.
*/
Expand All @@ -105,14 +100,18 @@ public static class OldItemData {
* If this represents all possible items.
*/
boolean isAnything;


/**
* ItemStack, which is used for everything but serialization.
*/
transient @Nullable ItemStack stack;

/**
* When this ItemData represents a block, this contains information to
* allow comparing it against other blocks.
*/
@Nullable
BlockValues blockValues;

@Nullable BlockValues blockValues;

/**
* Whether this represents an item (that definitely cannot have
* block states) or a block, which might have them.
Expand Down Expand Up @@ -140,32 +139,39 @@ public static class OldItemData {

public ItemData(Material type, @Nullable String tags) {
this.type = type;

this.stack = new ItemStack(type);
this.blockValues = BlockCompat.INSTANCE.getBlockValues(stack);

if (type.isItem())
this.stack = new ItemStack(type);
this.blockValues = BlockCompat.INSTANCE.getBlockValues(type);
if (tags != null) {
applyTags(tags);
}
}

public ItemData(Material type, int amount) {
this.type = type;
this.stack = new ItemStack(type, Math.abs(amount));
this.blockValues = BlockCompat.INSTANCE.getBlockValues(stack);
if (type.isItem())
this.stack = new ItemStack(type, Math.abs(amount));
this.blockValues = BlockCompat.INSTANCE.getBlockValues(type);
}

public ItemData(Material type) {
this(type, 1);
}

public ItemData(ItemData data) {
this.stack = data.stack.clone();
this.stack = data.stack != null ? data.stack.clone() : null;
this.type = data.type;
this.blockValues = data.blockValues;
this.isAlias = data.isAlias;
this.plain = data.plain;
this.itemFlags = data.itemFlags;
}

public ItemData(Material material, @Nullable BlockValues values) {
this.type = material;
this.blockValues = values;
}

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

public ItemData(BlockData blockData) {
this.type = blockData.getMaterial();
this.stack = new ItemStack(type);
if (type.isItem())
this.stack = new ItemStack(type);
this.blockValues = BlockCompat.INSTANCE.getBlockValues(blockData);
}

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

if (itemFlags != 0) { // Either stack has tags (or durability)
if (stack != null && itemFlags != 0) { // Either stack has tags (or durability)
if (ItemUtils.getDamage(stack) != ItemUtils.getDamage(item))
return false; // On 1.12 and below, damage is not in meta
if (stack.hasItemMeta() == item.hasItemMeta()) // Compare ItemMeta as in isSimilar() of ItemStack
return stack.hasItemMeta() ? itemFactory.equals(stack.getItemMeta(), item.getItemMeta()) : true;
else
return false;
return !stack.hasItemMeta() || itemFactory.equals(stack.getItemMeta(), item.getItemMeta());
return false;
}
return true;
}
Expand All @@ -249,7 +255,7 @@ public String toString() {

public String toString(final boolean debug, final boolean plural) {
StringBuilder builder = new StringBuilder(Aliases.getMaterialName(this, plural));
ItemMeta meta = stack.getItemMeta();
ItemMeta meta = stack != null ? stack.getItemMeta() : null;
if (meta != null && meta.hasDisplayName()) {
builder.append(" ").append(m_named).append(" ");
builder.append(meta.getDisplayName());
Expand Down Expand Up @@ -282,7 +288,7 @@ public boolean equals(final @Nullable Object obj) {
@Override
public int hashCode() {
int hash = type.hashCode(); // Has collisions, but probably not too many of them
if (blockValues == null || (blockValues != null && blockValues.isDefault())) {
if (blockValues == null || blockValues.isDefault()) {
hash = hash * 37 + 1;
}
return hash;
Expand Down Expand Up @@ -351,7 +357,7 @@ public MatchQuality matchAlias(ItemData item) {
}

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

// If given item doesn't care about meta, promote to SAME_ITEM
Expand Down Expand Up @@ -489,9 +495,13 @@ public ItemData intersection(final ItemData other) {
* It is not a copy, so please be careful.
* @return Item stack.
*/
public ItemStack getStack() {
public @Nullable ItemStack getStack() {
return stack;
}

private boolean hasItemMeta() {
return stack != null && stack.hasItemMeta();
}

@Override
public ItemData clone() {
Expand All @@ -508,7 +518,7 @@ public BlockValues getBlockValues() {
}

public ItemMeta getItemMeta() {
ItemMeta meta = stack.getItemMeta();
ItemMeta meta = stack != null ? stack.getItemMeta() : null;
if (meta == null) { // AIR has null item meta!
meta = itemFactory.getItemMeta(Material.STONE);
}
Expand All @@ -517,17 +527,23 @@ public ItemMeta getItemMeta() {
}

public void setItemMeta(ItemMeta meta) {
if (stack == null)
return;
stack.setItemMeta(meta);
isAlias = false; // This is no longer exact alias
plain = false; // This is no longer a plain item
itemFlags |= ItemFlags.CHANGED_TAGS;
}

public int getDurability() {
if (stack == null)
return 0; // no damage?
return ItemUtils.getDamage(stack);
}

public void setDurability(int durability) {
if (stack == null)
return;
ItemUtils.setDamage(stack, durability);
isAlias = false; // Change happened
plain = false; // This is no longer a plain item
Expand Down Expand Up @@ -567,7 +583,7 @@ public boolean matchPlain(ItemData other) {
public Fields serialize() throws NotSerializableException {
Fields fields = new Fields(this); // ItemStack is transient, will be ignored
fields.putPrimitive("id", type.ordinal());
fields.putObject("meta", stack.getItemMeta());
fields.putObject("meta", stack != null ? stack.getItemMeta() : null);
return fields;
}

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

// Initialize ItemStack
this.stack = new ItemStack(type);
stack.setItemMeta(meta); // Just set meta to it
if (meta != null && type.isItem()) {
this.stack = new ItemStack(type);
stack.setItemMeta(meta); // Just set meta to it
}

fields.setFields(this); // Everything but ItemStack and Material
}
Expand All @@ -598,17 +616,17 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria
*/
public ItemData aliasCopy() {
ItemData data = new ItemData();
data.stack = new ItemStack(type, 1);

if (stack.hasItemMeta()) {
ItemMeta meta = stack.getItemMeta(); // Creates a copy
meta.setDisplayName(null); // Clear display name
if (!itemFactory.getItemMeta(type).equals(meta)) // there may be different tags (e.g. potions)
data.itemFlags |= ItemFlags.CHANGED_TAGS;
data.stack.setItemMeta(meta);
if (stack != null) {
data.stack = new ItemStack(type, 1);
if (stack.hasItemMeta()) {
ItemMeta meta = stack.getItemMeta(); // Creates a copy
meta.setDisplayName(null); // Clear display name
if (!itemFactory.getItemMeta(type).equals(meta)) // there may be different tags (e.g. potions)
data.itemFlags |= ItemFlags.CHANGED_TAGS;
data.stack.setItemMeta(meta);
}
ItemUtils.setDamage(data.stack, 0); // Set to undamaged
}
ItemUtils.setDamage(data.stack, 0); // Set to undamaged

data.type = type;
data.blockValues = blockValues;
data.itemForm = itemForm;
Expand All @@ -620,6 +638,8 @@ public ItemData aliasCopy() {
* @param tags Tags in Mojang's JSON format.
*/
public void applyTags(String tags) {
if (stack == null)
return;
BukkitUnsafe.modifyItemStack(stack, tags);
itemFlags |= ItemFlags.CHANGED_TAGS;
}
Expand Down
36 changes: 26 additions & 10 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import java.util.Random;
import java.util.RandomAccess;
import java.util.Set;
import java.util.stream.Collectors;

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

public boolean isOfType(Material id) {
// TODO avoid object creation
return isOfType(new ItemData(id, null));
return isOfType(new ItemData(id, (String) null));
}

/**
Expand Down Expand Up @@ -343,7 +344,7 @@ public ItemType getBlock() {
*/
public boolean hasItem() {
for (ItemData d : types) {
if (!d.type.isBlock())
if (d.type.isItem())
return true;
}
return false;
Expand Down Expand Up @@ -487,9 +488,13 @@ public boolean hasNext() {

@Override
public ItemStack next() {
if (!hasNext())
throw new NoSuchElementException();
ItemStack is = iter.next().getStack().clone();
ItemStack is = null;
while (is == null) {
if (!hasNext())
throw new NoSuchElementException();
is = iter.next().getStack();
}
is = is.clone();
is.setAmount(getAmount());
return is;
}
Expand Down Expand Up @@ -588,10 +593,17 @@ public ItemType clone() {
* @see #removeFrom(ItemStack)
* @see #removeFrom(List...)
*/
public ItemStack getRandom() {
int numItems = types.size();
public @Nullable ItemStack getRandom() {
List<ItemData> datas = types.stream()
.filter(data -> data.stack != null)
.collect(Collectors.toList());
if (datas.isEmpty())
return null;
int numItems = datas.size();
int index = random.nextInt(numItems);
ItemStack is = types.get(index).getStack().clone();
ItemStack is = datas.get(index).getStack();
assert is != null; // verified above
is = is.clone();
is.setAmount(getAmount());
return is;
}
Expand Down Expand Up @@ -869,7 +881,9 @@ public final boolean removeFrom(boolean replaceWithNull, List<ItemStack>... list
*/
public void addTo(final List<ItemStack> list) {
if (!isAll()) {
list.add(getItem().getRandom());
ItemStack random = getItem().getRandom();
if (random != null)
list.add(getItem().getRandom());
return;
}
for (final ItemStack is : getItem().getAll())
Expand Down Expand Up @@ -936,7 +950,9 @@ private static boolean addTo(@Nullable ItemStack is, ItemStack[] buf) {

public boolean addTo(final ItemStack[] buf) {
if (!isAll()) {
return addTo(getItem().getRandom(), buf);
ItemStack random = getItem().getRandom();
if (random != null)
return addTo(getItem().getRandom(), buf);
}
boolean ok = true;
for (ItemStack is : getItem().getAll()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ default BlockValues getBlockValues(Block block) {
return getBlockValues(block.getBlockData());
}

@Nullable
BlockValues getBlockValues(Material material);

@Nullable
BlockValues getBlockValues(BlockData blockData);

Expand Down
Loading

0 comments on commit 585e8d1

Please sign in to comment.