Skip to content

Commit

Permalink
feat: crafting preview screen
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Sep 10, 2024
1 parent 4f0897c commit 8b656f1
Show file tree
Hide file tree
Showing 32 changed files with 465 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.api.autocrafting;

import com.refinedmods.refinedstorage.api.resource.list.ResourceList;

public record CraftingPreview(ResourceList missing, ResourceList toTake, ResourceList toCraft) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.constructordestructor.ConstructorStrategyFactory;
import com.refinedmods.refinedstorage.common.api.constructordestructor.DestructorStrategyFactory;
Expand Down Expand Up @@ -46,6 +47,7 @@
import com.refinedmods.refinedstorage.common.api.wirelesstransmitter.WirelessTransmitterRangeModifier;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -203,4 +205,6 @@ EnergyStorage asBlockItemEnergyStorage(
boolean canPlaceNetworkNode(ServerPlayer player, Level level, BlockPos pos, BlockState state);

Optional<Pattern> getPattern(ItemStack stack, Level level);

void openCraftingPreview(List<ResourceAmount> resources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.constructordestructor.ConstructorStrategyFactory;
import com.refinedmods.refinedstorage.common.api.constructordestructor.DestructorStrategyFactory;
Expand Down Expand Up @@ -46,6 +47,7 @@
import com.refinedmods.refinedstorage.common.api.wirelesstransmitter.WirelessTransmitterRangeModifier;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -401,6 +403,11 @@ public Optional<Pattern> getPattern(final ItemStack stack, final Level level) {
return ensureLoaded().getPattern(stack, level);
}

@Override
public void openCraftingPreview(final List<ResourceAmount> resources) {
ensureLoaded().openCraftingPreview(resources);
}

private RefinedStorageApi ensureLoaded() {
if (delegate == null) {
throw new IllegalStateException("API not loaded yet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage.api.network.impl.NetworkFactory;
import com.refinedmods.refinedstorage.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage.api.network.security.SecurityPolicy;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.api.autocrafting.PatternProviderItem;
Expand Down Expand Up @@ -49,6 +50,7 @@
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReferenceProvider;
import com.refinedmods.refinedstorage.common.api.upgrade.UpgradeRegistry;
import com.refinedmods.refinedstorage.common.api.wirelesstransmitter.WirelessTransmitterRangeModifier;
import com.refinedmods.refinedstorage.common.autocrafting.preview.CraftingPreviewScreen;
import com.refinedmods.refinedstorage.common.grid.NoopGridSynchronizer;
import com.refinedmods.refinedstorage.common.grid.screen.hint.GridInsertionHintsImpl;
import com.refinedmods.refinedstorage.common.grid.screen.hint.ItemGridInsertionHint;
Expand Down Expand Up @@ -99,12 +101,14 @@
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -595,4 +599,17 @@ public Optional<Pattern> getPattern(final ItemStack stack, final Level level) {
i -> providerItem.getPattern(stack, level).orElse(null)
));
}

@Override
public void openCraftingPreview(final List<ResourceAmount> resources) {
if (resources.isEmpty()) {
return;
}
final Minecraft minecraft = Minecraft.getInstance();
if (minecraft.screen == null || minecraft.player == null) {
return;
}
final Inventory inventory = minecraft.player.getInventory();
minecraft.setScreen(new CraftingPreviewScreen(minecraft.screen, inventory, resources.getFirst().resource()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static com.refinedmods.refinedstorage.common.autocrafting.AlternativesScreen.ALTERNATIVE_ROW_HEIGHT;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.getTagTranslationKey;

public class AlternativeContainerMenu extends AbstractResourceContainerMenu {
class AlternativeContainerMenu extends AbstractResourceContainerMenu {
private final List<Alternative> alternatives;
private final ResourceSlot amountSlot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ protected ResourceLocation getTexture() {
}

@Override
protected void accept(final Double amount) {
protected boolean confirm(final Double amount) {
slot.changeAmountOnClient(amount);
final Set<Alternative> allowedAlternatives = new HashSet<>();
for (int i = 0; i < alternativeCheckboxes.size(); ++i) {
Expand All @@ -502,6 +502,7 @@ protected void accept(final Double amount) {
}
}
getMenu().sendAllowedAlternatives(allowedAlternatives);
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void renderImage(final Font font, final int x, final int y, final GuiGrap
cycleStart = now;
}
if (outputText != null) {
graphics.drawString(font, outputText, x, y, requireNonNullElse(ChatFormatting.GRAY.getColor(), 15));
graphics.drawString(font, outputText, x, y, 0xAAAAAA);
}
renderInputSlots(x, y + 9 + 2, graphics);
renderArrow(x, y + 9 + 2, graphics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void renderOutputText(final Font font, final int x, final int y, final G
outputTexts.get(i),
x,
y + (i * 9),
requireNonNullElse(ChatFormatting.GRAY.getColor(), 15)
0xAAAAAA
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SmithingTablePatternClientTooltipComponent implements ClientTooltipCompone

@Override
public void renderImage(final Font font, final int x, final int y, final GuiGraphics graphics) {
graphics.drawString(font, outputText, x, y, requireNonNullElse(ChatFormatting.GRAY.getColor(), 15));
graphics.drawString(font, outputText, x, y, 0xAAAAAA);
final int slotsY = y + 9 + 2;
graphics.blitSprite(Sprites.SLOT, x, slotsY, 18, 18);
final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(ItemResource.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StonecutterPatternClientTooltipComponent implements ClientTooltipComponent

@Override
public void renderImage(final Font font, final int x, final int y, final GuiGraphics graphics) {
graphics.drawString(font, outputText, x, y, requireNonNullElse(ChatFormatting.GRAY.getColor(), 15));
graphics.drawString(font, outputText, x, y, 0xAAAAAA);
graphics.blitSprite(SLOT, x, y + 9 + 2, 18, 18);
final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(ItemResource.class);
rendering.render(input, graphics, x + 1, y + 9 + 2 + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import java.util.List;

record CraftingPreview(boolean missing, List<CraftingPreviewItem> items) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage.common.support.containermenu.AbstractResourceContainerMenu;
import com.refinedmods.refinedstorage.common.support.containermenu.DisabledResourceSlot;
import com.refinedmods.refinedstorage.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerImpl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;

import net.minecraft.network.chat.Component;

class CraftingPreviewContainerMenu extends AbstractResourceContainerMenu {
@Nullable
private CraftingPreview preview;

CraftingPreviewContainerMenu(final ResourceKey resource) {
super(null, 0);
final ResourceContainer resourceContainer = ResourceContainerImpl.createForFilter(1);
resourceContainer.set(0, new ResourceAmount(resource, 1));
addSlot(new DisabledResourceSlot(
resourceContainer,
0,
Component.empty(),
157,
48,
ResourceSlotType.FILTER
));

final List<CraftingPreviewItem> items = new ArrayList<>();
for (int i = 0; i < 31; ++i) {
items.add(new CraftingPreviewItem(resource, i, i % 2 == 0 ? 999 : 0, i % 2 == 0 ? 0 : 1000));
}
preview = new CraftingPreview(true, items);
}

@Nullable
public CraftingPreview getPreview() {
return preview;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;

record CraftingPreviewItem(ResourceKey resource, long available, long missing, long toCraft) {
}
Loading

0 comments on commit 8b656f1

Please sign in to comment.