mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-26 06:42:57 +00:00
Move IngredientWithout and IngredientIntersection from TConsturct
This commit is contained in:
1 parent
3c35ecd859
commit
02af84f70f
6 files changed
+290
No files matched your search
@@ -2,6 +2,7 @@ package slimeknights.mantle;
|
||||
|
||||
import net.minecraft.item.crafting.IRecipeSerializer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.ModLoadingContext;
|
||||
@@ -16,6 +17,8 @@ import slimeknights.mantle.loot.MantleLoot;
|
||||
import slimeknights.mantle.network.MantleNetwork;
|
||||
import slimeknights.mantle.recipe.crafting.ShapedFallbackRecipe;
|
||||
import slimeknights.mantle.recipe.crafting.ShapedRetexturedRecipe;
|
||||
import slimeknights.mantle.recipe.ingredient.IngredientIntersection;
|
||||
import slimeknights.mantle.recipe.ingredient.IngredientWithout;
|
||||
import slimeknights.mantle.registration.adapter.RegistryAdapter;
|
||||
|
||||
/**
|
||||
@@ -55,6 +58,9 @@ public class Mantle {
|
||||
adapter.register(new ShapedFallbackRecipe.Serializer(), "crafting_shaped_fallback");
|
||||
adapter.register(new ShapedRetexturedRecipe.Serializer(), "crafting_shaped_retextured");
|
||||
|
||||
CraftingHelper.register(IngredientWithout.ID, IngredientWithout.SERIALIZER);
|
||||
CraftingHelper.register(IngredientIntersection.ID, IngredientIntersection.SERIALIZER);
|
||||
|
||||
// done here as no dedicated event
|
||||
MantleLoot.register();
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Ingredient accepting an entity or an entity tag as an input
|
||||
* TODO: move to ingredient package in 1.17
|
||||
*/
|
||||
public abstract class EntityIngredient implements Predicate<EntityType<?>> {
|
||||
/** Empty entity ingredient, matching nothing */
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// TODO: move to ingredient package in 1.17
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class FluidIngredient {
|
||||
/** Empty fluid ingredient, matches nothing */
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
package slimeknights.mantle.recipe.ingredient;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.item.crafting.RecipeItemHelper;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.IIngredientSerializer;
|
||||
import slimeknights.mantle.Mantle;
|
||||
import slimeknights.mantle.util.JsonHelper;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/** Ingredient that matches anything that matches all of the sub ingredients */
|
||||
public class IngredientIntersection extends Ingredient {
|
||||
public static final ResourceLocation ID = Mantle.getResource("intersection");
|
||||
public static final IIngredientSerializer<IngredientIntersection> SERIALIZER = new Serializer();
|
||||
|
||||
private final List<Ingredient> ingredients;
|
||||
private ItemStack[] intersectedMatchingStacks;
|
||||
private IntList packedMatchingStacks;
|
||||
public IngredientIntersection(List<Ingredient> ingredients) {
|
||||
super(Stream.empty());
|
||||
this.ingredients = ingredients;
|
||||
}
|
||||
|
||||
public IngredientIntersection(Ingredient... ingredients) {
|
||||
this(ImmutableList.copyOf(ingredients));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack stack) {
|
||||
if (stack == null || stack.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (Ingredient ingredient : ingredients) {
|
||||
if (!ingredient.test(stack)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack[] getMatchingStacks() {
|
||||
if (this.intersectedMatchingStacks == null) {
|
||||
if (ingredients.isEmpty()) {
|
||||
this.intersectedMatchingStacks = new ItemStack[0];
|
||||
} else {
|
||||
this.intersectedMatchingStacks = Arrays
|
||||
.stream(ingredients.get(0).getMatchingStacks())
|
||||
.filter(stack -> {
|
||||
for (int i = 1; i < ingredients.size(); i++) {
|
||||
if (!ingredients.get(i).test(stack)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}).toArray(ItemStack[]::new);
|
||||
}
|
||||
}
|
||||
return intersectedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNoMatchingItems() {
|
||||
return getMatchingStacks().length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple() {
|
||||
for (Ingredient ingredient : ingredients) {
|
||||
if (!ingredient.isSimple()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidate() {
|
||||
super.invalidate();
|
||||
this.intersectedMatchingStacks = null;
|
||||
this.packedMatchingStacks = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntList getValidItemStacksPacked() {
|
||||
if (this.packedMatchingStacks == null) {
|
||||
ItemStack[] matchingStacks = getMatchingStacks();
|
||||
this.packedMatchingStacks = new IntArrayList(matchingStacks.length);
|
||||
for(ItemStack stack : matchingStacks) {
|
||||
this.packedMatchingStacks.add(RecipeItemHelper.pack(stack));
|
||||
}
|
||||
this.packedMatchingStacks.sort(IntComparators.NATURAL_COMPARATOR);
|
||||
}
|
||||
return packedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("type", ID.toString());
|
||||
JsonArray array = new JsonArray();
|
||||
for (Ingredient ingredient : ingredients) {
|
||||
array.add(ingredient.serialize());
|
||||
}
|
||||
json.add("ingredients", array);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<IngredientIntersection> getSerializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
private static class Serializer implements IIngredientSerializer<IngredientIntersection> {
|
||||
@Override
|
||||
public IngredientIntersection parse(JsonObject json) {
|
||||
List<Ingredient> ingredients = JsonHelper.parseList(json, "ingredients", (element, name) -> Ingredient.deserialize(element));
|
||||
return new IngredientIntersection(ingredients);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IngredientIntersection parse(PacketBuffer buffer) {
|
||||
int size = buffer.readVarInt();
|
||||
ImmutableList.Builder<Ingredient> builder = ImmutableList.builder();
|
||||
for (int i = 0; i < size; i++) {
|
||||
builder.add(Ingredient.read(buffer));
|
||||
}
|
||||
return new IngredientIntersection(builder.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketBuffer buffer, IngredientIntersection intersection) {
|
||||
buffer.writeVarInt(intersection.ingredients.size());
|
||||
for (Ingredient ingredient : intersection.ingredients) {
|
||||
CraftingHelper.write(buffer, ingredient);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package slimeknights.mantle.recipe.ingredient;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.item.crafting.RecipeItemHelper;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.IIngredientSerializer;
|
||||
import slimeknights.mantle.Mantle;
|
||||
import slimeknights.mantle.util.JsonHelper;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Ingredient that matches everything from another ingredient, without a second
|
||||
*/
|
||||
public class IngredientWithout extends Ingredient {
|
||||
public static final ResourceLocation ID = Mantle.getResource("without");
|
||||
public static final IIngredientSerializer<IngredientWithout> SERIALIZER = new Serializer();
|
||||
|
||||
private final Ingredient base;
|
||||
private final Ingredient without;
|
||||
private ItemStack[] filteredMatchingStacks;
|
||||
private IntList packedMatchingStacks;
|
||||
public IngredientWithout(Ingredient base, Ingredient without) {
|
||||
super(Stream.empty());
|
||||
this.base = base;
|
||||
this.without = without;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack stack) {
|
||||
if (stack == null || stack.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return base.test(stack) && !without.test(stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack[] getMatchingStacks() {
|
||||
if (this.filteredMatchingStacks == null) {
|
||||
this.filteredMatchingStacks = Arrays.stream(base.getMatchingStacks())
|
||||
.filter(stack -> !without.test(stack))
|
||||
.toArray(ItemStack[]::new);
|
||||
}
|
||||
return filteredMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNoMatchingItems() {
|
||||
return getMatchingStacks().length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple() {
|
||||
return base.isSimple() && without.isSimple();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidate() {
|
||||
super.invalidate();
|
||||
this.filteredMatchingStacks = null;
|
||||
this.packedMatchingStacks = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntList getValidItemStacksPacked() {
|
||||
if (this.packedMatchingStacks == null) {
|
||||
ItemStack[] matchingStacks = getMatchingStacks();
|
||||
this.packedMatchingStacks = new IntArrayList(matchingStacks.length);
|
||||
for(ItemStack stack : matchingStacks) {
|
||||
this.packedMatchingStacks.add(RecipeItemHelper.pack(stack));
|
||||
}
|
||||
this.packedMatchingStacks.sort(IntComparators.NATURAL_COMPARATOR);
|
||||
}
|
||||
return packedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("type", ID.toString());
|
||||
json.add("base", base.serialize());
|
||||
json.add("without", without.serialize());
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<IngredientWithout> getSerializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
private static class Serializer implements IIngredientSerializer<IngredientWithout> {
|
||||
@Override
|
||||
public IngredientWithout parse(JsonObject json) {
|
||||
Ingredient base = Ingredient.deserialize(JsonHelper.getElement(json, "base"));
|
||||
Ingredient without = Ingredient.deserialize(JsonHelper.getElement(json, "without"));
|
||||
return new IngredientWithout(base, without);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IngredientWithout parse(PacketBuffer buffer) {
|
||||
Ingredient base = Ingredient.read(buffer);
|
||||
Ingredient without = Ingredient.read(buffer);
|
||||
return new IngredientWithout(base, without);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketBuffer buffer, IngredientWithout ingredient) {
|
||||
CraftingHelper.write(buffer, ingredient.base);
|
||||
CraftingHelper.write(buffer, ingredient.without);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package slimeknights.mantle.recipe.ingredient;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
Reference in new issue
Block a user