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 10, 2024
1 parent 30d00dc commit ef38226
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 57 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
@@ -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
Expand Up @@ -52,7 +52,7 @@ public class ConfigImpl implements Config {
private final SimpleEnergyUsageEntry fallbackSecurityCard;
private final SimpleEnergyUsageEntry securityManager;
private final RelayEntry relay;
private final SimpleEnergyUsageEntry autocrafter;
private final AutocrafterEntryImpl autocrafter;

public ConfigImpl() {
screenSize = builder
Expand Down Expand Up @@ -97,7 +97,7 @@ public ConfigImpl() {
);
securityManager = new SimpleEnergyUsageEntryImpl("securityManager", DefaultEnergyUsage.SECURITY_MANAGER);
relay = new RelayEntryImpl();
autocrafter = new SimpleEnergyUsageEntryImpl("autocrafter", DefaultEnergyUsage.AUTOCRAFTER);
autocrafter = new AutocrafterEntryImpl();
spec = builder.build();
}

Expand Down Expand Up @@ -264,7 +264,7 @@ public RelayEntry getRelay() {
}

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

Expand Down Expand Up @@ -925,4 +925,30 @@ public long getOutputNetworkEnergyUsage() {
return outputNetworkEnergyUsage.get();
}
}

private class AutocrafterEntryImpl implements AutocrafterEntry {
private final ModConfigSpec.LongValue energyUsage;
private final ModConfigSpec.LongValue energyUsagePerPattern;

AutocrafterEntryImpl() {
builder.translation(translationKey("autocrafter")).push("autocrafter");
energyUsage = builder
.translation(translationKey("autocrafter." + ENERGY_USAGE))
.defineInRange(ENERGY_USAGE, DefaultEnergyUsage.AUTOCRAFTER, 0, Long.MAX_VALUE);
energyUsagePerPattern = builder
.translation(translationKey("autocrafter.energyUsagePerPattern"))
.defineInRange("energyUsagePerPattern", DefaultEnergyUsage.AUTOCRAFTER_PER_PATTERN, 0, Long.MAX_VALUE);
builder.pop();
}

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

@Override
public long getEnergyUsage() {
return energyUsage.get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import com.refinedmods.refinedstorage.api.network.autocrafting.PatternProvider;
import com.refinedmods.refinedstorage.api.network.impl.node.SimpleNetworkNode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

public class PatternProviderNetworkNode extends SimpleNetworkNode implements PatternProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
package com.refinedmods.refinedstorage.api.network.impl.node.relay;

public enum RelayComponentType {
ENERGY,
SECURITY,
STORAGE,
AUTOCRAFTING
import com.refinedmods.refinedstorage.api.core.NullableType;
import com.refinedmods.refinedstorage.api.network.Network;
import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent;
import com.refinedmods.refinedstorage.api.network.energy.EnergyNetworkComponent;
import com.refinedmods.refinedstorage.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent;

import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

public class RelayComponentType<T> {
public static final RelayComponentType<EnergyNetworkComponent> ENERGY = new RelayComponentType<>(
network -> network.getComponent(EnergyNetworkComponent.class),
output -> output::setEnergyDelegate
);
public static final RelayComponentType<SecurityNetworkComponent> SECURITY = new RelayComponentType<>(
network -> network.getComponent(SecurityNetworkComponent.class),
output -> output::setSecurityDelegate
);
public static final RelayComponentType<StorageNetworkComponent> STORAGE = new RelayComponentType<>(
network -> network.getComponent(StorageNetworkComponent.class),
output -> output::setStorageDelegate
);
public static final RelayComponentType<AutocraftingNetworkComponent> AUTOCRAFTING = new RelayComponentType<>(
network -> network.getComponent(AutocraftingNetworkComponent.class),
output -> output::setAutocraftingDelegate
);
public static final Set<RelayComponentType<?>> ALL = Set.of(ENERGY, SECURITY, STORAGE, AUTOCRAFTING);

private final Function<Network, T> componentProvider;
private final Function<RelayOutputNetworkNode, Consumer<@NullableType T>> componentApplier;

private RelayComponentType(final Function<Network, T> componentProvider,
final Function<RelayOutputNetworkNode, Consumer<@NullableType T>> componentApplier) {
this.componentProvider = componentProvider;
this.componentApplier = componentApplier;
}

void apply(final Network network, final RelayOutputNetworkNode output) {
final T component = componentProvider.apply(network);
componentApplier.apply(output).accept(component);
}

void remove(final RelayOutputNetworkNode output) {
componentApplier.apply(output).accept(null);
}
}
Loading

0 comments on commit ef38226

Please sign in to comment.