Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ItemStack (and components) Json serialising #47

Merged
merged 12 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/net/id/incubus_core/recipe/IngredientStack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.id.incubus_core.recipe;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.id.incubus_core.recipe.matchbook.*;
import net.minecraft.inventory.*;
import net.minecraft.item.*;
Expand Down Expand Up @@ -81,6 +84,15 @@ public void write(PacketByteBuf buf) {
buf.writeInt(count);
}

public JsonElement toJson() {
JsonObject main = new JsonObject();
main.add("ingredient", this.ingredient.toJson());
if (this.count > 1) main.add(RecipeParser.COUNT, new JsonPrimitive(this.count));
if (!this.matchbook.isEmpty()) main.add(RecipeParser.MATCHBOOK, this.matchbook.toJson());
if (this.recipeViewNbt.isPresent()) main.add("recipeViewNbt", RecipeParser.asJson(recipeViewNbt.get()));
return main;
}

public static IngredientStack fromByteBuf(PacketByteBuf buf) {
return new IngredientStack(Ingredient.fromPacket(buf), Matchbook.fromByteBuf(buf), buf.readBoolean() ? Optional.ofNullable(buf.readNbt()) : Optional.empty(), buf.readInt());
}
Expand Down
53 changes: 39 additions & 14 deletions src/main/java/net/id/incubus_core/recipe/RecipeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,38 @@

