Skip to content

Commit

Permalink
feat: portable grid menu
Browse files Browse the repository at this point in the history
Generifies disk inventory and extracts the internal multistorage as a "StateTrackedStorage".
The storage channel type component of the internal multistorage has been moved into TypedStorage, as
it's not necessary for the portable grid or StateTrackedStorage.
  • Loading branch information
raoulvdberge committed Dec 30, 2023
1 parent bc8bca8 commit ecbb220
Show file tree
Hide file tree
Showing 44 changed files with 628 additions and 322 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import java.util.List;
import java.util.Optional;

class MultiStorageExposedStorage<T> extends AbstractImmutableConfiguredProxyStorage<T, CompositeStorageImpl<T>>
class ExposedStorage<T> extends AbstractImmutableConfiguredProxyStorage<T, CompositeStorageImpl<T>>
implements CompositeStorage<T>, CompositeAwareChild<T> {
protected MultiStorageExposedStorage(final StorageConfiguration config) {
protected ExposedStorage(final StorageConfiguration config) {
super(config, new CompositeStorageImpl<>(new ResourceListImpl<>()));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.refinedmods.refinedstorage2.api.network.impl.node.multistorage;

import com.refinedmods.refinedstorage2.api.network.component.StorageProvider;
import com.refinedmods.refinedstorage2.api.network.impl.node.StorageState;
import com.refinedmods.refinedstorage2.api.network.node.AbstractStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageState;
import com.refinedmods.refinedstorage2.api.storage.TypedStorage;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;

import java.util.ArrayList;
Expand All @@ -28,13 +30,13 @@ public class MultiStorageNetworkNode extends AbstractStorageNetworkNode implemen
@Nullable
private MultiStorageProvider provider;
@Nullable
private MultiStorageListener listener;
private StateTrackedStorage.Listener listener;

private final long energyUsage;
private final long energyUsagePerStorage;

private final MultiStorageInternalStorage<?>[] cache;
private final Map<StorageChannelType<?>, MultiStorageExposedStorage<?>> exposedStorages;
private final TypedStorage<?, StateTrackedStorage<?>>[] cache;
private final Map<StorageChannelType<?>, ExposedStorage<?>> exposedStorages;
private int activeStorages;

public MultiStorageNetworkNode(final long energyUsage,
Expand All @@ -44,10 +46,10 @@ public MultiStorageNetworkNode(final long energyUsage,
this.energyUsage = energyUsage;
this.energyUsagePerStorage = energyUsagePerStorage;
this.exposedStorages = createExposedStorages(storageChannelTypes);
this.cache = new MultiStorageInternalStorage[size];
this.cache = new TypedStorage[size];
}

private Map<StorageChannelType<?>, MultiStorageExposedStorage<?>> createExposedStorages(
private Map<StorageChannelType<?>, ExposedStorage<?>> createExposedStorages(
final Collection<? extends StorageChannelType<?>> storageChannelTypes
) {
return storageChannelTypes.stream().collect(Collectors.toUnmodifiableMap(
Expand All @@ -56,8 +58,8 @@ private Map<StorageChannelType<?>, MultiStorageExposedStorage<?>> createExposedS
));
}

private MultiStorageExposedStorage<?> createExposedStorage(final StorageChannelType<?> type) {
return new MultiStorageExposedStorage<>(this);
private ExposedStorage<?> createExposedStorage(final StorageChannelType<?> type) {
return new ExposedStorage<>(this);
}

public void setProvider(final MultiStorageProvider provider) {
Expand Down Expand Up @@ -86,29 +88,31 @@ public void onStorageChanged(final int index) {
@SuppressWarnings({"rawtypes", "unchecked"})
private Set<StorageChange> initializeStorage(final int index) {
final Set<StorageChange> results = new HashSet<>();

if (cache[index] != null) {
final StorageChannelType<?> removedType = cache[index].getStorageChannelType();
final MultiStorageExposedStorage<?> relevantComposite = exposedStorages.get(removedType);
results.add(new StorageChange(true, relevantComposite, cache[index]));
final StorageChannelType<?> removedType = cache[index].storageChannelType();
final ExposedStorage<?> relevantComposite = exposedStorages.get(removedType);
results.add(new StorageChange(true, relevantComposite, cache[index].storage()));
}

if (provider != null) {
provider.resolve(index).ifPresentOrElse(resolved -> {
cache[index] = new MultiStorageInternalStorage(
resolved.storage(),
resolved.storageChannelType(),
listener
);
final MultiStorageExposedStorage<?> relevantComposite = exposedStorages.get(
resolved.storageChannelType()
);
results.add(new StorageChange(false, relevantComposite, cache[index]));
cache[index] = (TypedStorage) trackState(resolved);
final ExposedStorage<?> relevantComposite = exposedStorages.get(resolved.storageChannelType());
results.add(new StorageChange(false, relevantComposite, cache[index].storage()));
}, () -> cache[index] = null);
}

return results;
}

private <T> TypedStorage<T, StateTrackedStorage<T>> trackState(final TypedStorage<T, Storage<T>> resolved) {
return new TypedStorage<>(
new StateTrackedStorage<>(resolved.storage(), listener),
resolved.storageChannelType()
);
}

@SuppressWarnings({"unchecked", "rawtypes"})
private void processStorageChange(final StorageChange change) {
if (!isActive()) {
Expand Down Expand Up @@ -145,19 +149,19 @@ private void enableAllStorages() {

@SuppressWarnings({"unchecked", "rawtypes"})
private void enableAllStoragesForChannel(final StorageChannelType<?> type,
final MultiStorageExposedStorage<?> exposedStorage) {
for (final MultiStorageInternalStorage<?> internalStorage : cache) {
if (internalStorage != null && internalStorage.getStorageChannelType() == type) {
exposedStorage.addSource((MultiStorageInternalStorage) internalStorage);
final ExposedStorage<?> exposedStorage) {
for (final TypedStorage<?, StateTrackedStorage<?>> internalStorage : cache) {
if (internalStorage != null && internalStorage.storageChannelType() == type) {
exposedStorage.addSource((StateTrackedStorage) internalStorage.storage());
}
}
}

private void disableAllStorages() {
exposedStorages.values().forEach(MultiStorageExposedStorage::clearSources);
exposedStorages.values().forEach(ExposedStorage::clearSources);
}

public void setListener(final MultiStorageListener listener) {
public void setListener(final StateTrackedStorage.Listener listener) {
this.listener = listener;
}

Expand All @@ -178,7 +182,7 @@ public StorageState getState(final int index) {
if (!isActive()) {
return StorageState.INACTIVE;
}
return storage.getState();
return storage.storage().getState();
}

@Override
Expand All @@ -189,15 +193,15 @@ protected Set<? extends StorageChannelType<?>> getRelevantStorageChannelTypes()
@Override
@SuppressWarnings("unchecked")
public <T> Optional<Storage<T>> getStorageForChannel(final StorageChannelType<T> channelType) {
final MultiStorageExposedStorage<?> storage = exposedStorages.get(channelType);
final ExposedStorage<?> storage = exposedStorages.get(channelType);
if (storage != null) {
return Optional.of((Storage<T>) storage);
}
return Optional.empty();
}

private record StorageChange(boolean removed,
MultiStorageExposedStorage<?> exposedStorage,
MultiStorageInternalStorage<?> internalStorage) {
ExposedStorage<?> exposedStorage,
StateTrackedStorage<?> internalStorage) {
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.refinedmods.refinedstorage2.api.network.impl.node.multistorage;

import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.TypedStorage;

import java.util.Optional;

@FunctionalInterface
public interface MultiStorageProvider {
Optional<TypedStorage<?>> resolve(int index);
<T> Optional<TypedStorage<T, Storage<T>>> resolve(int index);
}

This file was deleted.

Loading

0 comments on commit ecbb220

Please sign in to comment.