2021-10-13 05:08:31 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.crafting;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
2022-01-19 02:11:23 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2021-10-13 05:08:31 +02:00
|
|
|
import net.minecraft.data.IFinishedRecipe;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2021-11-13 17:30:53 +01:00
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
|
|
import net.minecraft.item.crafting.IRecipeSerializer;
|
|
|
|
import net.minecraft.item.crafting.IRecipeType;
|
|
|
|
import net.minecraft.item.crafting.Ingredient;
|
2021-10-13 05:08:31 +02:00
|
|
|
import net.minecraft.network.PacketBuffer;
|
2021-10-13 22:42:48 +02:00
|
|
|
import net.minecraft.util.IItemProvider;
|
2021-10-13 05:08:31 +02:00
|
|
|
import net.minecraft.util.JSONUtils;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.registries.ForgeRegistryEntry;
|
|
|
|
|
2022-08-03 23:48:00 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2021-10-13 05:08:31 +02:00
|
|
|
import javax.annotation.Nullable;
|
2022-01-19 02:11:23 +01:00
|
|
|
import java.util.Optional;
|
2021-10-13 05:08:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
public class LaserRecipe implements IRecipe<IInventory> {
|
|
|
|
public static String NAME = "laser";
|
|
|
|
private ItemStack result;
|
|
|
|
private Ingredient itemIngredient;
|
|
|
|
private int energy;
|
|
|
|
private ResourceLocation id;
|
|
|
|
|
|
|
|
public LaserRecipe(ResourceLocation id, ItemStack result, Ingredient itemIngredient, int energy) {
|
|
|
|
this.result = result;
|
|
|
|
this.itemIngredient = itemIngredient;
|
|
|
|
this.energy = energy;
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getEnergy() {
|
|
|
|
return energy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean matches(ItemStack itemStack, int energyIn) {
|
|
|
|
return itemIngredient.test(itemStack) && (energyIn >= energy);
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:11:23 +01:00
|
|
|
public boolean matches(ItemStack itemStack) {
|
|
|
|
return itemIngredient.test(itemStack);
|
|
|
|
}
|
|
|
|
|
2022-12-31 01:24:21 +01:00
|
|
|
public Ingredient getInput() {
|
|
|
|
return itemIngredient;
|
|
|
|
}
|
|
|
|
|
2021-10-13 05:08:31 +02:00
|
|
|
//nah
|
|
|
|
@Override
|
|
|
|
public boolean matches(IInventory pInv, World pLevel) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-15 22:46:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean isSpecial() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-13 05:08:31 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack assemble(IInventory pInv) {
|
2021-10-14 01:54:14 +02:00
|
|
|
return result.copy();
|
2021-10-13 05:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canCraftInDimensions(int pWidth, int pHeight) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getResultItem() {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IRecipeSerializer<?> getSerializer() {
|
|
|
|
return ActuallyRecipes.LASER_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IRecipeType<?> getType() {
|
|
|
|
return ActuallyRecipes.Types.LASER;
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:11:23 +01:00
|
|
|
public static Optional<LaserRecipe> getRecipeForStack(ItemStack stack) {
|
|
|
|
return ActuallyAdditionsAPI.CONVERSION_LASER_RECIPES.stream().filter(recipe -> recipe.matches(stack)).findFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean validInput(ItemStack stack) {
|
|
|
|
return getRecipeForStack(stack).isPresent();
|
|
|
|
}
|
|
|
|
|
2021-10-13 05:08:31 +02:00
|
|
|
public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<LaserRecipe> {
|
|
|
|
@Override
|
2022-08-03 23:48:00 +02:00
|
|
|
public LaserRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
|
2021-10-13 05:08:31 +02:00
|
|
|
Ingredient ingredient = Ingredient.fromJson(JSONUtils.getAsJsonObject(pJson, "ingredient"));
|
|
|
|
int energy = JSONUtils.getAsInt(pJson, "energy");
|
2021-11-13 17:30:53 +01:00
|
|
|
JsonObject resultObject = JSONUtils.getAsJsonObject(pJson, "result");
|
|
|
|
ItemStack result = new ItemStack(JSONUtils.getAsItem(resultObject, "item"));
|
2021-10-13 05:08:31 +02:00
|
|
|
|
|
|
|
return new LaserRecipe(pRecipeId, result, ingredient, energy);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2022-08-03 23:48:00 +02:00
|
|
|
public LaserRecipe fromNetwork(@Nonnull ResourceLocation pRecipeId, @Nonnull PacketBuffer pBuffer) {
|
2021-10-13 05:08:31 +02:00
|
|
|
Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
|
|
|
|
int energy = pBuffer.readInt();
|
|
|
|
ItemStack result = pBuffer.readItem();
|
|
|
|
return new LaserRecipe(pRecipeId, result, ingredient, energy);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-08-03 23:48:00 +02:00
|
|
|
public void toNetwork(@Nonnull PacketBuffer pBuffer, LaserRecipe pRecipe) {
|
2021-10-13 05:08:31 +02:00
|
|
|
pRecipe.itemIngredient.toNetwork(pBuffer);
|
|
|
|
pBuffer.writeInt(pRecipe.energy);
|
|
|
|
pBuffer.writeItem(pRecipe.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class FinishedRecipe implements IFinishedRecipe {
|
|
|
|
private ResourceLocation id;
|
|
|
|
private Ingredient itemIngredient;
|
|
|
|
private int energy;
|
2021-10-13 22:42:48 +02:00
|
|
|
private IItemProvider output;
|
2021-10-13 05:08:31 +02:00
|
|
|
|
2021-10-13 22:42:48 +02:00
|
|
|
public FinishedRecipe(ResourceLocation id, Ingredient itemIngredient, int energy, IItemProvider output) {
|
2021-10-13 05:08:31 +02:00
|
|
|
this.id = id;
|
|
|
|
this.itemIngredient = itemIngredient;
|
|
|
|
this.energy = energy;
|
|
|
|
this.output = output;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serializeRecipeData(JsonObject pJson) {
|
2021-11-13 17:30:53 +01:00
|
|
|
pJson.add("ingredient", itemIngredient.toJson());
|
2021-10-13 05:08:31 +02:00
|
|
|
pJson.addProperty("energy", energy);
|
2021-11-13 17:30:53 +01:00
|
|
|
|
|
|
|
JsonObject resultObject = new JsonObject();
|
|
|
|
resultObject.addProperty("item", output.asItem().getRegistryName().toString());
|
|
|
|
|
|
|
|
pJson.add("result", resultObject);
|
2021-10-13 05:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IRecipeSerializer<?> getType() {
|
|
|
|
return ActuallyRecipes.LASER_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public JsonObject serializeAdvancement() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getAdvancementId() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|