2022-08-03 23:48:00 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.crafting;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
2022-08-17 17:29:31 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
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.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-03 23:48:00 +02:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
2022-08-17 17:29:31 +02:00
|
|
|
import java.util.Optional;
|
2022-08-03 23:48:00 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class ColorChangeRecipe implements Recipe<Container> {
|
2022-08-03 23:48:00 +02:00
|
|
|
public static final String NAME = "color_change";
|
|
|
|
|
|
|
|
private final Ingredient input;
|
|
|
|
private final ItemStack output;
|
|
|
|
private final ResourceLocation id;
|
|
|
|
|
|
|
|
public ColorChangeRecipe(ResourceLocation id, ItemStack output, Ingredient input) {
|
|
|
|
this.input = input;
|
|
|
|
this.output = output;
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public boolean matches(@Nonnull Container pInv, @Nonnull Level pLevel) {
|
2022-08-03 23:48:00 +02:00
|
|
|
return input.test(pInv.getItem(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean matches(ItemStack stack) {
|
|
|
|
return input.test(stack);
|
|
|
|
}
|
|
|
|
|
2023-01-15 22:46:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean isSpecial() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-03 23:48:00 +02:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack assemble(Container pInv, RegistryAccess pRegistryAccess) {
|
2022-08-03 23:48:00 +02:00
|
|
|
return output.copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canCraftInDimensions(int pWidth, int pHeight) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:29:31 +02:00
|
|
|
public static Optional<ColorChangeRecipe> getRecipeForStack(ItemStack stack) {
|
|
|
|
return ActuallyAdditionsAPI.COLOR_CHANGE_RECIPES.stream().filter(recipe -> recipe.matches(stack)).findFirst();
|
|
|
|
}
|
|
|
|
|
2022-08-03 23:48:00 +02:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack getResultItem(RegistryAccess pRegistryAccess) {
|
2022-08-03 23:48:00 +02:00
|
|
|
return output.copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeSerializer<?> getSerializer() {
|
2022-08-03 23:48:00 +02:00
|
|
|
return ActuallyRecipes.COLOR_CHANGE_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeType<?> getType() {
|
2022-08-03 23:48:00 +02:00
|
|
|
return ActuallyRecipes.Types.COLOR_CHANGE;
|
|
|
|
}
|
|
|
|
|
2024-03-03 01:20:53 +01:00
|
|
|
public static class Serializer implements RecipeSerializer<ColorChangeRecipe> {
|
2022-08-03 23:48:00 +02:00
|
|
|
@Override
|
|
|
|
public ColorChangeRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
|
2024-03-02 21:23:08 +01:00
|
|
|
Ingredient ingredient = Ingredient.fromJson(GsonHelper.getAsJsonObject(pJson, "ingredient"));
|
|
|
|
JsonObject resultObject = GsonHelper.getAsJsonObject(pJson, "result");
|
|
|
|
ItemStack result = new ItemStack(GsonHelper.getAsItem(resultObject, "item"));
|
2022-08-03 23:48:00 +02:00
|
|
|
|
|
|
|
return new ColorChangeRecipe(pRecipeId, result, ingredient);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public ColorChangeRecipe fromNetwork(@Nonnull ResourceLocation pRecipeId, @Nonnull FriendlyByteBuf pBuffer) {
|
2022-08-03 23:48:00 +02:00
|
|
|
Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
|
|
|
|
ItemStack result = pBuffer.readItem();
|
|
|
|
return new ColorChangeRecipe(pRecipeId, result, ingredient);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void toNetwork(@Nonnull FriendlyByteBuf pBuffer, ColorChangeRecipe pRecipe) {
|
2022-08-03 23:48:00 +02:00
|
|
|
pRecipe.input.toNetwork(pBuffer);
|
|
|
|
pBuffer.writeItem(pRecipe.output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static class Result implements FinishedRecipe {
|
2022-08-03 23:48:00 +02:00
|
|
|
private final ResourceLocation id;
|
|
|
|
private final Ingredient itemIngredient;
|
2024-03-02 21:23:08 +01:00
|
|
|
private final ItemLike output;
|
2022-08-03 23:48:00 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public Result(ResourceLocation id, Ingredient itemIngredient, ItemLike output) {
|
2022-08-03 23:48:00 +02:00
|
|
|
this.id = id;
|
|
|
|
this.itemIngredient = itemIngredient;
|
|
|
|
this.output = output;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serializeRecipeData(JsonObject pJson) {
|
|
|
|
pJson.add("ingredient", itemIngredient.toJson());
|
|
|
|
|
|
|
|
JsonObject resultObject = new JsonObject();
|
2024-03-03 01:20:53 +01:00
|
|
|
resultObject.addProperty("item", ForgeRegistries.ITEMS.getKey(output.asItem()).toString());
|
2022-08-03 23:48:00 +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-03 23:48:00 +02:00
|
|
|
return ActuallyRecipes.COLOR_CHANGE_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public JsonObject serializeAdvancement() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getAdvancementId() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|