Skip to content

Commit

Permalink
Merge branch 'dev/feature' into fix/enchantmenttype-to-string
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Jul 10, 2024
2 parents 81b01c3 + c782652 commit cc9d907
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void setMaxDamage(ItemStack itemStack, int maxDamage) {
public static void setDamage(ItemStack itemStack, int damage) {
ItemMeta meta = itemStack.getItemMeta();
if (meta instanceof Damageable) {
((Damageable) meta).setDamage(damage);
((Damageable) meta).setDamage(Math.max(0, damage));
itemStack.setItemMeta(meta);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ public RegistrySerializer(Registry<R> registry) {
@Override
public @NotNull Fields serialize(R o) {
Fields fields = new Fields();
fields.putPrimitive("name", o.getKey().toString());
fields.putObject("name", o.getKey().toString());
return fields;
}

@Override
protected R deserialize(Fields fields) {
try {
String name = fields.getAndRemovePrimitive("name", String.class);
NamespacedKey namespacedKey;
if (!name.contains(":")) {
// Old variables
namespacedKey = NamespacedKey.minecraft(name);
} else {
namespacedKey = NamespacedKey.fromString(name);
}
if (namespacedKey == null)
return null;
return registry.get(namespacedKey);
} catch (StreamCorruptedException e) {
return null;
protected R deserialize(Fields fields) throws StreamCorruptedException {
String name = fields.getAndRemoveObject("name", String.class);
assert name != null;
NamespacedKey namespacedKey;
if (!name.contains(":")) {
// Old variables
namespacedKey = NamespacedKey.minecraft(name);
} else {
namespacedKey = NamespacedKey.fromString(name);
}
if (namespacedKey == null)
throw new StreamCorruptedException("Invalid namespacedkey: " + name);
R object = registry.get(namespacedKey);
if (object == null)
throw new StreamCorruptedException("Invalid object from registry: " + namespacedKey);
return object;
}

@Override
Expand Down
8 changes: 8 additions & 0 deletions src/test/skript/tests/misc/registry.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test "registry" when minecraft version is "1.14":

# Test namespaced keys
assert curse of vanishing = minecraft:vanishing_curse with "'curse of vanishing' enchant should match namespace key"

# Test serialization
set {test::enchantment} to minecraft:sharpness
assert {test::enchantment} = sharpness with "variable should have been set to sharpness enchantment"
9 changes: 9 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprDurability.sk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ test "durability":
assert damage of {_i} is {_max} with "max item damage failed"
assert durability of {_i} is 0 with "zero item durability failed"

# Test out of bound values
set durability of {_i} to 500
assert damage of {_i} is 0 with "damage of item should be 0 when setting durability higher than max"
assert durability of {_i} is {_max} with "durability of item should be max when setting higher than max"

set damage of {_i} to -1
assert damage of {_i} is 0 with "damage of item should be 0 when setting damage less than 0"
assert durability of {_i} is {_max} with "durability of item should be max when setting damage below 0"

test "durability - custom" when running minecraft "1.20.5":
set {_i} to 1 of iron sword
set max durability of {_i} to 1000
Expand Down

0 comments on commit cc9d907

Please sign in to comment.