mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-27 23:45:57 +00:00
209 lines
5.6 KiB
Java
209 lines
5.6 KiB
Java
/*
|
|
* This class is distributed as part of the Botania Mod.
|
|
* Get the Source Code in github:
|
|
* https://github.com/Vazkii/Botania
|
|
*
|
|
* Botania is Open Source and distributed under the
|
|
* Botania License: http://botaniamod.net/license.php
|
|
*/
|
|
package vazkii.botania.api.recipe;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import net.minecraft.inventory.IInventory;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
import net.minecraft.item.crafting.IRecipeSerializer;
|
|
import net.minecraft.item.crafting.IRecipeType;
|
|
import net.minecraft.item.crafting.Ingredient;
|
|
import net.minecraft.network.PacketBuffer;
|
|
import net.minecraft.util.JSONUtils;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.common.crafting.CraftingHelper;
|
|
import net.minecraftforge.registries.ForgeRegistryEntry;
|
|
import vazkii.botania.common.crafting.ModRecipeTypes;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import javax.annotation.Nullable;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
public class RecipeElvenTrade implements IRecipe<IInventory> {
|
|
private final ResourceLocation id;
|
|
private final ImmutableList<ItemStack> outputs;
|
|
private final ImmutableList<Ingredient> inputs;
|
|
|
|
public RecipeElvenTrade(ResourceLocation id, ItemStack output, Ingredient... inputs) {
|
|
this(id, new ItemStack[] { output }, inputs);
|
|
}
|
|
|
|
public RecipeElvenTrade(ResourceLocation id, ItemStack[] outputs, Ingredient... inputs) {
|
|
this.id = id;
|
|
this.outputs = ImmutableList.copyOf(outputs);
|
|
this.inputs = ImmutableList.copyOf(inputs);
|
|
}
|
|
|
|
/**
|
|
* Attempts to match the recipe
|
|
*
|
|
* @param stacks Entire contents of the portal's buffer
|
|
* @return {@link Optional#empty()} if recipe doesn't match, Optional with a set of items used by recipe
|
|
* otherwise
|
|
*/
|
|
public Optional<List<ItemStack>> match(List<ItemStack> stacks) {
|
|
List<Ingredient> inputsMissing = new ArrayList<>(inputs);
|
|
List<ItemStack> stacksToRemove = new ArrayList<>();
|
|
|
|
for (ItemStack stack : stacks) {
|
|
if (stack.isEmpty()) {
|
|
continue;
|
|
}
|
|
if (inputsMissing.isEmpty()) {
|
|
break;
|
|
}
|
|
|
|
int stackIndex = -1;
|
|
|
|
for (int i = 0; i < inputsMissing.size(); i++) {
|
|
Ingredient ingr = inputsMissing.get(i);
|
|
if (ingr.test(stack)) {
|
|
if (!stacksToRemove.contains(stack)) {
|
|
stacksToRemove.add(stack);
|
|
}
|
|
stackIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (stackIndex != -1) {
|
|
inputsMissing.remove(stackIndex);
|
|
}
|
|
}
|
|
|
|
return inputsMissing.isEmpty() ? Optional.of(stacksToRemove) : Optional.empty();
|
|
}
|
|
|
|
/**
|
|
* If the recipe does not contain the item, it will be destroyed upon entering the portal.
|
|
*/
|
|
public boolean containsItem(ItemStack stack) {
|
|
for (Ingredient input : inputs) {
|
|
if (input.test(stack)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean matches(@Nonnull IInventory inv, @Nonnull World world) {
|
|
return false;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public ItemStack getCraftingResult(@Nonnull IInventory inv) {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
|
|
@Override
|
|
public boolean canFit(int width, int height) {
|
|
return false;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public ItemStack getRecipeOutput() {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public ResourceLocation getId() {
|
|
return id;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public IRecipeSerializer<?> getSerializer() {
|
|
return ModRecipeTypes.ELVEN_TRADE_SERIALIZER;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public IRecipeType<?> getType() {
|
|
return ModRecipeTypes.ELVEN_TRADE_TYPE;
|
|
}
|
|
|
|
public List<Ingredient> getInputs() {
|
|
return inputs;
|
|
}
|
|
|
|
public List<ItemStack> getOutputs() {
|
|
return outputs;
|
|
}
|
|
|
|
public List<ItemStack> getOutputs(List<ItemStack> inputs) {
|
|
return getOutputs();
|
|
}
|
|
|
|
public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<RecipeElvenTrade> {
|
|
|
|
@Nonnull
|
|
@Override
|
|
public RecipeElvenTrade read(@Nonnull ResourceLocation id, @Nonnull JsonObject json) {
|
|
JsonElement output = json.get("output");
|
|
List<ItemStack> outputStacks = new ArrayList<>();
|
|
if (output.isJsonArray()) {
|
|
for (JsonElement e : output.getAsJsonArray()) {
|
|
JsonObject o = JSONUtils.getJsonObject(e, "output stack");
|
|
outputStacks.add(CraftingHelper.getItemStack(o, true));
|
|
}
|
|
} else {
|
|
JsonObject o = JSONUtils.getJsonObject(output, "output stack");
|
|
outputStacks.add(CraftingHelper.getItemStack(o, true));
|
|
}
|
|
|
|
List<Ingredient> inputs = new ArrayList<>();
|
|
for (JsonElement e : JSONUtils.getJsonArray(json, "ingredients")) {
|
|
Ingredient ing = Ingredient.deserialize(e);
|
|
if (!ing.hasNoMatchingItems()) {
|
|
inputs.add(ing);
|
|
}
|
|
}
|
|
|
|
return new RecipeElvenTrade(id, outputStacks.toArray(new ItemStack[0]), inputs.toArray(new Ingredient[0]));
|
|
}
|
|
|
|
@Override
|
|
public RecipeElvenTrade read(@Nonnull ResourceLocation id, PacketBuffer buf) {
|
|
Ingredient[] inputs = new Ingredient[buf.readVarInt()];
|
|
for (int i = 0; i < inputs.length; i++) {
|
|
inputs[i] = Ingredient.read(buf);
|
|
}
|
|
ItemStack[] outputs = new ItemStack[buf.readVarInt()];
|
|
for (int i = 0; i < outputs.length; i++) {
|
|
outputs[i] = buf.readItemStack();
|
|
}
|
|
return new RecipeElvenTrade(id, outputs, inputs);
|
|
}
|
|
|
|
@Override
|
|
public void write(PacketBuffer buf, RecipeElvenTrade recipe) {
|
|
buf.writeVarInt(recipe.getInputs().size());
|
|
for (Ingredient input : recipe.getInputs()) {
|
|
input.write(buf);
|
|
}
|
|
buf.writeVarInt(recipe.getOutputs().size());
|
|
for (ItemStack output : recipe.getOutputs()) {
|
|
buf.writeItemStack(output, false);
|
|
}
|
|
}
|
|
}
|
|
}
|