-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de9f7b0
commit 3f5133e
Showing
15 changed files
with
954 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...finedmods/refinedstorage2/network/test/nodefactory/StorageTransferNetworkNodeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.refinedmods.refinedstorage2.network.test.nodefactory; | ||
|
||
import com.refinedmods.refinedstorage2.api.network.impl.node.AbstractNetworkNode; | ||
import com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer.StorageTransferNetworkNode; | ||
import com.refinedmods.refinedstorage2.network.test.AddNetworkNode; | ||
|
||
import java.util.Map; | ||
|
||
public class StorageTransferNetworkNodeFactory extends AbstractNetworkNodeFactory { | ||
public static final String PROPERTY_ENERGY_USAGE_PER_STORAGE = "energy_usage_per_storage"; | ||
public static final String PROPERTY_SIZE = "size"; | ||
|
||
@Override | ||
protected AbstractNetworkNode innerCreate(final AddNetworkNode ctx, final Map<String, Object> properties) { | ||
final long energyUsagePerStorage = (long) properties.getOrDefault(PROPERTY_ENERGY_USAGE_PER_STORAGE, 0L); | ||
final int size = (int) properties.getOrDefault(PROPERTY_SIZE, 6); | ||
return new StorageTransferNetworkNode( | ||
getEnergyUsage(properties), | ||
energyUsagePerStorage, | ||
size | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...edmods/refinedstorage2/api/network/impl/node/storagetransfer/StorageTransferListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer; | ||
|
||
@FunctionalInterface | ||
public interface StorageTransferListener { | ||
void onTransferReady(int index); | ||
} |
6 changes: 6 additions & 0 deletions
6
...efinedmods/refinedstorage2/api/network/impl/node/storagetransfer/StorageTransferMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer; | ||
|
||
public enum StorageTransferMode { | ||
INSERT, | ||
EXTRACT | ||
} |
158 changes: 158 additions & 0 deletions
158
...ods/refinedstorage2/api/network/impl/node/storagetransfer/StorageTransferNetworkNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer; | ||
|
||
import com.refinedmods.refinedstorage2.api.network.impl.node.AbstractStorageContainerNetworkNode; | ||
import com.refinedmods.refinedstorage2.api.network.node.NetworkNodeActor; | ||
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent; | ||
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; | ||
import com.refinedmods.refinedstorage2.api.resource.ResourceKey; | ||
import com.refinedmods.refinedstorage2.api.resource.filter.Filter; | ||
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode; | ||
import com.refinedmods.refinedstorage2.api.storage.Actor; | ||
import com.refinedmods.refinedstorage2.api.storage.Storage; | ||
import com.refinedmods.refinedstorage2.api.storage.TransferHelper; | ||
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorage; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.function.ToLongFunction; | ||
import java.util.function.UnaryOperator; | ||
import javax.annotation.Nullable; | ||
|
||
public class StorageTransferNetworkNode extends AbstractStorageContainerNetworkNode { | ||
private final Filter filter = new Filter(); | ||
private final Actor actor = new NetworkNodeActor(this); | ||
|
||
private StorageTransferMode mode = StorageTransferMode.INSERT; | ||
@Nullable | ||
private ToLongFunction<Storage> transferQuotaProvider; | ||
@Nullable | ||
private StorageTransferListener listener; | ||
|
||
public StorageTransferNetworkNode(final long energyUsage, final long energyUsagePerStorage, final int size) { | ||
super(energyUsage, energyUsagePerStorage, size); | ||
} | ||
|
||
public void setMode(final StorageTransferMode mode) { | ||
this.mode = mode; | ||
} | ||
|
||
public void setFilters(final Set<ResourceKey> filters) { | ||
filter.setFilters(filters); | ||
} | ||
|
||
public void setFilterMode(final FilterMode filterMode) { | ||
filter.setMode(filterMode); | ||
} | ||
|
||
public void setNormalizer(final UnaryOperator<ResourceKey> normalizer) { | ||
filter.setNormalizer(normalizer); | ||
} | ||
|
||
public void setTransferQuotaProvider(final ToLongFunction<Storage> transferQuotaProvider) { | ||
this.transferQuotaProvider = transferQuotaProvider; | ||
} | ||
|
||
public void setListener(@Nullable final StorageTransferListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public void doWork() { | ||
super.doWork(); | ||
if (!isActive() || network == null) { | ||
return; | ||
} | ||
final StorageNetworkComponent networkStorage = network.getComponent(StorageNetworkComponent.class); | ||
for (int i = 0; i < storages.length / 2; ++i) { | ||
final Storage storage = storages[i]; | ||
if (storage == null) { | ||
continue; | ||
} | ||
final Result result = transfer(storage, networkStorage); | ||
if (processResult(result, i)) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
private Result transfer(final Storage storage, final StorageNetworkComponent networkStorage) { | ||
if (transferQuotaProvider == null) { | ||
return Result.FAILURE; | ||
} | ||
final long transferQuota = transferQuotaProvider.applyAsLong(storage); | ||
if (mode == StorageTransferMode.INSERT) { | ||
return transfer(storage, networkStorage, transferQuota, this::hasNoExtractableResources); | ||
} | ||
return transfer( | ||
networkStorage, | ||
storage, | ||
transferQuota, | ||
source -> hasNoExtractableResources(source) || storageIsFull(storage) | ||
); | ||
} | ||
|
||
private Result transfer(final Storage source, | ||
final Storage destination, | ||
final long transferQuota, | ||
final Predicate<Storage> readyPredicate) { | ||
if (readyPredicate.test(source)) { | ||
return Result.SUCCESS; | ||
} | ||
if (transfer(source, destination, transferQuota)) { | ||
return readyPredicate.test(source) | ||
? Result.SUCCESS | ||
: Result.PARTIAL; | ||
} | ||
return Result.FAILURE; | ||
} | ||
|
||
private boolean transfer(final Storage source, final Storage destination, final long transferQuota) { | ||
long remainder = transferQuota; | ||
final Collection<ResourceAmount> all = new LinkedHashSet<>(source.getAll()); | ||
for (final ResourceAmount resourceAmount : all) { | ||
final ResourceKey resource = resourceAmount.getResource(); | ||
if (!filter.isAllowed(resource)) { | ||
continue; | ||
} | ||
final long amount = Math.min(remainder, resourceAmount.getAmount()); | ||
final long transferred = TransferHelper.transfer(resource, amount, actor, source, destination, source); | ||
remainder -= transferred; | ||
if (remainder == 0) { | ||
break; | ||
} | ||
} | ||
return remainder != transferQuota; | ||
} | ||
|
||
private boolean hasNoExtractableResources(final Storage source) { | ||
return source.getAll().stream().noneMatch(resourceAmount -> filter.isAllowed(resourceAmount.getResource())); | ||
} | ||
|
||
private boolean storageIsFull(final Storage storage) { | ||
return storage instanceof LimitedStorage limitedStorage | ||
&& limitedStorage.getCapacity() > 0 | ||
&& limitedStorage.getCapacity() == limitedStorage.getStored(); | ||
} | ||
|
||
private boolean processResult(final Result result, final int index) { | ||
if (result.isSuccess()) { | ||
if (result == Result.SUCCESS && listener != null) { | ||
listener.onTransferReady(index); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private enum Result { | ||
SUCCESS, | ||
PARTIAL, | ||
FAILURE; | ||
|
||
private boolean isSuccess() { | ||
return this == PARTIAL || this == SUCCESS; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...a/com/refinedmods/refinedstorage2/api/network/impl/node/storagetransfer/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@FieldsAndMethodsAreNonnullByDefault | ||
package com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer; | ||
|
||
import com.refinedmods.refinedstorage2.api.core.FieldsAndMethodsAreNonnullByDefault; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
5 changes: 2 additions & 3 deletions
5
...orage/StorageNetworkNodeProviderImpl.java → ...2/api/network/impl/node/ProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.