@SuppressWarnings("unused")
public class RecipeParser {

private static final JsonParser PARSER = new JsonParser();
public static final String COUNT = "count";
public static final String ITEM = "item";
public static final String KEY = "key";
public static final String MATCHBOOK = "matchbook";
public static final String MAX = "max";
public static final String MIN = "min";
public static final String TARGET = "target";
public static final String TYPE = "type";

public static JsonObject fromInputStream(InputStream in) {
return PARSER.parse(new JsonReader(new InputStreamReader(in, StandardCharsets.UTF_8))).getAsJsonObject();
return JsonParser.parseReader(new InputStreamReader(in, StandardCharsets.UTF_8)).getAsJsonObject();
}

public static ItemStack stackFromJson(JsonObject json, String elementName) {
Item item = Registry.ITEM.get(Identifier.tryParse(json.get(elementName).getAsString()));
int count = json.has("count") ? json.get("count").getAsInt() : 1;
int count = json.has(COUNT) ? json.get(COUNT).getAsInt() : 1;
return item != Items.AIR ? new ItemStack(item, count) : ItemStack.EMPTY;
}

public static ItemStack stackFromJson(JsonObject json) {
return stackFromJson(json, "item");
return stackFromJson(json, ITEM);
}

public static IngredientStack ingredientStackFromJson(JsonObject json) {
Ingredient ingredient = json.has("ingredient") ? Ingredient.fromJson(json.getAsJsonObject("ingredient")) : Ingredient.fromJson(json);
var matchbook = Matchbook.empty();
NbtCompound recipeViewNbt = null;
int count = json.has("count") ? json.get("count").getAsInt() : 1;
int count = json.has(COUNT) ? json.get(COUNT).getAsInt() : 1;

if (json.has("matchbook")) {
if (json.has(MATCHBOOK)) {
try {
matchbook = matchbookFromJson(json.getAsJsonObject("matchbook"));
matchbook = matchbookFromJson(json.getAsJsonObject(MATCHBOOK));
} catch (MalformedJsonException e) {
IncubusCore.LOG.error("RELAYED EXCEPTION. " + e);
}
Expand Down Expand Up @@ -77,9 +83,9 @@ public static List<IngredientStack> ingredientStacksFromJson(JsonArray array, in
}

public static OptionalStack optionalStackFromJson(JsonObject json) throws MalformedJsonException {
int count = json.has("count") ? json.get("count").getAsInt() : 1;
if(json.has("item")) {
Item item = Registry.ITEM.get(Identifier.tryParse(json.get("item").getAsString()));
int count = json.has(COUNT) ? json.get(COUNT).getAsInt() : 1;
if(json.has(ITEM)) {
Item item = Registry.ITEM.get(Identifier.tryParse(json.get(ITEM).getAsString()));
return item != Items.AIR ? new OptionalStack(new ItemStack(item, count), count) : OptionalStack.EMPTY;
}
else if(json.has("tag")) {
Expand Down Expand Up @@ -115,8 +121,8 @@ public static Matchbook matchbookFromJson(JsonObject json) throws MalformedJsonE

for (int i = 0; i < matchArray.size(); i++) {
var entry = matchArray.get(i).getAsJsonObject();
var id = entry.get("type").getAsString();
var key = entry.get("key").getAsString();
var id = entry.get(TYPE).getAsString();
var key = entry.get(KEY).getAsString();

var optional = MatchRegistry.getOptional(id);
if(optional.isEmpty()) {
Expand All @@ -143,7 +149,7 @@ public static ItemStack getItemStackWithNbtFromJson(JsonObject json) {
throw new JsonParseException("Disallowed data tag found");
}

int count = JsonHelper.getInt(json, "count", 1);
int count = JsonHelper.getInt(json, COUNT, 1);
if (count < 1) {
throw new JsonSyntaxException("Invalid output count: " + count);
}
Expand All @@ -165,4 +171,23 @@ public static ItemStack getItemStackWithNbtFromJson(JsonObject json) {
return stack;
}

public static JsonElement asJson(NbtElement nbt) {
if (nbt == null) {
return JsonNull.INSTANCE;
}
if (nbt instanceof NbtString s) return new JsonPrimitive(s.asString());
if (nbt instanceof NbtByte b) return new JsonPrimitive(b.byteValue() == 1);
if (nbt instanceof AbstractNbtNumber n) return new JsonPrimitive(n.numberValue());
if (nbt instanceof AbstractNbtList<?> l) {
JsonArray arr = new JsonArray();
l.stream().map(RecipeParser::asJson).forEach(arr::add);
return arr;
}
if (nbt instanceof NbtCompound c) {
JsonObject o = new JsonObject();
c.getKeys().forEach(k -> o.add(k, asJson(c.get(k))));
return o;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.id.incubus_core.recipe.matchbook;

import com.google.gson.*;
import net.id.incubus_core.recipe.RecipeParser;
import net.minecraft.nbt.*;
import net.minecraft.network.*;

public class BooleanMatch extends Match {
public static final String TYPE = "boolean";

private boolean booleanValue;

Expand All @@ -31,6 +33,15 @@ void configure(PacketByteBuf buf) {
booleanValue = buf.readBoolean();
}

@Override
JsonObject toJson() {
JsonObject main = new JsonObject();
main.add(RecipeParser.TYPE, new JsonPrimitive(TYPE));
main.add(RecipeParser.KEY, new JsonPrimitive(this.name));
main.add("value", new JsonPrimitive(this.booleanValue));
return main;
}

@Override
void write(PacketByteBuf buf) {
buf.writeBoolean(booleanValue);
Expand All @@ -39,7 +50,7 @@ void write(PacketByteBuf buf) {
public static class Factory extends MatchFactory<BooleanMatch> {

public Factory() {
super("boolean");
super(TYPE);
}

@Override
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/net/id/incubus_core/recipe/matchbook/ByteMatch.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.id.incubus_core.recipe.matchbook;

import com.google.gson.*;
import net.id.incubus_core.recipe.RecipeParser;
import net.minecraft.nbt.*;
import net.minecraft.network.*;

public class ByteMatch extends Match {
public static final String TYPE = "byte";

private byte targetByte;

Expand All @@ -23,14 +25,23 @@ boolean matches(NbtCompound nbt) {

@Override
void configure(JsonObject json) {
targetByte = json.get("target").getAsByte();
targetByte = json.get(RecipeParser.TARGET).getAsByte();
}

@Override
void configure(PacketByteBuf buf) {
targetByte = buf.readByte();
}

@Override
JsonObject toJson() {
JsonObject main = new JsonObject();
main.add(RecipeParser.TYPE, new JsonPrimitive(TYPE));
main.add(RecipeParser.KEY, new JsonPrimitive(this.name));
main.add(RecipeParser.TARGET, new JsonPrimitive(this.targetByte));
return main;
}

@Override
void write(PacketByteBuf buf) {
buf.writeByte(targetByte);
Expand All @@ -39,7 +50,7 @@ void write(PacketByteBuf buf) {
public static class Factory extends MatchFactory<ByteMatch> {

public Factory() {
super("byte");
super(TYPE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.id.incubus_core.recipe.matchbook;

import com.google.gson.*;
import net.id.incubus_core.recipe.RecipeParser;
import net.minecraft.nbt.*;
import net.minecraft.network.*;

public class EnchantmentMatch extends Match {
public static final String TYPE = "enchantment";

private boolean singular;
private int minLevel;
Expand Down Expand Up @@ -66,6 +68,18 @@ void configure(PacketByteBuf buf) {
enchantmentId = buf.readString();
}

@Override
JsonObject toJson() {
JsonObject main = new JsonObject();
main.add(RecipeParser.TYPE, new JsonPrimitive(TYPE));
main.add(RecipeParser.KEY, new JsonPrimitive(this.name));
main.add("singular", new JsonPrimitive(singular));
main.add("minLevel", new JsonPrimitive(minLevel));
main.add("maxLevel", new JsonPrimitive(maxLevel));
main.add("enchantmentId", new JsonPrimitive(enchantmentId));
return main;
}

@Override
void write(PacketByteBuf buf) {
buf.writeBoolean(singular);
Expand All @@ -77,7 +91,7 @@ void write(PacketByteBuf buf) {
public static class Factory extends MatchFactory<EnchantmentMatch> {

public Factory() {
super("enchantment");
super(TYPE);
}

@Override
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/net/id/incubus_core/recipe/matchbook/FloatMatch.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package net.id.incubus_core.recipe.matchbook;

import com.google.gson.*;
import net.id.incubus_core.recipe.RecipeParser;
import net.minecraft.nbt.*;
import net.minecraft.network.*;

/**
* Exclusive.
*/
public class FloatMatch extends Match {
public static final String TYPE = "float";

private float min;
private float max;
Expand All @@ -28,8 +30,8 @@ boolean matches(NbtCompound nbt) {

@Override
void configure(JsonObject json) {
min = json.get("min").getAsFloat();
min = json.get("max").getAsFloat();
min = json.get(RecipeParser.MIN).getAsFloat();
min = json.get(RecipeParser.MAX).getAsFloat();
}

@Override
Expand All @@ -38,6 +40,16 @@ void configure(PacketByteBuf buf) {
max = buf.readFloat();
}

@Override
JsonObject toJson() {
JsonObject main = new JsonObject();
main.add(RecipeParser.TYPE, new JsonPrimitive(TYPE));
main.add(RecipeParser.KEY, new JsonPrimitive(this.name));
main.add(RecipeParser.MIN, new JsonPrimitive(min));
main.add(RecipeParser.MAX, new JsonPrimitive(max));
return main;
}

@Override
void write(PacketByteBuf buf) {
buf.writeFloat(min);
Expand All @@ -47,7 +59,7 @@ void write(PacketByteBuf buf) {
public static class Factory extends MatchFactory<FloatMatch> {

public Factory() {
super("float");
super(TYPE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
public class IncubusMatches {

public static void init() {
MatchRegistry.registerInternal("int", new IntMatch.Factory());
MatchRegistry.registerInternal("intRange", new IntRangeMatch.Factory());
MatchRegistry.registerInternal("float", new FloatMatch.Factory());
MatchRegistry.registerInternal("long", new LongMatch.Factory());
MatchRegistry.registerInternal("short", new ShortMatch.Factory());
MatchRegistry.registerInternal("byte", new ByteMatch.Factory());
MatchRegistry.registerInternal("string", new StringMatch.Factory());
MatchRegistry.registerInternal("boolean", new BooleanMatch.Factory());
MatchRegistry.registerInternal("enchantment", new EnchantmentMatch.Factory());
MatchRegistry.registerInternal(IntMatch.TYPE, new IntMatch.Factory());
MatchRegistry.registerInternal(IntRangeMatch.TYPE, new IntRangeMatch.Factory());
MatchRegistry.registerInternal(FloatMatch.TYPE, new FloatMatch.Factory());
MatchRegistry.registerInternal(LongMatch.TYPE, new LongMatch.Factory());
MatchRegistry.registerInternal(ShortMatch.TYPE, new ShortMatch.Factory());
MatchRegistry.registerInternal(ByteMatch.TYPE, new ByteMatch.Factory());
MatchRegistry.registerInternal(StringMatch.TYPE, new StringMatch.Factory());
MatchRegistry.registerInternal(BooleanMatch.TYPE, new BooleanMatch.Factory());
MatchRegistry.registerInternal(EnchantmentMatch.TYPE, new EnchantmentMatch.Factory());
}
}
15 changes: 13 additions & 2 deletions src/main/java/net/id/incubus_core/recipe/matchbook/IntMatch.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.id.incubus_core.recipe.matchbook;

import com.google.gson.*;
import net.id.incubus_core.recipe.RecipeParser;
import net.minecraft.nbt.*;
import net.minecraft.network.*;

public class IntMatch extends Match {
public static final String TYPE = "int";

private int targetInt;

Expand All @@ -23,14 +25,23 @@ boolean matches(NbtCompound nbt) {

@Override
void configure(JsonObject json) {
targetInt = json.get("target").getAsInt();
targetInt = json.get(RecipeParser.TARGET).getAsInt();
}

@Override
void configure(PacketByteBuf buf) {
targetInt = buf.readInt();
}

@Override
JsonObject toJson() {
JsonObject main = new JsonObject();
main.add(RecipeParser.TYPE, new JsonPrimitive(TYPE));
main.add(RecipeParser.KEY, new JsonPrimitive(this.name));
main.add(RecipeParser.TARGET, new JsonPrimitive(targetInt));
return main;
}

@Override
void write(PacketByteBuf buf) {
buf.writeInt(targetInt);
Expand All @@ -39,7 +50,7 @@ void write(PacketByteBuf buf) {
public static class Factory extends MatchFactory<IntMatch> {

public Factory() {
super("int");
super(TYPE);
}

@Override
Expand Down
Loading