Skip to content

Commit

Permalink
Merge pull request #411 from refinedmods/feat/GH-228/exposed-fluid-in…
Browse files Browse the repository at this point in the history
…terface

Exposed fluid interface
  • Loading branch information
raoulvdberge authored Aug 16, 2023
2 parents cde7fa9 + 74988a6 commit 5e0b16c
Show file tree
Hide file tree
Showing 44 changed files with 592 additions and 185 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Fixed

- Fixed filter slot hints not being aware of the resource types that they can show in a slot.
- Fixed Exporter only exporting 1 mB per cycle on Forge.

## [2.0.0-milestone.2.12] - 2023-08-06

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ <A, B> boolean isExportedResourceValid(ResourceTemplate<A> want,
void growExportedAmount(int slotIndex, long amount);

<T> long insert(StorageChannelType<T> storageChannelType, T resource, long amount, Action action);

<T> long extract(T resource, long amount, Action action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,7 @@ public long extract(final T resource, final long amount, final Action action, fi
if (exportState == null) {
return 0;
}
return doExtract(resource, amount, action, exportState);
}

private long doExtract(final T resource,
final long amount,
final Action action,
final InterfaceExportState exportState) {
long extracted = 0;
for (int i = 0; i < exportState.getSlots(); ++i) {
final ResourceTemplate<?> exportedResource = exportState.getExportedResource(i);
if (exportedResource == null || !resource.equals(exportedResource.resource())) {
continue;
}
final long stillNeeded = amount - extracted;
final long toExtract = Math.min(
exportState.getExportedAmount(i),
stillNeeded
);
if (action == Action.EXECUTE) {
exportState.shrinkExportedAmount(i, stillNeeded);
}
extracted += toExtract;
}
return extracted;
return exportState.extract(resource, amount, action);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
public class InterfaceExportStateImpl implements InterfaceExportState {
private final Map<Integer, ResourceAmount<String>> requested = new HashMap<>();
private final Map<Integer, ResourceAmount<String>> current = new HashMap<>();

private final int slots;

public InterfaceExportStateImpl(final int slots) {
Expand Down Expand Up @@ -61,10 +60,7 @@ private Collection<String> expandExportCandidates(final StorageChannel<String> s
}

@Override
public <A, B> boolean isExportedResourceValid(
final ResourceTemplate<A> want,
final ResourceTemplate<B> got
) {
public <A, B> boolean isExportedResourceValid(final ResourceTemplate<A> want, final ResourceTemplate<B> got) {
if ("A".equals(want.resource())) {
return ((String) got.resource()).startsWith("A");
}
Expand Down Expand Up @@ -166,4 +162,23 @@ public <T> long insert(final StorageChannelType<T> storageChannelType,
}
return 0;
}

@Override
public <T> long extract(final T resource, final long amount, final Action action) {
long extracted = 0;
for (int i = 0; i < getSlots(); ++i) {
final ResourceTemplate<?> slot = getExportedResource(i);
if (slot != null && slot.resource().equals(resource)) {
final long maxAmount = Math.min(getExportedAmount(i), amount - extracted);
extracted += maxAmount;
if (action == Action.EXECUTE) {
shrinkExportedAmount(i, maxAmount);
}
if (extracted == amount) {
break;
}
}
}
return extracted;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.refinedmods.refinedstorage2.platform.api.resource;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.storage.ResourceTemplate;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;

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

import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import org.apiguardian.api.API;

/**
* Represents a {@link Container} that can hold any resource type.
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.13")
public interface ResourceContainer {
ResourceContainerType getType();

void setListener(@Nullable Runnable listener);

void change(int index, ItemStack stack, boolean tryAlternatives);

<T> void set(int index, ResourceAmountTemplate<T> resourceAmount);

long getAmount(int index);

void grow(int index, long amount);

void shrink(int index, long amount);

void setAmount(int index, long amount);

<T> long getMaxAmount(ResourceAmountTemplate<T> resourceAmount);

void remove(int index);

int size();

@Nullable
ResourceAmountTemplate<?> get(int index);

Set<Object> getUniqueTemplates();

List<ResourceTemplate<?>> getTemplates();

void writeToUpdatePacket(FriendlyByteBuf buf);

void readFromUpdatePacket(int index, FriendlyByteBuf buf);

CompoundTag toTag();

void fromTag(CompoundTag tag);

List<ClientTooltipComponent> getHelpTooltip(ItemStack carried);

Container toItemContainer();

<T> long insert(StorageChannelType<T> storageChannelType, T resource, long amount, Action action);

<T> long extract(T resource, long amount, Action action);

ResourceContainer copy();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.platform.common.internal.resource;
package com.refinedmods.refinedstorage2.platform.api.resource;

public enum ResourceContainerType {
FILTER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ public interface PlatformStorageChannelType<T> extends StorageChannelType<T> {

double getDisplayAmount(long amount);

long getInterfaceExportLimit(T resource);
long getInterfaceExportLimit();

default long getInterfaceExportLimit(T resource) {
return getInterfaceExportLimit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.refinedmods.refinedstorage2.api.network.node.AbstractNetworkNode;
import com.refinedmods.refinedstorage2.api.network.node.task.TaskExecutor;
import com.refinedmods.refinedstorage2.api.storage.ResourceTemplate;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;

Expand Down Expand Up @@ -34,7 +34,7 @@ protected AbstractSchedulingNetworkNodeContainerBlockEntity(
super(type, pos, state, node, destination);
this.schedulingMode = new SchedulingMode<>(this::setChanged, this::setTaskExecutor);
this.filter = FilterWithFuzzyMode.createAndListenForTemplates(
ResourceContainer.createForFilter(),
ResourceContainerImpl.createForFilter(),
this::setChanged,
templates -> setFilterTemplates(
templates.stream().map(ResourceTemplate::resource).collect(Collectors.toList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.refinedmods.refinedstorage2.api.storage.ResourceTemplate;
import com.refinedmods.refinedstorage2.platform.api.resource.FuzzyModeNormalizer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.api.resource.ResourceContainer;

import java.util.List;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.refinedmods.refinedstorage2.platform.common.containermenu.ImporterContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.Items;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;

Expand Down Expand Up @@ -53,7 +53,7 @@ public ImporterBlockEntity(final BlockPos pos, final BlockState state) {
UpgradeDestinations.IMPORTER
);
this.filter = FilterWithFuzzyMode.createAndListenForUniqueTemplates(
ResourceContainer.createForFilter(),
ResourceContainerImpl.createForFilter(),
this::setChanged,
templates -> getNode().setFilterTemplates(templates)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.refinedmods.refinedstorage2.platform.common.block.entity.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.containermenu.DestructorContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.internal.upgrade.UpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;

Expand Down Expand Up @@ -56,7 +56,7 @@ public DestructorBlockEntity(final BlockPos pos, final BlockState state) {
);
this.actor = new NetworkNodeActor(getNode());
this.filterWithFuzzyMode = FilterWithFuzzyMode.createAndListenForUniqueTemplates(
ResourceContainer.createForFilter(),
ResourceContainerImpl.createForFilter(),
this::setChanged,
filter::setTemplates
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.detector.DetectorMode;
import com.refinedmods.refinedstorage2.api.network.impl.node.detector.DetectorNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.resource.ResourceAmountTemplate;
import com.refinedmods.refinedstorage2.platform.api.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.block.DetectorBlock;
import com.refinedmods.refinedstorage2.platform.common.block.entity.AbstractInternalNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.block.entity.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.containermenu.AbstractSingleAmountContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.containermenu.detector.DetectorContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -50,7 +51,7 @@ public DetectorBlockEntity(final BlockPos pos, final BlockState state) {
super(BlockEntities.INSTANCE.getDetector(), pos, state, new DetectorNetworkNode(
Platform.INSTANCE.getConfig().getDetector().getEnergyUsage()
));
final ResourceContainer resourceContainer = ResourceContainer.createForFilter(1);
final ResourceContainer resourceContainer = ResourceContainerImpl.createForFilter(1);
this.filter = FilterWithFuzzyMode.createAndListenForTemplates(
resourceContainer,
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.refinedmods.refinedstorage2.platform.common.containermenu.storage.diskdrive.DiskDriveContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.containermenu.storage.diskdrive.EmptyStorageDiskInfoAccessor;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.util.ContainerUtil;
import com.refinedmods.refinedstorage2.platform.common.util.LevelUtil;
Expand Down Expand Up @@ -77,7 +77,7 @@ protected AbstractDiskDriveBlockEntity(final BlockPos pos, final BlockState stat
));
this.diskInventory = new DiskDriveInventory(this, getNode().getSize());
this.filter = FilterWithFuzzyMode.createAndListenForUniqueTemplates(
ResourceContainer.createForFilter(),
ResourceContainerImpl.createForFilter(),
this::setChanged,
templates -> getNode().setFilterTemplates(templates)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.refinedmods.refinedstorage2.platform.common.block.entity.StorageConfigurationContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.containermenu.storage.ExternalStorageContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider;

import java.util.Optional;
Expand Down Expand Up @@ -52,7 +52,7 @@ public ExternalStorageBlockEntity(final BlockPos pos, final BlockState state) {
Platform.INSTANCE.getConfig().getExternalStorage().getEnergyUsage()
));
this.filter = FilterWithFuzzyMode.createAndListenForUniqueTemplates(
ResourceContainer.createForFilter(),
ResourceContainerImpl.createForFilter(),
this::setChanged,
templates -> getNode().setFilterTemplates(templates)
);
Expand Down
Loading

0 comments on commit 5e0b16c

Please sign in to comment.