-
-
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.
refactor: introduce common storage container network node
- Loading branch information
1 parent
ac9755c
commit 96841ec
Showing
5 changed files
with
134 additions
and
108 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
...efinedmods/refinedstorage2/api/network/impl/node/AbstractStorageContainerNetworkNode.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,119 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.node; | ||
|
||
import com.refinedmods.refinedstorage2.api.network.impl.storage.AbstractStorageNetworkNode; | ||
import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; | ||
import com.refinedmods.refinedstorage2.api.storage.Storage; | ||
import com.refinedmods.refinedstorage2.api.storage.StorageState; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import javax.annotation.Nullable; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class AbstractStorageContainerNetworkNode extends AbstractStorageNetworkNode { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStorageContainerNetworkNode.class); | ||
|
||
protected final StateTrackedStorage[] storages; | ||
|
||
private final long energyUsage; | ||
private final long energyUsagePerStorage; | ||
|
||
@Nullable | ||
private Provider provider; | ||
@Nullable | ||
private StateTrackedStorage.Listener listener; | ||
private int activeStorages; | ||
|
||
protected AbstractStorageContainerNetworkNode(final long energyUsage, | ||
final long energyUsagePerStorage, | ||
final int size) { | ||
this.energyUsage = energyUsage; | ||
this.energyUsagePerStorage = energyUsagePerStorage; | ||
this.storages = new StateTrackedStorage[size]; | ||
} | ||
|
||
public void setListener(final StateTrackedStorage.Listener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
public void setProvider(final Provider provider) { | ||
this.provider = provider; | ||
final List<StorageChange> changes = new ArrayList<>(); | ||
for (int i = 0; i < storages.length; ++i) { | ||
changes.addAll(initializeStorage(i)); | ||
} | ||
// If we are already initialized, update all the storages to keep the exposed storages in sync. | ||
// If we are not initialized, update nothing as we have to wait for an activeness update. | ||
if (activeStorages > 0) { | ||
changes.forEach(this::onStorageChange); | ||
} | ||
updateActiveStorageCount(); | ||
} | ||
|
||
public void onStorageChanged(final int index) { | ||
if (index < 0 || index >= storages.length) { | ||
LOGGER.warn("Invalid index {}", index); | ||
return; | ||
} | ||
initializeStorage(index).forEach(this::onStorageChange); | ||
updateActiveStorageCount(); | ||
} | ||
|
||
protected void onStorageChange(final StorageChange change) { | ||
// no op | ||
} | ||
|
||
private Set<StorageChange> initializeStorage(final int index) { | ||
final Set<StorageChange> results = new HashSet<>(); | ||
if (storages[index] != null) { | ||
results.add(new StorageChange(true, storages[index])); | ||
} | ||
if (provider != null) { | ||
provider.resolve(index).ifPresentOrElse(resolved -> { | ||
final StateTrackedStorage newStorage = new StateTrackedStorage(resolved, listener); | ||
storages[index] = newStorage; | ||
results.add(new StorageChange(false, newStorage)); | ||
}, () -> storages[index] = null); | ||
} | ||
return results; | ||
} | ||
|
||
private void updateActiveStorageCount() { | ||
this.activeStorages = (int) Arrays.stream(storages).filter(Objects::nonNull).count(); | ||
} | ||
|
||
@Override | ||
public long getEnergyUsage() { | ||
return energyUsage + (energyUsagePerStorage * activeStorages); | ||
} | ||
|
||
public int getSize() { | ||
return storages.length; | ||
} | ||
|
||
public StorageState getState(final int index) { | ||
final var storage = storages[index]; | ||
if (storage == null) { | ||
return StorageState.NONE; | ||
} | ||
if (!isActive()) { | ||
return StorageState.INACTIVE; | ||
} | ||
return storage.getState(); | ||
} | ||
|
||
protected record StorageChange(boolean removed, StateTrackedStorage storage) { | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Provider { | ||
Optional<Storage> resolve(int index); | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
...nedmods/refinedstorage2/api/network/impl/node/storage/StorageNetworkNodeProviderImpl.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