Skip to content

Commit

Permalink
Merge pull request #651 from refinedmods/fix/NO-ISSUE/pattern-extract
Browse files Browse the repository at this point in the history
Fix pattern extract
  • Loading branch information
raoulvdberge authored Aug 8, 2024
2 parents 82ee865 + 1deb3ae commit e8a6142
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- The Pattern now shows the recipe in the tooltip.
- When a Pattern is created for a recipe, the Pattern will have a different texture and name to differentiate between empty patterns.
- The Pattern Grid now has additional support for encoding stonecutter and smithing table recipes.
- The Pattern output is now always rendered in the Pattern Grid result slot.

### Fixed

Expand Down
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 @@ -25,6 +25,7 @@
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ResultContainer;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SmithingTemplateItem;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class PatternGridContainerMenu extends AbstractGridContainerMenu {
private PatternGridListener listener;
@Nullable
private PatternGridBlockEntity patternGrid;
@Nullable
private Slot patternOutputSlot;

public PatternGridContainerMenu(final int syncId,
final Inventory playerInventory,
Expand Down Expand Up @@ -198,7 +201,7 @@ private void addPatternSlots(final int playerInventoryY) {
playerInventoryY - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_PATTERN_INPUT_SLOT,
PatternGridBlockEntity::isValidPattern
));
addSlot(new ValidatedSlot(
patternOutputSlot = addSlot(new ValidatedSlot(
patternOutput,
0,
152,
Expand All @@ -224,6 +227,11 @@ public void set(final ItemStack stack) {
transferManager.addTransfer(patternOutput, playerInventory);
}

@Nullable
Slot getPatternOutputSlot() {
return patternOutputSlot;
}

private void addCraftingMatrixSlots(final int playerInventoryY) {
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
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
Expand Up @@ -7,6 +7,7 @@

import java.util.Optional;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
Expand All @@ -16,7 +17,18 @@ private PatternRendering() {
}

public static boolean canDisplayOutput(final ItemStack stack) {
return stack.getItem() instanceof PatternProviderItem && Screen.hasShiftDown();
if (!(stack.getItem() instanceof PatternProviderItem)) {
return false;
}
if (Screen.hasShiftDown()) {
return true;
}
final Screen screen = Minecraft.getInstance().screen;
if (screen instanceof PatternGridScreen patternGridScreen) {
return patternGridScreen.getMenu().getPatternOutputSlot() != null
&& patternGridScreen.getMenu().getPatternOutputSlot().getItem() == stack;
}
return false;
}

public static Optional<ItemStack> getOutput(final ItemStack stack) {
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 e8a6142

Please sign in to comment.