Skip to content

Commit

Permalink
Add a LootTable Serializer (#7528)
Browse files Browse the repository at this point in the history
  • Loading branch information
Burbulinis authored Feb 1, 2025
1 parent 757b5e7 commit b4db4b1
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.util.SimpleEvent;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.EventValues;
import ch.njol.yggdrasil.Fields;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
Expand All @@ -18,6 +20,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.StreamCorruptedException;

public class LootTableModule {

Expand Down Expand Up @@ -53,6 +56,42 @@ public String toVariableNameString(LootTable o) {
return "loot table:" + o.getKey();
}
})
.serializer(new Serializer<>() {
@Override
public Fields serialize(LootTable lootTable) {
Fields fields = new Fields();
fields.putObject("key", lootTable.getKey().toString());
return fields;
}

@Override
public void deserialize(LootTable lootTable, Fields fields) {
assert false;
}

@Override
protected LootTable deserialize(Fields fields) throws StreamCorruptedException {
String key = fields.getAndRemoveObject("key", String.class);
if (key == null)
throw new StreamCorruptedException();

NamespacedKey namespacedKey = NamespacedKey.fromString(key);
if (namespacedKey == null)
throw new StreamCorruptedException();

return Bukkit.getLootTable(namespacedKey);
}

@Override
public boolean mustSyncDeserialization() {
return true;
}

@Override
protected boolean canBeInstantiated() {
return false;
}
})
);

Classes.registerClass(new ClassInfo<>(LootContext.class, "lootcontext")
Expand Down

0 comments on commit b4db4b1

Please sign in to comment.