Skip to content

Commit

Permalink
Merge pull request #425 from refinedmods/feat/GH-88/wireless-grid
Browse files Browse the repository at this point in the history
feat: wireless grid
  • Loading branch information
raoulvdberge authored Aug 24, 2023
2 parents 075debe + e2e7ca7 commit ad42d6a
Show file tree
Hide file tree
Showing 115 changed files with 1,908 additions and 621 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Wireless Grid
- Creative Wireless Grid

### Changed

- The Forge variant now targets NeoForge instead of Forge.
- You can now always open the Wireless Grid, even if there is no network bound or if the Wireless Grid is out of
energy.

### Fixed

- Fixed inactive Grid slots still rendering resources.
- Fixed being able to interact with inactive Grid.

## [2.0.0-milestone.2.14] - 2023-08-19

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.api.grid.service;
package com.refinedmods.refinedstorage2.api.grid.operations;

import org.apiguardian.api.API;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.api.grid.service;
package com.refinedmods.refinedstorage2.api.grid.operations;

import org.apiguardian.api.API;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.refinedmods.refinedstorage2.api.grid.service;
package com.refinedmods.refinedstorage2.api.grid.operations;

import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
import com.refinedmods.refinedstorage2.api.storage.InsertableStorage;

import org.apiguardian.api.API;

/**
* The grid service is the service that the grid uses to interact with the storage network.
* Grid operations, used for grids to interact with the storage network.
*
* @param <T> the resource type
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2")
public interface GridService<T> {
public interface GridOperations<T> {
/**
* Tries to move a resource from the network storage to the destination.
* The amount being extracted depends on the extraction mode.
Expand All @@ -20,7 +20,7 @@ public interface GridService<T> {
* @param extractMode the extract mode
* @param destination the destination
*/
void extract(T resource, GridExtractMode extractMode, InsertableStorage<T> destination);
boolean extract(T resource, GridExtractMode extractMode, InsertableStorage<T> destination);

/**
* Tries to move a resource from the source to the network storage.
Expand All @@ -30,5 +30,5 @@ public interface GridService<T> {
* @param insertMode the insertion mode
* @param source the source
*/
void insert(T resource, GridInsertMode insertMode, ExtractableStorage<T> source);
boolean insert(T resource, GridInsertMode insertMode, ExtractableStorage<T> source);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.api.grid.service;
package com.refinedmods.refinedstorage2.api.grid.operations;

