Skip to content

Commit

Permalink
Merge pull request #448 from refinedmods/feat/GH-97/node-connections
Browse files Browse the repository at this point in the history
feat: network node containers can now determine their outgoing connections
  • Loading branch information
raoulvdberge authored Nov 3, 2023
2 parents adde7b8 + 203f35b commit cefe319
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.refinedmods.refinedstorage2.api.network.node.NetworkNode;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;

import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
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<T extends NetworkNode> extends BlockEntity
implements PlatformNetworkNodeContainer {
Expand Down Expand Up @@ -63,12 +63,14 @@ public T getNode() {
}

@Override
public boolean canPerformOutgoingConnection(final Direction direction) {
return true;
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 direction, final BlockState other) {
public boolean canAcceptIncomingConnection(final Direction incomingDirection, final BlockState connectingState) {
return true;
}

Expand All @@ -77,15 +79,9 @@ public BlockState getContainerBlockState() {
return getBlockState();
}

@Nullable
@Override
public Level getContainerLevel() {
return getLevel();
}

@Override
public BlockPos getContainerPosition() {
return getBlockPos();
public GlobalPos getContainerPosition() {
return GlobalPos.of(requireNonNull(level).dimension(), worldPosition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.refinedmods.refinedstorage2.platform.api.support.network;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.2")
@FunctionalInterface
public interface ConnectionSink {
void tryConnectInSameDimension(BlockPos pos, Direction incomingDirection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,37 @@

import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;

import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
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 {
/**
* Returns whether the already discovered node can perform an outgoing connection.
* Changes to this return value won't cause a rebuild of the network state.
* If the return value ever changes, call {@link
* Called when a node is about to be added into the network graph.
* Add outgoing connections here.
* If the outgoing connections ever change, call {@link
* com.refinedmods.refinedstorage2.platform.api.PlatformApi#requestNetworkNodeUpdate(NetworkNodeContainer, Level)}.
*
* @param direction the outgoing direction
* @return whether the node can perform an outgoing connection
* @param sink the sink that accepts outgoing connections
*/
boolean canPerformOutgoingConnection(Direction direction);
void addOutgoingConnections(ConnectionSink sink);

/**
* Returns whether the not yet discovered node can accept an incoming connection.
* 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(NetworkNodeContainer, Level)}.
*
* @param direction the incoming direction
* @param other the state wanting to connect
* @param incomingDirection the incoming direction
* @param connectingState the state wanting to connect
* @return whether the node can accept an incoming connection
*/
boolean canAcceptIncomingConnection(Direction direction, BlockState other);
boolean canAcceptIncomingConnection(Direction incomingDirection, BlockState connectingState);

BlockState getContainerBlockState();

@Nullable
Level getContainerLevel();

BlockPos getContainerPosition();
GlobalPos getContainerPosition();
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import com.refinedmods.refinedstorage2.platform.common.storagemonitor.CompositeStorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyItemHelperImpl;
import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage;
import com.refinedmods.refinedstorage2.platform.common.support.network.LevelConnectionProvider;
import com.refinedmods.refinedstorage2.platform.common.support.network.ConnectionProviderImpl;
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.InventorySlotReferenceFactory;
Expand Down Expand Up @@ -291,7 +291,7 @@ public BuiltinUpgradeDestinations getBuiltinUpgradeDestinations() {
public void requestNetworkNodeInitialization(final NetworkNodeContainer container,
final Level level,
final Runnable callback) {
final LevelConnectionProvider connectionProvider = new LevelConnectionProvider(level);
final ConnectionProviderImpl connectionProvider = new ConnectionProviderImpl(level);
ServerEventQueue.queue(() -> {
networkBuilder.initialize(container, connectionProvider);
callback.run();
Expand All @@ -300,13 +300,13 @@ public void requestNetworkNodeInitialization(final NetworkNodeContainer containe

@Override
public void requestNetworkNodeRemoval(final NetworkNodeContainer container, final Level level) {
final LevelConnectionProvider connectionProvider = new LevelConnectionProvider(level);
final ConnectionProviderImpl connectionProvider = new ConnectionProviderImpl(level);
networkBuilder.remove(container, connectionProvider);
}

@Override
public void requestNetworkNodeUpdate(final NetworkNodeContainer container, final Level level) {
final LevelConnectionProvider connectionProvider = new LevelConnectionProvider(level);
final ConnectionProviderImpl connectionProvider = new ConnectionProviderImpl(level);
networkBuilder.update(container, connectionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.detector.DetectorAmountStrategyImpl;
import com.refinedmods.refinedstorage2.api.network.impl.node.detector.DetectorMode;
import com.refinedmods.refinedstorage2.api.network.impl.node.detector.DetectorNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionSink;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceAmountTemplate;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage2.platform.common.Platform;
Expand Down Expand Up @@ -152,23 +153,28 @@ public void updateActiveness(final BlockState state, @Nullable final BooleanProp
}

@Override
public boolean canAcceptIncomingConnection(final Direction direction, final BlockState other) {
if (!colorsAllowConnecting(other)) {
return false;
}
public void addOutgoingConnections(final ConnectionSink sink) {
final Direction myDirection = getDirection();
if (myDirection != null) {
return myDirection != direction;
if (myDirection == null) {
return;
}
for (final Direction direction : Direction.values()) {
if (direction == myDirection.getOpposite()) {
continue;
}
sink.tryConnectInSameDimension(worldPosition.relative(direction), direction.getOpposite());
}
return true;
}

@Override
public boolean canPerformOutgoingConnection(final Direction direction) {
public boolean canAcceptIncomingConnection(final Direction incomingDirection, final BlockState connectingState) {
if (!colorsAllowConnecting(connectingState)) {
return false;
}
final Direction myDirection = getDirection();
if (myDirection == null) {
return true;
if (myDirection != null) {
return myDirection != incomingDirection.getOpposite();
}
return myDirection != direction.getOpposite();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ private static boolean hasVisualConnection(
if (!(world.getBlockEntity(offsetPos) instanceof PlatformNetworkNodeContainer neighboringContainer)) {
return false;
}
return neighboringContainer.canAcceptIncomingConnection(direction, blockState);
return neighboringContainer.canAcceptIncomingConnection(direction.getOpposite(), blockState);
}
}
Loading

0 comments on commit cefe319

Please sign in to comment.