Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocrafting support in the Relay #700

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Autocrafter
- The Relay now has support for propagating autocrafting when not in pass-through mode.

### Changed

Expand Down
9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ project.extensions.getByType<SonarExtension>().apply {
)
}
}

allprojects {
apply(plugin = "publishing")
publishing {
repositories {
mavenLocal()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
public interface AutocraftingPreviewProvider {
Optional<AutocraftingPreview> getPreview(ResourceKey resource, long amount);

boolean start(ResourceKey resource, long amount);
boolean startTask(ResourceKey resource, long amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface PatternRepository {
void remove(Pattern pattern);

Set<ResourceKey> getOutputs();

Set<Pattern> getAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

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

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class PatternRepositoryImpl implements PatternRepository {
private final Set<Pattern> patterns = new HashSet<>();
private final Set<Pattern> patternsView = Collections.unmodifiableSet(patterns);
private final Set<ResourceKey> outputs = new HashSet<>();

@Override
Expand All @@ -31,4 +33,9 @@ public void remove(final Pattern pattern) {
public Set<ResourceKey> getOutputs() {
return outputs;
}

@Override
public Set<Pattern> getAll() {
return patternsView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,43 @@ void setUp() {
void testDefaultState() {
// Assert
assertThat(sut.getOutputs()).isEmpty();
assertThat(sut.getAll()).isEmpty();
}

@Test
void shouldAddPattern() {
// Act
sut.add(new SimplePattern(FakeResources.A));
sut.add(new SimplePattern(ResourceFixtures.A));

// Assert
assertThat(sut.getOutputs()).usingRecursiveFieldByFieldElementComparator().containsExactly(FakeResources.A);
assertThat(sut.getOutputs()).usingRecursiveFieldByFieldElementComparator().containsExactly(ResourceFixtures.A);
assertThat(sut.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new SimplePattern(ResourceFixtures.A)
);
}

@Test
void shouldAddMultiplePatterns() {
// Act
sut.add(new SimplePattern(FakeResources.A));
sut.add(new SimplePattern(FakeResources.B));
sut.add(new SimplePattern(ResourceFixtures.A));
sut.add(new SimplePattern(ResourceFixtures.B));

// Assert
assertThat(sut.getOutputs()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
FakeResources.A,
FakeResources.B
ResourceFixtures.A,
ResourceFixtures.B
);
assertThat(sut.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new SimplePattern(ResourceFixtures.A),
new SimplePattern(ResourceFixtures.B)
);
}

@Test
void shouldRemovePattern() {
// Arrange
final SimplePattern a = new SimplePattern(FakeResources.A);
final SimplePattern b = new SimplePattern(FakeResources.B);
final SimplePattern a = new SimplePattern(ResourceFixtures.A);
final SimplePattern b = new SimplePattern(ResourceFixtures.B);

sut.add(a);
sut.add(b);
Expand All @@ -54,14 +62,17 @@ void shouldRemovePattern() {
sut.remove(a);

// Assert
assertThat(sut.getOutputs()).usingRecursiveFieldByFieldElementComparator().containsExactly(FakeResources.B);
assertThat(sut.getOutputs()).usingRecursiveFieldByFieldElementComparator().containsExactly(ResourceFixtures.B);
assertThat(sut.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new SimplePattern(ResourceFixtures.B)
);
}

@Test
void shouldRemoveMultiplePatterns() {
// Arrange
final SimplePattern a = new SimplePattern(FakeResources.A);
final SimplePattern b = new SimplePattern(FakeResources.B);
final SimplePattern a = new SimplePattern(ResourceFixtures.A);
final SimplePattern b = new SimplePattern(ResourceFixtures.B);

sut.add(a);
sut.add(b);
Expand All @@ -72,13 +83,14 @@ void shouldRemoveMultiplePatterns() {

// Assert
assertThat(sut.getOutputs()).isEmpty();
assertThat(sut.getAll()).isEmpty();
}

@Test
void shouldRemovePatternButNotRemoveOutputIfAnotherPatternStillHasThatOutput() {
// Arrange
final SimplePattern a = new SimplePattern(FakeResources.A);
final SimplePattern b = new SimplePattern(FakeResources.B, FakeResources.A);
final SimplePattern a = new SimplePattern(ResourceFixtures.A);
final SimplePattern b = new SimplePattern(ResourceFixtures.B, ResourceFixtures.A);

sut.add(a);
sut.add(b);
Expand All @@ -88,8 +100,11 @@ void shouldRemovePatternButNotRemoveOutputIfAnotherPatternStillHasThatOutput() {

// Assert
assertThat(sut.getOutputs()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
FakeResources.A,
FakeResources.B
ResourceFixtures.A,
ResourceFixtures.B
);
assertThat(sut.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new SimplePattern(ResourceFixtures.B, ResourceFixtures.A)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

public enum FakeResources implements ResourceKey {
enum ResourceFixtures implements ResourceKey {
A,
B,
C
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import java.util.Set;

public class SimplePattern implements Pattern {
class SimplePattern implements Pattern {
private final Set<ResourceKey> outputs;

public SimplePattern(final ResourceKey... outputs) {
SimplePattern(final ResourceKey... outputs) {
this.outputs = Set.of(outputs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,5 @@ EnergyStorage asBlockItemEnergyStorage(

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

void openCraftingPreview(List<ResourceAmount> requests);
void openAutocraftingPreview(List<ResourceAmount> requests, @Nullable Object parentScreen);
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ public Optional<Pattern> getPattern(final ItemStack stack, final Level level) {
}

@Override
public void openCraftingPreview(final List<ResourceAmount> requests) {
ensureLoaded().openCraftingPreview(requests);
public void openAutocraftingPreview(final List<ResourceAmount> requests, @Nullable final Object parentScreen) {
ensureLoaded().openAutocraftingPreview(requests, parentScreen);
}

private RefinedStorageApi ensureLoaded() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.refinedmods.refinedstorage.common;

import com.refinedmods.refinedstorage.api.autocrafting.PatternRepositoryImpl;
import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent;
import com.refinedmods.refinedstorage.api.network.energy.EnergyNetworkComponent;
import com.refinedmods.refinedstorage.api.network.impl.autocrafting.AutocraftingNetworkComponentImpl;
Expand Down Expand Up @@ -271,7 +270,7 @@ private void registerNetworkComponents() {
);
RefinedStorageApi.INSTANCE.getNetworkComponentMapFactory().addFactory(
AutocraftingNetworkComponent.class,
network -> new AutocraftingNetworkComponentImpl(new PatternRepositoryImpl())
network -> new AutocraftingNetworkComponentImpl()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public interface Config {

RelayEntry getRelay();

SimpleEnergyUsageEntry getAutocrafter();
AutocrafterEntry getAutocrafter();

interface SimpleEnergyUsageEntry {
long getEnergyUsage();
Expand Down Expand Up @@ -208,4 +208,8 @@ interface RelayEntry {

long getOutputNetworkEnergyUsage();
}

interface AutocrafterEntry extends SimpleEnergyUsageEntry {
long getEnergyUsagePerPattern();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
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.AutocraftingPreviewScreen;
import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingRequest;
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 @@ -82,6 +80,7 @@
import com.refinedmods.refinedstorage.common.support.slotreference.CompositeSlotReferenceProvider;
import com.refinedmods.refinedstorage.common.support.slotreference.InventorySlotReference;
import com.refinedmods.refinedstorage.common.upgrade.UpgradeRegistryImpl;
import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil;
import com.refinedmods.refinedstorage.common.util.IdentifierUtil;
import com.refinedmods.refinedstorage.common.util.ServerEventQueue;

Expand All @@ -102,14 +101,12 @@
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 @@ -602,19 +599,10 @@ public Optional<Pattern> getPattern(final ItemStack stack, final Level level) {
}

@Override
public void openCraftingPreview(final List<ResourceAmount> requests) {
public void openAutocraftingPreview(final List<ResourceAmount> requests, @Nullable final Object parentScreen) {
if (requests.isEmpty()) {
return;
}
final Minecraft minecraft = Minecraft.getInstance();
if (minecraft.screen == null || minecraft.player == null) {
return;
}
final Inventory inventory = minecraft.player.getInventory();
minecraft.setScreen(new AutocraftingPreviewScreen(
minecraft.screen,
inventory,
requests.stream().map(AutocraftingRequest::of).toList()
));
ClientPlatformUtil.openCraftingPreview(requests, parentScreen);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@

import static com.refinedmods.refinedstorage.common.support.AbstractDirectionalBlock.tryExtractDirection;

// TODO: More energy usage for more patterns.
public class AutocrafterBlockEntity extends AbstractBaseNetworkNodeContainerBlockEntity<PatternProviderNetworkNode>
implements ExtendedMenuProvider<AutocrafterData>, BlockEntityWithDrops, PatternInventory.Listener {
static final int PATTERNS = 9;

private static final int MAX_CHAINED_CRAFTERS = 8;
private static final int MAX_CHAINED_AUTOCRAFTERS = 8;
private static final String TAG_UPGRADES = "upgr";
private static final String TAG_PATTERNS = "patterns";
private static final String TAG_LOCK_MODE = "lm";
Expand All @@ -65,11 +64,18 @@ public AutocrafterBlockEntity(final BlockPos pos, final BlockState state) {
);
this.upgradeContainer = new UpgradeContainer(UpgradeDestinations.AUTOCRAFTER, upgradeEnergyUsage -> {
final long baseEnergyUsage = Platform.INSTANCE.getConfig().getAutocrafter().getEnergyUsage();
mainNetworkNode.setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage);
final long patternEnergyUsage = patternContainer.getEnergyUsage();
mainNetworkNode.setEnergyUsage(baseEnergyUsage + patternEnergyUsage + upgradeEnergyUsage);
setChanged();
});
patternContainer.addListener(container -> setChanged());
patternContainer.setListener(this);
this.patternContainer.addListener(container -> {
final long upgradeEnergyUsage = upgradeContainer.getEnergyUsage();
final long baseEnergyUsage = Platform.INSTANCE.getConfig().getAutocrafter().getEnergyUsage();
final long patternEnergyUsage = patternContainer.getEnergyUsage();
mainNetworkNode.setEnergyUsage(baseEnergyUsage + patternEnergyUsage + upgradeEnergyUsage);
setChanged();
});
this.patternContainer.setListener(this);
}

@Override
Expand All @@ -91,7 +97,7 @@ private boolean isPartOfChain() {
return getChainingRoot() != this;
}

// if there is another autocrafter next to us, that is pointing in our direction,
// If there is another autocrafter next to us, that is pointing in our direction,
// and we are not part of a chain, we are the head of the chain
private boolean isHeadOfChain() {
if (level == null || isPartOfChain()) {
Expand Down Expand Up @@ -119,7 +125,7 @@ private AutocrafterBlockEntity getChainingRoot() {

private AutocrafterBlockEntity getChainingRoot(final int depth, final AutocrafterBlockEntity origin) {
final Direction direction = tryExtractDirection(getBlockState());
if (level == null || direction == null || depth >= MAX_CHAINED_CRAFTERS) {
if (level == null || direction == null || depth >= MAX_CHAINED_AUTOCRAFTERS) {
return origin;
}
final BlockEntity neighbor = getConnectedMachine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,6 @@ protected void renderBg(final GuiGraphics graphics, final float delta, final int
}
}

@Override
public boolean mouseClicked(final double mouseX, final double mouseY, final int clickedButton) {
if (!editName && getMenu().canChangeName() && isHovering(
titleLabelX, titleLabelY, titleMarquee.getEffectiveWidth(font), font.lineHeight, mouseX, mouseY
)) {
setEditName(true);
return true;
}
return super.mouseClicked(mouseX, mouseY, clickedButton);
}

@Override
protected void init() {
super.init();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage.common.autocrafting;

import com.refinedmods.refinedstorage.api.core.NullableType;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.support.FilteredContainer;

import java.util.Optional;
Expand Down Expand Up @@ -44,6 +45,17 @@ public void setItem(final int slot, final ItemStack stack) {
}
}

long getEnergyUsage() {
long patterns = 0;
for (int i = 0; i < getContainerSize(); i++) {
final ItemStack stack = getItem(i);
if (!stack.isEmpty()) {
patterns++;
}
}
return patterns * Platform.INSTANCE.getConfig().getAutocrafter().getEnergyUsagePerPattern();
}

interface Listener {
void patternChanged(int slot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void renderMatrixSlot(final GuiGraphics graphics,
final ResourceSlot resourceSlot,
final ProcessingMatrixResourceSlot matrixSlot) {
if (matrixSlot.getResource() != null && menu.getView().isAutocraftable(matrixSlot.getResource())) {
AbstractGridScreen.renderCraftableBackground(
AbstractGridScreen.renderSlotBackground(
graphics,
resourceSlot.x + leftPos,
resourceSlot.y + topPos,
Expand Down
Loading
Loading