import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.Actor;
Expand All @@ -12,36 +12,37 @@
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2")
public class GridServiceImpl<T> implements GridService<T> {
public class GridOperationsImpl<T> implements GridOperations<T> {
private final StorageChannel<T> storageChannel;
private final Actor actor;
private final ToLongFunction<T> maxAmountProvider;
private final long singleAmount;

/**
* @param storageChannel the storage channel to act on
* @param actor the actor performing the grid interactions
* @param actor the actor performing the grid operations
* @param maxAmountProvider provider for the maximum amount of a given resource
* @param singleAmount amount that needs to be extracted when using
* {@link GridInsertMode#SINGLE_RESOURCE} or {@link GridExtractMode#SINGLE_RESOURCE}
*/
public GridServiceImpl(final StorageChannel<T> storageChannel,
final Actor actor,
final ToLongFunction<T> maxAmountProvider,
final long singleAmount) {
public GridOperationsImpl(final StorageChannel<T> storageChannel,
final Actor actor,
final ToLongFunction<T> maxAmountProvider,
final long singleAmount) {
this.storageChannel = storageChannel;
this.actor = actor;
this.maxAmountProvider = maxAmountProvider;
this.singleAmount = singleAmount;
}

@Override
public void extract(final T resource, final GridExtractMode extractMode, final InsertableStorage<T> destination) {
public boolean extract(final T resource, final GridExtractMode extractMode,
final InsertableStorage<T> destination) {
final long amount = getExtractableAmount(resource, extractMode);
if (amount == 0) {
return;
return false;
}
TransferHelper.transfer(resource, amount, actor, storageChannel, destination, storageChannel);
return TransferHelper.transfer(resource, amount, actor, storageChannel, destination, storageChannel) > 0;
}

private long getExtractableAmount(final T resource, final GridExtractMode extractMode) {
Expand All @@ -65,11 +66,11 @@ private long adjustExtractableAmountAccordingToExtractMode(final GridExtractMode
}

@Override
public void insert(final T resource, final GridInsertMode insertMode, final ExtractableStorage<T> source) {
public boolean insert(final T resource, final GridInsertMode insertMode, final ExtractableStorage<T> source) {
final long amount = switch (insertMode) {
case ENTIRE_RESOURCE -> maxAmountProvider.applyAsLong(resource);
case SINGLE_RESOURCE -> singleAmount;
};
TransferHelper.transfer(resource, amount, actor, source, storageChannel, null);
return TransferHelper.transfer(resource, amount, actor, source, storageChannel, null) > 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@ParametersAreNonnullByDefault
@FieldsAndMethodsAreNonnullByDefault
package com.refinedmods.refinedstorage2.api.grid.service;
package com.refinedmods.refinedstorage2.api.grid.operations;

import com.refinedmods.refinedstorage2.api.core.FieldsAndMethodsAreNonnullByDefault;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.refinedmods.refinedstorage2.api.grid.service;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode;
import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode;
import com.refinedmods.refinedstorage2.api.grid.operations.GridOperationsImpl;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.EmptyActor;
Expand All @@ -19,16 +22,16 @@

import static org.assertj.core.api.Assertions.assertThat;

class GridServiceImplTest {
class GridOperationsImplTest {
private static final long MAX_COUNT = 15;

private StorageChannel<String> storageChannel;
private GridServiceImpl<String> sut;
private GridOperationsImpl<String> sut;

@BeforeEach
void setUp() {
storageChannel = new StorageChannelImpl<>();
sut = new GridServiceImpl<>(storageChannel, GridActor.INSTANCE, r -> MAX_COUNT, 1);
sut = new GridOperationsImpl<>(storageChannel, GridActor.INSTANCE, r -> MAX_COUNT, 1);
}

@Nested
Expand All @@ -44,9 +47,11 @@ void shouldInsertIntoDestination(final GridInsertMode insertMode) {
storageChannel.addSource(destination);

// Act
sut.insert("A", insertMode, source);
final boolean success = sut.insert("A", insertMode, source);

// Assert
assertThat(success).isTrue();

final long expectedAmount = switch (insertMode) {
case ENTIRE_RESOURCE -> MAX_COUNT;
case SINGLE_RESOURCE -> 1;
Expand Down Expand Up @@ -74,9 +79,10 @@ void shouldNotInsertIntoDestinationWhenResourceIsNotPresentInSource(final GridIn
storageChannel.addSource(destination);

// Act
sut.insert("A", insertMode, source);
final boolean success = sut.insert("A", insertMode, source);

// Assert
assertThat(success).isFalse();
assertThat(storageChannel.getAll()).isEmpty();
assertThat(source.getAll()).isEmpty();
assertThat(storageChannel.findTrackedResourceByActorType("A", GridActor.class)).isEmpty();
Expand All @@ -94,9 +100,10 @@ void shouldNotInsertIntoDestinationWhenNoSpaceIsPresentInDestination(final GridI
storageChannel.insert("A", 100, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.insert("A", insertMode, source);
final boolean success = sut.insert("A", insertMode, source);

// Assert
assertThat(success).isFalse();
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 100)
);
Expand All @@ -120,9 +127,10 @@ void shouldInsertIntoDestinationWithRemainder() {
storageChannel.insert("A", 100 - MAX_COUNT + 1, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.insert("A", GridInsertMode.ENTIRE_RESOURCE, source);
final boolean success = sut.insert("A", GridInsertMode.ENTIRE_RESOURCE, source);

// Assert
assertThat(success).isTrue();
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 100)
);
Expand Down Expand Up @@ -163,9 +171,10 @@ public long extract(final String resource,
storageChannel.insert("A", 100 - MAX_COUNT + 1, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.insert("A", GridInsertMode.ENTIRE_RESOURCE, source);
final boolean success = sut.insert("A", GridInsertMode.ENTIRE_RESOURCE, source);

// Assert
assertThat(success).isFalse();
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 100 - MAX_COUNT + 1)
);
Expand All @@ -189,9 +198,11 @@ void shouldExtractFromSourceToDestination(final GridExtractMode extractMode) {
storageChannel.insert("A", 100, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.extract("A", extractMode, destination);
final boolean success = sut.extract("A", extractMode, destination);

// Assert
assertThat(success).isTrue();

final long expectedExtracted = switch (extractMode) {
case ENTIRE_RESOURCE -> MAX_COUNT;
case HALF_RESOURCE -> MAX_COUNT / 2;
Expand Down Expand Up @@ -220,9 +231,10 @@ void shouldNotExtractFromSourceWhenResourceIsNotPresentInSource(final GridExtrac
storageChannel.addSource(source);

// Act
sut.extract("A", extractMode, destination);
final boolean success = sut.extract("A", extractMode, destination);

// Assert
assertThat(success).isFalse();
assertThat(storageChannel.getAll()).isEmpty();
assertThat(destination.getAll()).isEmpty();
assertThat(storageChannel.findTrackedResourceByActorType("A", GridActor.class)).isNotPresent();
Expand All @@ -240,9 +252,10 @@ void shouldNotExtractFromSourceIfThereIsNoSpaceInDestination(final GridExtractMo
storageChannel.insert("A", 100, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.extract("A", extractMode, destination);
final boolean success = sut.extract("A", extractMode, destination);

// Assert
assertThat(success).isFalse();
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 100)
);
Expand All @@ -265,9 +278,10 @@ void shouldExtractEntireResourceFromSourceToDestinationIfResourceIsLessThanMaxCo
storageChannel.insert("A", MAX_COUNT - 1, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.extract("A", GridExtractMode.ENTIRE_RESOURCE, destination);
final boolean success = sut.extract("A", GridExtractMode.ENTIRE_RESOURCE, destination);

// Assert
assertThat(success).isTrue();
assertThat(storageChannel.getAll()).isEmpty();
assertThat(destination.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", MAX_COUNT - 1)
Expand All @@ -288,9 +302,10 @@ void shouldExtractEntireResourceWithRemainderFromSourceToDestinationIfThereIsNot
storageChannel.insert("A", 100, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.extract("A", GridExtractMode.ENTIRE_RESOURCE, destination);
final boolean success = sut.extract("A", GridExtractMode.ENTIRE_RESOURCE, destination);

// Assert
assertThat(success).isTrue();
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 100 - MAX_COUNT + 1)
);
Expand All @@ -316,9 +331,10 @@ void shouldExtractSingleAmountIfResourceHasSingleAmountWhenExtractingHalfResourc
storageChannel.insert("A", 1, Action.EXECUTE, EmptyActor.INSTANCE);

// Act
sut.extract("A", GridExtractMode.HALF_RESOURCE, destination);
final boolean success = sut.extract("A", GridExtractMode.HALF_RESOURCE, destination);

// Assert
assertThat(success).isTrue();
assertThat(storageChannel.getAll()).isEmpty();
assertThat(destination.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 1)
Expand Down
1 change: 1 addition & 0 deletions refinedstorage2-network-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies {
implementation project(':refinedstorage2-core-api')
implementation project(':refinedstorage2-resource-api')
implementation project(':refinedstorage2-storage-api')
implementation project(':refinedstorage2-grid-api')
}

enableJavadoc()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.refinedmods.refinedstorage2.api.network.component;

import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;

import java.util.List;
import java.util.function.Predicate;

import org.apiguardian.api.API;
Expand All @@ -13,4 +16,6 @@ public interface StorageNetworkComponent extends NetworkComponent {
<T> StorageChannel<T> getStorageChannel(StorageChannelType<T> type);

boolean hasSource(Predicate<Storage<?>> matcher);

<T> List<TrackedResourceAmount<T>> getResources(StorageChannelType<T> type, Class<? extends Actor> actorType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.component.StorageProvider;
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -84,4 +87,14 @@ public boolean hasSource(final Predicate<Storage<?>> matcher) {
}
return false;
}

@Override
public <T> List<TrackedResourceAmount<T>> getResources(final StorageChannelType<T> type,
final Class<? extends Actor> actorType) {
final StorageChannel<T> storageChannel = getStorageChannel(type);
return storageChannel.getAll().stream().map(resourceAmount -> new TrackedResourceAmount<>(
resourceAmount,
storageChannel.findTrackedResourceByActorType(resourceAmount.getResource(), actorType).orElse(null)
)).toList();
}
}
Loading

0 comments on commit ad42d6a

Please sign in to comment.