-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemStackSerializer.java
42 lines (35 loc) · 1.51 KB
/
ItemStackSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.auioc.mcmod.arnicalib.game.item;
import org.auioc.mcmod.arnicalib.base.validate.Validate;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
public class ItemStackSerializer {
public static ItemStack fromJson(JsonObject json) {
var item = ItemRegistry.getOrElseThrow(GsonHelper.getAsString(json, "id"));
int count = GsonHelper.getAsInt(json, "count", 1);
try {
Validate.isPositive(count, "The item count must be positive: " + count);
int maxStackSize = ItemUtils.getMaxStackSize(item);
Validate.isTrue(
count <= maxStackSize,
"The specified count " + count + " is too large, the max stack size of item '" + ItemUtils.toString(item) + "' is " + maxStackSize
);
} catch (IllegalArgumentException e) {
throw new JsonSyntaxException(e);
}
CompoundTag nbt = null;
var snbt = GsonHelper.getAsString(json, "nbt", null);
if (snbt != null) {
try {
nbt = TagParser.parseTag(snbt);
} catch (CommandSyntaxException e) {
throw new JsonSyntaxException("Invalid NBT: " + e.getMessage());
}
}
return ItemUtils.createItemStack(item, count, nbt);
}
}