diff --git a/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/node/container/NetworkNodeContainer.java b/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/node/container/NetworkNodeContainer.java index 1bbc43a2b..de14588a4 100644 --- a/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/node/container/NetworkNodeContainer.java +++ b/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/node/container/NetworkNodeContainer.java @@ -1,6 +1,5 @@ package com.refinedmods.refinedstorage2.api.network.node.container; -import com.refinedmods.refinedstorage2.api.network.node.GraphNetworkComponent; import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import javax.annotation.Nullable; @@ -18,7 +17,8 @@ public interface NetworkNodeContainer { * The key must be kept stable, and must stay the same for the lifetime of the container. * If it changes after adding it into the graph, the container would not be removed from the key index when the * container is removed! - * The container can be queried by {@link GraphNetworkComponent#getContainer(Object)}. + * The container can be queried by + * {@link com.refinedmods.refinedstorage2.api.network.node.GraphNetworkComponent#getContainer(Object)}. * * @return the key, or null if indexing is not required */ diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java index a0414004d..089962ee7 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApi.java @@ -4,6 +4,7 @@ import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.network.NetworkComponent; import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import com.refinedmods.refinedstorage2.api.network.security.SecurityPolicy; import com.refinedmods.refinedstorage2.api.resource.ResourceKey; import com.refinedmods.refinedstorage2.platform.api.constructordestructor.ConstructorStrategyFactory; @@ -30,7 +31,8 @@ import com.refinedmods.refinedstorage2.platform.api.storagemonitor.StorageMonitorExtractionStrategy; import com.refinedmods.refinedstorage2.platform.api.storagemonitor.StorageMonitorInsertionStrategy; import com.refinedmods.refinedstorage2.platform.api.support.energy.EnergyItemHelper; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionLogic; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemHelper; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReferenceFactory; @@ -46,6 +48,8 @@ import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; +import javax.annotation.Nullable; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; @@ -57,6 +61,7 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import org.apiguardian.api.API; @@ -107,11 +112,20 @@ public interface PlatformApi { BuiltinUpgradeDestinations getBuiltinUpgradeDestinations(); - void requestNetworkNodeInitialization(PlatformNetworkNodeContainer container, Level level, Runnable callback); + InWorldNetworkNodeContainer createInWorldNetworkNodeContainer(BlockEntity blockEntity, + NetworkNode node, + String name, + int priority, + @Nullable ConnectionLogic connectionLogic, + @Nullable Supplier keyProvider); - void requestNetworkNodeRemoval(PlatformNetworkNodeContainer container, Level level); + void onNetworkNodeContainerInitialized(InWorldNetworkNodeContainer container, + @Nullable Level level, + @Nullable Runnable callback); - void requestNetworkNodeUpdate(PlatformNetworkNodeContainer container, Level level); + void onNetworkNodeContainerRemoved(InWorldNetworkNodeContainer container, @Nullable Level level); + + void onNetworkNodeContainerUpdated(InWorldNetworkNodeContainer container, @Nullable Level level); GridInsertionStrategy createGridInsertionStrategy(AbstractContainerMenu containerMenu, ServerPlayer player, diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java index d67c643f9..526d5a230 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/PlatformApiProxy.java @@ -4,6 +4,7 @@ import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.network.NetworkComponent; import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import com.refinedmods.refinedstorage2.api.network.security.SecurityPolicy; import com.refinedmods.refinedstorage2.api.resource.ResourceKey; import com.refinedmods.refinedstorage2.platform.api.constructordestructor.ConstructorStrategyFactory; @@ -30,7 +31,8 @@ import com.refinedmods.refinedstorage2.platform.api.storagemonitor.StorageMonitorExtractionStrategy; import com.refinedmods.refinedstorage2.platform.api.storagemonitor.StorageMonitorInsertionStrategy; import com.refinedmods.refinedstorage2.platform.api.support.energy.EnergyItemHelper; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionLogic; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemHelper; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReferenceFactory; @@ -46,6 +48,7 @@ import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; @@ -58,6 +61,7 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; @@ -178,20 +182,41 @@ public BuiltinUpgradeDestinations getBuiltinUpgradeDestinations() { } @Override - public void requestNetworkNodeInitialization(final PlatformNetworkNodeContainer container, - final Level level, - final Runnable callback) { - ensureLoaded().requestNetworkNodeInitialization(container, level, callback); + public InWorldNetworkNodeContainer createInWorldNetworkNodeContainer( + final BlockEntity blockEntity, + final NetworkNode node, + final String name, + final int priority, + @Nullable final ConnectionLogic connectionLogic, + @Nullable final Supplier keyProvider + ) { + return ensureLoaded().createInWorldNetworkNodeContainer( + blockEntity, + node, + name, + priority, + connectionLogic, + keyProvider + ); } @Override - public void requestNetworkNodeRemoval(final PlatformNetworkNodeContainer container, final Level level) { - ensureLoaded().requestNetworkNodeRemoval(container, level); + public void onNetworkNodeContainerInitialized(final InWorldNetworkNodeContainer container, + @Nullable final Level level, + @Nullable final Runnable callback) { + ensureLoaded().onNetworkNodeContainerInitialized(container, level, callback); } @Override - public void requestNetworkNodeUpdate(final PlatformNetworkNodeContainer container, final Level level) { - ensureLoaded().requestNetworkNodeUpdate(container, level); + public void onNetworkNodeContainerRemoved(final InWorldNetworkNodeContainer container, + @Nullable final Level level) { + ensureLoaded().onNetworkNodeContainerRemoved(container, level); + } + + @Override + public void onNetworkNodeContainerUpdated(final InWorldNetworkNodeContainer container, + @Nullable final Level level) { + ensureLoaded().onNetworkNodeContainerUpdated(container, level); } @Override diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/security/SecurityHelper.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/security/SecurityHelper.java index 0c4ef5038..3e985e320 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/security/SecurityHelper.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/security/SecurityHelper.java @@ -3,6 +3,9 @@ import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import com.refinedmods.refinedstorage2.api.network.security.Permission; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; + +import java.util.Set; import net.minecraft.server.level.ServerPlayer; @@ -10,6 +13,17 @@ public final class SecurityHelper { private SecurityHelper() { } + public static boolean isAllowed(final ServerPlayer player, + final Permission permission, + final Set containers) { + for (final InWorldNetworkNodeContainer container : containers) { + if (!isAllowed(player, permission, container.getNode())) { + return false; + } + } + return true; + } + public static boolean isAllowed(final ServerPlayer player, final Permission permission, final NetworkNode node) { final Network network = node.getNetwork(); if (network == null) { diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/AbstractNetworkNodeContainerBlockEntity.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/AbstractNetworkNodeContainerBlockEntity.java index 1c6bacffd..aa27d8501 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/AbstractNetworkNodeContainerBlockEntity.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/AbstractNetworkNodeContainerBlockEntity.java @@ -3,87 +3,78 @@ import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; +import java.util.HashSet; +import java.util.Set; +import javax.annotation.Nullable; + import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.core.GlobalPos; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import org.apiguardian.api.API; -import static java.util.Objects.requireNonNull; - @API(status = API.Status.STABLE, since = "2.0.0-milestone.1.2") public abstract class AbstractNetworkNodeContainerBlockEntity extends BlockEntity - implements PlatformNetworkNodeContainer { - private final T node; + implements NetworkNodeContainerBlockEntity { + protected static final String MAIN_CONTAINER_NAME = "main"; + + protected final T mainNode; + protected final InWorldNetworkNodeContainer mainContainer; + + @Nullable + protected Runnable initializationCallback; + + private final Set containers = new HashSet<>(); protected AbstractNetworkNodeContainerBlockEntity(final BlockEntityType type, final BlockPos pos, final BlockState state, - final T node) { + final T mainNode) { super(type, pos, state); - this.node = node; - } - - @Override - public void clearRemoved() { - super.clearRemoved(); - if (level == null || level.isClientSide) { - return; - } - PlatformApi.INSTANCE.requestNetworkNodeInitialization(this, level, this::onNetworkInNodeInitialized); + this.mainContainer = PlatformApi.INSTANCE.createInWorldNetworkNodeContainer( + this, + mainNode, + MAIN_CONTAINER_NAME, + getPriority(), + getConnectionLogic(), + this::createKey + ); + this.containers.add(mainContainer); + this.mainNode = mainNode; } - @Override - public boolean isContainerRemoved() { - return isRemoved(); + @Nullable + protected ConnectionLogic getConnectionLogic() { + return null; } - protected void onNetworkInNodeInitialized() { + @Nullable + protected Object createKey() { + return null; } - @Override - public void setRemoved() { - super.setRemoved(); - if (level == null || level.isClientSide) { - return; - } - PlatformApi.INSTANCE.requestNetworkNodeRemoval(this, level); + protected int getPriority() { + return 0; } @Override - public T getNode() { - return node; - } - - @Override - public void addOutgoingConnections(final ConnectionSink sink) { - for (final Direction direction : Direction.values()) { - sink.tryConnectInSameDimension(worldPosition.relative(direction), direction.getOpposite()); - } - } - - @Override - public boolean canAcceptIncomingConnection(final Direction incomingDirection, final BlockState connectingState) { - return true; - } - - @Override - public BlockState getContainerBlockState() { - return getBlockState(); + public void clearRemoved() { + super.clearRemoved(); + containers.forEach(container -> PlatformApi.INSTANCE.onNetworkNodeContainerInitialized( + container, + level, + initializationCallback + )); } @Override - public GlobalPos getContainerPosition() { - return GlobalPos.of(requireNonNull(level).dimension(), worldPosition); + public void setRemoved() { + super.setRemoved(); + containers.forEach(container -> PlatformApi.INSTANCE.onNetworkNodeContainerRemoved(container, level)); } @Override - public String toString() { - return "NetworkNodeContainerBlockEntity{" - + "node=" + node - + ", worldPosition=" + worldPosition - + "}"; + public Set getContainers() { + return containers; } } diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/PlatformNetworkNodeContainer.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/ConnectionLogic.java similarity index 57% rename from refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/PlatformNetworkNodeContainer.java rename to refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/ConnectionLogic.java index e1151c134..e364a0136 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/PlatformNetworkNodeContainer.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/ConnectionLogic.java @@ -1,24 +1,18 @@ package com.refinedmods.refinedstorage2.platform.api.support.network; -import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer; -import com.refinedmods.refinedstorage2.platform.api.PlatformApi; -import com.refinedmods.refinedstorage2.platform.api.security.SecurityHelper; - import net.minecraft.core.Direction; -import net.minecraft.core.GlobalPos; -import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; import org.apiguardian.api.API; -@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.1") -public interface PlatformNetworkNodeContainer extends NetworkNodeContainer { +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.6") +public interface ConnectionLogic { /** - * Called when a node is about to be added into the network graph. * Add outgoing connections here. + * Called when a node is about to be added into the network graph. * If the outgoing connections ever change, call {@link - * com.refinedmods.refinedstorage2.platform.api.PlatformApi#requestNetworkNodeUpdate( - *PlatformNetworkNodeContainer, Level)}. + * com.refinedmods.refinedstorage2.platform.api.PlatformApi#onNetworkNodeContainerUpdated( + *InWorldNetworkNodeContainer, Level)}. * * @param sink the sink that accepts outgoing connections */ @@ -28,22 +22,12 @@ public interface PlatformNetworkNodeContainer extends NetworkNodeContainer { * Returns whether the (not yet discovered) node can accept an incoming connection. * Changes to this return value won't cause a rebuild of the network state. * If the return value ever changes, call {@link - * com.refinedmods.refinedstorage2.platform.api.PlatformApi#requestNetworkNodeUpdate( - *PlatformNetworkNodeContainer, Level)}. + * com.refinedmods.refinedstorage2.platform.api.PlatformApi#onNetworkNodeContainerUpdated( + *InWorldNetworkNodeContainer, Level)}. * * @param incomingDirection the incoming direction * @param connectingState the state wanting to connect * @return whether the node can accept an incoming connection */ boolean canAcceptIncomingConnection(Direction incomingDirection, BlockState connectingState); - - BlockState getContainerBlockState(); - - GlobalPos getContainerPosition(); - - boolean isContainerRemoved(); - - default boolean canBreakOrRotate(final ServerPlayer player) { - return SecurityHelper.isAllowed(player, PlatformApi.INSTANCE.getBuiltinPermissions().build(), getNode()); - } } diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/InWorldNetworkNodeContainer.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/InWorldNetworkNodeContainer.java new file mode 100644 index 000000000..a72eb59b8 --- /dev/null +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/InWorldNetworkNodeContainer.java @@ -0,0 +1,21 @@ +package com.refinedmods.refinedstorage2.platform.api.support.network; + +import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.GlobalPos; +import net.minecraft.world.level.block.state.BlockState; +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.2.1") +public interface InWorldNetworkNodeContainer extends NetworkNodeContainer, ConnectionLogic { + BlockState getBlockState(); + + boolean isRemoved(); + + GlobalPos getPosition(); + + BlockPos getLocalPosition(); + + String getName(); +} diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/NetworkNodeContainerBlockEntity.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/NetworkNodeContainerBlockEntity.java new file mode 100644 index 000000000..9dd1856bd --- /dev/null +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/NetworkNodeContainerBlockEntity.java @@ -0,0 +1,19 @@ +package com.refinedmods.refinedstorage2.platform.api.support.network; + +import com.refinedmods.refinedstorage2.platform.api.PlatformApi; +import com.refinedmods.refinedstorage2.platform.api.security.SecurityHelper; + +import java.util.Set; + +import net.minecraft.server.level.ServerPlayer; +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.6") +@FunctionalInterface +public interface NetworkNodeContainerBlockEntity { + Set getContainers(); + + default boolean canBuild(final ServerPlayer player) { + return SecurityHelper.isAllowed(player, PlatformApi.INSTANCE.getBuiltinPermissions().build(), getContainers()); + } +} diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/bounditem/NetworkBoundItemTargetBlockEntity.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/bounditem/NetworkBoundItemTargetBlockEntity.java new file mode 100644 index 000000000..265881940 --- /dev/null +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/support/network/bounditem/NetworkBoundItemTargetBlockEntity.java @@ -0,0 +1,13 @@ +package com.refinedmods.refinedstorage2.platform.api.support.network.bounditem; + +import com.refinedmods.refinedstorage2.api.network.Network; + +import javax.annotation.Nullable; + +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.6") +public interface NetworkBoundItemTargetBlockEntity { + @Nullable + Network getNetworkForBoundItem(); +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java index a346ee107..7d8409d4c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java @@ -98,7 +98,7 @@ import com.refinedmods.refinedstorage2.platform.common.support.SimpleBlock; import com.refinedmods.refinedstorage2.platform.common.support.SimpleItem; import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyLootItemFunction; -import com.refinedmods.refinedstorage2.platform.common.support.network.NetworkNodeContainerBlockEntityImpl; +import com.refinedmods.refinedstorage2.platform.common.support.network.BaseNetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage2.platform.common.support.network.bounditem.InventorySlotReferenceFactory; import com.refinedmods.refinedstorage2.platform.common.support.network.component.PlatformStorageNetworkComponent; import com.refinedmods.refinedstorage2.platform.common.support.resource.FluidResourceFactory; @@ -521,7 +521,7 @@ protected final void registerBlockEntities( ) { BlockEntities.INSTANCE.setCable(callback.register( CABLE, - () -> typeFactory.create((pos, state) -> new NetworkNodeContainerBlockEntityImpl<>( + () -> typeFactory.create((pos, state) -> new BaseNetworkNodeContainerBlockEntity<>( BlockEntities.INSTANCE.getCable(), pos, state, diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java index 803d80feb..cc96e28cc 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java @@ -7,6 +7,7 @@ import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; import com.refinedmods.refinedstorage2.api.network.impl.NetworkBuilderImpl; import com.refinedmods.refinedstorage2.api.network.impl.NetworkFactory; +import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import com.refinedmods.refinedstorage2.api.network.security.SecurityPolicy; import com.refinedmods.refinedstorage2.api.resource.ResourceKey; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; @@ -27,7 +28,6 @@ import com.refinedmods.refinedstorage2.platform.api.recipemod.IngredientConverter; import com.refinedmods.refinedstorage2.platform.api.security.BuiltinPermissions; import com.refinedmods.refinedstorage2.platform.api.security.PlatformPermission; -import com.refinedmods.refinedstorage2.platform.api.security.PlatformSecurityNetworkComponent; import com.refinedmods.refinedstorage2.platform.api.storage.StorageContainerItemHelper; import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository; import com.refinedmods.refinedstorage2.platform.api.storage.StorageType; @@ -35,7 +35,9 @@ import com.refinedmods.refinedstorage2.platform.api.storagemonitor.StorageMonitorExtractionStrategy; import com.refinedmods.refinedstorage2.platform.api.storagemonitor.StorageMonitorInsertionStrategy; import com.refinedmods.refinedstorage2.platform.api.support.energy.EnergyItemHelper; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionLogic; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.NetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemHelper; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReferenceFactory; @@ -66,6 +68,7 @@ import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemBlockEnergyStorage; import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage; import com.refinedmods.refinedstorage2.platform.common.support.network.ConnectionProviderImpl; +import com.refinedmods.refinedstorage2.platform.common.support.network.InWorldNetworkNodeContainerImpl; import com.refinedmods.refinedstorage2.platform.common.support.network.bounditem.CompositeSlotReferenceProvider; import com.refinedmods.refinedstorage2.platform.common.support.network.bounditem.InventorySlotReference; import com.refinedmods.refinedstorage2.platform.common.support.network.bounditem.NetworkBoundItemHelperImpl; @@ -90,7 +93,9 @@ import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; +import java.util.function.Supplier; import java.util.stream.Collectors; +import javax.annotation.Nullable; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -288,9 +293,24 @@ public BuiltinUpgradeDestinations getBuiltinUpgradeDestinations() { } @Override - public void requestNetworkNodeInitialization(final PlatformNetworkNodeContainer container, - final Level level, - final Runnable callback) { + public InWorldNetworkNodeContainer createInWorldNetworkNodeContainer( + final BlockEntity blockEntity, + final NetworkNode node, + final String name, + final int priority, + @Nullable final ConnectionLogic connectionLogic, + @Nullable final Supplier keyProvider + ) { + return new InWorldNetworkNodeContainerImpl(blockEntity, node, name, priority, connectionLogic, keyProvider); + } + + @Override + public void onNetworkNodeContainerInitialized(final InWorldNetworkNodeContainer container, + @Nullable final Level level, + @Nullable final Runnable callback) { + if (level == null || level.isClientSide()) { + return; + } final ConnectionProviderImpl connectionProvider = new ConnectionProviderImpl(level); ServerEventQueue.queue(() -> { // The container could've been removed by the time it has been placed, and by the time the event queue has @@ -298,16 +318,22 @@ public void requestNetworkNodeInitialization(final PlatformNetworkNodeContainer // This is a workaround for the "Carry On" mod. The mod places the block (which creates a block entity and // requests this network node initialization) and then overrides the placed block entity with their own // block entity. This triggers a new initialization, but then this one can no longer run! - if (container.isContainerRemoved()) { + if (container.isRemoved()) { return; } networkBuilder.initialize(container, connectionProvider); - callback.run(); + if (callback != null) { + callback.run(); + } }); } @Override - public void requestNetworkNodeRemoval(final PlatformNetworkNodeContainer container, final Level level) { + public void onNetworkNodeContainerRemoved(final InWorldNetworkNodeContainer container, + @Nullable final Level level) { + if (level == null || level.isClientSide()) { + return; + } // "Carry On" mod places the block (which creates a block entity and requests network node initialization) // and then overrides the placed block entity with their own information. // However, when the placed block entity is replaced, the server event queue hasn't run yet and there is @@ -321,7 +347,11 @@ public void requestNetworkNodeRemoval(final PlatformNetworkNodeContainer contain } @Override - public void requestNetworkNodeUpdate(final PlatformNetworkNodeContainer container, final Level level) { + public void onNetworkNodeContainerUpdated(final InWorldNetworkNodeContainer container, + @Nullable final Level level) { + if (level == null || level.isClientSide() || container.getNode().getNetwork() == null) { + return; + } final ConnectionProviderImpl connectionProvider = new ConnectionProviderImpl(level); networkBuilder.update(container, connectionProvider); } @@ -557,16 +587,10 @@ public boolean canPlaceNetworkNode(final ServerPlayer player, for (final Direction direction : Direction.values()) { final BlockPos adjacentPos = pos.relative(direction); final BlockEntity adjacentBlockEntity = level.getBlockEntity(adjacentPos); - if (!(adjacentBlockEntity instanceof PlatformNetworkNodeContainer platformNetworkNodeContainer) - || !platformNetworkNodeContainer.canAcceptIncomingConnection(direction.getOpposite(), state) - || platformNetworkNodeContainer.getNode().getNetwork() == null) { + if (!(adjacentBlockEntity instanceof NetworkNodeContainerBlockEntity adjacentContainerBlockEntity)) { continue; } - final PlatformSecurityNetworkComponent security = platformNetworkNodeContainer - .getNode() - .getNetwork() - .getComponent(PlatformSecurityNetworkComponent.class); - if (!security.isAllowed(BuiltinPermission.BUILD, player)) { + if (!adjacentContainerBlockEntity.canBuild(player)) { PlatformApi.INSTANCE.sendNoPermissionMessage( player, createTranslation("misc", "no_permission.build.place", state.getBlock().getName()) diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/ConstructorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/ConstructorBlockEntity.java index 05e3eae2d..4c870f75e 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/ConstructorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/ConstructorBlockEntity.java @@ -53,7 +53,7 @@ public ConstructorBlockEntity(final BlockPos pos, final BlockState state) { new SimpleNetworkNode(Platform.INSTANCE.getConfig().getConstructor().getEnergyUsage()), UpgradeDestinations.CONSTRUCTOR ); - this.actor = new NetworkNodeActor(getNode()); + this.actor = new NetworkNodeActor(mainNode); } @Override @@ -87,14 +87,14 @@ private ConstructorStrategy createStrategy(final ServerLevel serverLevel, final @Override public void postDoWork() { if (taskExecutor == null - || getNode().getNetwork() == null - || !getNode().isActive() + || mainNode.getNetwork() == null + || !mainNode.isActive() || !(level instanceof ServerLevel serverLevel)) { return; } final Player fakePlayer = getFakePlayer(serverLevel); taskExecutor.execute(tasks, new TaskContext( - getNode().getNetwork(), + mainNode.getNetwork(), fakePlayer )); } @@ -139,7 +139,7 @@ public AbstractContainerMenu createMenu(final int syncId, final Inventory invent @Override protected void setEnergyUsage(final long upgradeEnergyUsage) { final long baseEnergyUsage = Platform.INSTANCE.getConfig().getConstructor().getEnergyUsage(); - getNode().setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); + mainNode.setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/DestructorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/DestructorBlockEntity.java index 5f90c77bd..f75759a49 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/DestructorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/constructordestructor/DestructorBlockEntity.java @@ -53,7 +53,7 @@ public DestructorBlockEntity(final BlockPos pos, final BlockState state) { new SimpleNetworkNode(Platform.INSTANCE.getConfig().getDestructor().getEnergyUsage()), UpgradeDestinations.DESTRUCTOR ); - this.actor = new NetworkNodeActor(getNode()); + this.actor = new NetworkNodeActor(mainNode); this.filterWithFuzzyMode = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(), this::setChanged, @@ -105,7 +105,7 @@ public void readConfiguration(final CompoundTag tag) { @Override protected void setEnergyUsage(final long upgradeEnergyUsage) { final long baseEnergyUsage = Platform.INSTANCE.getConfig().getDestructor().getEnergyUsage(); - getNode().setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); + mainNode.setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); } @Override @@ -144,13 +144,13 @@ protected void initialize(final ServerLevel level, final Direction direction) { @Override public void postDoWork() { if (strategy == null - || getNode().getNetwork() == null - || !getNode().isActive() + || mainNode.getNetwork() == null + || !mainNode.isActive() || !(level instanceof ServerLevel serverLevel)) { return; } final Player fakePlayer = getFakePlayer(serverLevel); - strategy.apply(filter, actor, getNode()::getNetwork, fakePlayer); + strategy.apply(filter, actor, mainNode::getNetwork, fakePlayer); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/BlockEntities.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/BlockEntities.java index 3868df5bb..3d8ccc2a5 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/BlockEntities.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/BlockEntities.java @@ -21,7 +21,7 @@ import com.refinedmods.refinedstorage2.platform.common.storage.storageblock.FluidStorageBlockBlockEntity; import com.refinedmods.refinedstorage2.platform.common.storage.storageblock.ItemStorageBlockBlockEntity; import com.refinedmods.refinedstorage2.platform.common.storagemonitor.StorageMonitorBlockEntity; -import com.refinedmods.refinedstorage2.platform.common.support.network.NetworkNodeContainerBlockEntityImpl; +import com.refinedmods.refinedstorage2.platform.common.support.network.BaseNetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage2.platform.common.wirelesstransmitter.WirelessTransmitterBlockEntity; import java.util.EnumMap; @@ -37,7 +37,7 @@ public final class BlockEntities { public static final BlockEntities INSTANCE = new BlockEntities(); @Nullable - private Supplier>> cable; + private Supplier>> cable; @Nullable private Supplier> diskDrive; @Nullable @@ -84,12 +84,12 @@ public final class BlockEntities { private BlockEntities() { } - public BlockEntityType> getCable() { + public BlockEntityType> getCable() { return requireNonNull(cable).get(); } public void setCable( - final Supplier>> supplier + final Supplier>> supplier ) { this.cable = supplier; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java index e7d99bc8c..151bcea6c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/controller/ControllerBlockEntity.java @@ -42,7 +42,7 @@ public ControllerBlockEntity(final ControllerType type, final BlockPos pos, fina super(getBlockEntityType(type), pos, state, new ControllerNetworkNode()); this.type = type; this.energyStorage = createEnergyStorage(type, this); - this.getNode().setEnergyStorage(energyStorage); + this.mainNode.setEnergyStorage(energyStorage); } private static EnergyStorage createEnergyStorage(final ControllerType type, final BlockEntity blockEntity) { @@ -63,7 +63,7 @@ private static BlockEntityType getBlockEntityType(final C public void updateEnergyTypeInLevel(final BlockState state) { final ControllerEnergyType currentEnergyType = state.getValue(AbstractControllerBlock.ENERGY_TYPE); - final ControllerEnergyType newEnergyType = ControllerEnergyType.ofState(getNode().getState()); + final ControllerEnergyType newEnergyType = ControllerEnergyType.ofState(mainNode.getState()); if (newEnergyType != currentEnergyType && level != null && energyStateChangeRateLimiter.tryAcquire()) { LOGGER.debug( "Energy type state change for controller at {}: {} -> {}", @@ -78,12 +78,12 @@ public void updateEnergyTypeInLevel(final BlockState state) { @Override public void saveAdditional(final CompoundTag tag) { super.saveAdditional(tag); - ItemBlockEnergyStorage.writeToTag(tag, getNode().getActualStored()); + ItemBlockEnergyStorage.writeToTag(tag, mainNode.getActualStored()); saveRenderingInfo(tag); } private void saveRenderingInfo(final CompoundTag tag) { - tag.putLong(TAG_CAPACITY, getNode().getActualCapacity()); + tag.putLong(TAG_CAPACITY, mainNode.getActualCapacity()); } @Override @@ -109,11 +109,11 @@ public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByte } long getActualStored() { - return getNode().getActualStored(); + return mainNode.getActualStored(); } long getActualCapacity() { - return getNode().getActualCapacity(); + return mainNode.getActualCapacity(); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/detector/DetectorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/detector/DetectorBlockEntity.java index 87bf4875c..23ffabe08 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/detector/DetectorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/detector/DetectorBlockEntity.java @@ -57,7 +57,7 @@ public DetectorBlockEntity(final BlockPos pos, final BlockState state) { propagateAmount(); setChanged(); }, - filters -> getNode().setConfiguredResource(filters.isEmpty() ? null : filters.get(0)) + filters -> mainNode.setConfiguredResource(filters.isEmpty() ? null : filters.get(0)) ); initialize(); } @@ -67,7 +67,7 @@ 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())); + tag.putInt(TAG_MODE, DetectorModeSettings.getDetectorMode(mainNode.getMode())); } @Override @@ -78,7 +78,7 @@ public void readConfiguration(final CompoundTag tag) { this.amount = tag.getDouble(TAG_AMOUNT); } if (tag.contains(TAG_MODE)) { - getNode().setMode(DetectorModeSettings.getDetectorMode(tag.getInt(TAG_MODE))); + mainNode.setMode(DetectorModeSettings.getDetectorMode(tag.getInt(TAG_MODE))); } initialize(); propagateAmount(); @@ -96,7 +96,7 @@ private void propagateAmount() { ? (long) amount : configuredResource.getResourceType().normalizeAmount(amount); LOGGER.debug("Updating detector amount of {} normalized as {}", amount, normalizedAmount); - getNode().setAmount(normalizedAmount); + mainNode.setAmount(normalizedAmount); } boolean isFuzzyMode() { @@ -109,12 +109,12 @@ void setFuzzyMode(final boolean fuzzyMode) { } void setMode(final DetectorMode mode) { - getNode().setMode(mode); + mainNode.setMode(mode); setChanged(); } DetectorMode getMode() { - return getNode().getMode(); + return mainNode.getMode(); } private void initialize() { @@ -122,7 +122,7 @@ private void initialize() { final DetectorAmountStrategy strategy = isFuzzyMode() ? new FuzzyDetectorAmountStrategy(defaultStrategy) : defaultStrategy; - getNode().setAmountStrategy(strategy); + mainNode.setAmountStrategy(strategy); } @Override @@ -144,7 +144,7 @@ public AbstractContainerMenu createMenu(final int syncId, final Inventory invent @Override public void updateActiveness(final BlockState state, @Nullable final BooleanProperty activenessProperty) { super.updateActiveness(state, activenessProperty); - final boolean powered = getNode().isActive() && getNode().isActivated(); + final boolean powered = mainNode.isActive() && mainNode.isActivated(); final boolean needToUpdatePowered = state.getValue(DetectorBlock.POWERED) != powered; if (level != null && needToUpdatePowered && poweredChangeRateLimiter.tryAcquire()) { level.setBlockAndUpdate(getBlockPos(), state.setValue(DetectorBlock.POWERED, powered)); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterBlockEntity.java index 36bf2469d..5ea3d2f31 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/exporter/ExporterBlockEntity.java @@ -50,7 +50,7 @@ public ExporterBlockEntity(final BlockPos pos, final BlockState state) { protected void initialize(final ServerLevel level, final Direction direction) { final ExporterTransferStrategy strategy = createStrategy(level, direction); LOGGER.debug("Initialized exporter at {} with strategy {}", worldPosition, strategy); - getNode().setTransferStrategy(strategy); + mainNode.setTransferStrategy(strategy); } private ExporterTransferStrategy createStrategy(final ServerLevel serverLevel, final Direction direction) { @@ -75,7 +75,7 @@ private ExporterTransferStrategy createStrategy(final ServerLevel serverLevel, f @Override protected void setEnergyUsage(final long upgradeEnergyUsage) { final long baseEnergyUsage = Platform.INSTANCE.getConfig().getExporter().getEnergyUsage(); - getNode().setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); + mainNode.setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); } @Override @@ -91,12 +91,12 @@ public AbstractContainerMenu createMenu(final int syncId, final Inventory invent @Override protected void setTaskExecutor(final TaskExecutor taskExecutor) { - getNode().setTaskExecutor(taskExecutor); + mainNode.setTaskExecutor(taskExecutor); } @Override protected void setFilters(final List filters) { - getNode().setFilters(filters); + mainNode.setFilters(filters); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java index ba9054fae..58e6b01d2 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java @@ -46,14 +46,14 @@ public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByte @Override public List getResources(final Class actorType) { - return requireNonNull(getNode().getNetwork()) + return requireNonNull(mainNode.getNetwork()) .getComponent(StorageNetworkComponent.class) .getResources(actorType); } @Override public GridOperations createOperations(final ResourceType resourceType, final ServerPlayer player) { - final Network network = requireNonNull(getNode().getNetwork()); + final Network network = requireNonNull(mainNode.getNetwork()); final StorageChannel storageChannel = network.getComponent(StorageNetworkComponent.class); final PlatformSecurityNetworkComponent security = network.getComponent(PlatformSecurityNetworkComponent.class); final GridOperations operations = resourceType.createGridOperations(storageChannel, new PlayerActor(player)); @@ -62,22 +62,22 @@ public GridOperations createOperations(final ResourceType resourceType, final Se @Override public boolean isGridActive() { - return getNode().isActive(); + return mainNode.isActive(); } @Override public Storage getItemStorage() { - return requireNonNull(getNode().getNetwork()).getComponent(StorageNetworkComponent.class); + return requireNonNull(mainNode.getNetwork()).getComponent(StorageNetworkComponent.class); } @Override public void addWatcher(final GridWatcher watcher, final Class actorType) { - getNode().addWatcher(watcher, actorType); + mainNode.addWatcher(watcher, actorType); } @Override public void removeWatcher(final GridWatcher watcher) { - getNode().removeWatcher(watcher); + mainNode.removeWatcher(watcher); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/CraftingGridBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/CraftingGridBlockEntity.java index e331a52e4..8b092ef68 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/CraftingGridBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/CraftingGridBlockEntity.java @@ -2,7 +2,6 @@ import com.refinedmods.refinedstorage2.api.core.Action; import com.refinedmods.refinedstorage2.api.network.Network; -import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridNetworkNode; import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; import com.refinedmods.refinedstorage2.platform.api.storage.PlayerActor; @@ -129,11 +128,10 @@ public void setLevel(final Level level) { } Optional getNetwork() { - final GridNetworkNode node = getNode(); - if (!node.isActive()) { + if (!mainNode.isActive()) { return Optional.empty(); } - return Optional.ofNullable(node.getNetwork()); + return Optional.ofNullable(mainNode.getNetwork()); } Optional getStorageChannel() { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceBlockEntity.java index c09b5ad03..71a15ddaa 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceBlockEntity.java @@ -49,13 +49,13 @@ public InterfaceBlockEntity(final BlockPos pos, final BlockState state) { state, new InterfaceNetworkNode(Platform.INSTANCE.getConfig().getInterface().getEnergyUsage()) ); - getNode().setTransferQuotaProvider(InterfaceBlockEntity::getTransferQuota); + this.mainNode.setTransferQuotaProvider(InterfaceBlockEntity::getTransferQuota); this.filter = FilterWithFuzzyMode.create(createFilterContainer(), this::setChanged); this.exportedResources = createExportedResourcesContainer(filter); this.exportedResources.setListener(this::setChanged); - getNode().setExportState(exportedResources); + this.mainNode.setExportState(exportedResources); this.exportedResourcesAsContainer = exportedResources.toItemContainer(); - this.externalStorageProvider = new InterfaceExternalStorageProviderImpl(getNode()); + this.externalStorageProvider = new InterfaceExternalStorageProviderImpl(mainNode); } static ResourceContainer createFilterContainer() { @@ -156,4 +156,8 @@ public NonNullList getDrops() { InterfaceExternalStorageProvider getExternalStorageProvider() { return externalStorageProvider; } + + InterfaceNetworkNode getInterface() { + return mainNode; + } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceProxyExternalStorageProvider.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceProxyExternalStorageProvider.java index 329681e7c..4eeaf204f 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceProxyExternalStorageProvider.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/iface/InterfaceProxyExternalStorageProvider.java @@ -6,7 +6,6 @@ import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; import com.refinedmods.refinedstorage2.api.resource.ResourceKey; import com.refinedmods.refinedstorage2.api.storage.Actor; -import com.refinedmods.refinedstorage2.platform.api.support.network.AbstractNetworkNodeContainerBlockEntity; import java.util.Collections; import java.util.Iterator; @@ -54,6 +53,6 @@ public Iterator iterator() { @Override @Nullable public InterfaceNetworkNode getInterface() { - return tryGetInterface().map(AbstractNetworkNodeContainerBlockEntity::getNode).orElse(null); + return tryGetInterface().map(InterfaceBlockEntity::getInterface).orElse(null); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java index 20aedfa69..1945dfab1 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/importer/ImporterBlockEntity.java @@ -57,16 +57,16 @@ public ImporterBlockEntity(final BlockPos pos, final BlockState state) { this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(), this::setChanged, - filters -> getNode().setFilters(filters) + mainNode::setFilters ); - getNode().setNormalizer(filter.createNormalizer()); + this.mainNode.setNormalizer(filter.createNormalizer()); } @Override protected void initialize(final ServerLevel level, final Direction direction) { final List strategies = createStrategies(level, direction); LOGGER.debug("Initialized importer at {} with strategies {}", worldPosition, strategies); - getNode().setTransferStrategies(strategies); + mainNode.setTransferStrategies(strategies); } private List createStrategies(final ServerLevel serverLevel, final Direction direction) { @@ -83,7 +83,7 @@ private List createStrategies(final ServerLevel server @Override public void writeConfiguration(final CompoundTag tag) { super.writeConfiguration(tag); - tag.putInt(TAG_FILTER_MODE, FilterModeSettings.getFilterMode(getNode().getFilterMode())); + tag.putInt(TAG_FILTER_MODE, FilterModeSettings.getFilterMode(mainNode.getFilterMode())); filter.save(tag); } @@ -91,7 +91,7 @@ public void writeConfiguration(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))); + mainNode.setFilterMode(FilterModeSettings.getFilterMode(tag.getInt(TAG_FILTER_MODE))); } filter.load(tag); } @@ -105,18 +105,18 @@ void setFuzzyMode(final boolean fuzzyMode) { } FilterMode getFilterMode() { - return getNode().getFilterMode(); + return mainNode.getFilterMode(); } void setFilterMode(final FilterMode mode) { - getNode().setFilterMode(mode); + mainNode.setFilterMode(mode); setChanged(); } @Override protected void setEnergyUsage(final long upgradeEnergyUsage) { final long baseEnergyUsage = Platform.INSTANCE.getConfig().getImporter().getEnergyUsage(); - getNode().setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); + mainNode.setEnergyUsage(baseEnergyUsage + upgradeEnergyUsage); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/CableBlock.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/CableBlock.java index 06ecfa786..7cad3973c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/CableBlock.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/CableBlock.java @@ -13,8 +13,8 @@ import com.refinedmods.refinedstorage2.platform.common.support.CableShapeCacheKey; import com.refinedmods.refinedstorage2.platform.common.support.ColorableBlock; import com.refinedmods.refinedstorage2.platform.common.support.NetworkNodeBlockItem; +import com.refinedmods.refinedstorage2.platform.common.support.network.BaseNetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage2.platform.common.support.network.NetworkNodeBlockEntityTicker; -import com.refinedmods.refinedstorage2.platform.common.support.network.NetworkNodeContainerBlockEntityImpl; import javax.annotation.Nullable; @@ -43,7 +43,7 @@ public class CableBlock extends AbstractColoredBlock implements ColorableBlock, SimpleWaterloggedBlock, EntityBlock { - private static final AbstractBlockEntityTicker> TICKER = + private static final AbstractBlockEntityTicker> TICKER = new NetworkNodeBlockEntityTicker<>(BlockEntities.INSTANCE::getCable); public CableBlock(final DyeColor color, final MutableComponent name) { @@ -111,7 +111,7 @@ public VoxelShape getShape(final BlockState state, @Override public BlockEntity newBlockEntity(final BlockPos pos, final BlockState state) { - return new NetworkNodeContainerBlockEntityImpl<>( + return new BaseNetworkNodeContainerBlockEntity<>( BlockEntities.INSTANCE.getCable(), pos, state, diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkReceiverBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkReceiverBlockEntity.java index 920d77dc4..6e53ae79c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkReceiverBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkReceiverBlockEntity.java @@ -3,14 +3,14 @@ import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode; import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities; -import com.refinedmods.refinedstorage2.platform.common.support.network.NetworkNodeContainerBlockEntityImpl; +import com.refinedmods.refinedstorage2.platform.common.support.network.BaseNetworkNodeContainerBlockEntity; import javax.annotation.Nullable; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.state.BlockState; -public class NetworkReceiverBlockEntity extends NetworkNodeContainerBlockEntityImpl { +public class NetworkReceiverBlockEntity extends BaseNetworkNodeContainerBlockEntity { public NetworkReceiverBlockEntity(final BlockPos pos, final BlockState state) { super( BlockEntities.INSTANCE.getNetworkReceiver(), @@ -23,6 +23,6 @@ public NetworkReceiverBlockEntity(final BlockPos pos, final BlockState state) { @Nullable @Override public Object createKey() { - return new NetworkReceiverKey(getContainerPosition()); + return new NetworkReceiverKey(mainContainer.getPosition()); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkTransmitterBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkTransmitterBlockEntity.java index 37ad5e2cf..4433c8323 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkTransmitterBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/networking/NetworkTransmitterBlockEntity.java @@ -64,11 +64,11 @@ public NetworkTransmitterBlockEntity(final BlockPos pos, final BlockState state) Platform.INSTANCE.getConfig().getNetworkTransmitter().getEnergyUsage() )); networkCardInventory.addListener(container -> { - setChanged(); updateReceiverLocation(); if (level != null) { LOGGER.debug("Network card was changed at {}, sending network update", worldPosition); - PlatformApi.INSTANCE.requestNetworkNodeUpdate(this, level); + setChanged(); + PlatformApi.INSTANCE.onNetworkNodeContainerUpdated(mainContainer, level); } }); } @@ -76,10 +76,7 @@ public NetworkTransmitterBlockEntity(final BlockPos pos, final BlockState state) @Override protected void activenessChanged(final boolean newActive) { super.activenessChanged(newActive); - if (level == null) { - return; - } - PlatformApi.INSTANCE.requestNetworkNodeUpdate(this, level); + PlatformApi.INSTANCE.onNetworkNodeContainerUpdated(mainContainer, level); } public void updateStateInLevel(final BlockState state) { @@ -98,7 +95,7 @@ private NetworkTransmitterState getState() { if (receiverKey == null) { return NetworkTransmitterState.ERROR; } - final Network network = getNode().getNetwork(); + final Network network = mainNode.getNetwork(); if (network == null) { return NetworkTransmitterState.ERROR; } @@ -107,7 +104,7 @@ private NetworkTransmitterState getState() { } NetworkTransmitterStatus getStatus() { - final Network network = getNode().getNetwork(); + final Network network = mainNode.getNetwork(); if (!isActive() || network == null || level == null) { return INACTIVE; } @@ -129,25 +126,22 @@ NetworkTransmitterStatus getStatus() { @Override public void doWork() { super.doWork(); - if (!isActive() || getNode().getNetwork() == null || receiverKey == null) { + if (!isActive() || mainNode.getNetwork() == null || receiverKey == null) { return; } - final boolean receiverFound = isReceiverFoundInNetwork(getNode().getNetwork(), receiverKey); + final boolean receiverFound = isReceiverFoundInNetwork(mainNode.getNetwork(), receiverKey); if (!receiverFound && networkRebuildRetryRateLimiter.tryAcquire()) { tryReconnectingWithReceiver(); } } private void tryReconnectingWithReceiver() { - if (level == null) { - return; - } LOGGER.debug( "Receiver {} was not found in network for transmitter at {}, retrying and sending network update", receiverKey, worldPosition ); - PlatformApi.INSTANCE.requestNetworkNodeUpdate(this, level); + PlatformApi.INSTANCE.onNetworkNodeContainerUpdated(mainContainer, level); } private static boolean isReceiverFoundInNetwork(final Network network, final NetworkReceiverKey key) { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/security/SecurityManagerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/security/SecurityManagerBlockEntity.java index 929c2a1bc..cceb99850 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/security/SecurityManagerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/security/SecurityManagerBlockEntity.java @@ -60,7 +60,7 @@ public SecurityManagerBlockEntity(final BlockPos pos, final BlockState state) { ); securityCards.addListener(c -> invalidate()); fallbackSecurityCard.addListener(c -> invalidate()); - getNode().setDelegate(securityDecisionProvider); + mainNode.setDelegate(securityDecisionProvider); } private void invalidate() { @@ -80,7 +80,7 @@ private void invalidate() { actor -> securityDecisionProvider.setPolicy(actor, policy))); } energyUsage += updateDefaultPolicyAndGetEnergyUsage(); - getNode().setEnergyUsage(energyUsage); + mainNode.setEnergyUsage(energyUsage); } private long updateDefaultPolicyAndGetEnergyUsage() { @@ -161,13 +161,13 @@ public AbstractContainerMenu createMenu(final int syncId, final Inventory invent @Override public boolean canOpen(final ServerPlayer player) { final boolean isAllowedViaSecuritySystem = NetworkNodeMenuProvider.super.canOpen(player) - && SecurityHelper.isAllowed(player, BuiltinPermission.SECURITY, getNode()); + && SecurityHelper.isAllowed(player, BuiltinPermission.SECURITY, getContainers()); return isAllowedViaSecuritySystem || isPlacedBy(player.getGameProfile().getId()); } @Override - public boolean canBreakOrRotate(final ServerPlayer player) { - return super.canBreakOrRotate(player) || isPlacedBy(player.getGameProfile().getId()); + public boolean canBuild(final ServerPlayer player) { + return super.canBuild(player) || isPlacedBy(player.getGameProfile().getId()); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java index 872113d49..ca814bdb5 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java @@ -60,21 +60,24 @@ protected AbstractDiskDriveBlockEntity(final BlockPos pos, final BlockState stat Platform.INSTANCE.getConfig().getDiskDrive().getEnergyUsagePerDisk(), AMOUNT_OF_DISKS )); - this.diskInventory = new DiskInventory((inventory, slot) -> onDiskChanged(slot), getNode().getSize()); + this.diskInventory = new DiskInventory((inventory, slot) -> onDiskChanged(slot), mainNode.getSize()); this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(), this::setChanged, - filters -> getNode().setFilters(filters) + mainNode::setFilters ); this.configContainer = new StorageConfigurationContainerImpl( - getNode(), + mainNode, filter, this::setChanged, this::getRedstoneMode, this::setRedstoneMode ); - getNode().setListener(diskStateListener); - getNode().setNormalizer(filter.createNormalizer()); + this.mainNode.setListener(diskStateListener); + this.mainNode.setNormalizer(filter.createNormalizer()); + // It's important to sync here as the initial update packet might have failed as the network + // could possibly be not initialized yet. + this.initializationCallback = diskStateListener::immediateUpdate; } @Nullable @@ -123,7 +126,7 @@ public void setChanged() { private void initialize(final Level level) { diskInventory.setStorageRepository(PlatformApi.INSTANCE.getStorageRepository(level)); - getNode().setProvider(diskInventory); + mainNode.setProvider(diskInventory); } @Override @@ -169,23 +172,15 @@ private void onDiskChanged(final int slot) { // Level will not yet be present final boolean isJustPlacedIntoLevelOrLoading = level == null || level.isClientSide(); // Level will be present, but network not yet - final boolean isPlacedThroughDismantlingMode = getNode().getNetwork() == null; + final boolean isPlacedThroughDismantlingMode = mainNode.getNetwork() == null; if (isJustPlacedIntoLevelOrLoading || isPlacedThroughDismantlingMode) { return; } - getNode().onStorageChanged(slot); + mainNode.onStorageChanged(slot); diskStateListener.immediateUpdate(); setChanged(); } - @Override - protected void onNetworkInNodeInitialized() { - super.onNetworkInNodeInitialized(); - // It's important to sync here as the initial update packet might have failed as the network - // could possibly be not initialized yet. - diskStateListener.immediateUpdate(); - } - private void fromClientTag(final CompoundTag tag) { if (!tag.contains(TAG_DISKS)) { return; @@ -207,10 +202,10 @@ public Packet getUpdatePacket() { public CompoundTag getUpdateTag() { final CompoundTag tag = new CompoundTag(); // This null check is important. #getUpdateTag() can be called before the node's network is initialized! - if (getNode().getNetwork() == null) { + if (mainNode.getNetwork() == null) { return tag; } - tag.put(TAG_DISKS, diskInventory.toSyncTag(getNode()::getState)); + tag.put(TAG_DISKS, diskInventory.toSyncTag(mainNode::getState)); return tag; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/externalstorage/ExternalStorageBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/externalstorage/ExternalStorageBlockEntity.java index d037b1834..f29fcb6fd 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/externalstorage/ExternalStorageBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/externalstorage/ExternalStorageBlockEntity.java @@ -50,12 +50,12 @@ public ExternalStorageBlockEntity(final BlockPos pos, final BlockState state) { this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(), this::setChanged, - filters -> getNode().setFilters(filters) + mainNode::setFilters ); - getNode().setNormalizer(filter.createNormalizer()); - getNode().setTrackingRepository(trackedStorageRepository); + mainNode.setNormalizer(filter.createNormalizer()); + mainNode.setTrackingRepository(trackedStorageRepository); this.configContainer = new StorageConfigurationContainerImpl( - getNode(), + mainNode, filter, this::setChanged, this::getRedstoneMode, @@ -89,7 +89,7 @@ void loadStorage(final ServerLevel serverLevel) { if (direction == null) { return; } - getNode().initialize(() -> { + mainNode.initialize(() -> { final Direction incomingDirection = direction.getOpposite(); final BlockPos sourcePosition = worldPosition.relative(direction); return PlatformApi.INSTANCE @@ -104,7 +104,7 @@ void loadStorage(final ServerLevel serverLevel) { public void doWork() { super.doWork(); if (workRate.canDoWork()) { - final boolean hasChanges = getNode().detectChanges(); + final boolean hasChanges = mainNode.detectChanges(); if (hasChanges) { LOGGER.debug("External storage @ {} has changed!", worldPosition); workRate.faster(); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/storageblock/AbstractStorageBlockBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/storageblock/AbstractStorageBlockBlockEntity.java index 4063cd07a..c0d533097 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/storageblock/AbstractStorageBlockBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/storageblock/AbstractStorageBlockBlockEntity.java @@ -49,16 +49,16 @@ protected AbstractStorageBlockBlockEntity(final BlockEntityType type, this.filter = FilterWithFuzzyMode.createAndListenForUniqueFilters( ResourceContainerImpl.createForFilter(resourceFactory), this::setChanged, - filters -> getNode().setFilters(filters) + mainNode::setFilters ); this.configContainer = new StorageConfigurationContainerImpl( - getNode(), + mainNode, filter, this::setChanged, this::getRedstoneMode, this::setRedstoneMode ); - getNode().setNormalizer(filter.createNormalizer()); + mainNode.setNormalizer(filter.createNormalizer()); } protected abstract Storage createStorage(Runnable listener); @@ -80,11 +80,11 @@ public void setLevel(final Level level) { storageId = UUID.randomUUID(); final Storage storage = createStorage(storageRepository::markAsChanged); storageRepository.set(storageId, storage); - getNode().setStorage(storage); + mainNode.setStorage(storage); } else { // The existing block entity got loaded in the level (#load(CompoundTag) -> #setLevel(Level)). storageRepository.get(storageId).ifPresentOrElse( - storage -> getNode().setStorage(storage), + mainNode::setStorage, () -> LOGGER.warn("Storage {} could not be resolved", storageId) ); } @@ -136,7 +136,7 @@ private void cleanupUnneededInitialStorageAndReinitialize(final UUID actualStora () -> LOGGER.warn("Unneeded storage {} could not be removed", storageId) ); storageRepository.get(actualStorageId).ifPresentOrElse( - storage -> getNode().setStorage(storage), + mainNode::setStorage, () -> LOGGER.warn("Actual storage ID {} could not be resolved!", actualStorageId) ); } @@ -173,8 +173,8 @@ protected final ResourceContainer getFilterContainer() { @Override public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) { - buf.writeLong(getNode().getStored()); - buf.writeLong(getNode().getCapacity()); + buf.writeLong(mainNode.getStored()); + buf.writeLong(mainNode.getCapacity()); filter.getFilterContainer().writeToUpdatePacket(buf); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storagemonitor/StorageMonitorBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storagemonitor/StorageMonitorBlockEntity.java index 316f0cd16..1043d23cb 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storagemonitor/StorageMonitorBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storagemonitor/StorageMonitorBlockEntity.java @@ -79,7 +79,7 @@ public void doWork() { private void trySendDisplayUpdate(final Level level) { final long amount = getAmount(); - final boolean active = getNode().isActive(); + final boolean active = mainNode.isActive(); if ((amount != currentAmount || active != currentlyActive) && displayUpdateRateLimiter.tryAcquire()) { sendDisplayUpdate(level, amount, active); } @@ -90,7 +90,7 @@ private long getAmount() { if (configuredResource == null) { return 0; } - final Network network = getNode().getNetwork(); + final Network network = mainNode.getNetwork(); if (network == null) { return 0; } @@ -109,7 +109,7 @@ public void extract(final Player player) { if (level == null) { return; } - final Network network = getNode().getNetwork(); + final Network network = mainNode.getNetwork(); if (network == null) { return; } @@ -154,7 +154,7 @@ public void insert(final Player player, final InteractionHand hand) { } private boolean doInsert(final Player player, final InteractionHand hand) { - final Network network = getNode().getNetwork(); + final Network network = mainNode.getNetwork(); if (network == null) { return false; } @@ -195,7 +195,7 @@ private boolean doInsertAll(final Player player) { } private boolean doInsertAll(final Player player, final ItemResource lastInsertedItem) { - final Network network = getNode().getNetwork(); + final Network network = mainNode.getNetwork(); if (network == null) { return false; } @@ -313,7 +313,7 @@ private void sendDisplayUpdate() { if (level == null) { return; } - sendDisplayUpdate(level, getAmount(), getNode().isActive()); + sendDisplayUpdate(level, getAmount(), mainNode.isActive()); } private void sendDisplayUpdate(final Level level, final long amount, final boolean active) { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/AbstractBaseBlock.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/AbstractBaseBlock.java index 59909a309..220180bcf 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/AbstractBaseBlock.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/AbstractBaseBlock.java @@ -1,8 +1,7 @@ package com.refinedmods.refinedstorage2.platform.common.support; -import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.NetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.content.BlockColorMap; import com.refinedmods.refinedstorage2.platform.common.content.Sounds; @@ -12,7 +11,6 @@ import javax.annotation.Nullable; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundSource; import net.minecraft.world.Containers; @@ -185,41 +183,19 @@ private boolean rotate(final BlockState state, final BlockPos pos, final ServerPlayer player) { final BlockEntity blockEntity = level.getBlockEntity(pos); - if (blockEntity instanceof PlatformNetworkNodeContainer platformNetworkNodeContainer) { - final Network network = platformNetworkNodeContainer.getNode().getNetwork(); - if (!platformNetworkNodeContainer.canBreakOrRotate(player) - || mightMakeConnectionWithAnotherSecuredNetwork(level, pos, player, network)) { - PlatformApi.INSTANCE.sendNoPermissionMessage( - player, - createTranslation("misc", "no_permission.build.rotate", getName()) - ); - return false; - } + if (blockEntity instanceof NetworkNodeContainerBlockEntity networkNodeContainerBlockEntity + && !networkNodeContainerBlockEntity.canBuild(player)) { + PlatformApi.INSTANCE.sendNoPermissionMessage( + player, + createTranslation("misc", "no_permission.build.rotate", getName()) + ); + return false; } final BlockState rotated = getRotatedBlockState(state, level, pos); level.setBlockAndUpdate(pos, rotated); return !state.equals(rotated); } - private boolean mightMakeConnectionWithAnotherSecuredNetwork(final Level level, - final BlockPos pos, - final ServerPlayer player, - @Nullable final Network rotatedNetwork) { - for (final Direction direction : Direction.values()) { - final BlockPos neighborPos = pos.relative(direction); - final BlockEntity neighborBlockEntity = level.getBlockEntity(neighborPos); - if (neighborBlockEntity instanceof PlatformNetworkNodeContainer neighborNetworkNodeContainer - && neighborNetworkNodeContainer.getNode().getNetwork() != rotatedNetwork) { - PlatformApi.INSTANCE.sendNoPermissionMessage( - player, - createTranslation("misc", "no_permission.build.rotate", getName()) - ); - return true; - } - } - return false; - } - @SuppressWarnings("deprecation") protected BlockState getRotatedBlockState(final BlockState state, final Level level, final BlockPos pos) { return state.rotate(Rotation.CLOCKWISE_90); @@ -234,8 +210,8 @@ private boolean dismantle(final BlockState state, final BlockHitResult hitResult, final ServerPlayer player) { final BlockEntity blockEntity = level.getBlockEntity(hitResult.getBlockPos()); - if (blockEntity instanceof PlatformNetworkNodeContainer platformNetworkNodeContainer - && !platformNetworkNodeContainer.canBreakOrRotate(player)) { + if (blockEntity instanceof NetworkNodeContainerBlockEntity networkNodeContainerBlockEntity + && !networkNodeContainerBlockEntity.canBuild(player)) { PlatformApi.INSTANCE.sendNoPermissionMessage( player, createTranslation("misc", "no_permission.build.dismantle", getName()) diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/CableBlockSupport.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/CableBlockSupport.java index d43160251..0142bc472 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/CableBlockSupport.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/CableBlockSupport.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage2.platform.common.support; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.NetworkNodeContainerBlockEntity; import java.util.HashMap; import java.util.Map; @@ -114,9 +114,11 @@ private static boolean hasVisualConnection( return false; } final BlockPos offsetPos = pos.relative(direction); - if (!(level.getBlockEntity(offsetPos) instanceof PlatformNetworkNodeContainer neighboringContainer)) { + if (!(level.getBlockEntity(offsetPos) instanceof NetworkNodeContainerBlockEntity neighbor)) { return false; } - return neighboringContainer.canAcceptIncomingConnection(direction.getOpposite(), blockState); + return neighbor.getContainers() + .stream() + .anyMatch(container -> container.canAcceptIncomingConnection(direction.getOpposite(), blockState)); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/containermenu/NetworkNodeMenuProvider.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/containermenu/NetworkNodeMenuProvider.java index c24cd3bb6..f8b3338d3 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/containermenu/NetworkNodeMenuProvider.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/containermenu/NetworkNodeMenuProvider.java @@ -1,15 +1,17 @@ package com.refinedmods.refinedstorage2.platform.common.support.containermenu; -import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; import com.refinedmods.refinedstorage2.platform.api.security.SecurityHelper; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; import com.refinedmods.refinedstorage2.platform.common.security.BuiltinPermission; +import java.util.Set; + import net.minecraft.server.level.ServerPlayer; public interface NetworkNodeMenuProvider extends ExtendedMenuProvider { - NetworkNode getNode(); + Set getContainers(); default boolean canOpen(final ServerPlayer player) { - return SecurityHelper.isAllowed(player, BuiltinPermission.OPEN, getNode()); + return SecurityHelper.isAllowed(player, BuiltinPermission.OPEN, getContainers()); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/AbstractRedstoneModeNetworkNodeContainerBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/AbstractRedstoneModeNetworkNodeContainerBlockEntity.java index d2d53be3e..f4393f4fa 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/AbstractRedstoneModeNetworkNodeContainerBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/AbstractRedstoneModeNetworkNodeContainerBlockEntity.java @@ -19,7 +19,7 @@ import net.minecraft.world.level.block.state.BlockState; public abstract class AbstractRedstoneModeNetworkNodeContainerBlockEntity - extends NetworkNodeContainerBlockEntityImpl implements PlayerAwareBlockEntity, ConfigurationCardTarget { + extends BaseNetworkNodeContainerBlockEntity implements PlayerAwareBlockEntity, ConfigurationCardTarget { private static final String TAG_REDSTONE_MODE = "rm"; private static final String TAG_PLACED_BY_PLAYER_ID = "pbpid"; diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeContainerBlockEntityImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/BaseNetworkNodeContainerBlockEntity.java similarity index 81% rename from refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeContainerBlockEntityImpl.java rename to refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/BaseNetworkNodeContainerBlockEntity.java index 47e629a7c..25abd396f 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeContainerBlockEntityImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/BaseNetworkNodeContainerBlockEntity.java @@ -1,10 +1,13 @@ package com.refinedmods.refinedstorage2.platform.common.support.network; +import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.network.energy.EnergyNetworkComponent; import com.refinedmods.refinedstorage2.api.network.impl.storage.AbstractNetworkNode; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; import com.refinedmods.refinedstorage2.platform.api.support.network.AbstractNetworkNodeContainerBlockEntity; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionLogic; import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionSink; +import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemTargetBlockEntity; import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock; import com.refinedmods.refinedstorage2.platform.common.support.ColorableBlock; @@ -19,30 +22,37 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class NetworkNodeContainerBlockEntityImpl - extends AbstractNetworkNodeContainerBlockEntity { - private static final Logger LOGGER = LoggerFactory.getLogger(NetworkNodeContainerBlockEntityImpl.class); +public class BaseNetworkNodeContainerBlockEntity + extends AbstractNetworkNodeContainerBlockEntity + implements ConnectionLogic, NetworkBoundItemTargetBlockEntity { + private static final Logger LOGGER = LoggerFactory.getLogger(BaseNetworkNodeContainerBlockEntity.class); private final RateLimiter activenessChangeRateLimiter = RateLimiter.create(1); - public NetworkNodeContainerBlockEntityImpl(final BlockEntityType type, + public BaseNetworkNodeContainerBlockEntity(final BlockEntityType type, final BlockPos pos, final BlockState state, final T networkNode) { super(type, pos, state, networkNode); } + @Nullable + @Override + protected ConnectionLogic getConnectionLogic() { + return this; + } + protected boolean isActive() { - final long energyUsage = getNode().getEnergyUsage(); + final long energyUsage = mainNode.getEnergyUsage(); final boolean hasLevel = level != null && level.isLoaded(worldPosition); return hasLevel - && getNode().getNetwork() != null - && getNode().getNetwork().getComponent(EnergyNetworkComponent.class).getStored() >= energyUsage; + && mainNode.getNetwork() != null + && mainNode.getNetwork().getComponent(EnergyNetworkComponent.class).getStored() >= energyUsage; } public void updateActiveness(final BlockState state, @Nullable final BooleanProperty activenessProperty) { final boolean newActive = isActive(); - final boolean nodeActivenessNeedsUpdate = newActive != getNode().isActive(); + final boolean nodeActivenessNeedsUpdate = newActive != mainNode.isActive(); final boolean blockStateActivenessNeedsUpdate = activenessProperty != null && state.getValue(activenessProperty) != newActive; final boolean activenessNeedsUpdate = nodeActivenessNeedsUpdate || blockStateActivenessNeedsUpdate; @@ -57,8 +67,8 @@ public void updateActiveness(final BlockState state, @Nullable final BooleanProp } protected void activenessChanged(final boolean newActive) { - LOGGER.debug("Activeness change for node at {}: {} -> {}", getBlockPos(), getNode().isActive(), newActive); - getNode().setActive(newActive); + LOGGER.debug("Activeness change for node at {}: {} -> {}", getBlockPos(), mainNode.isActive(), newActive); + mainNode.setActive(newActive); } private void updateActivenessBlockState(final BlockState state, @@ -76,14 +86,13 @@ private void updateActivenessBlockState(final BlockState state, } public void doWork() { - getNode().doWork(); + mainNode.doWork(); } @Override public void addOutgoingConnections(final ConnectionSink sink) { final Direction myDirection = getDirection(); if (myDirection == null) { - super.addOutgoingConnections(sink); return; } for (final Direction direction : Direction.values()) { @@ -151,9 +160,12 @@ public void setBlockState(final BlockState newBlockState) { if (!doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState)) { return; } - if (level == null || level.isClientSide || getNode().getNetwork() == null) { - return; - } - PlatformApi.INSTANCE.requestNetworkNodeUpdate(this, level); + PlatformApi.INSTANCE.onNetworkNodeContainerUpdated(mainContainer, level); + } + + @Nullable + @Override + public Network getNetworkForBoundItem() { + return mainNode.getNetwork(); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/ConnectionProviderImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/ConnectionProviderImpl.java index 4c0abfa92..0e8f528e9 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/ConnectionProviderImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/ConnectionProviderImpl.java @@ -3,13 +3,16 @@ import com.refinedmods.refinedstorage2.api.network.ConnectionProvider; import com.refinedmods.refinedstorage2.api.network.Connections; import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.NetworkNodeContainerBlockEntity; import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Queue; import java.util.Set; import java.util.stream.Collectors; @@ -36,18 +39,18 @@ public ConnectionProviderImpl(final Level originLevel) { @Override public Connections findConnections(final NetworkNodeContainer pivot, final Set existingConnections) { - final Set existingPlatformConnections = existingConnections.stream() - .filter(PlatformNetworkNodeContainer.class::isInstance) - .map(PlatformNetworkNodeContainer.class::cast) + final Set existingInWorldConnections = existingConnections.stream() + .filter(InWorldNetworkNodeContainer.class::isInstance) + .map(InWorldNetworkNodeContainer.class::cast) .collect(Collectors.toSet()); LOGGER.debug( "Finding connections for pivot {} with {} existing connections", pivot, existingConnections.size() ); - final ScanState scanState = new ScanState(existingPlatformConnections); + final ScanState scanState = new ScanState(existingInWorldConnections); addStartContainer(pivot, scanState); - PlatformNetworkNodeContainer currentContainer; + InWorldNetworkNodeContainer currentContainer; int requests = 0; while ((currentContainer = scanState.toCheck.poll()) != null) { visit(scanState, new ScanEntry(currentContainer)); @@ -65,7 +68,7 @@ public Connections findConnections(final NetworkNodeContainer pivot, } private void addStartContainer(final NetworkNodeContainer pivot, final ScanState scanState) { - if (!(pivot instanceof PlatformNetworkNodeContainer platformPivot)) { + if (!(pivot instanceof InWorldNetworkNodeContainer platformPivot)) { return; } scanState.toCheck.add(platformPivot); @@ -80,48 +83,44 @@ private void visit(final ScanState state, final ScanEntry entry) { state.newEntries.add(entry); } state.removedEntries.remove(entry); - final List connections = findConnectionsAt(entry.getContainer()); + final List connections = findConnectionsAt(entry.getContainer()); state.toCheck.addAll(connections); } - private List findConnectionsAt(final PlatformNetworkNodeContainer from) { - final ConnectionSinkImpl sink = new ConnectionSinkImpl(from.getContainerPosition()); + private List findConnectionsAt(final InWorldNetworkNodeContainer from) { + final GlobalPos pos = from.getPosition(); + final ConnectionSinkImpl sink = new ConnectionSinkImpl(pos); from.addOutgoingConnections(sink); - final List connections = new ArrayList<>(); + final List connections = new ArrayList<>(); for (final ConnectionSinkImpl.Connection connection : sink.getConnections()) { - final PlatformNetworkNodeContainer connectionContainer = getConnection(from, connection); - if (connectionContainer != null) { - connections.add(connectionContainer); - } + connections.addAll(getConnections(from, connection)); } return connections; } - @Nullable - private PlatformNetworkNodeContainer getConnection(final PlatformNetworkNodeContainer from, - final ConnectionSinkImpl.Connection connection) { + private Set getConnections(final InWorldNetworkNodeContainer from, + final ConnectionSinkImpl.Connection connection) { final BlockEntity connectionBlockEntity = getBlockEntitySafely(connection.pos()); - if (!(connectionBlockEntity instanceof PlatformNetworkNodeContainer connectionContainer)) { - return null; + if (!(connectionBlockEntity instanceof NetworkNodeContainerBlockEntity networkNodeContainerBlockEntity)) { + return Collections.emptySet(); } if (connection.incomingDirection() == null) { - return connectionContainer; - } - final boolean acceptsIncomingDirection = connectionContainer.canAcceptIncomingConnection( - connection.incomingDirection(), - from.getContainerBlockState() - ); - if (!acceptsIncomingDirection) { - return null; + return networkNodeContainerBlockEntity.getContainers(); } - return connectionContainer; + return networkNodeContainerBlockEntity.getContainers() + .stream() + .filter(container -> container.canAcceptIncomingConnection( + connection.incomingDirection(), + from.getBlockState() + )) + .collect(Collectors.toSet()); } @Override public List sortDeterministically(final Set containers) { return containers .stream() - .sorted(Comparator.comparing(container -> ((BlockEntity) container).getBlockPos())) + .sorted(Comparator.comparing(container -> ((InWorldNetworkNodeContainer) container).getLocalPosition())) .toList(); } @@ -161,9 +160,9 @@ private static class ScanState { private final Set foundEntries = new HashSet<>(); private final Set newEntries = new HashSet<>(); private final Set removedEntries; - private final Queue toCheck = new ArrayDeque<>(); + private final Queue toCheck = new ArrayDeque<>(); - ScanState(final Set existingConnections) { + ScanState(final Set existingConnections) { this.currentEntries = toScanEntries(existingConnections); this.removedEntries = new HashSet<>(currentEntries); } @@ -180,21 +179,23 @@ private Set toContainers(final Set entries) { return entries.stream().map(ScanEntry::getContainer).collect(Collectors.toSet()); } - private static Set toScanEntries(final Set existingConnections) { + private static Set toScanEntries(final Set existingConnections) { return existingConnections.stream().map(ScanEntry::new).collect(Collectors.toSet()); } } private static class ScanEntry { - private final PlatformNetworkNodeContainer container; + private final InWorldNetworkNodeContainer container; private final GlobalPos pos; + private final String name; - ScanEntry(final PlatformNetworkNodeContainer container) { + ScanEntry(final InWorldNetworkNodeContainer container) { this.container = container; - this.pos = container.getContainerPosition(); + this.pos = container.getPosition(); + this.name = container.getName(); } - public PlatformNetworkNodeContainer getContainer() { + private InWorldNetworkNodeContainer getContainer() { return container; } @@ -206,13 +207,13 @@ public boolean equals(final Object o) { if (o == null || getClass() != o.getClass()) { return false; } - final ScanEntry that = (ScanEntry) o; - return pos.equals(that.pos); + final ScanEntry scanEntry = (ScanEntry) o; + return Objects.equals(pos, scanEntry.pos) && Objects.equals(name, scanEntry.name); } @Override public int hashCode() { - return pos.hashCode(); + return Objects.hash(pos, name); } } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/DefaultConnectionLogic.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/DefaultConnectionLogic.java new file mode 100644 index 000000000..9ae48c012 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/DefaultConnectionLogic.java @@ -0,0 +1,28 @@ +package com.refinedmods.refinedstorage2.platform.common.support.network; + +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionLogic; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionSink; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.block.state.BlockState; + +public class DefaultConnectionLogic implements ConnectionLogic { + private final BlockPos pos; + + public DefaultConnectionLogic(final BlockPos pos) { + this.pos = pos; + } + + @Override + public void addOutgoingConnections(final ConnectionSink sink) { + for (final Direction direction : Direction.values()) { + sink.tryConnectInSameDimension(pos.relative(direction), direction.getOpposite()); + } + } + + @Override + public boolean canAcceptIncomingConnection(final Direction incomingDirection, final BlockState connectingState) { + return true; + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/InWorldNetworkNodeContainerImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/InWorldNetworkNodeContainerImpl.java new file mode 100644 index 000000000..4c21a0ec4 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/InWorldNetworkNodeContainerImpl.java @@ -0,0 +1,94 @@ +package com.refinedmods.refinedstorage2.platform.common.support.network; + +import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionLogic; +import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionSink; +import com.refinedmods.refinedstorage2.platform.api.support.network.InWorldNetworkNodeContainer; + +import java.util.function.Supplier; +import javax.annotation.Nullable; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.core.GlobalPos; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; + +import static java.util.Objects.requireNonNull; + +public class InWorldNetworkNodeContainerImpl implements InWorldNetworkNodeContainer { + private final BlockEntity blockEntity; + private final NetworkNode node; + private final String name; + private final int priority; + private final ConnectionLogic connectionLogic; + @Nullable + private final Supplier keyProvider; + + public InWorldNetworkNodeContainerImpl(final BlockEntity blockEntity, + final NetworkNode node, + final String name, + final int priority, + @Nullable final ConnectionLogic connectionLogic, + @Nullable final Supplier keyProvider) { + this.blockEntity = blockEntity; + this.node = node; + this.name = name; + this.priority = priority; + this.connectionLogic = connectionLogic == null + ? new DefaultConnectionLogic(blockEntity.getBlockPos()) + : connectionLogic; + this.keyProvider = keyProvider; + } + + @Override + public NetworkNode getNode() { + return node; + } + + @Override + public void addOutgoingConnections(final ConnectionSink sink) { + connectionLogic.addOutgoingConnections(sink); + } + + @Override + public boolean canAcceptIncomingConnection(final Direction incomingDirection, final BlockState connectingState) { + return connectionLogic.canAcceptIncomingConnection(incomingDirection, connectingState); + } + + @Override + public BlockState getBlockState() { + return blockEntity.getBlockState(); + } + + @Override + public boolean isRemoved() { + return blockEntity.isRemoved(); + } + + @Override + public GlobalPos getPosition() { + return GlobalPos.of(requireNonNull(blockEntity.getLevel()).dimension(), blockEntity.getBlockPos()); + } + + @Override + public BlockPos getLocalPosition() { + return blockEntity.getBlockPos(); + } + + @Override + public String getName() { + return name; + } + + @Override + public int getPriority() { + return priority; + } + + @Nullable + @Override + public Object createKey() { + return keyProvider != null ? keyProvider.get() : null; + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeBlockEntityTicker.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeBlockEntityTicker.java index 37cfc37af..9522e3acf 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeBlockEntityTicker.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/NetworkNodeBlockEntityTicker.java @@ -14,7 +14,7 @@ public class NetworkNodeBlockEntityTicker< N extends AbstractNetworkNode, - T extends NetworkNodeContainerBlockEntityImpl + T extends BaseNetworkNodeContainerBlockEntity > extends AbstractBlockEntityTicker { @Nullable private final BooleanProperty activenessProperty; diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemHelperImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemHelperImpl.java index 50ee4e1b3..2e67583a4 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemHelperImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemHelperImpl.java @@ -1,9 +1,9 @@ package com.refinedmods.refinedstorage2.platform.common.support.network.bounditem; import com.refinedmods.refinedstorage2.platform.api.support.HelpTooltipComponent; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemHelper; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemSession; +import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemTargetBlockEntity; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; import java.util.List; @@ -67,7 +67,7 @@ public InteractionResult bind(final UseOnContext ctx) { } final ItemStack stack = ctx.getPlayer().getItemInHand(ctx.getHand()); final BlockEntity blockEntity = ctx.getLevel().getBlockEntity(ctx.getClickedPos()); - if (!(blockEntity instanceof PlatformNetworkNodeContainer)) { + if (!(blockEntity instanceof NetworkBoundItemTargetBlockEntity)) { return InteractionResult.PASS; } final CompoundTag tag = stack.getOrCreateTag(); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemSessionImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemSessionImpl.java index 5011671a2..dccc47afd 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemSessionImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/network/bounditem/NetworkBoundItemSessionImpl.java @@ -3,9 +3,8 @@ import com.refinedmods.refinedstorage2.api.core.Action; import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.network.node.GraphNetworkComponent; -import com.refinedmods.refinedstorage2.api.network.node.NetworkNode; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemSession; +import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemTargetBlockEntity; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; import com.refinedmods.refinedstorage2.platform.api.wirelesstransmitter.WirelessTransmitter; import com.refinedmods.refinedstorage2.platform.common.Platform; @@ -19,14 +18,14 @@ import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; -public class NetworkBoundItemSessionImpl implements NetworkBoundItemSession { +class NetworkBoundItemSessionImpl implements NetworkBoundItemSession { private final Player player; private final Vec3 playerPosition; private final SlotReference slotReference; @Nullable private final NetworkReference networkReference; - public NetworkBoundItemSessionImpl( + NetworkBoundItemSessionImpl( final Player player, final SlotReference slotReference, @Nullable final NetworkReference networkReference @@ -50,10 +49,9 @@ public Optional resolveNetwork() { .map(server -> server.getLevel(networkReference.dimensionKey())) .filter(level -> level.isLoaded(networkReference.pos())) .map(level -> level.getBlockEntity(networkReference.pos())) - .filter(PlatformNetworkNodeContainer.class::isInstance) - .map(PlatformNetworkNodeContainer.class::cast) - .map(PlatformNetworkNodeContainer::getNode) - .map(NetworkNode::getNetwork) + .filter(NetworkBoundItemTargetBlockEntity.class::isInstance) + .map(NetworkBoundItemTargetBlockEntity.class::cast) + .map(NetworkBoundItemTargetBlockEntity::getNetworkForBoundItem) .filter(this::isInRange); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/wirelesstransmitter/WirelessTransmitterBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/wirelesstransmitter/WirelessTransmitterBlockEntity.java index 387c76246..2c92d6adf 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/wirelesstransmitter/WirelessTransmitterBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/wirelesstransmitter/WirelessTransmitterBlockEntity.java @@ -107,7 +107,7 @@ private void upgradeContainerChanged() { private void configureAccordingToUpgrades() { final long baseUsage = Platform.INSTANCE.getConfig().getWirelessTransmitter().getEnergyUsage(); - getNode().setEnergyUsage(baseUsage + upgradeContainer.getEnergyUsage()); + mainNode.setEnergyUsage(baseUsage + upgradeContainer.getEnergyUsage()); } @Override @@ -116,7 +116,7 @@ public boolean isInRange(final ResourceKey dimension, final Vec3 position if (level == null || level.dimension() != dimension) { return false; } - if (!getNode().isActive()) { + if (!mainNode.isActive()) { return false; } final double distance = Math.sqrt( diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/security/NetworkNodeBreakSecurityEventListener.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/security/NetworkNodeBreakSecurityEventListener.java index 92ae8d63c..b39693f10 100644 --- a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/security/NetworkNodeBreakSecurityEventListener.java +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/security/NetworkNodeBreakSecurityEventListener.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage2.platform.fabric.security; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.NetworkNodeContainerBlockEntity; import javax.annotation.Nullable; @@ -22,9 +22,9 @@ public boolean beforeBlockBreak(final Level world, final BlockPos pos, final BlockState state, @Nullable final BlockEntity blockEntity) { - if (blockEntity instanceof PlatformNetworkNodeContainer platformNetworkNodeContainer + if (blockEntity instanceof NetworkNodeContainerBlockEntity networkNodeContainerBlockEntity && player instanceof ServerPlayer serverPlayer - && !platformNetworkNodeContainer.canBreakOrRotate(serverPlayer)) { + && !networkNodeContainerBlockEntity.canBuild(serverPlayer)) { PlatformApi.INSTANCE.sendNoPermissionMessage( serverPlayer, createTranslation("misc", "no_permission.build.break", state.getBlock().getName()) diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java index eb988a812..b5cb440f7 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage2.platform.forge; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; -import com.refinedmods.refinedstorage2.platform.api.support.network.PlatformNetworkNodeContainer; +import com.refinedmods.refinedstorage2.platform.api.support.network.NetworkNodeContainerBlockEntity; import com.refinedmods.refinedstorage2.platform.common.AbstractModInitializer; import com.refinedmods.refinedstorage2.platform.common.PlatformProxy; import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities; @@ -438,9 +438,9 @@ public void registerWrenchingEvent(final PlayerInteractEvent.RightClickBlock e) @SubscribeEvent public void registerSecurityBlockBreakEvent(final BlockEvent.BreakEvent e) { final BlockEntity blockEntity = e.getLevel().getBlockEntity(e.getPos()); - if (blockEntity instanceof PlatformNetworkNodeContainer platformNetworkNodeContainer + if (blockEntity instanceof NetworkNodeContainerBlockEntity networkNodeContainerBlockEntity && e.getPlayer() instanceof ServerPlayer serverPlayer - && !platformNetworkNodeContainer.canBreakOrRotate(serverPlayer)) { + && !networkNodeContainerBlockEntity.canBuild(serverPlayer)) { PlatformApi.INSTANCE.sendNoPermissionMessage( serverPlayer, createTranslation("misc", "no_permission.build.break", e.getState().getBlock().getName())