Skip to content

Commit 6a9d099

Browse files
committed
Use namespacedkeys instead of ordinals for serialization
1 parent 7bcd5cb commit 6a9d099

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
1212
import org.bukkit.Bukkit;
1313
import org.bukkit.Material;
14+
import org.bukkit.NamespacedKey;
1415
import org.bukkit.block.Block;
1516
import org.bukkit.block.BlockState;
1617
import org.bukkit.block.data.BlockData;
@@ -564,7 +565,7 @@ public boolean matchPlain(ItemData other) {
564565
@Override
565566
public Fields serialize() throws NotSerializableException {
566567
Fields fields = new Fields(this); // ItemStack is transient, will be ignored
567-
fields.putPrimitive("id", type.ordinal());
568+
fields.putObject("key", type.getKey());
568569
fields.putObject("meta", stack != null ? stack.getItemMeta() : null);
569570
return fields;
570571
}
@@ -573,7 +574,16 @@ public Fields serialize() throws NotSerializableException {
573574

574575
@Override
575576
public void deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException {
576-
this.type = materials[fields.getAndRemovePrimitive("id", int.class)];
577+
if (fields.hasField("key")) {
578+
NamespacedKey key = fields.getAndRemoveObject("key", NamespacedKey.class);
579+
if (key == null)
580+
throw new StreamCorruptedException("Material key is null");
581+
this.type = Material.matchMaterial(key.toString());
582+
} else {
583+
// attempt back compat deserialization, though using ordinals is not reliable
584+
this.type = materials[fields.getAndRemovePrimitive("id", int.class)];
585+
}
586+
577587
ItemMeta meta = fields.getAndRemoveObject("meta", ItemMeta.class);
578588

579589
// Initialize ItemStack

0 commit comments

Comments
 (0)