Skip to content

Commit

Permalink
feat: additional energy usage for patterns in the autocrafter
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Oct 11, 2024
1 parent 30d00dc commit 784c172
Show file tree
Hide file tree
Showing 35 changed files with 334 additions and 224 deletions.
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
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
@@ -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
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 @@ -40,7 +40,6 @@

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;
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
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 @@ -23,6 +23,7 @@ public final class DefaultEnergyUsage {
public static final long RELAY_INPUT_NETWORK = 8;
public static final long RELAY_OUTPUT_NETWORK = 8;
public static final long AUTOCRAFTER = 4;
public static final long AUTOCRAFTER_PER_PATTERN = 2;

public static final long CONTROLLER_CAPACITY = 1000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.refinedmods.refinedstorage.api.grid.view.GridView;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage.common.content.Menus;
import com.refinedmods.refinedstorage.common.grid.view.ItemGridResource;
import com.refinedmods.refinedstorage.common.support.RedstoneMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ public void readConfiguration(final CompoundTag tag, final HolderLookup.Provider
mainNetworkNode.setPriority(priority);
}

private Set<RelayComponentType> getComponentTypes(final CompoundTag tag) {
final Set<RelayComponentType> types = new HashSet<>();
private Set<RelayComponentType<?>> getComponentTypes(final CompoundTag tag) {
final Set<RelayComponentType<?>> types = new HashSet<>();
if (tag.getBoolean(TAG_PASS_ENERGY)) {
types.add(RelayComponentType.ENERGY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private void addFilterButtons(final boolean visible) {
);
fuzzyModeButton.visible = visible;
addSideButton(fuzzyModeButton);
}

private void addStorageButtons(final boolean visible) {
accessModeButton = new AccessModeSideButtonWidget(getMenu().getProperty(StoragePropertyTypes.ACCESS_MODE));
accessModeButton.visible = visible;
addSideButton(accessModeButton);
}

private void addStorageButtons(final boolean visible) {
priorityButton = PrioritySideButtonWidget.forStorage(
getMenu().getProperty(StoragePropertyTypes.PRIORITY),
playerInventory,
Expand Down Expand Up @@ -154,12 +154,12 @@ private void updateFilterButtons(final boolean visible) {
if (fuzzyModeButton != null) {
fuzzyModeButton.visible = visible;
}
if (accessModeButton != null) {
accessModeButton.visible = visible;
}
}

private void updateStorageButtons(final boolean visible) {
if (accessModeButton != null) {
accessModeButton.visible = visible;
}
if (priorityButton != null) {
priorityButton.visible = visible;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@
"text.autoconfig.refinedstorage.option.autocrafter.tooltip": "Configuration for the Autocrafter.",
"text.autoconfig.refinedstorage.option.autocrafter.energyUsage": "Energy usage",
"text.autoconfig.refinedstorage.option.autocrafter.energyUsage.tooltip": "The energy used by the Autocrafter.",
"text.autoconfig.refinedstorage.option.autocrafter.energyUsagePerPattern": "Energy usage per pattern",
"text.autoconfig.refinedstorage.option.autocrafter.energyUsagePerPattern.tooltip": "The additional energy used per pattern.",
"advancements.refinedstorage.root.description": "Use one or multiple Controllers in a storage network to provide your network with energy",
"advancements.refinedstorage.connecting": "Connecting",
"advancements.refinedstorage.connecting.description": "Use Cable to connect devices with each other, or place devices against each other",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public class ConfigImpl implements ConfigData, com.refinedmods.refinedstorage.co
private RelayEntryImpl relay = new RelayEntryImpl();

@ConfigEntry.Gui.CollapsibleObject
private SimpleEnergyUsageEntryImpl autocrafter = new SimpleEnergyUsageEntryImpl(
DefaultEnergyUsage.AUTOCRAFTER
);
private AutocrafterEntryImpl autocrafter = new AutocrafterEntryImpl();

public static ConfigImpl get() {
return AutoConfig.getConfigHolder(ConfigImpl.class).getConfig();
Expand Down Expand Up @@ -285,7 +283,7 @@ public RelayEntry getRelay() {
}

@Override
public SimpleEnergyUsageEntry getAutocrafter() {
public AutocrafterEntry getAutocrafter() {
return autocrafter;
}

Expand Down Expand Up @@ -725,4 +723,20 @@ public long getOutputNetworkEnergyUsage() {
return outputNetworkEnergyUsage;
}
}

private static class AutocrafterEntryImpl implements AutocrafterEntry {
private long energyUsage = DefaultEnergyUsage.AUTOCRAFTER;

private long energyUsagePerPattern = DefaultEnergyUsage.AUTOCRAFTER_PER_PATTERN;

@Override
public long getEnergyUsagePerPattern() {
return energyUsagePerPattern;
}

@Override
public long getEnergyUsage() {
return energyUsage;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.refinedmods.refinedstorage.api.grid.query;

import com.refinedmods.refinedstorage.api.grid.view.FakeGridResourceAttributeKeys;
import com.refinedmods.refinedstorage.api.grid.view.GridResourceAttributeKeys;
import com.refinedmods.refinedstorage.api.grid.view.GridResource;

Check failure on line 4 in refinedstorage-grid-api/src/test/java/com/refinedmods/refinedstorage/api/grid/query/GridQueryParserImplTest.java

View workflow job for this annotation

GitHub Actions / build / build

Wrong lexicographical order for 'com.refinedmods.refinedstorage.api.grid.view.GridResource' import. Should be before 'com.refinedmods.refinedstorage.api.grid.view.GridResourceAttributeKeys'.
import com.refinedmods.refinedstorage.api.grid.view.GridResourceAttributeKey;

Check failure on line 5 in refinedstorage-grid-api/src/test/java/com/refinedmods/refinedstorage/api/grid/query/GridQueryParserImplTest.java

View workflow job for this annotation

GitHub Actions / build / build

Wrong lexicographical order for 'com.refinedmods.refinedstorage.api.grid.view.GridResourceAttributeKey' import. Should be before 'com.refinedmods.refinedstorage.api.grid.view.GridResourceAttributeKeys'.
import com.refinedmods.refinedstorage.api.grid.view.GridResourceImpl;
Expand Down Expand Up @@ -30,7 +30,7 @@ class GridQueryParserImplTest {
private final GridQueryParser queryParser = new GridQueryParserImpl(
LexerTokenMappings.DEFAULT_MAPPINGS,
ParserOperatorMappings.DEFAULT_MAPPINGS,
FakeGridResourceAttributeKeys.UNARY_OPERATOR_TO_ATTRIBUTE_KEY_MAPPING
GridResourceAttributeKeys.UNARY_OPERATOR_TO_ATTRIBUTE_KEY_MAPPING
);

private final GridView view = new GridViewImpl(
Expand Down Expand Up @@ -297,9 +297,9 @@ private static class R implements GridResource {
this.name = name;
this.amount = amount;
this.attributes = Map.of(
FakeGridResourceAttributeKeys.MOD_ID, Set.of(modId),
FakeGridResourceAttributeKeys.MOD_NAME, Set.of(modName),
FakeGridResourceAttributeKeys.TAGS, tags
GridResourceAttributeKeys.MOD_ID, Set.of(modId),
GridResourceAttributeKeys.MOD_NAME, Set.of(modName),
GridResourceAttributeKeys.TAGS, tags
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Map;
import java.util.Set;

public enum FakeGridResourceAttributeKeys implements GridResourceAttributeKey {
public enum GridResourceAttributeKeys implements GridResourceAttributeKey {
MOD_ID,
MOD_NAME,
TAGS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public GridResourceImpl(final ResourceKey resource) {
public GridResourceImpl(final ResourceKey resource, final boolean autocraftable) {
this.resource = resource;
this.attributes = Map.of(
FakeGridResourceAttributeKeys.MOD_ID, Set.of(resource.toString()),
FakeGridResourceAttributeKeys.MOD_NAME, Set.of(resource.toString())
GridResourceAttributeKeys.MOD_ID, Set.of(resource.toString()),
GridResourceAttributeKeys.MOD_NAME, Set.of(resource.toString())
);
this.autocraftable = autocraftable;
}
Expand Down
Loading

0 comments on commit 784c172

Please sign in to comment.