mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-18 19:29:32 +00:00
Port recipe utils from Tinkers Construct
Includes inventory wrappers, fluid ingredient, and recipe getter utils
This commit is contained in:
1 parent
e658d4b5f9
commit
a8899f976c
11 files changed
+894
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package slimeknights.mantle.recipe;
|
||||
|
||||
import net.minecraft.advancements.Advancement;
|
||||
import net.minecraft.advancements.AdvancementRewards;
|
||||
import net.minecraft.advancements.ICriterionInstance;
|
||||
import net.minecraft.advancements.IRequirementsStrategy;
|
||||
import net.minecraft.advancements.criterion.RecipeUnlockedTrigger;
|
||||
import net.minecraft.data.IFinishedRecipe;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Common logic to create a recipe builder class
|
||||
* @param <T>
|
||||
*/
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
public abstract class AbstractRecipeBuilder<T extends AbstractRecipeBuilder<T>> {
|
||||
/** Advancement builder for this class */
|
||||
protected final Advancement.Builder advancementBuilder = Advancement.Builder.builder();
|
||||
/** Group for this recipe */
|
||||
@Nullable
|
||||
protected String group;
|
||||
|
||||
/**
|
||||
* Adds a criteria to the recipe
|
||||
* @param name Criteria name
|
||||
* @param criteria Criteria instance
|
||||
* @return Builder
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T addCriterion(String name, ICriterionInstance criteria) {
|
||||
this.advancementBuilder.withCriterion(name, criteria);
|
||||
return (T)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the group for this recipe
|
||||
* @param group Recipe group
|
||||
* @return Builder
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T setGroup(String group) {
|
||||
this.group = group;
|
||||
return (T)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the recipe with a default recipe ID, typically based on the output
|
||||
* @param consumerIn Recipe consumer
|
||||
*/
|
||||
public abstract void build(Consumer<IFinishedRecipe> consumerIn);
|
||||
|
||||
/**
|
||||
* Builds the recipe
|
||||
* @param consumerIn Recipe consumer
|
||||
* @param id Recipe ID
|
||||
*/
|
||||
public abstract void build(Consumer<IFinishedRecipe> consumerIn, ResourceLocation id);
|
||||
|
||||
/**
|
||||
* Builds and validates the advancement, intended to be called in {@link #build(Consumer, ResourceLocation)}
|
||||
* @param id Recipe ID
|
||||
* @param folder Group folder for saving recipes. Vanilla typically uses item groups, but for mods might as well base on the recipe
|
||||
* @return Advancement ID
|
||||
*/
|
||||
protected ResourceLocation buildAdvancement(ResourceLocation id, String folder) {
|
||||
if (this.advancementBuilder.getCriteria().isEmpty()) {
|
||||
throw new IllegalStateException("No way of obtaining recipe " + id);
|
||||
}
|
||||
this.advancementBuilder
|
||||
.withParentId(new ResourceLocation("recipes/root"))
|
||||
.withCriterion("has_the_recipe", RecipeUnlockedTrigger.func_235675_a_(id))
|
||||
.withRewards(AdvancementRewards.Builder.recipe(id))
|
||||
.withRequirementsStrategy(IRequirementsStrategy.OR);
|
||||
return new ResourceLocation(id.getNamespace(), "recipes/" + folder + "/" + id.getPath());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,410 @@
|
||||
package slimeknights.mantle.recipe;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.tags.FluidTags;
|
||||
import net.minecraft.tags.ITag;
|
||||
import net.minecraft.tags.Tag;
|
||||
import net.minecraft.tags.TagCollectionManager;
|
||||
import net.minecraft.util.JSONUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class FluidIngredient {
|
||||
/** Empty fluid ingredient, matches nothing */
|
||||
public static final FluidIngredient EMPTY = new Empty();
|
||||
|
||||
/**
|
||||
* Checks if the given fluid matches this ingredient
|
||||
* @param fluid Fluid to check
|
||||
* @return True if the fluid matches
|
||||
*/
|
||||
public abstract boolean test(Fluid fluid);
|
||||
|
||||
/**
|
||||
* Gets the amount of the given fluid needed for the recipe
|
||||
* @param fluid Fluid to check
|
||||
* @return Amount of the fluid needed
|
||||
*/
|
||||
public abstract int getAmount(Fluid fluid);
|
||||
|
||||
/**
|
||||
* Checks if the given fluid stack argument matches this ingredient
|
||||
* @param stack Fluid stack to check
|
||||
* @return True if the fluid matches this ingredient and the amount is equal or greater than this
|
||||
*/
|
||||
public boolean test(FluidStack stack) {
|
||||
Fluid fluid = stack.getFluid();
|
||||
return stack.getAmount() >= getAmount(fluid) && test(stack.getFluid());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of fluid stacks contained in this ingredient for display
|
||||
* @return List of fluid stacks for this ingredient
|
||||
*/
|
||||
public abstract List<FluidStack> getFluids();
|
||||
|
||||
/**
|
||||
* Serializes the Fluid Ingredient into JSON
|
||||
* @return FluidIngredient JSON
|
||||
*/
|
||||
public abstract JsonElement serialize();
|
||||
|
||||
/**
|
||||
* Writes the ingredient into the packet buffer
|
||||
* @param buffer Packet buffer instance
|
||||
*/
|
||||
public void write(PacketBuffer buffer) {
|
||||
Collection<FluidStack> fluids = getFluids();
|
||||
buffer.writeInt(fluids.size());
|
||||
for (FluidStack stack : fluids) {
|
||||
buffer.writeString(Objects.requireNonNull(stack.getFluid().getRegistryName()).toString());
|
||||
buffer.writeInt(stack.getAmount());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Instance creation
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a new ingredient using the given fluid and amount
|
||||
* @param fluid Fluid to check
|
||||
* @param amount Minimum fluid amount
|
||||
* @return Fluid ingredient for this fluid
|
||||
*/
|
||||
public static FluidIngredient of(Fluid fluid, int amount) {
|
||||
return new FluidIngredient.FluidMatch(fluid, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ingredient using the given fluidstack
|
||||
* @param stack Fluid stack
|
||||
* @return Fluid ingredient for this fluid stack
|
||||
*/
|
||||
public static FluidIngredient of(FluidStack stack) {
|
||||
return of(stack.getFluid(), stack.getAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new fluid ingredient from the given tag
|
||||
* @param fluid Fluid tag
|
||||
* @param amount Minimum fluid amount
|
||||
* @return Fluid ingredient from a tag
|
||||
*/
|
||||
public static FluidIngredient of(Tag<Fluid> fluid, int amount) {
|
||||
return new FluidIngredient.TagMatch(fluid, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new compound ingredient from the given list of ingredients
|
||||
* @param ingredients Ingredient list
|
||||
* @return Compound ingredient
|
||||
*/
|
||||
public static FluidIngredient of(FluidIngredient... ingredients) {
|
||||
return new FluidIngredient.Compound(ingredients);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* JSON deserializing
|
||||
*/
|
||||
|
||||
/**
|
||||
* Deserializes the fluid ingredient from JSON
|
||||
* @param parent Parent containing the fluid JSON
|
||||
* @param name Name of the key to fetch from the parent object
|
||||
* @return Fluid ingredient instance
|
||||
* @throws JsonSyntaxException if syntax is invalid
|
||||
*/
|
||||
public static FluidIngredient deserialize(JsonObject parent, String name) {
|
||||
JsonElement json = parent.get(name);
|
||||
|
||||
// single ingredient object
|
||||
if (json.isJsonObject()) {
|
||||
return deserializeObject(json.getAsJsonObject());
|
||||
}
|
||||
|
||||
// array
|
||||
if (json.isJsonArray()) {
|
||||
return Compound.deserialize(json.getAsJsonArray(), name);
|
||||
}
|
||||
|
||||
throw new JsonSyntaxException("Fluid ingredient " + name + " must be either an object or array");
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the fluid ingredient from JSON
|
||||
* @param json JSON object
|
||||
* @return Fluid Ingredient
|
||||
* @throws JsonSyntaxException if syntax is invalid
|
||||
*/
|
||||
private static FluidIngredient deserializeObject(JsonObject json) {
|
||||
if (json.entrySet().isEmpty()) {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
// fluid match
|
||||
if (json.has("name")) {
|
||||
// don't set both, obviously an error
|
||||
if (json.has("tag")) {
|
||||
throw new JsonSyntaxException("An ingredient entry is either a tag or an fluid, not both");
|
||||
}
|
||||
|
||||
// parse a fluid
|
||||
return FluidMatch.deserialize(json);
|
||||
}
|
||||
|
||||
// tag match
|
||||
if (json.has("tag")) {
|
||||
return TagMatch.deserialize(json);
|
||||
}
|
||||
|
||||
throw new JsonSyntaxException("An ingredient entry needs either a tag or an fluid");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Packet buffers
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reads a fluid ingredient from the packet buffer
|
||||
* @param buffer Buffer instance
|
||||
* @return Fluid ingredient instance
|
||||
*/
|
||||
public static FluidIngredient read(PacketBuffer buffer) {
|
||||
int count = buffer.readInt();
|
||||
FluidIngredient[] ingredients = new FluidIngredient[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(buffer.readString(32767)));
|
||||
if (fluid == null) {
|
||||
fluid = Fluids.EMPTY;
|
||||
}
|
||||
int amount = buffer.readInt();
|
||||
ingredients[i] = of(fluid, amount);
|
||||
}
|
||||
// if a single ingredient, do not wrap in compound
|
||||
if (count == 1) {
|
||||
return ingredients[0];
|
||||
}
|
||||
// compound for anything else
|
||||
return of(ingredients);
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty fluid ingredient, matches only empty fluid stacks
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
private static class Empty extends FluidIngredient {
|
||||
@Override
|
||||
public boolean test(Fluid fluid) {
|
||||
return fluid == Fluids.EMPTY;
|
||||
}
|
||||
@Override
|
||||
public boolean test(FluidStack fluid) {
|
||||
return fluid.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount(Fluid fluid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FluidStack> getFluids() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonObject();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fluid ingredient that matches a single fluid
|
||||
*/
|
||||
@AllArgsConstructor(access=AccessLevel.PRIVATE)
|
||||
private static class FluidMatch extends FluidIngredient {
|
||||
private final Fluid fluid;
|
||||
private final int amount;
|
||||
|
||||
@Override
|
||||
public boolean test(Fluid fluid) {
|
||||
return fluid == this.fluid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount(Fluid fluid) {
|
||||
return amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FluidStack> getFluids() {
|
||||
return Collections.singletonList(new FluidStack(fluid, amount));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("name", Objects.requireNonNull(fluid.getRegistryName()).toString());
|
||||
object.addProperty("amount", amount);
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketBuffer buffer) {
|
||||
// count
|
||||
buffer.writeInt(1);
|
||||
// single fluid
|
||||
buffer.writeString(Objects.requireNonNull(fluid.getRegistryName()).toString());
|
||||
buffer.writeInt(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserailizes the ingredient from JSON
|
||||
* @param json JSON object
|
||||
* @return Fluid ingredient instance
|
||||
*/
|
||||
private static FluidMatch deserialize(JsonObject json) {
|
||||
String fluidName = JSONUtils.getString(json, "name");
|
||||
Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(fluidName));
|
||||
if (fluid == null || fluid == Fluids.EMPTY) {
|
||||
throw new JsonSyntaxException("Unknown fluid '" + fluidName + "'");
|
||||
}
|
||||
int amount = JSONUtils.getInt(json, "amount");
|
||||
return new FluidMatch(fluid, amount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fluid ingredient that matches a tag
|
||||
*/
|
||||
@AllArgsConstructor(access=AccessLevel.PRIVATE)
|
||||
private static class TagMatch extends FluidIngredient {
|
||||
private final ITag<Fluid> tag;
|
||||
private final int amount;
|
||||
|
||||
@Override
|
||||
public boolean test(Fluid fluid) {
|
||||
return tag.contains(fluid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount(Fluid fluid) {
|
||||
return amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FluidStack> getFluids() {
|
||||
return tag.getAllElements().stream().map((fluid) -> new FluidStack(fluid, amount)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("tag", TagCollectionManager.func_232928_e_().func_232926_c_().func_232975_b_(this.tag).toString());
|
||||
object.addProperty("amount", amount);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deseralizes the ingredient from JSON
|
||||
* @param json JSON object
|
||||
* @return Fluid ingredient instance
|
||||
*/
|
||||
private static TagMatch deserialize(JsonObject json) {
|
||||
String tagName = JSONUtils.getString(json, "tag");
|
||||
ITag<Fluid> tag = FluidTags.getCollection().get(new ResourceLocation(tagName));
|
||||
if (tag == null) {
|
||||
throw new JsonSyntaxException("Unknown fluid tag '" + tagName + "'");
|
||||
}
|
||||
int amount = JSONUtils.getInt(json, "amount");
|
||||
return new TagMatch(tag, amount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fluid ingredient that matches a list of ingredients
|
||||
*/
|
||||
private static class Compound extends FluidIngredient {
|
||||
private final List<FluidIngredient> ingredients;
|
||||
private Compound(FluidIngredient[] ingredients) {
|
||||
this.ingredients = Arrays.asList(ingredients);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Fluid fluid) {
|
||||
return ingredients.stream().anyMatch(ingredient -> ingredient.test(fluid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(FluidStack stack) {
|
||||
return ingredients.stream().anyMatch(ingredient -> ingredient.test(stack));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount(Fluid fluid) {
|
||||
return ingredients.stream()
|
||||
.filter(ingredient -> ingredient.test(fluid))
|
||||
.mapToInt(ingredient -> ingredient.getAmount(fluid))
|
||||
.findFirst()
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FluidStack> getFluids() {
|
||||
return ingredients.stream()
|
||||
.flatMap(ingredient -> ingredient.getFluids().stream())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return ingredients.stream()
|
||||
.map(FluidIngredient::serialize)
|
||||
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes a compound ingredient from JSON
|
||||
* @param array JSON array
|
||||
* @param name Array key
|
||||
* @return Compound fluid ingredient instance
|
||||
*/
|
||||
private static Compound deserialize(JsonArray array, String name) {
|
||||
// size must be valid
|
||||
int size = array.size();
|
||||
if (size == 0) {
|
||||
throw new JsonSyntaxException("Fluid array cannot be empty, at least one fluid must be defined");
|
||||
}
|
||||
|
||||
// parse all ingredients
|
||||
FluidIngredient[] ingredients = new FluidIngredient[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
// no reason to an array in an array
|
||||
ingredients[i] = deserializeObject(JSONUtils.getJsonObject(array.get(i), name + "[" + i + "]"));
|
||||
}
|
||||
return new Compound(ingredients);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package slimeknights.mantle.recipe;
|
||||
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This interface is intended to be used on dynamic recipes to return a full list of valid recipes.
|
||||
* @param <T> Recipe type for the return
|
||||
*/
|
||||
public interface IMultiRecipe<T extends IRecipe<?>> {
|
||||
/**
|
||||
* Gets a list of recipes for display in JEI
|
||||
* @return List of recipes
|
||||
*/
|
||||
List<T> getRecipes();
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package slimeknights.mantle.recipe;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import io.netty.handler.codec.DecoderException;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.item.crafting.IRecipeType;
|
||||
import net.minecraft.item.crafting.RecipeManager;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.IItemProvider;
|
||||
import net.minecraft.util.JSONUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Helpers used in creation of recipes
|
||||
*/
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class RecipeHelper {
|
||||
|
||||
/* Recipe manager utils */
|
||||
|
||||
/**
|
||||
* Gets a recipe of a specific class type by name from the manager
|
||||
* @param manager Recipe manager
|
||||
* @param name Recipe name
|
||||
* @param clazz Output class
|
||||
* @param <C> Return type
|
||||
* @return Optional of the recipe, or empty if the recipe is missing
|
||||
*/
|
||||
public static <C extends IRecipe<?>> Optional<C> getRecipe(RecipeManager manager, ResourceLocation name, Class<C> clazz) {
|
||||
return manager.getRecipe(name).filter(clazz::isInstance).map(clazz::cast);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all recipes from the manager, safely casting to the specified type. Multi Recipes are kept as a single recipe instance
|
||||
* @param manager Recipe manager
|
||||
* @param type Recipe type
|
||||
* @param clazz Preferred recipe class type
|
||||
* @param <I> Inventory interface type
|
||||
* @param <T> Recipe class
|
||||
* @param <C> Return type
|
||||
* @return List of recipes from the manager
|
||||
*/
|
||||
public static <I extends IInventory, T extends IRecipe<I>, C extends T> List<C> getRecipes(RecipeManager manager, IRecipeType<T> type, Class<C> clazz) {
|
||||
return manager.getRecipes(type).values().stream()
|
||||
.filter(clazz::isInstance)
|
||||
.map(clazz::cast)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of recipes for display in a UI list, such as UI buttons. Will be sorted to keep the order the same on both sides, and filtered based on the given predicate and class
|
||||
* @param manager Recipe manager
|
||||
* @param type Recipe type
|
||||
* @param clazz Preferred recipe class type
|
||||
* @param filter Filter for which recipes to add to the list
|
||||
* @param <I> Inventory interface type
|
||||
* @param <T> Recipe class
|
||||
* @param <C> Return type
|
||||
* @return Recipe list
|
||||
*/
|
||||
public static <I extends IInventory, T extends IRecipe<I>, C extends T> List<C> getUIRecipes(RecipeManager manager, IRecipeType<T> type, Class<C> clazz, Predicate<? super C> filter) {
|
||||
return manager.getRecipes(type).values().stream()
|
||||
.filter(clazz::isInstance)
|
||||
.map(clazz::cast)
|
||||
.filter(filter)
|
||||
.sorted(Comparator.comparing(IRecipe::getId))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all recipes from the manager, expanding multi recipes. Intended for use in recipe display such as JEI
|
||||
* @param manager Recipe manager
|
||||
* @param type Recipe type
|
||||
* @param clazz Preferred recipe class type
|
||||
* @param <I> Inventory interface type
|
||||
* @param <T> Recipe class
|
||||
* @param <C> Return type
|
||||
* @return List of flattened recipes from the manager
|
||||
*/
|
||||
public static <I extends IInventory, T extends IRecipe<I>, C extends T> List<C> getJEIRecipes(RecipeManager manager, IRecipeType<T> type, Class<C> clazz) {
|
||||
return manager.getRecipes(type).values().stream()
|
||||
.sorted((r1, r2) -> {
|
||||
// if one is multi, and the other not, the multi recipe is larger
|
||||
boolean m1 = r1 instanceof IMultiRecipe<?>;
|
||||
boolean m2 = r2 instanceof IMultiRecipe<?>;
|
||||
if (m1 && !m2) {
|
||||
return 1;
|
||||
}
|
||||
if (!m1 && m2) {
|
||||
return -1;
|
||||
}
|
||||
// fall back to recipe ID
|
||||
return r1.getId().compareTo(r2.getId());
|
||||
})
|
||||
.flatMap((recipe) -> {
|
||||
// if its a multi recipe, extract child recipes and stream those
|
||||
if (recipe instanceof IMultiRecipe<?>) {
|
||||
return ((IMultiRecipe<?>)recipe).getRecipes().stream();
|
||||
}
|
||||
return Stream.of(recipe);
|
||||
})
|
||||
.filter(clazz::isInstance)
|
||||
.map(clazz::cast)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
/* JSON */
|
||||
|
||||
/**
|
||||
* Serializes the fluid stack into JSON
|
||||
* @param stack Stack to serialize
|
||||
* @return JSON data
|
||||
*/
|
||||
public static JsonObject serializeFluidStack(FluidStack stack) {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("fluid", Objects.requireNonNull(stack.getFluid().getRegistryName()).toString());
|
||||
json.addProperty("amount", stack.getAmount());
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the fluid stack from JSON
|
||||
* @param json JSON data
|
||||
* @return Fluid stack instance
|
||||
* @throws JsonSyntaxException if syntax is invalid
|
||||
*/
|
||||
public static FluidStack deserializeFluidStack(JsonObject json) {
|
||||
String fluidName = JSONUtils.getString(json, "fluid");
|
||||
Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(fluidName));
|
||||
if (fluid == null || fluid == Fluids.EMPTY) {
|
||||
throw new JsonSyntaxException("Unknown fluid " + fluidName);
|
||||
}
|
||||
int amount = JSONUtils.getInt(json, "amount");
|
||||
return new FluidStack(fluid, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an item from JSON and validates it class type
|
||||
* @param name String containing an item name
|
||||
* @param key Key to use for errors
|
||||
* @param clazz Output class
|
||||
* @param <C> Class type
|
||||
* @return Item read from JSON with the given class type
|
||||
* @throws JsonSyntaxException If the key is missing, or the value is not the right class
|
||||
*/
|
||||
public static <C> C deserializeItem(String name, String key, Class<C> clazz) {
|
||||
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(name));
|
||||
if (item == null) {
|
||||
throw new JsonSyntaxException("Invalid " + key + ": Unknown item " + name + "'");
|
||||
}
|
||||
if (!clazz.isInstance(item)) {
|
||||
throw new JsonSyntaxException("Invalid " + key + ": must be " + clazz.getSimpleName());
|
||||
}
|
||||
return clazz.cast(item);
|
||||
}
|
||||
|
||||
|
||||
/* Packet buffer utils */
|
||||
|
||||
/**
|
||||
* Reads an item from the packet buffer
|
||||
* @param buffer Buffer instance
|
||||
* @return Item read from the buffer
|
||||
*/
|
||||
public static Item readItem(PacketBuffer buffer) {
|
||||
return Item.getItemById(buffer.readVarInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an item from the packet buffer and validates its class type
|
||||
* @param buffer Buffer instance
|
||||
* @param clazz Output class
|
||||
* @param <T> Class type
|
||||
* @return Item read from the buffer with the given class type
|
||||
* @throws DecoderException If the value is not the right class
|
||||
*/
|
||||
public static <T> T readItem(PacketBuffer buffer, Class<T> clazz) {
|
||||
Item item = readItem(buffer);
|
||||
if (!clazz.isInstance(item)) {
|
||||
throw new DecoderException("Invalid item '" + item.getRegistryName() + "', must be " + clazz.getSimpleName());
|
||||
}
|
||||
return clazz.cast(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an item to the packet buffer
|
||||
* @param buffer Buffer instance
|
||||
* @param item Item to write
|
||||
*/
|
||||
public static void writeItem(PacketBuffer buffer, IItemProvider item) {
|
||||
buffer.writeVarInt(Item.getIdFromItem(item.asItem()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package slimeknights.mantle.recipe.inventory;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* Base inventory for inventories that do not use items
|
||||
*/
|
||||
public interface IEmptyInventory extends IReadOnlyInventory {
|
||||
/** @deprecated unused method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default ItemStack getStackInSlot(int index) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
/** @deprecated unused method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default boolean isEmpty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @deprecated always 0, not useful */
|
||||
@Deprecated
|
||||
@Override
|
||||
default int getSizeInventory() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package slimeknights.mantle.recipe.inventory;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* IInventory extension for a recipe that only needs read access.
|
||||
* Used to control which slots an recipe gets and to prevent the need to implement IInventory to get the recipe.
|
||||
*/
|
||||
public interface IReadOnlyInventory extends IInventory {
|
||||
/* Unsupported operations */
|
||||
|
||||
/** @deprecated unsupported method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default ItemStack decrStackSize(int index, int count) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/** @deprecated unsupported method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default ItemStack removeStackFromSlot(int index) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/** @deprecated unsupported method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default void setInventorySlotContents(int index, ItemStack stack) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/** @deprecated unsupported method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/* Unused */
|
||||
|
||||
/** @deprecated unused method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default void markDirty() {}
|
||||
|
||||
/** @deprecated unused method */
|
||||
@Deprecated
|
||||
@Override
|
||||
default boolean isUsableByPlayer(PlayerEntity player) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package slimeknights.mantle.recipe.inventory;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* IInventory extension for an inventory wrapper containing a single item. Primarily used for furnace like recipes.
|
||||
*/
|
||||
public interface ISingleItemInventory extends IReadOnlyInventory {
|
||||
/**
|
||||
* Gets the relevant item in this inventory
|
||||
* @return Contained item
|
||||
*/
|
||||
ItemStack getStack();
|
||||
|
||||
/* Multistack methods, redundant now */
|
||||
|
||||
/** @deprecated use {{@link #getStack()}} */
|
||||
@Deprecated
|
||||
@Override
|
||||
default ItemStack getStackInSlot(int index) {
|
||||
return index == 0 ? getStack() : ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean isEmpty() {
|
||||
return getStack().isEmpty();
|
||||
}
|
||||
|
||||
/** @deprecated always 1, not useful */
|
||||
@Deprecated
|
||||
@Override
|
||||
default int getSizeInventory() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* Utils made more efficient */
|
||||
|
||||
@Override
|
||||
default int count(Item itemIn) {
|
||||
ItemStack stack = getStack();
|
||||
if (stack.getItem() == itemIn) {
|
||||
return stack.getCount();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean hasAny(Set<Item> set) {
|
||||
ItemStack stack = getStack();
|
||||
return !stack.isEmpty() && set.contains(getStack().getItem());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package slimeknights.mantle.recipe.inventory;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* Implementation of {@link ISingleItemInventory} to wrap another {@link IInventory}
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class InventorySlotWrapper implements ISingleItemInventory {
|
||||
private final IInventory parent;
|
||||
private final int index;
|
||||
|
||||
@Override
|
||||
public ItemStack getStack() {
|
||||
return parent.getStackInSlot(index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package slimeknights.mantle.recipe.inventory;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@@ -0,0 +1,7 @@
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package slimeknights.mantle.recipe;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
Reference in new issue
Block a user