From b8695091f5b750ed2b0f1361843fbbb2e13b317f Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sat, 30 Nov 2024 13:42:12 +0100 Subject: [PATCH] fix: external storage not connecting properly to fluid storages Also fixes interface not respecting maximum stack size. (Reproduceable in the Mekanism addon with creative chemical tank) Also fixes crash potential crash when building cable shapes. On Fabric it crashed with a ConcurrentModificationException. --- CHANGELOG.md | 6 ++++++ .../common/AbstractModInitializer.java | 4 ++-- .../AbstractConstructorDestructorBlock.java | 6 +++--- .../common/exporter/ExporterBlock.java | 4 ++-- ...rfacePlatformExternalStorageProviderFactory.java | 9 ++++----- .../common/importer/ImporterBlock.java | 6 +++--- .../storage/StorageContainerUpgradeRecipe.java | 4 +++- .../externalstorage/ExternalStorageBlock.java | 6 +++--- .../support/AbstractDirectionalCableBlock.java | 7 ++++--- .../refinedstorage/common/support/CableShapes.java | 4 ++-- .../support/resource/ResourceContainerImpl.java | 12 ++++++++++-- .../refinedstorage/fabric/ModInitializerImpl.java | 6 ++++-- ...oragePlatformExternalStorageProviderFactory.java | 13 ++++++++++++- ...ndlerPlatformExternalStorageProviderFactory.java | 10 +++++++++- ...ndlerPlatformExternalStorageProviderFactory.java | 4 +++- 15 files changed, 70 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eeb0eb344..4d89d31ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - The Autocrafting Monitor now has a sidebar with all tasks instead of using tabs. - The auto-selected search box mode is now a global option used in the Autocrafter Manager as well. +### Fixed + +- Fixed External Fluid not connecting properly to fluid storages. +- Fixed Interface filter not respecting maximum stack size of a resource. +- Fixed potential crash when trying to build cable shapes. + ### Removed - Block of Quartz Enriched Iron (has been moved to addon mod) diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java index 41566deb2..e3ee5c201 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java @@ -420,8 +420,8 @@ private void registerItemStorageItems(final RegistryCallback callback, if (variant != ItemStorageVariant.CREATIVE) { Items.INSTANCE.setItemStoragePart(variant, callback.register( ContentIds.forItemStoragePart(variant), - SimpleItem::new) - ); + SimpleItem::new + )); } Items.INSTANCE.setItemStorageDisk(variant, callback.register( ContentIds.forStorageDisk(variant), diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/constructordestructor/AbstractConstructorDestructorBlock.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/constructordestructor/AbstractConstructorDestructorBlock.java index 5c66e1589..5d5a06cbe 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/constructordestructor/AbstractConstructorDestructorBlock.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/constructordestructor/AbstractConstructorDestructorBlock.java @@ -6,8 +6,7 @@ import com.refinedmods.refinedstorage.common.support.ColorableBlock; import com.refinedmods.refinedstorage.common.support.DirectionalCableBlockShapes; -import java.util.HashMap; -import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import net.minecraft.core.Direction; @@ -31,7 +30,8 @@ public abstract class AbstractConstructorDestructorBlock, EntityBlock { public static final BooleanProperty ACTIVE = BooleanProperty.create("active"); - private static final Map SHAPE_CACHE = new HashMap<>(); + private static final ConcurrentHashMap SHAPE_CACHE = + new ConcurrentHashMap<>(); private final AbstractBlockEntityTicker ticker; private final DyeColor color; diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/exporter/ExporterBlock.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/exporter/ExporterBlock.java index 9f29833f6..3999b7b3d 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/exporter/ExporterBlock.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/exporter/ExporterBlock.java @@ -13,7 +13,6 @@ import com.refinedmods.refinedstorage.common.support.NetworkNodeBlockItem; import com.refinedmods.refinedstorage.common.support.network.NetworkNodeBlockEntityTicker; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; @@ -35,7 +34,8 @@ public class ExporterBlock extends AbstractDirectionalCableBlock implements ColorableBlock, EntityBlock, BlockItemProvider { private static final Component HELP = createTranslation("item", "exporter.help"); - private static final Map SHAPE_CACHE = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap SHAPE_CACHE = + new ConcurrentHashMap<>(); private static final AbstractBlockEntityTicker TICKER = new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getExporter); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfacePlatformExternalStorageProviderFactory.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfacePlatformExternalStorageProviderFactory.java index 27120e408..881879e6c 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfacePlatformExternalStorageProviderFactory.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/iface/InterfacePlatformExternalStorageProviderFactory.java @@ -14,14 +14,13 @@ public class InterfacePlatformExternalStorageProviderFactory implements Platform public Optional create(final ServerLevel level, final BlockPos pos, final Direction direction) { - if (!(level.getBlockEntity(pos) instanceof InterfaceBlockEntity)) { - return Optional.empty(); - } - return Optional.of(new InterfaceProxyExternalStorageProvider(level, pos)); + return Optional.ofNullable(level.getBlockEntity(pos)) + .filter(blockEntity -> blockEntity instanceof InterfaceBlockEntity) + .map(blockEntity -> new InterfaceProxyExternalStorageProvider(level, pos)); } @Override public int getPriority() { - return -1; + return Integer.MIN_VALUE; } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/importer/ImporterBlock.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/importer/ImporterBlock.java index cf33a246f..9acca806c 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/importer/ImporterBlock.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/importer/ImporterBlock.java @@ -13,8 +13,7 @@ import com.refinedmods.refinedstorage.common.support.NetworkNodeBlockItem; import com.refinedmods.refinedstorage.common.support.network.NetworkNodeBlockEntityTicker; -import java.util.HashMap; -import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; @@ -35,7 +34,8 @@ public class ImporterBlock extends AbstractDirectionalCableBlock implements ColorableBlock, EntityBlock, BlockItemProvider { private static final Component HELP = createTranslation("item", "importer.help"); - private static final Map SHAPE_CACHE = new HashMap<>(); + private static final ConcurrentHashMap SHAPE_CACHE = + new ConcurrentHashMap<>(); private static final AbstractBlockEntityTicker TICKER = new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getImporter); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageContainerUpgradeRecipe.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageContainerUpgradeRecipe.java index de1c4ab58..0949fc23d 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageContainerUpgradeRecipe.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/StorageContainerUpgradeRecipe.java @@ -30,7 +30,9 @@ public StorageContainerUpgradeRecipe(final T[] variants, containerProvider.apply(to).asItem().getDefaultInstance(), NonNullList.of( Ingredient.of(getValidSourceContainers(to, variants, containerProvider) - .stream().map(ItemLike::asItem).map(Item::getDefaultInstance)), + .stream() + .map(ItemLike::asItem) + .toArray(Item[]::new)), Ingredient.of(to.getStoragePart()) ) ); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlock.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlock.java index 6d0c9cab5..1e56ad3f5 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlock.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/externalstorage/ExternalStorageBlock.java @@ -13,8 +13,7 @@ import com.refinedmods.refinedstorage.common.support.NetworkNodeBlockItem; import com.refinedmods.refinedstorage.common.support.network.NetworkNodeBlockEntityTicker; -import java.util.HashMap; -import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; @@ -39,7 +38,8 @@ public class ExternalStorageBlock extends AbstractDirectionalCableBlock implements ColorableBlock, EntityBlock, BlockItemProvider { private static final Component HELP = createTranslation("item", "external_storage.help"); - private static final Map SHAPE_CACHE = new HashMap<>(); + private static final ConcurrentHashMap SHAPE_CACHE = + new ConcurrentHashMap<>(); private static final AbstractBlockEntityTicker TICKER = new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getExternalStorage); private static final Logger LOGGER = LoggerFactory.getLogger(ExternalStorageBlock.class); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractDirectionalCableBlock.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractDirectionalCableBlock.java index 87c3f585e..c730ac475 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractDirectionalCableBlock.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/AbstractDirectionalCableBlock.java @@ -7,7 +7,7 @@ import com.refinedmods.refinedstorage.common.support.direction.DirectionType; import com.refinedmods.refinedstorage.common.util.PlatformUtil; -import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; @@ -31,9 +31,10 @@ public abstract class AbstractDirectionalCableBlock extends AbstractDirectionalBlock implements SimpleWaterloggedBlock { - private final Map shapeCache; + private final ConcurrentHashMap shapeCache; - protected AbstractDirectionalCableBlock(final Map shapeCache) { + protected AbstractDirectionalCableBlock(final ConcurrentHashMap + shapeCache) { super(BlockConstants.CABLE_PROPERTIES); this.shapeCache = shapeCache; } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/CableShapes.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/CableShapes.java index f16bbc602..bf723ee5f 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/CableShapes.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/CableShapes.java @@ -2,8 +2,8 @@ import com.refinedmods.refinedstorage.common.networking.CableConnections; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; @@ -18,7 +18,7 @@ public final class CableShapes { private static final VoxelShape WEST = box(0, 6, 6, 6, 10, 10); private static final VoxelShape UP = box(6, 10, 6, 10, 16, 10); private static final VoxelShape DOWN = box(6, 0, 6, 10, 6, 10); - private static final Map CACHE = new HashMap<>(); + private static final Map CACHE = new ConcurrentHashMap<>(); private CableShapes() { } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java index dc48a4d51..cea778c36 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/resource/ResourceContainerImpl.java @@ -55,19 +55,27 @@ public void setListener(@Nullable final Runnable listener) { public void change(final int index, final ItemStack stack, final boolean tryAlternatives) { if (tryAlternatives) { for (final ResourceFactory resourceFactory : alternativeResourceFactories) { - final var result = resourceFactory.create(stack); + final var result = resourceFactory.create(stack).map(this::respectMaxAmount); if (result.isPresent()) { set(index, result.get()); return; } } } - primaryResourceFactory.create(stack).ifPresentOrElse( + primaryResourceFactory.create(stack).map(this::respectMaxAmount).ifPresentOrElse( resource -> set(index, resource), () -> remove(index) ); } + private ResourceAmount respectMaxAmount(final ResourceAmount resourceAmount) { + final long maxAmount = getMaxAmount(resourceAmount.resource()); + if (resourceAmount.amount() > maxAmount) { + return new ResourceAmount(resourceAmount.resource(), maxAmount); + } + return resourceAmount; + } + @Override public void set(final int index, final ResourceAmount resourceAmount) { setSilently(index, resourceAmount); diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java index 90339f198..465bdd2c0 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java @@ -279,7 +279,8 @@ private void registerExternalStorageProviderFactories() { ItemStorage.SIDED, VariantUtil::ofItemVariant, resource -> resource instanceof ItemResource itemResource - ? toItemVariant(itemResource) : null + ? toItemVariant(itemResource) : null, + 0 ) ); RefinedStorageApi.INSTANCE.addExternalStorageProviderFactory( @@ -287,7 +288,8 @@ private void registerExternalStorageProviderFactories() { FluidStorage.SIDED, VariantUtil::ofFluidVariant, resource -> resource instanceof FluidResource fluidResource - ? toFluidVariant(fluidResource) : null + ? toFluidVariant(fluidResource) : null, + -1 ) ); } diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/externalstorage/FabricStoragePlatformExternalStorageProviderFactory.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/externalstorage/FabricStoragePlatformExternalStorageProviderFactory.java index 5d2f82e4c..87bfadb83 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/externalstorage/FabricStoragePlatformExternalStorageProviderFactory.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/externalstorage/FabricStoragePlatformExternalStorageProviderFactory.java @@ -19,20 +19,26 @@ public class FabricStoragePlatformExternalStorageProviderFactory private final BlockApiLookup, Direction> lookup; private final Function fromPlatformMapper; private final Function toPlatformMapper; + private final int priority; public FabricStoragePlatformExternalStorageProviderFactory(final BlockApiLookup, Direction> lookup, final Function fromPlatformMapper, @NullableType final Function - toPlatformMapper) { + toPlatformMapper, + final int priority) { this.lookup = lookup; this.fromPlatformMapper = fromPlatformMapper; this.toPlatformMapper = toPlatformMapper; + this.priority = priority; } @Override public Optional create(final ServerLevel level, final BlockPos pos, final Direction direction) { + if (lookup.find(level, pos, direction) == null) { + return Optional.empty(); + } return Optional.of(new FabricStorageExternalStorageProvider<>( lookup, fromPlatformMapper, @@ -42,4 +48,9 @@ public Optional create(final ServerLevel level, direction )); } + + @Override + public int getPriority() { + return priority; + } } diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/FluidHandlerPlatformExternalStorageProviderFactory.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/FluidHandlerPlatformExternalStorageProviderFactory.java index 09fd6b077..78ac47a51 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/FluidHandlerPlatformExternalStorageProviderFactory.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/FluidHandlerPlatformExternalStorageProviderFactory.java @@ -2,6 +2,7 @@ import com.refinedmods.refinedstorage.api.storage.external.ExternalStorageProvider; import com.refinedmods.refinedstorage.common.api.storage.externalstorage.PlatformExternalStorageProviderFactory; +import com.refinedmods.refinedstorage.neoforge.storage.CapabilityCache; import com.refinedmods.refinedstorage.neoforge.storage.CapabilityCacheImpl; import java.util.Optional; @@ -15,6 +16,13 @@ public class FluidHandlerPlatformExternalStorageProviderFactory implements Platf public Optional create(final ServerLevel level, final BlockPos pos, final Direction direction) { - return Optional.of(new FluidHandlerExternalStorageProvider(new CapabilityCacheImpl(level, pos, direction))); + final CapabilityCache capabilityCache = new CapabilityCacheImpl(level, pos, direction); + return capabilityCache.getFluidHandler() + .map(handler -> new FluidHandlerExternalStorageProvider(capabilityCache)); + } + + @Override + public int getPriority() { + return -1; } } diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/ItemHandlerPlatformExternalStorageProviderFactory.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/ItemHandlerPlatformExternalStorageProviderFactory.java index bd5ef8295..e8150da97 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/ItemHandlerPlatformExternalStorageProviderFactory.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/storage/externalstorage/ItemHandlerPlatformExternalStorageProviderFactory.java @@ -2,6 +2,7 @@ import com.refinedmods.refinedstorage.api.storage.external.ExternalStorageProvider; import com.refinedmods.refinedstorage.common.api.storage.externalstorage.PlatformExternalStorageProviderFactory; +import com.refinedmods.refinedstorage.neoforge.storage.CapabilityCache; import com.refinedmods.refinedstorage.neoforge.storage.CapabilityCacheImpl; import java.util.Optional; @@ -15,6 +16,7 @@ public class ItemHandlerPlatformExternalStorageProviderFactory implements Platfo public Optional create(final ServerLevel level, final BlockPos pos, final Direction direction) { - return Optional.of(new ItemHandlerExternalStorageProvider(new CapabilityCacheImpl(level, pos, direction))); + final CapabilityCache capabilityCache = new CapabilityCacheImpl(level, pos, direction); + return capabilityCache.getItemHandler().map(handler -> new ItemHandlerExternalStorageProvider(capabilityCache)); } }