Fixed some Recipe related issues

This commit is contained in:
Frank 2023-12-19 15:06:48 +01:00
parent 9a758565d7
commit 951a6d110c
8 changed files with 170 additions and 144 deletions

View file

@ -14,6 +14,7 @@ import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.inventory.*; import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
@ -89,8 +90,8 @@ public class EndStoneSmelterMenu extends RecipeBookMenu<Container> {
} }
@Override @Override
public boolean recipeMatches(Recipe<? super Container> recipe) { public boolean recipeMatches(RecipeHolder<? extends Recipe<Container>> recipeHolder) {
return recipe.matches(inventory, world); return recipeHolder.value().matches(inventory, world);
} }
@Override @Override

View file

@ -10,6 +10,7 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
@ -17,6 +18,7 @@ import net.fabricmc.api.Environment;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.jetbrains.annotations.NotNull;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class EndStoneSmelterRecipeBookScreen extends BlastingRecipeBookComponent { public class EndStoneSmelterRecipeBookScreen extends BlastingRecipeBookComponent {
@ -24,7 +26,7 @@ public class EndStoneSmelterRecipeBookScreen extends BlastingRecipeBookComponent
private Slot fuelSlot; private Slot fuelSlot;
@Override @Override
protected Set<Item> getFuelItems() { protected @NotNull Set<Item> getFuelItems() {
return EndStoneSmelterBlockEntity.availableFuels().keySet(); return EndStoneSmelterBlockEntity.availableFuels().keySet();
} }
@ -37,10 +39,12 @@ public class EndStoneSmelterRecipeBookScreen extends BlastingRecipeBookComponent
} }
@Override @Override
public void setupGhostRecipe(Recipe<?> recipe, List<Slot> slots) { public void setupGhostRecipe(RecipeHolder<?> recipeHolder, List<Slot> slots) {
if (Minecraft.getInstance().level == null) return;
this.ghostRecipe.clear(); this.ghostRecipe.clear();
final Recipe<?> recipe = recipeHolder.value();
ItemStack result = recipe.getResultItem(Minecraft.getInstance().level.registryAccess()); ItemStack result = recipe.getResultItem(Minecraft.getInstance().level.registryAccess());
this.ghostRecipe.setRecipe(recipe); this.ghostRecipe.setRecipe(recipeHolder);
this.ghostRecipe.addIngredient(Ingredient.of(result), (slots.get(3)).x, (slots.get(3)).y); this.ghostRecipe.addIngredient(Ingredient.of(result), (slots.get(3)).x, (slots.get(3)).y);
NonNullList<Ingredient> inputs = recipe.getIngredients(); NonNullList<Ingredient> inputs = recipe.getIngredients();

View file

@ -8,7 +8,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeHolder;
import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay; import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay;
@ -25,21 +25,27 @@ import org.jetbrains.annotations.NotNull;
public class REIAlloyingDisplay extends BasicDisplay implements SimpleGridMenuDisplay { public class REIAlloyingDisplay extends BasicDisplay implements SimpleGridMenuDisplay {
private static final List<EntryStack> fuel; private static final List<EntryStack<?>> fuel;
private final Recipe<?> recipe; private final RecipeHolder<?> recipe;
private final float xp; private final float xp;
private final double smeltTime; private final double smeltTime;
public REIAlloyingDisplay(AlloyingRecipe recipe) { public REIAlloyingDisplay(RecipeHolder<AlloyingRecipe> recipe) {
this(recipe, recipe.getExperience(), recipe.getSmeltTime()); this(recipe, recipe.value().getExperience(), recipe.value().getSmeltTime());
} }
protected REIAlloyingDisplay(Recipe<?> recipe, float xp, double smeltTime) { protected REIAlloyingDisplay(RecipeHolder<?> recipe, float xp, double smeltTime) {
super( super(
EntryIngredients.ofIngredients(recipe.getIngredients()), EntryIngredients.ofIngredients(recipe.value().getIngredients()),
Collections.singletonList(EntryIngredients.of(recipe.getResultItem(Minecraft.getInstance().level.registryAccess()))) Collections.singletonList(
EntryIngredients.of(
recipe
.value()
.getResultItem(Minecraft.getInstance().level.registryAccess())
)
)
); );
this.recipe = recipe; this.recipe = recipe;
this.xp = xp; this.xp = xp;
@ -47,13 +53,13 @@ public class REIAlloyingDisplay extends BasicDisplay implements SimpleGridMenuDi
} }
public static List<EntryStack> getFuel() { public static List<EntryStack<?>> getFuel() {
return fuel; return fuel;
} }
@Override @Override
public @NotNull Optional<ResourceLocation> getDisplayLocation() { public @NotNull Optional<ResourceLocation> getDisplayLocation() {
return Optional.ofNullable(recipe).map(Recipe::getId); return Optional.ofNullable(recipe).map(RecipeHolder::id);
} }
@Override @Override
@ -74,10 +80,6 @@ public class REIAlloyingDisplay extends BasicDisplay implements SimpleGridMenuDi
return this.smeltTime; return this.smeltTime;
} }
public Optional<Recipe<?>> getOptionalRecipe() {
return Optional.ofNullable(recipe);
}
@Override @Override
public int getWidth() { public int getWidth() {
return 2; return 2;

View file

@ -5,7 +5,7 @@ import org.betterx.bclib.recipes.AnvilRecipe;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeHolder;
import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay; import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay;
@ -18,33 +18,37 @@ import org.jetbrains.annotations.NotNull;
public class REIAnvilDisplay extends BasicDisplay implements SimpleGridMenuDisplay { public class REIAnvilDisplay extends BasicDisplay implements SimpleGridMenuDisplay {
private final AnvilRecipe recipe; private final RecipeHolder<AnvilRecipe> recipe;
public REIAnvilDisplay(AnvilRecipe recipe) { public REIAnvilDisplay(RecipeHolder<AnvilRecipe> recipe) {
super( super(
EntryIngredients.ofIngredients(recipe.getIngredients()), EntryIngredients.ofIngredients(recipe.value().getIngredients()),
Collections.singletonList(EntryIngredients.of(recipe.getResultItem(Minecraft.getInstance().level.registryAccess()))) Collections.singletonList(
EntryIngredients.of(
recipe.value().getResultItem(Minecraft.getInstance().level.registryAccess())
)
)
); );
this.recipe = recipe; this.recipe = recipe;
inputs.get(1).forEach(entryStack -> { inputs.get(1).forEach(entryStack -> {
if (entryStack.getValue() instanceof ItemStack itemStack) { if (entryStack.getValue() instanceof ItemStack itemStack) {
itemStack.setCount(recipe.getInputCount()); itemStack.setCount(recipe.value().getInputCount());
} }
}); });
} }
public int getDamage() { public int getDamage() {
return recipe.getDamage(); return recipe.value().getDamage();
} }
public int getAnvilLevel() { public int getAnvilLevel() {
return recipe.getAnvilLevel(); return recipe.value().getAnvilLevel();
} }
@Override @Override
public @NotNull Optional<ResourceLocation> getDisplayLocation() { public @NotNull Optional<ResourceLocation> getDisplayLocation() {
return Optional.ofNullable(recipe).map(Recipe::getId); return Optional.ofNullable(recipe).map(RecipeHolder::id);
} }
@Override @Override

View file

@ -1,9 +1,10 @@
package org.betterx.betterend.integration.rei; package org.betterx.betterend.integration.rei;
import net.minecraft.world.item.crafting.BlastingRecipe; import net.minecraft.world.item.crafting.BlastingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder;
public class REIBlastingDisplay extends REIAlloyingDisplay { public class REIBlastingDisplay extends REIAlloyingDisplay {
public REIBlastingDisplay(BlastingRecipe recipe) { public REIBlastingDisplay(RecipeHolder<BlastingRecipe> recipe) {
super(recipe, recipe.getExperience(), recipe.getCookingTime()); super(recipe, recipe.value().getExperience(), recipe.value().getCookingTime());
} }
} }

View file

@ -4,7 +4,7 @@ import org.betterx.betterend.recipe.builders.InfusionRecipe;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeHolder;
import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay; import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay;
@ -17,16 +17,20 @@ import org.jetbrains.annotations.NotNull;
public class REIInfusionDisplay extends BasicDisplay implements SimpleGridMenuDisplay { public class REIInfusionDisplay extends BasicDisplay implements SimpleGridMenuDisplay {
private final InfusionRecipe recipe; private final RecipeHolder<InfusionRecipe> recipe;
private final int time; private final int time;
public REIInfusionDisplay(InfusionRecipe recipe) { public REIInfusionDisplay(RecipeHolder<InfusionRecipe> recipe) {
super( super(
EntryIngredients.ofIngredients(recipe.getIngredients()), EntryIngredients.ofIngredients(recipe.value().getIngredients()),
Collections.singletonList(EntryIngredients.of(recipe.getResultItem(Minecraft.getInstance().level.registryAccess()))) Collections.singletonList(
EntryIngredients.of(
recipe.value().getResultItem(Minecraft.getInstance().level.registryAccess())
)
)
); );
this.recipe = recipe; this.recipe = recipe;
this.time = recipe.getInfusionTime(); this.time = recipe.value().getInfusionTime();
} }
public int getInfusionTime() { public int getInfusionTime() {
@ -35,7 +39,7 @@ public class REIInfusionDisplay extends BasicDisplay implements SimpleGridMenuDi
@Override @Override
public @NotNull Optional<ResourceLocation> getDisplayLocation() { public @NotNull Optional<ResourceLocation> getDisplayLocation() {
return Optional.ofNullable(recipe).map(Recipe::getId); return Optional.ofNullable(recipe).map(RecipeHolder::id);
} }
@Override @Override

View file

@ -8,12 +8,14 @@ import org.betterx.bclib.util.ItemUtil;
import org.betterx.betterend.BetterEnd; import org.betterx.betterend.BetterEnd;
import org.betterx.betterend.rituals.InfusionRitual; import org.betterx.betterend.rituals.InfusionRitual;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.NonNullList; import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.util.GsonHelper; import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.EnchantedBookItem; import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -30,12 +32,35 @@ import net.minecraft.world.level.Level;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBookCategory { public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBookCategory {
public static final Codec<Ingredient[]> CODEC_CATALYSTS = RecordCodecBuilder.create(instance -> instance
.group(
Builder.catalyst(Catalysts.NORTH),
Builder.catalyst(Catalysts.NORTH_EAST),
Builder.catalyst(Catalysts.EAST),
Builder.catalyst(Catalysts.SOUTH_EAST),
Builder.catalyst(Catalysts.SOUTH),
Builder.catalyst(Catalysts.SOUTH_WEST),
Builder.catalyst(Catalysts.WEST),
Builder.catalyst(Catalysts.NORTH_WEST)
).apply(instance, Builder::fromCodec)
);
public static final Codec<InfusionRecipe> CODEC = RecordCodecBuilder.create(instance -> instance
.group(
ItemUtil.CODEC_INGREDIENT_WITH_NBT.fieldOf("input").forGetter(i -> i.input),
ItemUtil.CODEC_ITEM_STACK_WITH_NBT.fieldOf("result").forGetter(i -> i.output),
Codec.INT.optionalFieldOf("time", 1).forGetter(InfusionRecipe::getInfusionTime),
CODEC_CATALYSTS.fieldOf("catalysts").forGetter(i -> i.catalysts),
ExtraCodecs.strictOptionalField(Codec.STRING, "group")
.forGetter(i -> Optional.ofNullable(i.group))
).apply(instance, InfusionRecipe::new)
);
public final static String GROUP = "infusion"; public final static String GROUP = "infusion";
public final static RecipeType<InfusionRecipe> TYPE = BCLRecipeManager.registerType(BetterEnd.MOD_ID, GROUP); public final static RecipeType<InfusionRecipe> TYPE = BCLRecipeManager.registerType(BetterEnd.MOD_ID, GROUP);
public final static Serializer SERIALIZER = BCLRecipeManager.registerSerializer( public final static Serializer SERIALIZER = BCLRecipeManager.registerSerializer(
@ -44,25 +69,39 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
new Serializer() new Serializer()
); );
private final ResourceLocation id;
private final Ingredient[] catalysts; private final Ingredient[] catalysts;
private Ingredient input; private Ingredient input;
private ItemStack output; private ItemStack output;
private int time = 1; private int time;
private String group; private String group;
private InfusionRecipe(ResourceLocation id) { private InfusionRecipe() {
this(id, null, null); this(null, null);
} }
private InfusionRecipe(ResourceLocation id, Ingredient input, ItemStack output) { private InfusionRecipe(Ingredient input, ItemStack output) {
this.id = id; this(input, output, 1, new Ingredient[]{
this.input = input;
this.output = output;
this.catalysts = new Ingredient[]{
Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY,
Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY, Ingredient.EMPTY
}; }, (String) null);
}
private InfusionRecipe(
Ingredient input,
ItemStack output,
int time,
Ingredient[] catalysts,
Optional<String> group
) {
this(input, output, time, catalysts, group.orElse(null));
}
private InfusionRecipe(Ingredient input, ItemStack output, int time, Ingredient[] catalysts, String group) {
this.input = input;
this.output = output;
this.catalysts = catalysts;
this.time = time;
this.group = group;
} }
public static Builder create(String id, ItemLike output) { public static Builder create(String id, ItemLike output) {
@ -110,7 +149,7 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
} }
@Override @Override
public ItemStack assemble(InfusionRitual ritual, RegistryAccess acc) { public @NotNull ItemStack assemble(InfusionRitual ritual, RegistryAccess acc) {
return output.copy(); return output.copy();
} }
@ -120,7 +159,7 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
} }
@Override @Override
public NonNullList<Ingredient> getIngredients() { public @NotNull NonNullList<Ingredient> getIngredients() {
NonNullList<Ingredient> defaultedList = NonNullList.create(); NonNullList<Ingredient> defaultedList = NonNullList.create();
defaultedList.add(input); defaultedList.add(input);
defaultedList.addAll(Arrays.asList(catalysts)); defaultedList.addAll(Arrays.asList(catalysts));
@ -128,32 +167,60 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
} }
@Override @Override
public ItemStack getResultItem(RegistryAccess acc) { public @NotNull ItemStack getResultItem(RegistryAccess acc) {
return this.output; return this.output;
} }
@Override
public ResourceLocation getId() {
return this.id;
}
@Override @Override
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public String getGroup() { public @NotNull String getGroup() {
return this.group; return this.group;
} }
@Override @Override
public RecipeSerializer<?> getSerializer() { public @NotNull RecipeSerializer<?> getSerializer() {
return SERIALIZER; return SERIALIZER;
} }
@Override @Override
public RecipeType<?> getType() { public @NotNull RecipeType<?> getType() {
return TYPE; return TYPE;
} }
public static class Builder extends AbstractSingleInputRecipeBuilder<Builder, InfusionRecipe> { public static class Builder extends AbstractSingleInputRecipeBuilder<Builder, InfusionRecipe> {
@NotNull
private static RecordCodecBuilder<Ingredient[], Optional<Ingredient>> catalyst(Catalysts slot) {
return ExtraCodecs.strictOptionalField(ItemUtil.CODEC_INGREDIENT_WITH_NBT, slot.key)
.forGetter(builder ->
builder[slot.index] == null || builder[slot.index].isEmpty()
? Optional.empty()
: Optional.of(builder[slot.index])
);
}
private static Ingredient[] fromCodec(
Optional<Ingredient> north,
Optional<Ingredient> north_east,
Optional<Ingredient> east,
Optional<Ingredient> south_east,
Optional<Ingredient> south,
Optional<Ingredient> south_west,
Optional<Ingredient> west,
Optional<Ingredient> north_west
) {
Ingredient[] result = new Ingredient[8];
result[Catalysts.NORTH.index] = north.orElse(null);
result[Catalysts.NORTH_EAST.index] = north_east.orElse(null);
result[Catalysts.EAST.index] = east.orElse(null);
result[Catalysts.SOUTH_EAST.index] = south_east.orElse(null);
result[Catalysts.SOUTH.index] = south.orElse(null);
result[Catalysts.SOUTH_WEST.index] = south_west.orElse(null);
result[Catalysts.WEST.index] = west.orElse(null);
result[Catalysts.NORTH_WEST.index] = north_west.orElse(null);
return result;
}
private final Ingredient[] catalysts; private final Ingredient[] catalysts;
private int time; private int time;
@ -170,14 +237,20 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
this.time = 1; this.time = 1;
} }
@Override
protected InfusionRecipe createRecipe(ResourceLocation id) {
checkRecipe();
return new InfusionRecipe(this.primaryInput, this.output, this.time, this.catalysts, this.group);
}
@Override @Override
public Builder setGroup(String group) { public Builder setGroup(String group) {
return super.setGroup(group); return super.setGroup(group);
} }
/** /**
* @param input * @param input -
* @return * @return -
* @deprecated use {@link #setPrimaryInput(ItemLike...)} * @deprecated use {@link #setPrimaryInput(ItemLike...)}
*/ */
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
@ -218,23 +291,6 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
return super.checkRecipe(); return super.checkRecipe();
} }
@Override
protected void serializeRecipeData(JsonObject root) {
super.serializeRecipeData(root);
if (time != 1) {
root.addProperty("time", time);
}
JsonObject catalystObject = new JsonObject();
for (var cat : Catalysts.values()) {
if (catalysts[cat.index] != null && !catalysts[cat.index].isEmpty()) {
catalystObject.add(cat.key, ItemUtil.toJsonIngredientWithNBT(catalysts[cat.index]));
}
}
root.add("catalysts", catalystObject);
}
@Override @Override
protected RecipeSerializer<InfusionRecipe> getSerializer() { protected RecipeSerializer<InfusionRecipe> getSerializer() {
return SERIALIZER; return SERIALIZER;
@ -261,64 +317,14 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, UnknownReceipBook
} }
public static class Serializer implements RecipeSerializer<InfusionRecipe> { public static class Serializer implements RecipeSerializer<InfusionRecipe> {
private Ingredient readIngredient(JsonObject obj, String key) { @Override
if (obj.has(key)) { public @NotNull Codec<InfusionRecipe> codec() {
JsonElement el = obj.get(key); return InfusionRecipe.CODEC;
if (el.isJsonObject()) {
JsonObject o = el.getAsJsonObject();
//directly read as ingredient
if (o.has("tag")) {
final Ingredient res = ItemUtil.fromJsonIngredientWithNBT(o);
if (res == null) return Ingredient.EMPTY;
return res;
} else {
final Ingredient res = Ingredient.of(ItemUtil.fromJsonRecipeWithNBT(o));
if (res == null) return Ingredient.EMPTY;
return res;
}
} else if (el.isJsonArray()) {
//this is an Ingredient-Array, so read as such
final Ingredient res = Ingredient.fromJson(el);
if (res == null) return Ingredient.EMPTY;
return res;
} else if (obj.isJsonPrimitive()) {
String s = GsonHelper.getAsString(obj, key, "");
ItemStack catalyst = ItemUtil.fromStackString(s);
return (catalyst != null && !catalyst.isEmpty())
? Ingredient.of(catalyst.getItem())
: Ingredient.EMPTY;
} else {
throw new IllegalStateException("Invalid catalyst ingredient for " + key + ": " + el.toString());
}
}
return Ingredient.EMPTY;
} }
@Override @Override
public InfusionRecipe fromJson(ResourceLocation id, JsonObject json) { public @NotNull InfusionRecipe fromNetwork(FriendlyByteBuf buffer) {
InfusionRecipe recipe = new InfusionRecipe(id); InfusionRecipe recipe = new InfusionRecipe();
JsonObject inputObject = GsonHelper.getAsJsonObject(json, "input");
recipe.input = ItemUtil.fromJsonIngredientWithNBT(inputObject);
JsonObject result = GsonHelper.getAsJsonObject(json, "result");
recipe.output = ItemUtil.fromJsonRecipeWithNBT(result);
if (recipe.output == null) {
throw new IllegalStateException("Output item does not exists!");
}
recipe.group = GsonHelper.getAsString(json, "group", GROUP);
recipe.time = GsonHelper.getAsInt(json, "time", 1);
JsonObject catalysts = GsonHelper.getAsJsonObject(json, "catalysts");
for (var cat : Catalysts.values()) {
recipe.catalysts[cat.index] = readIngredient(catalysts, cat.key);
}
return recipe;
}
@Override
public InfusionRecipe fromNetwork(ResourceLocation id, FriendlyByteBuf buffer) {
InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromNetwork(buffer); recipe.input = Ingredient.fromNetwork(buffer);
recipe.output = buffer.readItem(); recipe.output = buffer.readItem();
recipe.group = buffer.readUtf(); recipe.group = buffer.readUtf();

View file

@ -14,6 +14,7 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container; import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.LevelChunk.EntityCreationType; import net.minecraft.world.level.chunk.LevelChunk.EntityCreationType;
@ -22,6 +23,7 @@ import net.minecraft.world.phys.Vec3;
import java.awt.*; import java.awt.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import org.jetbrains.annotations.NotNull;
public class InfusionRitual implements Container { public class InfusionRitual implements Container {
private static final Point[] PEDESTALS_MAP = new Point[]{ private static final Point[] PEDESTALS_MAP = new Point[]{
@ -37,7 +39,7 @@ public class InfusionRitual implements Container {
private Level world; private Level world;
private BlockPos worldPos; private BlockPos worldPos;
private InfusionRecipe activeRecipe; private RecipeHolder<InfusionRecipe> activeRecipe;
private boolean isDirty = false; private boolean isDirty = false;
private boolean hasRecipe = false; private boolean hasRecipe = false;
private int progress = 0; private int progress = 0;
@ -70,12 +72,14 @@ public class InfusionRitual implements Container {
public boolean checkRecipe() { public boolean checkRecipe() {
if (!isValid()) return false; if (!isValid()) return false;
InfusionRecipe recipe = world.getRecipeManager().getRecipeFor(InfusionRecipe.TYPE, this, world).orElse(null); RecipeHolder<InfusionRecipe> recipe = world.getRecipeManager()
.getRecipeFor(InfusionRecipe.TYPE, this, world)
.orElse(null);
if (hasRecipe()) { if (hasRecipe()) {
if (recipe == null) { if (recipe == null) {
reset(); reset();
return false; return false;
} else if (activeRecipe == null || recipe.getInfusionTime() != time) { } else if (activeRecipe == null || recipe.value().getInfusionTime() != time) {
updateRecipe(recipe); updateRecipe(recipe);
} }
return true; return true;
@ -87,7 +91,7 @@ public class InfusionRitual implements Container {
return false; return false;
} }
private void updateRecipe(InfusionRecipe recipe) { private void updateRecipe(RecipeHolder<InfusionRecipe> recipe) {
activeRecipe = recipe; activeRecipe = recipe;
hasRecipe = true; hasRecipe = true;
resetTimer(); resetTimer();
@ -95,7 +99,7 @@ public class InfusionRitual implements Container {
} }
private void resetTimer() { private void resetTimer() {
time = activeRecipe != null ? activeRecipe.getInfusionTime() : 0; time = activeRecipe != null ? activeRecipe.value().getInfusionTime() : 0;
progress = 0; progress = 0;
} }
@ -115,7 +119,7 @@ public class InfusionRitual implements Container {
progress++; progress++;
if (progress == time) { if (progress == time) {
clearContent(); clearContent();
input.setItem(0, activeRecipe.assemble(this, world.registryAccess())); input.setItem(0, activeRecipe.value().assemble(this, world.registryAccess()));
if (world instanceof ServerLevel sl) { if (world instanceof ServerLevel sl) {
sl.getPlayers(p -> p.position() sl.getPlayers(p -> p.position()
.subtract(new Vec3(worldPos.getX(), worldPos.getY(), worldPos.getZ())) .subtract(new Vec3(worldPos.getX(), worldPos.getY(), worldPos.getZ()))
@ -190,7 +194,7 @@ public class InfusionRitual implements Container {
} }
@Override @Override
public ItemStack getItem(int slot) { public @NotNull ItemStack getItem(int slot) {
if (slot > 8) return ItemStack.EMPTY; if (slot > 8) return ItemStack.EMPTY;
if (slot == 0) { if (slot == 0) {
return input.getItem(0); return input.getItem(0);
@ -200,12 +204,12 @@ public class InfusionRitual implements Container {
} }
@Override @Override
public ItemStack removeItem(int slot, int amount) { public @NotNull ItemStack removeItem(int slot, int amount) {
return removeItemNoUpdate(slot); return removeItemNoUpdate(slot);
} }
@Override @Override
public ItemStack removeItemNoUpdate(int slot) { public @NotNull ItemStack removeItemNoUpdate(int slot) {
if (slot > 8) return ItemStack.EMPTY; if (slot > 8) return ItemStack.EMPTY;
if (slot == 0) { if (slot == 0) {
return input.removeItemNoUpdate(0); return input.removeItemNoUpdate(0);