Skip to content

Commit

Permalink
feat: processing pattern encoding
Browse files Browse the repository at this point in the history
No alternatives selection yet.
  • Loading branch information
raoulvdberge committed Aug 1, 2024
1 parent 112ec9f commit cf0d591
Show file tree
Hide file tree
Showing 68 changed files with 1,136 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;

import java.util.Collection;
import java.util.List;

import org.apiguardian.api.API;
Expand All @@ -12,8 +11,4 @@
public record CraftingPattern(List<List<PlatformResourceKey>> inputs,
ResourceAmount output,
List<ResourceAmount> byproducts) implements Pattern {
@Override
public Collection<ResourceAmount> getOutputs() {
return List.of(output);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.refinedmods.refinedstorage.common.api.autocrafting;

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

import java.util.Collection;

import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.6")
public interface Pattern {
Collection<ResourceAmount> getOutputs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

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

import java.util.Collection;
import java.util.List;

import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.6")
public record ProcessingPattern(List<List<ResourceAmount>> inputs, List<ResourceAmount> outputs) implements Pattern {
@Override
public Collection<ResourceAmount> getOutputs() {
return outputs;
}
public record ProcessingPattern(List<ResourceAmount> inputs, List<ResourceAmount> outputs) implements Pattern {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ public interface ResourceContainer {

void remove(int index);

void clear();

int size();

default boolean isEmpty() {
for (int i = 0; i < size(); i++) {
if (!isEmpty(i)) {
return false;
}
}
return true;
}

default boolean isEmpty(int index) {
return get(index) == null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.refinedmods.refinedstorage.common.autocrafting.PatternGridData;
import com.refinedmods.refinedstorage.common.autocrafting.PatternItem;
import com.refinedmods.refinedstorage.common.autocrafting.PatternState;
import com.refinedmods.refinedstorage.common.autocrafting.ProcessingPatternState;
import com.refinedmods.refinedstorage.common.configurationcard.ConfigurationCardItem;
import com.refinedmods.refinedstorage.common.configurationcard.ConfigurationCardState;
import com.refinedmods.refinedstorage.common.constructordestructor.BlockBreakDestructorStrategyFactory;
Expand Down Expand Up @@ -866,6 +867,12 @@ protected final void registerDataComponents(final RegistryCallback<DataComponent
.persistent(CraftingPatternState.CODEC)
.networkSynchronized(CraftingPatternState.STREAM_CODEC)
.build()));
DataComponents.INSTANCE.setProcessingPatternState(
callback.register(createIdentifier("processing_pattern_state"),
() -> DataComponentType.<ProcessingPatternState>builder()
.persistent(ProcessingPatternState.CODEC)
.networkSynchronized(ProcessingPatternState.STREAM_CODEC)
.build()));
}

protected final void registerInventorySlotReference() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
Expand Down Expand Up @@ -133,4 +134,6 @@ void saveSavedData(SavedData savedData,
NetworkNodeContainerProvider getContainerProviderSafely(Level level, BlockPos pos, @Nullable Direction direction);

int getItemColor(ItemStack stack, int tintIndex);

void setSlotY(Slot slot, int y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
Expand Down Expand Up @@ -259,6 +260,11 @@ public int getItemColor(final ItemStack stack, final int tintIndex) {
return ensureLoaded().getItemColor(stack, tintIndex);
}

@Override
public void setSlotY(final Slot slot, final int y) {
ensureLoaded().setSlotY(slot, y);
}

private Platform ensureLoaded() {
if (platform == null) {
throw new IllegalStateException("Platform not loaded yet");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.api.autocrafting.CraftingPattern;
import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage.common.support.resource.ItemResource;

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

import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.Font;
Expand All @@ -20,7 +19,9 @@
import static com.refinedmods.refinedstorage.common.support.TextureIds.LIGHT_ARROW;
import static com.refinedmods.refinedstorage.common.support.TextureIds.LIGHT_ARROW_HEIGHT;
import static com.refinedmods.refinedstorage.common.support.TextureIds.LIGHT_ARROW_WIDTH;
import static com.refinedmods.refinedstorage.common.support.TextureIds.SLOT;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier;
import static java.util.Objects.requireNonNullElse;

public class CraftingPatternClientTooltipComponent implements ClientTooltipComponent {
private static final long CYCLE_MS = 1000;
Expand All @@ -29,7 +30,6 @@ public class CraftingPatternClientTooltipComponent implements ClientTooltipCompo

private static final int ARROW_SPACING = 8;

private static final ResourceLocation SLOT = createIdentifier("slot");
private static final ResourceLocation LARGE_SLOT = createIdentifier("large_slot");
private static final int LARGE_SLOT_WIDTH = 26;
private static final int LARGE_SLOT_HEIGHT = 26;
Expand All @@ -38,12 +38,27 @@ public class CraftingPatternClientTooltipComponent implements ClientTooltipCompo
private final int height;
private final CraftingPattern craftingPattern;

@Nullable
private final ItemStack outputStack;
@Nullable
private final Component outputText;

public CraftingPatternClientTooltipComponent(final int width,
final int height,
final CraftingPattern craftingPattern) {
this.width = width;
this.height = height;
this.craftingPattern = craftingPattern;
final ItemResource outputResource = craftingPattern.output().getResource() instanceof ItemResource itemResource
? itemResource
: null;
this.outputStack = outputResource != null
? outputResource.toItemStack(craftingPattern.output().getAmount())
: null;
this.outputText = outputResource != null
? Component.literal(String.format("%dx ", craftingPattern.output().getAmount()))
.append(outputResource.toItemStack().getHoverName())
.withStyle(ChatFormatting.GRAY) : null;
}

@Override
Expand All @@ -66,16 +81,8 @@ public void renderImage(final Font font, final int x, final int y, final GuiGrap
currentCycle++;
cycleStart = now;
}
if (craftingPattern.output().getResource() instanceof ItemResource itemResource) {
graphics.drawString(
font,
Component.literal(String.format("%dx ", craftingPattern.output().getAmount()))
.append(itemResource.toItemStack().getHoverName())
.withStyle(ChatFormatting.GRAY),
x,
y,
Objects.requireNonNullElse(ChatFormatting.GRAY.getColor(), 15)
);
if (outputText != null) {
graphics.drawString(font, outputText, x, y, requireNonNullElse(ChatFormatting.GRAY.getColor(), 15));
}
renderInputSlots(x, y + 9 + 2, graphics);
renderArrow(x, y + 9 + 2, graphics);
Expand Down Expand Up @@ -120,13 +127,11 @@ private void renderResultSlot(final Font font, final int x, final int y, final G
final int slotX = x + width * 18 + ARROW_SPACING + LIGHT_ARROW_WIDTH + ARROW_SPACING;
final int slotY = y + ((height * 18) / 2) - (LARGE_SLOT_HEIGHT / 2);
graphics.blitSprite(LARGE_SLOT, slotX, slotY, LARGE_SLOT_WIDTH, LARGE_SLOT_HEIGHT);
final ResourceAmount output = craftingPattern.output();
if (output.getResource() instanceof ItemResource itemResource) {
final ItemStack resultItemStack = itemResource.toItemStack(output.getAmount());
if (outputStack != null) {
final int stackX = slotX + 5;
final int stackY = slotY + 5;
graphics.renderItem(resultItemStack, stackX, stackY);
graphics.renderItemDecorations(font, resultItemStack, stackX, stackY);
graphics.renderItem(outputStack, stackX, stackY);
graphics.renderItemDecorations(font, outputStack, stackX, stackY);
}
}
}
Loading

0 comments on commit cf0d591

Please sign in to comment.