First look at data generated machine recipes.
Doesn't support all the features but should be a good start.
This commit is contained in:
parent
9e6b4385f6
commit
32368f8388
14 changed files with 380 additions and 150 deletions
|
@ -43,7 +43,6 @@ import reborncore.common.blockentity.MachineBaseBlockEntity;
|
|||
import reborncore.common.blocks.BlockWrenchEventHandler;
|
||||
import reborncore.common.chunkloading.ChunkLoaderManager;
|
||||
import reborncore.common.config.Configuration;
|
||||
import reborncore.common.crafting.ingredient.IngredientManager;
|
||||
import reborncore.common.misc.ModSounds;
|
||||
import reborncore.common.misc.RebornCoreTags;
|
||||
import reborncore.common.multiblock.MultiblockRegistry;
|
||||
|
@ -108,8 +107,6 @@ public class RebornCore implements ModInitializer {
|
|||
// packets
|
||||
ServerBoundPackets.init();
|
||||
|
||||
IngredientManager.setup();
|
||||
|
||||
RebornCoreCommands.setup();
|
||||
|
||||
RebornCoreTags.WATER_EXPLOSION_ITEM.toString();
|
||||
|
|
|
@ -56,7 +56,6 @@ public class FluidIngredient extends RebornIngredient {
|
|||
private final Lazy<Ingredient> previewIngredient;
|
||||
|
||||
public FluidIngredient(Fluid fluid, Optional<List<Item>> holders, Optional<Integer> count) {
|
||||
super(IngredientManager.FLUID_RECIPE_TYPE);
|
||||
this.fluid = fluid;
|
||||
this.holders = holders;
|
||||
this.count = count;
|
||||
|
|
|
@ -24,42 +24,31 @@
|
|||
|
||||
package reborncore.common.crafting.ingredient;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.recipe.Ingredient;
|
||||
import net.minecraft.util.Identifier;
|
||||
import com.google.gson.JsonParseException;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
public class IngredientFactory {
|
||||
public static RebornIngredient deserialize(@Nullable JsonElement jsonElement) {
|
||||
if (jsonElement == null || !jsonElement.isJsonObject()) {
|
||||
throw new JsonParseException("ingredient must be a json object");
|
||||
}
|
||||
|
||||
public class DummyIngredient extends RebornIngredient {
|
||||
final JsonObject json = jsonElement.getAsJsonObject();
|
||||
|
||||
public DummyIngredient() {
|
||||
super(new Identifier("reborncore", "dummy"));
|
||||
}
|
||||
if (json.has("type")) {
|
||||
throw new UnsupportedOperationException("type is unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(ItemStack itemStack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ingredient getPreview() {
|
||||
return Ingredient.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> getPreviewStacks() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject toJson(boolean networkSync) {
|
||||
return new JsonObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 0;
|
||||
if (json.has("fluid")) {
|
||||
return FluidIngredient.deserialize(json);
|
||||
} else if (json.has("tag") || json.has("tag_server_sync")) {
|
||||
return TagIngredient.deserialize(json);
|
||||
} else if (json.has("item")) {
|
||||
return StackIngredient.deserialize(json);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unable to determine ingredient: " + json);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
/*
|
||||
* This file is part of RebornCore, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2021 TeamReborn
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package reborncore.common.crafting.ingredient;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class IngredientManager {
|
||||
|
||||
public static final Identifier STACK_RECIPE_TYPE = new Identifier("reborncore", "stack");
|
||||
public static final Identifier FLUID_RECIPE_TYPE = new Identifier("reborncore", "fluid");
|
||||
public static final Identifier TAG_RECIPE_TYPE = new Identifier("reborncore", "tag");
|
||||
|
||||
private static final HashMap<Identifier, Function<JsonObject, RebornIngredient>> recipeTypes = new HashMap<>();
|
||||
|
||||
public static void setup() {
|
||||
recipeTypes.put(STACK_RECIPE_TYPE, StackIngredient::deserialize);
|
||||
recipeTypes.put(FLUID_RECIPE_TYPE, FluidIngredient::deserialize);
|
||||
recipeTypes.put(TAG_RECIPE_TYPE, TagIngredient::deserialize);
|
||||
}
|
||||
|
||||
public static RebornIngredient deserialize(@Nullable JsonElement jsonElement) {
|
||||
if (jsonElement == null || !jsonElement.isJsonObject()) {
|
||||
throw new JsonParseException("ingredient must be a json object");
|
||||
}
|
||||
|
||||
JsonObject json = jsonElement.getAsJsonObject();
|
||||
|
||||
Identifier recipeTypeIdent = STACK_RECIPE_TYPE;
|
||||
//TODO find a better way to do this.
|
||||
if (json.has("fluid")) {
|
||||
recipeTypeIdent = FLUID_RECIPE_TYPE;
|
||||
} else if (json.has("tag")) {
|
||||
recipeTypeIdent = TAG_RECIPE_TYPE;
|
||||
}
|
||||
|
||||
if (json.has("type")) {
|
||||
recipeTypeIdent = new Identifier(JsonHelper.getString(json, "type"));
|
||||
}
|
||||
|
||||
Function<JsonObject, RebornIngredient> recipeTypeFunction = recipeTypes.get(recipeTypeIdent);
|
||||
if (recipeTypeFunction == null) {
|
||||
throw new JsonParseException("No recipe type found for " + recipeTypeIdent.toString());
|
||||
}
|
||||
return recipeTypeFunction.apply(json);
|
||||
}
|
||||
}
|
|
@ -27,20 +27,11 @@ package reborncore.common.crafting.ingredient;
|
|||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.recipe.Ingredient;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public abstract class RebornIngredient implements Predicate<ItemStack> {
|
||||
|
||||
private final Identifier ingredientType;
|
||||
|
||||
public RebornIngredient(Identifier ingredientType) {
|
||||
this.ingredientType = ingredientType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean test(ItemStack itemStack);
|
||||
|
||||
|
@ -48,26 +39,7 @@ public abstract class RebornIngredient implements Predicate<ItemStack> {
|
|||
|
||||
public abstract List<ItemStack> getPreviewStacks();
|
||||
|
||||
protected abstract JsonObject toJson(boolean networkSync);
|
||||
public abstract JsonObject toJson(boolean networkSync);
|
||||
|
||||
public abstract int getCount();
|
||||
|
||||
//Same as above but adds the type
|
||||
public final JsonObject writeToJson(boolean networkSync) {
|
||||
JsonObject jsonObject = toJson(networkSync);
|
||||
jsonObject.addProperty("type", ingredientType.toString());
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public final JsonObject writeToSyncJson() {
|
||||
return writeToJson(true);
|
||||
}
|
||||
|
||||
public <T extends RebornIngredient> void ifType(Class<T> clazz, Consumer<T> consumer) {
|
||||
if (this.getClass().isAssignableFrom(clazz)) {
|
||||
//noinspection unchecked
|
||||
consumer.accept((T) this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -54,7 +54,6 @@ public class StackIngredient extends RebornIngredient {
|
|||
private final boolean requireEmptyNbt;
|
||||
|
||||
public StackIngredient(List<ItemStack> stacks, Optional<Integer> count, Optional<NbtCompound> nbt, boolean requireEmptyNbt) {
|
||||
super(IngredientManager.STACK_RECIPE_TYPE);
|
||||
this.stacks = stacks;
|
||||
this.count = count;
|
||||
this.nbt = nbt;
|
||||
|
|
|
@ -48,7 +48,6 @@ public class TagIngredient extends RebornIngredient {
|
|||
private final Optional<Integer> count;
|
||||
|
||||
public TagIngredient(Tag.Identified<Item> tag, Optional<Integer> count) {
|
||||
super(IngredientManager.TAG_RECIPE_TYPE);
|
||||
this.tag = tag;
|
||||
this.count = count;
|
||||
}
|
||||
|
@ -81,7 +80,7 @@ public class TagIngredient extends RebornIngredient {
|
|||
count = Optional.of(JsonHelper.getInt(json, "count"));
|
||||
}
|
||||
|
||||
if (json.has("server_sync")) {
|
||||
if (json.has("tag_server_sync")) {
|
||||
Identifier tagIdent = new Identifier(JsonHelper.getString(json, "tag_identifier"));
|
||||
List<Item> items = new ArrayList<>();
|
||||
for (int i = 0; i < JsonHelper.getInt(json, "items"); i++) {
|
||||
|
@ -109,13 +108,15 @@ public class TagIngredient extends RebornIngredient {
|
|||
return toItemJsonObject();
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("tag", tag.getId().toString());
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
private JsonObject toItemJsonObject() {
|
||||
//Tags are not synced across the server so we sync all the items
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("server_sync", true);
|
||||
jsonObject.addProperty("tag_server_sync", true);
|
||||
|
||||
Item[] items = tag.values().toArray(new Item[0]);
|
||||
jsonObject.addProperty("items", items.length);
|
||||
|
|
|
@ -34,7 +34,7 @@ import net.minecraft.util.JsonHelper;
|
|||
import net.minecraft.util.registry.Registry;
|
||||
import reborncore.common.crafting.RebornRecipe;
|
||||
import reborncore.common.crafting.RecipeUtils;
|
||||
import reborncore.common.crafting.ingredient.IngredientManager;
|
||||
import reborncore.common.crafting.ingredient.IngredientFactory;
|
||||
import reborncore.common.crafting.ingredient.RebornIngredient;
|
||||
import reborncore.common.util.DefaultedListCollector;
|
||||
import reborncore.common.util.serialization.SerializationUtil;
|
||||
|
@ -52,7 +52,7 @@ public abstract class AbstractRecipeSerde<R extends RebornRecipe> implements Rec
|
|||
|
||||
protected List<RebornIngredient> getIngredients(JsonObject jsonObject) {
|
||||
return SerializationUtil.stream(JsonHelper.getArray(jsonObject, "ingredients"))
|
||||
.map(IngredientManager::deserialize)
|
||||
.map(IngredientFactory::deserialize)
|
||||
.collect(DefaultedListCollector.toList());
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ public abstract class AbstractRecipeSerde<R extends RebornRecipe> implements Rec
|
|||
|
||||
protected void writeIngredients(R recipe, JsonObject jsonObject, boolean networkSync) {
|
||||
final JsonArray ingredientsArray = new JsonArray();
|
||||
recipe.getRebornIngredients().stream().map(RebornIngredient::writeToSyncJson).forEach(ingredientsArray::add);
|
||||
recipe.getRebornIngredients().stream().map(ingredient -> ingredient.toJson(networkSync)).forEach(ingredientsArray::add);
|
||||
jsonObject.add("ingredients", ingredientsArray);
|
||||
}
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ public abstract class RebornRecipeSerde<R extends RebornRecipe> extends Abstract
|
|||
public final void toJson(R recipe, JsonObject jsonObject, boolean networkSync) {
|
||||
writePower(recipe, jsonObject);
|
||||
writeTime(recipe, jsonObject);
|
||||
writeIngredients(recipe, jsonObject, true);
|
||||
writeIngredients(recipe, jsonObject, networkSync);
|
||||
writeOutputs(recipe, jsonObject);
|
||||
|
||||
collectJsonData(recipe, jsonObject, networkSync);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue