Skip to content

Commit

Permalink
fix: not being able to extract patterns
Browse files Browse the repository at this point in the history
They couldn't be extracted as ItemStack
does not implement equals/hashCode and that
breaks the extract comparison for the
stack component.
  • Loading branch information
raoulvdberge committed Aug 8, 2024
1 parent 82ee865 commit 2e39bd6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ private ItemStack createStonecutterPattern() {
return null;
}
final ItemStack result = createPatternStack(PatternType.STONECUTTER);
final StonecutterPatternState state = new StonecutterPatternState(input, selectedOutput);
final StonecutterPatternState state = new StonecutterPatternState(
ItemResource.ofItemStack(input),
ItemResource.ofItemStack(selectedOutput)
);
result.set(DataComponents.INSTANCE.getStonecutterPatternState(), state);
return result;
}
Expand All @@ -355,9 +358,9 @@ private ItemStack createSmithingTablePattern() {
}
final ItemStack result = createPatternStack(PatternType.SMITHING_TABLE);
final SmithingTablePatternState state = new SmithingTablePatternState(
smithingTableRecipe.getMatrix().getItem(0),
smithingTableRecipe.getMatrix().getItem(1),
smithingTableRecipe.getMatrix().getItem(2)
ItemResource.ofItemStack(smithingTableRecipe.getMatrix().getItem(0)),
ItemResource.ofItemStack(smithingTableRecipe.getMatrix().getItem(1)),
ItemResource.ofItemStack(smithingTableRecipe.getMatrix().getItem(2))
);
result.set(DataComponents.INSTANCE.getSmithingTablePatternState(), state);
return result;
Expand Down Expand Up @@ -440,9 +443,9 @@ private void copyProcessingPattern(final ProcessingPatternState state) {
}

private void copyStonecutterPattern(final StonecutterPatternState state) {
final ItemStack input = state.input();
final ItemStack selectedOutput = state.selectedOutput();
setStonecutterInputAndSelectedRecipe(input, selectedOutput);
final ItemResource input = state.input();
final ItemResource selectedOutput = state.selectedOutput();
setStonecutterInputAndSelectedRecipe(input.toItemStack(), selectedOutput.toItemStack());
}

