Skip to content

Commit

Permalink
Merge branch 'dev/feature' into fix/registry-doc-spam
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth authored Jul 1, 2024
2 parents 1beaf74 + dfaacd6 commit 8596961
Show file tree
Hide file tree
Showing 38 changed files with 1,299 additions and 443 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 java11 = 11

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

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
4 changes: 4 additions & 0 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,13 +752,17 @@ protected void afterErrors() {
TestTracker.JUnitTestFailed(test, message);
Skript.exception(failure.getException(), "JUnit test '" + failure.getTestHeader() + " failed.");
});
if (SkriptJUnitTest.class.isAssignableFrom(clazz))
((SkriptJUnitTest) clazz.getConstructor().newInstance()).cleanup();
SkriptJUnitTest.clearJUnitTest();
}
} catch (IOException e) {
Skript.exception(e, "Failed to execute JUnit runtime tests.");
} catch (ClassNotFoundException e) {
// Should be the Skript test jar gradle task.
assert false : "Class 'ch.njol.skript.variables.FlatFileStorageTest' was not found.";
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
Skript.exception(e, "Failed to initalize test JUnit classes.");
}
if (ignored > 0)
Skript.warning("There were " + ignored + " ignored test cases! This can mean they are not properly setup in order in that class!");
Expand Down
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
Loading

0 comments on commit 8596961

Please sign in to comment.