2022-08-03 23:48:00 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.crafting;
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
import com.mojang.serialization.Codec;
|
|
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
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.network.FriendlyByteBuf;
|
|
|
|
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;
|
2024-03-04 20:21:48 +01:00
|
|
|
import net.minecraft.world.item.crafting.RecipeHolder;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.world.item.crafting.RecipeSerializer;
|
|
|
|
import net.minecraft.world.item.crafting.RecipeType;
|
|
|
|
import net.minecraft.world.level.Level;
|
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;
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public ColorChangeRecipe(ItemStack output, Ingredient input) {
|
2022-08-03 23:48:00 +02:00
|
|
|
this.input = input;
|
|
|
|
this.output = output;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public static Optional<RecipeHolder<ColorChangeRecipe>> getRecipeForStack(ItemStack stack) {
|
|
|
|
return ActuallyAdditionsAPI.COLOR_CHANGE_RECIPES.stream().filter(recipe -> recipe.value().matches(stack)).findFirst();
|
2022-08-17 17:29:31 +02:00
|
|
|
}
|
|
|
|
|
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
|
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() {
|
2024-03-03 17:48:28 +01:00
|
|
|
return ActuallyRecipes.Types.COLOR_CHANGE.get();
|
2022-08-03 23:48:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-03 01:20:53 +01:00
|
|
|
public static class Serializer implements RecipeSerializer<ColorChangeRecipe> {
|
2024-03-04 20:21:48 +01:00
|
|
|
private static final Codec<ColorChangeRecipe> CODEC = RecordCodecBuilder.create(
|
|
|
|
instance -> instance.group(
|
|
|
|
ItemStack.RESULT_CODEC.fieldOf("result").forGetter(recipe -> recipe.output),
|
|
|
|
Ingredient.CODEC_NONEMPTY.fieldOf("ingredient").forGetter(recipe -> recipe.input)
|
|
|
|
)
|
|
|
|
.apply(instance, ColorChangeRecipe::new)
|
|
|
|
);
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
// public ColorChangeRecipe fromJson(@Nonnull ResourceLocation pRecipeId, @Nonnull JsonObject pJson) {
|
|
|
|
// Ingredient ingredient = Ingredient.fromJson(GsonHelper.getAsJsonObject(pJson, "ingredient"));
|
|
|
|
// JsonObject resultObject = GsonHelper.getAsJsonObject(pJson, "result");
|
|
|
|
// ItemStack result = new ItemStack(GsonHelper.getAsItem(resultObject, "item"));
|
|
|
|
//
|
|
|
|
// return new ColorChangeRecipe(pRecipeId, result, ingredient);
|
|
|
|
// }
|
2022-08-03 23:48:00 +02:00
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
@Override
|
|
|
|
public Codec<ColorChangeRecipe> codec() {
|
|
|
|
return CODEC;
|
2022-08-03 23:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-04 20:21:48 +01:00
|
|
|
public ColorChangeRecipe fromNetwork(@Nonnull FriendlyByteBuf pBuffer) {
|
2022-08-03 23:48:00 +02:00
|
|
|
Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
|
|
|
|
ItemStack result = pBuffer.readItem();
|
2024-03-04 20:21:48 +01:00
|
|
|
return new ColorChangeRecipe(result, ingredient);
|
2022-08-03 23:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|