private void setStonecutterInputAndSelectedRecipe(final ItemStack input, final ItemStack selectedOutput) {
Expand All @@ -465,10 +468,10 @@ private void setStonecutterInputAndSelectedRecipe(final ItemStack input, final I
}

private void copySmithingTablePattern(final SmithingTablePatternState state) {
final ItemStack template = state.template();
final ItemStack base = state.base();
final ItemStack addition = state.addition();
setSmithingTableInput(template, base, addition);
final ItemResource template = state.template();
final ItemResource base = state.base();
final ItemResource addition = state.addition();
setSmithingTableInput(template.toItemStack(), base.toItemStack(), addition.toItemStack());
}

private void setSmithingTableInput(final ItemStack template, final ItemStack base, final ItemStack addition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ private Optional<Pattern> getStonecutterPattern(final ItemStack stack, final Lev
}

private Optional<Pattern> getStonecutterPattern(final Level level, final StonecutterPatternState state) {
final SingleRecipeInput input = new SingleRecipeInput(state.input());
final SingleRecipeInput input = new SingleRecipeInput(state.input().toItemStack());
final ItemStack selectedOutput = state.selectedOutput().toItemStack();
final var recipes = level.getRecipeManager().getRecipesFor(RecipeType.STONECUTTING, input, level);
for (final var recipe : recipes) {
final ItemStack output = recipe.value().assemble(input, level.registryAccess());
if (ItemStack.isSameItemSameComponents(output, state.selectedOutput())) {
if (ItemStack.isSameItemSameComponents(output, selectedOutput)) {
return Optional.of(new StonecutterPattern(
ItemResource.ofItemStack(state.input()),
state.input(),
ItemResource.ofItemStack(output)
));
}
Expand All @@ -279,13 +280,16 @@ private Optional<Pattern> getSmithingTablePattern(final ItemStack stack, final L
}

private Optional<Pattern> getSmithingTablePattern(final Level level, final SmithingTablePatternState state) {
final SmithingRecipeInput input = new SmithingRecipeInput(state.template(), state.base(), state.addition());
return level.getRecipeManager()
.getRecipeFor(RecipeType.SMITHING, input, level)
final SmithingRecipeInput input = new SmithingRecipeInput(
state.template().toItemStack(),
state.base().toItemStack(),
state.addition().toItemStack()
);
return level.getRecipeManager().getRecipeFor(RecipeType.SMITHING, input, level)
.map(recipe -> new SmithingTablePattern(
ItemResource.ofItemStack(state.template()),
ItemResource.ofItemStack(state.base()),
ItemResource.ofItemStack(state.addition()),
state.template(),
state.base(),
state.addition(),
ItemResource.ofItemStack(recipe.value().assemble(input, level.registryAccess())))
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.common.support.resource.ItemResource;
import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;

public record SmithingTablePatternState(ItemStack template,
ItemStack base,
ItemStack addition) {
public record SmithingTablePatternState(ItemResource template,
ItemResource base,
ItemResource addition) {
public static final Codec<SmithingTablePatternState> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ItemStack.CODEC.fieldOf("template").forGetter(SmithingTablePatternState::template),
ItemStack.CODEC.fieldOf("base").forGetter(SmithingTablePatternState::base),
ItemStack.CODEC.fieldOf("addition").forGetter(SmithingTablePatternState::addition)
ResourceCodecs.ITEM_CODEC.fieldOf("template").forGetter(SmithingTablePatternState::template),
ResourceCodecs.ITEM_CODEC.fieldOf("base").forGetter(SmithingTablePatternState::base),
ResourceCodecs.ITEM_CODEC.fieldOf("addition").forGetter(SmithingTablePatternState::addition)
).apply(instance, SmithingTablePatternState::new));

public static final StreamCodec<RegistryFriendlyByteBuf, SmithingTablePatternState> STREAM_CODEC =
StreamCodec.composite(
ItemStack.STREAM_CODEC, SmithingTablePatternState::template,
ItemStack.STREAM_CODEC, SmithingTablePatternState::base,
ItemStack.STREAM_CODEC, SmithingTablePatternState::addition,
ResourceCodecs.ITEM_STREAM_CODEC, SmithingTablePatternState::template,
ResourceCodecs.ITEM_STREAM_CODEC, SmithingTablePatternState::base,
ResourceCodecs.ITEM_STREAM_CODEC, SmithingTablePatternState::addition,
SmithingTablePatternState::new
);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.common.support.resource.ItemResource;
import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;

public record StonecutterPatternState(ItemStack input, ItemStack selectedOutput) {
public record StonecutterPatternState(ItemResource input, ItemResource selectedOutput) {
public static final Codec<StonecutterPatternState> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ItemStack.CODEC.fieldOf("input").forGetter(StonecutterPatternState::input),
ItemStack.CODEC.fieldOf("selectedOutput").forGetter(StonecutterPatternState::selectedOutput)
ResourceCodecs.ITEM_CODEC.fieldOf("input").forGetter(StonecutterPatternState::input),
ResourceCodecs.ITEM_CODEC.fieldOf("selectedOutput").forGetter(StonecutterPatternState::selectedOutput)
).apply(instance, StonecutterPatternState::new));

public static final StreamCodec<RegistryFriendlyByteBuf, StonecutterPatternState> STREAM_CODEC =
StreamCodec.composite(
ItemStack.STREAM_CODEC, StonecutterPatternState::input,
ItemStack.STREAM_CODEC, StonecutterPatternState::selectedOutput,
ResourceCodecs.ITEM_STREAM_CODEC, StonecutterPatternState::input,
ResourceCodecs.ITEM_STREAM_CODEC, StonecutterPatternState::selectedOutput,
StonecutterPatternState::new
);
}

0 comments on commit 2e39bd6

Please sign in to comment.