ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/crafting/MiningLensRecipe.java

170 lines
5.1 KiB
Java
Raw Normal View History

2022-08-31 00:20:32 +02:00
package de.ellpeck.actuallyadditions.mod.crafting;
import com.google.gson.JsonObject;
2024-03-03 01:20:53 +01:00
import net.minecraft.core.RegistryAccess;
2024-03-02 21:23:08 +01:00
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.random.Weight;
import net.minecraft.util.random.WeightedEntry;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
2024-03-03 01:20:53 +01:00
import net.minecraftforge.registries.ForgeRegistries;
2022-08-31 00:20:32 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2024-03-02 21:23:08 +01:00
public class MiningLensRecipe implements Recipe<Container>, WeightedEntry {
2022-08-31 00:20:32 +02:00
public static final String NAME = "mining_lens";
private final ResourceLocation id;
2024-03-02 21:23:08 +01:00
private final int weight;
2022-08-31 00:20:32 +02:00
private final Ingredient input;
2022-08-31 19:07:18 +02:00
//private final int weight;
2022-08-31 00:20:32 +02:00
private final ItemStack output;
public MiningLensRecipe(ResourceLocation id, Ingredient input, int weight, ItemStack output) {
2024-03-02 21:23:08 +01:00
super();
this.weight = weight;
2022-08-31 00:20:32 +02:00
this.input = input;
2022-08-31 19:07:18 +02:00
//this.weight = weight;
2022-08-31 00:20:32 +02:00
this.output = output;
this.id = id;
}
2024-03-02 21:23:08 +01:00
public Weight getWeight() {
return Weight.of(weight);
2022-08-31 00:20:32 +02:00
}
public Ingredient getInput() {
return input;
}
public boolean matches(ItemStack test) {
return input.test(test);
}
@Override
2024-03-02 21:23:08 +01:00
public boolean matches(Container pInv, Level pLevel) {
2022-08-31 00:20:32 +02:00
return false;
}
@Override
public boolean isSpecial() {
return true;
}
2022-08-31 00:20:32 +02:00
@Nonnull
@Override
2024-03-03 01:20:53 +01:00
public ItemStack assemble(Container pInv, RegistryAccess pRegistryAccess) {
2022-08-31 00:20:32 +02:00
return ItemStack.EMPTY;
}
@Override
public boolean canCraftInDimensions(int pWidth, int pHeight) {
return false;
}
@Override
2024-03-03 01:20:53 +01:00
public ItemStack getResultItem(RegistryAccess pRegistryAccess) {
2022-08-31 19:07:18 +02:00
return output;
2022-08-31 00:20:32 +02:00
}
@Override
public ResourceLocation getId() {
return id;
}
@Override
2024-03-02 21:23:08 +01:00
public RecipeSerializer<?> getSerializer() {
2022-08-31 00:20:32 +02:00
return ActuallyRecipes.MINING_LENS_RECIPE.get();
}
@Override
2024-03-02 21:23:08 +01:00
public RecipeType<?> getType() {
2022-08-31 00:20:32 +02:00
return ActuallyRecipes.Types.MINING_LENS;
}
2024-03-03 01:20:53 +01:00
public static class Serializer implements RecipeSerializer<MiningLensRecipe> {
2022-08-31 00:20:32 +02:00
@Override
public MiningLensRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
2024-03-02 21:23:08 +01:00
Ingredient ingredient = Ingredient.fromJson(GsonHelper.getAsJsonObject(pJson, "ingredient"));
int weight = GsonHelper.getAsInt(pJson, "weight");
JsonObject resultObject = GsonHelper.getAsJsonObject(pJson, "result");
ItemStack result = new ItemStack(GsonHelper.getAsItem(resultObject, "item"));
2022-08-31 00:20:32 +02:00
return new MiningLensRecipe(pRecipeId, ingredient, weight, result);
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public MiningLensRecipe fromNetwork(@Nonnull ResourceLocation pRecipeId, @Nonnull FriendlyByteBuf pBuffer) {
2022-08-31 00:20:32 +02:00
Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
int weight = pBuffer.readInt();
ItemStack result = pBuffer.readItem();
return new MiningLensRecipe(pRecipeId, ingredient, weight, result);
}
@Override
2024-03-02 21:23:08 +01:00
public void toNetwork(@Nonnull FriendlyByteBuf pBuffer, MiningLensRecipe pRecipe) {
2022-08-31 00:20:32 +02:00
pRecipe.input.toNetwork(pBuffer);
pBuffer.writeInt(pRecipe.weight);
pBuffer.writeItem(pRecipe.output);
}
}
2024-03-02 21:23:08 +01:00
public static class Result implements FinishedRecipe {
2022-08-31 00:20:32 +02:00
private final ResourceLocation id;
private final Ingredient itemIngredient;
private final int weight;
2024-03-02 21:23:08 +01:00
private final ItemLike output;
2022-08-31 00:20:32 +02:00
2024-03-02 21:23:08 +01:00
public Result(ResourceLocation id, Ingredient itemIngredient, int weight, ItemLike output) {
2022-08-31 00:20:32 +02:00
this.id = id;
this.itemIngredient = itemIngredient;
this.weight = weight;
this.output = output;
}
@Override
public void serializeRecipeData(JsonObject pJson) {
pJson.add("ingredient", itemIngredient.toJson());
pJson.addProperty("weight", weight);
JsonObject resultObject = new JsonObject();
2024-03-03 01:20:53 +01:00
resultObject.addProperty("item", ForgeRegistries.ITEMS.getKey(output.asItem()).toString());
2022-08-31 00:20:32 +02:00
pJson.add("result", resultObject);
}
@Override
public ResourceLocation getId() {
return id;
}
@Override
2024-03-02 21:23:08 +01:00
public RecipeSerializer<?> getType() {
2022-08-31 00:20:32 +02:00
return ActuallyRecipes.MINING_LENS_RECIPE.get();
}
@Nullable
@Override
public JsonObject serializeAdvancement() {
return null;
}
@Nullable
@Override
public ResourceLocation getAdvancementId() {
return null;
}
}
}