Skip to content

Commit

Permalink
Complete the design of NBTToJson
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Oct 5, 2021
1 parent 1f0117a commit d18d78b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.tartaricacid.touhoulittlemaid.crafting.AltarRecipe;
import com.github.tartaricacid.touhoulittlemaid.init.InitItems;
import com.github.tartaricacid.touhoulittlemaid.init.InitRecipes;
import com.github.tartaricacid.touhoulittlemaid.util.NBTToJson;
import com.google.common.collect.Lists;
import com.google.gson.*;
import net.minecraft.data.DataGenerator;
Expand All @@ -30,6 +31,13 @@ public AltarRecipeProvider(DataGenerator generator) {
this.generator = generator;
}

protected void registerRecipes() {
addItemRecipes(new ResourceLocation(TouhouLittleMaid.MOD_ID, "craft_explosion_protect_bauble"),
InitItems.EXPLOSION_PROTECT_BAUBLE.get().getDefaultInstance(), 0.2f,
Ingredient.of(Items.NETHER_WART), Ingredient.of(Tags.Items.DYES_ORANGE), Ingredient.of(Tags.Items.OBSIDIAN),
Ingredient.of(Tags.Items.OBSIDIAN), Ingredient.of(Tags.Items.OBSIDIAN), Ingredient.of(Tags.Items.OBSIDIAN));
}

@Override
public void run(DirectoryCache cache) throws IOException {
Path path = this.generator.getOutputFolder();
Expand All @@ -42,13 +50,6 @@ public void run(DirectoryCache cache) throws IOException {
}
}

protected void registerRecipes() {
addItemRecipes(new ResourceLocation(TouhouLittleMaid.MOD_ID, "craft_explosion_protect_bauble"),
InitItems.EXPLOSION_PROTECT_BAUBLE.get().getDefaultInstance(), 0.2f,
Ingredient.of(Items.NETHER_WART), Ingredient.of(Tags.Items.DYES_ORANGE), Ingredient.of(Tags.Items.OBSIDIAN),
Ingredient.of(Tags.Items.OBSIDIAN), Ingredient.of(Tags.Items.OBSIDIAN), Ingredient.of(Tags.Items.OBSIDIAN));
}

public void addRecipes(AltarRecipe recipe) {
recipes.add(recipe);
}
Expand All @@ -69,9 +70,7 @@ private JsonObject recipeToJson(AltarRecipe recipe) {
throw new JsonParseException("Entity Registry Name Not Found");
}
output.addProperty("type", name.toString());
if (recipe.getExtraData() != null) {
output.addProperty("nbt", recipe.getExtraData().getAsString());
}
NBTToJson.getJson(recipe.getExtraData()).ifPresent(e -> output.add("nbt", e));
json.add("output", output);
}
json.addProperty("power", recipe.getPowerCost());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.tartaricacid.touhoulittlemaid.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.minecraft.nbt.*;

import javax.annotation.Nullable;
import java.util.Optional;

@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
public final class NBTToJson {
public static Optional<JsonElement> getJson(@Nullable INBT nbt) {
JsonElement element = null;
if (nbt instanceof CollectionNBT) {
element = collection((CollectionNBT<?>) nbt);
}
if (nbt instanceof CompoundNBT) {
element = compound((CompoundNBT) nbt);
}
if (nbt instanceof StringNBT) {
element = string((StringNBT) nbt);
}
if (nbt instanceof NumberNBT) {
element = number((NumberNBT) nbt);
}
return Optional.ofNullable(element);
}

private static JsonElement collection(CollectionNBT<?> nbt) {
JsonArray array = new JsonArray();
nbt.forEach(e -> getJson(e).ifPresent(array::add));
return array;
}

private static JsonElement compound(CompoundNBT nbt) {
JsonObject object = new JsonObject();
nbt.getAllKeys().forEach(k -> getJson(nbt.get(k)).ifPresent(e -> object.add(k, e)));
return object;
}

private static JsonElement string(StringNBT nbt) {
return new JsonPrimitive(nbt.getAsString());
}

private static JsonElement number(NumberNBT nbt) {
return new JsonPrimitive(nbt.getAsNumber());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
"type": "touhou_little_maid:altar_crafting",
"output": {
"type": "minecraft:item",
"nbt": "{Item:{id:\"touhou_little_maid:explosion_protect_bauble\",Count:1b,tag:{Damage:0}}}"
"nbt": {
"Item": {
"id": "touhou_little_maid:explosion_protect_bauble",
"Count": 1,
"tag": {
"Damage": 0
}
}
}
},
"power": 0.2,
"ingredients": [
Expand Down

0 comments on commit d18d78b

Please sign in to comment.