From 872885724a47539f5a3cd3c721b2d1a4aac9e706 Mon Sep 17 00:00:00 2001 From: Raoul Van den Berge Date: Tue, 31 Oct 2023 10:24:13 +0100 Subject: [PATCH] refactor: split up configuration read/writes from unique read/writes --- .../blockentity/ConfigurationCardTarget.java | 12 + ...ternalNetworkNodeContainerBlockEntity.java | 26 +- ...actingNetworkNodeContainerBlockEntity.java | 3 +- ...dulingNetworkNodeContainerBlockEntity.java | 8 +- .../block/entity/ImporterBlockEntity.java | 10 +- .../StorageConfigurationContainerImpl.java | 3 +- .../constructor/ConstructorBlockEntity.java | 8 +- .../destructor/DestructorBlockEntity.java | 8 +- .../entity/detector/DetectorBlockEntity.java | 16 +- .../AbstractDiskDriveBlockEntity.java | 13 +- .../ExternalStorageBlockEntity.java | 339 +++++++++--------- .../entity/iface/InterfaceBlockEntity.java | 12 +- .../AbstractStorageBlockBlockEntity.java | 12 +- .../StorageMonitorBlockEntity.java | 12 +- 14 files changed, 274 insertions(+), 208 deletions(-) create mode 100644 refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/blockentity/ConfigurationCardTarget.java diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/blockentity/ConfigurationCardTarget.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/blockentity/ConfigurationCardTarget.java new file mode 100644 index 000000000..09f7ebc5d --- /dev/null +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/blockentity/ConfigurationCardTarget.java @@ -0,0 +1,12 @@ +package com.refinedmods.refinedstorage2.platform.api.blockentity; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.level.block.entity.BlockEntityType; + +public interface ConfigurationCardTarget { + BlockEntityType getBlockEntityType(); + + void writeConfiguration(CompoundTag tag); + + void readConfiguration(CompoundTag tag); +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractInternalNetworkNodeContainerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractInternalNetworkNodeContainerBlockEntity.java index 7c32c3318..474cd4c8c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractInternalNetworkNodeContainerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractInternalNetworkNodeContainerBlockEntity.java @@ -3,6 +3,7 @@ import com.refinedmods.refinedstorage2.api.network.component.EnergyNetworkComponent; import com.refinedmods.refinedstorage2.api.network.node.AbstractNetworkNode; import com.refinedmods.refinedstorage2.platform.api.blockentity.AbstractNetworkNodeContainerBlockEntity; +import com.refinedmods.refinedstorage2.platform.api.blockentity.ConfigurationCardTarget; import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.block.AbstractDirectionalBlock; import com.refinedmods.refinedstorage2.platform.common.block.ColorableBlock; @@ -25,7 +26,7 @@ public abstract class AbstractInternalNetworkNodeContainerBlockEntity extends AbstractNetworkNodeContainerBlockEntity - implements PlayerAware { + implements PlayerAware, ConfigurationCardTarget { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInternalNetworkNodeContainerBlockEntity.class); private static final String TAG_REDSTONE_MODE = "rm"; @@ -56,23 +57,38 @@ && getNode().getNetwork() != null @Override public void saveAdditional(final CompoundTag tag) { super.saveAdditional(tag); - tag.putInt(TAG_REDSTONE_MODE, RedstoneModeSettings.getRedstoneMode(getRedstoneMode())); + writeConfiguration(tag); if (placedByPlayerId != null) { tag.putUUID(TAG_PLACED_BY_PLAYER_ID, placedByPlayerId); } } + @Override + public void writeConfiguration(final CompoundTag tag) { + tag.putInt(TAG_REDSTONE_MODE, RedstoneModeSettings.getRedstoneMode(getRedstoneMode())); + } + @Override public void load(final CompoundTag tag) { super.load(tag); - if (tag.contains(TAG_REDSTONE_MODE)) { - redstoneMode = RedstoneModeSettings.getRedstoneMode(tag.getInt(TAG_REDSTONE_MODE)); - } + readConfiguration(tag); if (tag.hasUUID(TAG_PLACED_BY_PLAYER_ID)) { placedByPlayerId = tag.getUUID(TAG_PLACED_BY_PLAYER_ID); } } + @Override + public void readConfiguration(final CompoundTag tag) { + if (tag.contains(TAG_REDSTONE_MODE)) { + redstoneMode = RedstoneModeSettings.getRedstoneMode(tag.getInt(TAG_REDSTONE_MODE)); + } + } + + @Override + public BlockEntityType getBlockEntityType() { + return getType(); + } + public void updateActiveness(final BlockState state, @Nullable final BooleanProperty activenessProperty) { final boolean newActive = isActive(); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractLevelInteractingNetworkNodeContainerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractLevelInteractingNetworkNodeContainerBlockEntity.java index 8c66c1785..a0904f263 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractLevelInteractingNetworkNodeContainerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractLevelInteractingNetworkNodeContainerBlockEntity.java @@ -14,7 +14,7 @@ public abstract class AbstractLevelInteractingNetworkNodeContainerBlockEntity extends AbstractInternalNetworkNodeContainerBlockEntity { private static final Logger LOGGER = LoggerFactory.getLogger( - AbstractLevelInteractingNetworkNodeContainerBlockEntity.class + AbstractLevelInteractingNetworkNodeContainerBlockEntity.class ); protected AbstractLevelInteractingNetworkNodeContainerBlockEntity( @@ -28,7 +28,6 @@ protected AbstractLevelInteractingNetworkNodeContainerBlockEntity( // used to handle rotations @Override - @SuppressWarnings("deprecation") public void setBlockState(final BlockState newBlockState) { super.setBlockState(newBlockState); if (!(level instanceof ServerLevel serverLevel)) { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractSchedulingNetworkNodeContainerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractSchedulingNetworkNodeContainerBlockEntity.java index e298d8b87..636538a98 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractSchedulingNetworkNodeContainerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/AbstractSchedulingNetworkNodeContainerBlockEntity.java @@ -43,17 +43,17 @@ protected AbstractSchedulingNetworkNodeContainerBlockEntity( } @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); schedulingMode.writeToTag(tag); filter.save(tag); } @Override - public void load(final CompoundTag tag) { + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); schedulingMode.load(tag); filter.load(tag); - super.load(tag); } public void setSchedulingModeType(final SchedulingModeType type) { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/ImporterBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/ImporterBlockEntity.java index 71b12cea2..be7b11fc4 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/ImporterBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/ImporterBlockEntity.java @@ -79,21 +79,19 @@ private CompositeImporterTransferStrategy createStrategy(final ServerLevel serve } @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); tag.putInt(TAG_FILTER_MODE, FilterModeSettings.getFilterMode(getNode().getFilterMode())); filter.save(tag); } @Override - public void load(final CompoundTag tag) { + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); if (tag.contains(TAG_FILTER_MODE)) { getNode().setFilterMode(FilterModeSettings.getFilterMode(tag.getInt(TAG_FILTER_MODE))); } - filter.load(tag); - - super.load(tag); } public boolean isFuzzyMode() { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/StorageConfigurationContainerImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/StorageConfigurationContainerImpl.java index 3fb40d138..f1890948f 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/StorageConfigurationContainerImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/StorageConfigurationContainerImpl.java @@ -26,8 +26,7 @@ public StorageConfigurationContainerImpl(final StorageConfiguration config, final FilterWithFuzzyMode filter, final Runnable listener, final Supplier redstoneModeSupplier, - final Consumer redstoneModeConsumer - ) { + final Consumer redstoneModeConsumer) { this.config = config; this.filter = filter; this.listener = listener; diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/constructor/ConstructorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/constructor/ConstructorBlockEntity.java index 104736dc4..6137b4f8a 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/constructor/ConstructorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/constructor/ConstructorBlockEntity.java @@ -99,17 +99,17 @@ public void postDoWork() { } @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); tag.putBoolean(TAG_DROP_ITEMS, dropItems); } @Override - public void load(final CompoundTag tag) { + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); if (tag.contains(TAG_DROP_ITEMS)) { dropItems = tag.getBoolean(TAG_DROP_ITEMS); } - super.load(tag); } public boolean isDropItems() { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/destructor/DestructorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/destructor/DestructorBlockEntity.java index f9e4f9aa9..3d2e7ad75 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/destructor/DestructorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/destructor/DestructorBlockEntity.java @@ -83,15 +83,16 @@ public void setFilterMode(final FilterMode mode) { } @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); tag.putInt(TAG_FILTER_MODE, FilterModeSettings.getFilterMode(filter.getMode())); tag.putBoolean(TAG_PICKUP_ITEMS, pickupItems); filterWithFuzzyMode.save(tag); } @Override - public void load(final CompoundTag tag) { + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); filterWithFuzzyMode.load(tag); if (tag.contains(TAG_FILTER_MODE)) { filter.setMode(FilterModeSettings.getFilterMode(tag.getInt(TAG_FILTER_MODE))); @@ -99,7 +100,6 @@ public void load(final CompoundTag tag) { if (tag.contains(TAG_PICKUP_ITEMS)) { pickupItems = tag.getBoolean(TAG_PICKUP_ITEMS); } - super.load(tag); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/detector/DetectorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/detector/DetectorBlockEntity.java index 98afeed4f..696c62b01 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/detector/DetectorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/detector/DetectorBlockEntity.java @@ -64,10 +64,9 @@ public DetectorBlockEntity(final BlockPos pos, final BlockState state) { initialize(); } - @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); filter.save(tag); tag.putDouble(TAG_AMOUNT, amount); tag.putInt(TAG_MODE, DetectorModeSettings.getDetectorMode(getNode().getMode())); @@ -75,6 +74,14 @@ public void saveAdditional(final CompoundTag tag) { @Override public void load(final CompoundTag tag) { + super.load(tag); + initialize(); + propagateAmount(); + } + + @Override + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); filter.load(tag); if (tag.contains(TAG_AMOUNT)) { this.amount = tag.getDouble(TAG_AMOUNT); @@ -82,9 +89,6 @@ public void load(final CompoundTag tag) { if (tag.contains(TAG_MODE)) { getNode().setMode(DetectorModeSettings.getDetectorMode(tag.getInt(TAG_MODE))); } - initialize(); - propagateAmount(); - super.load(tag); } public void setAmount(final double amount) { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/diskdrive/AbstractDiskDriveBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/diskdrive/AbstractDiskDriveBlockEntity.java index 2e99a8364..625bcd74d 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/diskdrive/AbstractDiskDriveBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/diskdrive/AbstractDiskDriveBlockEntity.java @@ -155,21 +155,28 @@ public void activenessChanged(final BlockState state, @Override public void load(final CompoundTag tag) { fromClientTag(tag); - if (tag.contains(TAG_DISK_INVENTORY)) { ContainerUtil.read(tag.getCompound(TAG_DISK_INVENTORY), diskInventory); } + super.load(tag); + } + @Override + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); configContainer.load(tag); filter.load(tag); - - super.load(tag); } @Override public void saveAdditional(final CompoundTag tag) { super.saveAdditional(tag); tag.put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory)); + } + + @Override + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); configContainer.save(tag); filter.save(tag); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/externalstorage/ExternalStorageBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/externalstorage/ExternalStorageBlockEntity.java index 94bebab8c..d16595b60 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/externalstorage/ExternalStorageBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/externalstorage/ExternalStorageBlockEntity.java @@ -1,165 +1,174 @@ -package com.refinedmods.refinedstorage2.platform.common.block.entity.externalstorage; - -import com.refinedmods.refinedstorage2.api.network.impl.node.externalstorage.ExternalStorageNetworkNode; -import com.refinedmods.refinedstorage2.api.network.node.externalstorage.ExternalStorageProviderFactory; -import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; -import com.refinedmods.refinedstorage2.api.storage.external.ExternalStorageProvider; -import com.refinedmods.refinedstorage2.platform.api.PlatformApi; -import com.refinedmods.refinedstorage2.platform.common.Platform; -import com.refinedmods.refinedstorage2.platform.common.block.entity.AbstractInternalNetworkNodeContainerBlockEntity; -import com.refinedmods.refinedstorage2.platform.common.block.entity.FilterWithFuzzyMode; -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.content.ContentNames; -import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl; -import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider; - -import java.util.Optional; -import javax.annotation.Nullable; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.chat.Component; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.block.state.properties.BooleanProperty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ExternalStorageBlockEntity - extends AbstractInternalNetworkNodeContainerBlockEntity - implements ExtendedMenuProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(ExternalStorageBlockEntity.class); - private static final String TAG_TRACKED_RESOURCES = "tr"; - - private final FilterWithFuzzyMode filter; - private final StorageConfigurationContainerImpl configContainer; - private final ExternalStorageTrackedStorageRepositoryProvider trackedStorageRepositoryProvider; - private final WorkRate workRate = new WorkRate(); - private boolean initialized; - - public ExternalStorageBlockEntity(final BlockPos pos, final BlockState state) { - super(BlockEntities.INSTANCE.getExternalStorage(), pos, state, new ExternalStorageNetworkNode( - Platform.INSTANCE.getConfig().getExternalStorage().getEnergyUsage() - )); - this.filter = FilterWithFuzzyMode.createAndListenForUniqueTemplates( - ResourceContainerImpl.createForFilter(), - this::setChanged, - templates -> getNode().setFilterTemplates(templates) - ); - this.trackedStorageRepositoryProvider = new ExternalStorageTrackedStorageRepositoryProvider( - PlatformApi.INSTANCE.getStorageChannelTypeRegistry(), - this::setChanged - ); - getNode().setNormalizer(filter.createNormalizer()); - getNode().initialize( - PlatformApi.INSTANCE.getStorageChannelTypeRegistry().getAll(), - System::currentTimeMillis, - trackedStorageRepositoryProvider - ); - this.configContainer = new StorageConfigurationContainerImpl( - getNode(), - filter, - this::setChanged, - this::getRedstoneMode, - this::setRedstoneMode - ); - } - - @Override - @SuppressWarnings("deprecation") - public void setBlockState(final BlockState newBlockState) { - super.setBlockState(newBlockState); - if (level instanceof ServerLevel serverLevel) { - LOGGER.debug("Reloading external storage @ {} as block state has changed", worldPosition); - loadStorage(serverLevel); - } - } - - @Override - protected void activenessChanged(final BlockState state, - final boolean newActive, - @Nullable final BooleanProperty activenessProperty) { - super.activenessChanged(state, newActive, activenessProperty); - if (!initialized && level instanceof ServerLevel serverLevel) { - LOGGER.debug("Triggering initial load of external storage {}", worldPosition); - loadStorage(serverLevel); - initialized = true; - } - } - - public void loadStorage(final ServerLevel serverLevel) { - final Direction direction = getDirection(); - LOGGER.debug("Loading storage for external storage with direction {} @ {}", direction, worldPosition); - if (direction == null) { - return; - } - getNode().initialize(new ExternalStorageProviderFactory() { - @Override - public Optional> create(final StorageChannelType channelType) { - final Direction incomingDirection = direction.getOpposite(); - final BlockPos sourcePosition = worldPosition.relative(direction); - return PlatformApi.INSTANCE - .getExternalStorageProviderFactories() - .stream() - .flatMap(factory -> factory.create(serverLevel, sourcePosition, incomingDirection, channelType) - .stream()) - .findFirst(); - } - }); - } - - @Override - public void doWork() { - super.doWork(); - if (workRate.canDoWork()) { - final boolean hasChanges = getNode().detectChanges(); - if (hasChanges) { - LOGGER.debug("External storage @ {} has changed!", worldPosition); - workRate.faster(); - } else { - workRate.slower(); - } - } - } - - @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); - filter.save(tag); - configContainer.save(tag); - tag.put(TAG_TRACKED_RESOURCES, trackedStorageRepositoryProvider.toTag()); - } - - @Override - public void load(final CompoundTag tag) { - super.load(tag); - filter.load(tag); - configContainer.load(tag); - trackedStorageRepositoryProvider.fromTag(tag.getList(TAG_TRACKED_RESOURCES, Tag.TAG_COMPOUND)); - } - - @Override - public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) { - filter.getFilterContainer().writeToUpdatePacket(buf); - } - - @Override - public Component getDisplayName() { - return ContentNames.EXTERNAL_STORAGE; - } - - @Nullable - @Override - public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) { - return new ExternalStorageContainerMenu(syncId, player, filter.getFilterContainer(), configContainer); - } -} +package com.refinedmods.refinedstorage2.platform.common.block.entity.externalstorage; + +import com.refinedmods.refinedstorage2.api.network.impl.node.externalstorage.ExternalStorageNetworkNode; +import com.refinedmods.refinedstorage2.api.network.node.externalstorage.ExternalStorageProviderFactory; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; +import com.refinedmods.refinedstorage2.api.storage.external.ExternalStorageProvider; +import com.refinedmods.refinedstorage2.platform.api.PlatformApi; +import com.refinedmods.refinedstorage2.platform.common.Platform; +import com.refinedmods.refinedstorage2.platform.common.block.entity.AbstractInternalNetworkNodeContainerBlockEntity; +import com.refinedmods.refinedstorage2.platform.common.block.entity.FilterWithFuzzyMode; +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.content.ContentNames; +import com.refinedmods.refinedstorage2.platform.common.internal.resource.ResourceContainerImpl; +import com.refinedmods.refinedstorage2.platform.common.menu.ExtendedMenuProvider; + +import java.util.Optional; +import javax.annotation.Nullable; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.BooleanProperty; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ExternalStorageBlockEntity + extends AbstractInternalNetworkNodeContainerBlockEntity + implements ExtendedMenuProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(ExternalStorageBlockEntity.class); + private static final String TAG_TRACKED_RESOURCES = "tr"; + + private final FilterWithFuzzyMode filter; + private final StorageConfigurationContainerImpl configContainer; + private final ExternalStorageTrackedStorageRepositoryProvider trackedStorageRepositoryProvider; + private final WorkRate workRate = new WorkRate(); + private boolean initialized; + + public ExternalStorageBlockEntity(final BlockPos pos, final BlockState state) { + super(BlockEntities.INSTANCE.getExternalStorage(), pos, state, new ExternalStorageNetworkNode( + Platform.INSTANCE.getConfig().getExternalStorage().getEnergyUsage() + )); + this.filter = FilterWithFuzzyMode.createAndListenForUniqueTemplates( + ResourceContainerImpl.createForFilter(), + this::setChanged, + templates -> getNode().setFilterTemplates(templates) + ); + this.trackedStorageRepositoryProvider = new ExternalStorageTrackedStorageRepositoryProvider( + PlatformApi.INSTANCE.getStorageChannelTypeRegistry(), + this::setChanged + ); + getNode().setNormalizer(filter.createNormalizer()); + getNode().initialize( + PlatformApi.INSTANCE.getStorageChannelTypeRegistry().getAll(), + System::currentTimeMillis, + trackedStorageRepositoryProvider + ); + this.configContainer = new StorageConfigurationContainerImpl( + getNode(), + filter, + this::setChanged, + this::getRedstoneMode, + this::setRedstoneMode + ); + } + + @Override + public void setBlockState(final BlockState newBlockState) { + super.setBlockState(newBlockState); + if (level instanceof ServerLevel serverLevel) { + LOGGER.debug("Reloading external storage @ {} as block state has changed", worldPosition); + loadStorage(serverLevel); + } + } + + @Override + protected void activenessChanged(final BlockState state, + final boolean newActive, + @Nullable final BooleanProperty activenessProperty) { + super.activenessChanged(state, newActive, activenessProperty); + if (!initialized && level instanceof ServerLevel serverLevel) { + LOGGER.debug("Triggering initial load of external storage {}", worldPosition); + loadStorage(serverLevel); + initialized = true; + } + } + + public void loadStorage(final ServerLevel serverLevel) { + final Direction direction = getDirection(); + LOGGER.debug("Loading storage for external storage with direction {} @ {}", direction, worldPosition); + if (direction == null) { + return; + } + getNode().initialize(new ExternalStorageProviderFactory() { + @Override + public Optional> create(final StorageChannelType channelType) { + final Direction incomingDirection = direction.getOpposite(); + final BlockPos sourcePosition = worldPosition.relative(direction); + return PlatformApi.INSTANCE + .getExternalStorageProviderFactories() + .stream() + .flatMap(factory -> factory.create(serverLevel, sourcePosition, incomingDirection, channelType) + .stream()) + .findFirst(); + } + }); + } + + @Override + public void doWork() { + super.doWork(); + if (workRate.canDoWork()) { + final boolean hasChanges = getNode().detectChanges(); + if (hasChanges) { + LOGGER.debug("External storage @ {} has changed!", worldPosition); + workRate.faster(); + } else { + workRate.slower(); + } + } + } + + @Override + public void saveAdditional(final CompoundTag tag) { + super.saveAdditional(tag); + tag.put(TAG_TRACKED_RESOURCES, trackedStorageRepositoryProvider.toTag()); + } + + @Override + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); + filter.save(tag); + configContainer.save(tag); + } + + @Override + public void load(final CompoundTag tag) { + super.load(tag); + trackedStorageRepositoryProvider.fromTag(tag.getList(TAG_TRACKED_RESOURCES, Tag.TAG_COMPOUND)); + } + + @Override + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); + filter.load(tag); + configContainer.load(tag); + } + + @Override + public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) { + filter.getFilterContainer().writeToUpdatePacket(buf); + } + + @Override + public Component getDisplayName() { + return ContentNames.EXTERNAL_STORAGE; + } + + @Nullable + @Override + public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) { + return new ExternalStorageContainerMenu(syncId, player, filter.getFilterContainer(), configContainer); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/iface/InterfaceBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/iface/InterfaceBlockEntity.java index 9edacdb05..d08df7577 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/iface/InterfaceBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/iface/InterfaceBlockEntity.java @@ -107,6 +107,11 @@ static long getTransferQuota(final ResourceTemplate resourceTemplate) { public void saveAdditional(final CompoundTag tag) { super.saveAdditional(tag); tag.put(TAG_EXPORT_ITEMS, exportedResources.toTag()); + } + + @Override + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); filter.save(tag); } @@ -115,10 +120,15 @@ public void load(final CompoundTag tag) { if (tag.contains(TAG_EXPORT_ITEMS)) { exportedResources.fromTag(tag.getCompound(TAG_EXPORT_ITEMS)); } - filter.load(tag); super.load(tag); } + @Override + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); + filter.load(tag); + } + public boolean isFuzzyMode() { return filter.isFuzzyMode(); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storage/AbstractStorageBlockBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storage/AbstractStorageBlockBlockEntity.java index ccce42bcf..df47b9537 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storage/AbstractStorageBlockBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storage/AbstractStorageBlockBlockEntity.java @@ -114,11 +114,14 @@ public void load(final CompoundTag tag) { } storageId = actualStorageId; } + super.load(tag); + } + @Override + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); configContainer.load(tag); filter.load(tag); - - super.load(tag); } @SuppressWarnings("unchecked") @@ -149,6 +152,11 @@ public void saveAdditional(final CompoundTag tag) { if (storageId != null) { tag.putUUID(TAG_STORAGE_ID, storageId); } + } + + @Override + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); configContainer.save(tag); filter.save(tag); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storagemonitor/StorageMonitorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storagemonitor/StorageMonitorBlockEntity.java index 2f3c0730c..caee98062 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storagemonitor/StorageMonitorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/block/entity/storagemonitor/StorageMonitorBlockEntity.java @@ -259,8 +259,8 @@ public boolean isCurrentlyActive() { } @Override - public void saveAdditional(final CompoundTag tag) { - super.saveAdditional(tag); + public void writeConfiguration(final CompoundTag tag) { + super.writeConfiguration(tag); filter.save(tag); } @@ -270,12 +270,16 @@ public void load(final CompoundTag tag) { filter.getFilterContainer().fromTag(tag.getCompound(TAG_CLIENT_FILTER)); currentAmount = tag.getLong(TAG_CLIENT_AMOUNT); currentlyActive = tag.getBoolean(TAG_CLIENT_ACTIVE); - } else { - filter.load(tag); } super.load(tag); } + @Override + public void readConfiguration(final CompoundTag tag) { + super.readConfiguration(tag); + filter.load(tag); + } + @Override public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) { filter.getFilterContainer().writeToUpdatePacket(buf);