Fix network scanning a little bit
This commit is contained in:
@@ -3,6 +3,7 @@ package com.raoulvdberge.refinedstorage.api.network;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Represents a node in the network.
|
||||
@@ -41,5 +42,6 @@ public interface INetworkNode {
|
||||
/**
|
||||
* @return the network
|
||||
*/
|
||||
@Nullable
|
||||
INetworkMaster getNetwork();
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
|
||||
|
||||
INetworkNeighborhoodAware.Operator operator = (world, pos, side) -> {
|
||||
TileEntity tile = world.getTileEntity(pos);
|
||||
if (tile != null) {
|
||||
if (tile != null && !tile.isInvalid()) {
|
||||
if (tile instanceof TileController) {
|
||||
removeOtherControler(world, pos);
|
||||
} else {
|
||||
@@ -57,10 +57,11 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
|
||||
};
|
||||
|
||||
BlockPos controllerPos = controller.getPos();
|
||||
World controlerWorld = controller.getWorld();
|
||||
World controllerWorld = controller.getWorld();
|
||||
|
||||
for (EnumFacing facing : EnumFacing.VALUES) {
|
||||
BlockPos pos = controllerPos.offset(facing);
|
||||
operator.apply(controlerWorld, pos, facing.getOpposite());
|
||||
operator.apply(controllerWorld, pos, facing.getOpposite());
|
||||
}
|
||||
|
||||
NodeToCheck currentNodeToCheck;
|
||||
@@ -139,16 +140,16 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
|
||||
if (!controller.getPos().equals(otherControllerPos)) {
|
||||
IBlockState state = world.getBlockState(otherControllerPos);
|
||||
|
||||
ItemStack itemStackToSpawn = ItemBlockController.createStackWithNBT(new ItemStack(RSBlocks.CONTROLLER, 1, state.getBlock().getMetaFromState(state)));
|
||||
ItemStack stackToSpawn = ItemBlockController.createStackWithNBT(new ItemStack(RSBlocks.CONTROLLER, 1, state.getBlock().getMetaFromState(state)));
|
||||
|
||||
world.setBlockToAir(otherControllerPos);
|
||||
|
||||
InventoryHelper.spawnItemStack(
|
||||
world,
|
||||
otherControllerPos.getX(),
|
||||
otherControllerPos.getY(),
|
||||
otherControllerPos.getZ(),
|
||||
itemStackToSpawn
|
||||
world,
|
||||
otherControllerPos.getX(),
|
||||
otherControllerPos.getY(),
|
||||
otherControllerPos.getZ(),
|
||||
stackToSpawn
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.raoulvdberge.refinedstorage.RSBlocks;
|
||||
import com.raoulvdberge.refinedstorage.RSGui;
|
||||
import com.raoulvdberge.refinedstorage.item.ItemBlockController;
|
||||
import com.raoulvdberge.refinedstorage.tile.TileController;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.properties.PropertyInteger;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
@@ -101,25 +100,6 @@ public class BlockController extends BlockBase {
|
||||
super.onBlockPlacedBy(world, pos, state, player, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void breakBlock(World world, BlockPos pos, IBlockState state) {
|
||||
if (!world.isRemote) {
|
||||
((TileController) world.getTileEntity(pos)).onDestroyed();
|
||||
}
|
||||
|
||||
super.breakBlock(world, pos, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos fromPos) {
|
||||
super.neighborChanged(state, world, pos, block, fromPos);
|
||||
|
||||
if (!world.isRemote) {
|
||||
((TileController) world.getTileEntity(pos)).getNodeGraph().rebuild();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
|
||||
List<ItemStack> drops = new ArrayList<>();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.raoulvdberge.refinedstorage.block;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
|
||||
import com.raoulvdberge.refinedstorage.api.network.INetworkNode;
|
||||
import com.raoulvdberge.refinedstorage.proxy.CapabilityNetworkNode;
|
||||
import com.raoulvdberge.refinedstorage.tile.TileNode;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
@@ -25,6 +26,25 @@ public abstract class BlockNode extends BlockBase {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
|
||||
super.onBlockPlacedBy(world, pos, state, placer, stack);
|
||||
|
||||
for (EnumFacing facing : EnumFacing.VALUES) {
|
||||
TileEntity tile = world.getTileEntity(pos.offset(facing));
|
||||
|
||||
if (tile != null && tile.hasCapability(CapabilityNetworkNode.NETWORK_NODE_CAPABILITY, facing.getOpposite())) {
|
||||
INetworkNode node = tile.getCapability(CapabilityNetworkNode.NETWORK_NODE_CAPABILITY, facing.getOpposite());
|
||||
|
||||
if (node.getNetwork() != null) {
|
||||
node.getNetwork().getNodeGraph().rebuild();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BlockStateContainer.Builder createBlockStateBuilder() {
|
||||
BlockStateContainer.Builder builder = super.createBlockStateBuilder();
|
||||
@@ -50,42 +70,6 @@ public abstract class BlockNode extends BlockBase {
|
||||
return super.getActualState(state, world, pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack) {
|
||||
super.onBlockPlacedBy(world, pos, state, player, stack);
|
||||
|
||||
if (!world.isRemote) {
|
||||
for (EnumFacing facing : EnumFacing.VALUES) {
|
||||
TileEntity tile = world.getTileEntity(pos.offset(facing));
|
||||
|
||||
if (tile instanceof TileNode && ((TileNode) tile).hasNetwork()) {
|
||||
((TileNode) tile).getNetwork().getNodeGraph().rebuild();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void breakBlock(World world, BlockPos pos, IBlockState state) {
|
||||
INetworkMaster network = null;
|
||||
|
||||
if (!world.isRemote) {
|
||||
TileEntity tile = world.getTileEntity(pos);
|
||||
|
||||
if (tile instanceof TileNode) {
|
||||
network = ((TileNode) tile).getNetwork();
|
||||
}
|
||||
}
|
||||
|
||||
super.breakBlock(world, pos, state);
|
||||
|
||||
if (network != null) {
|
||||
network.getNodeGraph().rebuild();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasConnectivityState() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -111,9 +111,9 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
|
||||
}
|
||||
|
||||
ClientNode clientNode = new ClientNode(
|
||||
itemStack,
|
||||
1,
|
||||
node.getEnergyUsage()
|
||||
itemStack,
|
||||
1,
|
||||
node.getEnergyUsage()
|
||||
);
|
||||
|
||||
if (nodes.contains(clientNode)) {
|
||||
@@ -329,7 +329,10 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
|
||||
return networkItemHandler;
|
||||
}
|
||||
|
||||
public void onDestroyed() {
|
||||
@Override
|
||||
public void invalidate() {
|
||||
super.invalidate();
|
||||
|
||||
nodeGraph.disconnectAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -165,7 +165,9 @@ public class TileNetworkTransmitter extends TileNode {
|
||||
if (canTransmit()) {
|
||||
if (!isSameDimension()) {
|
||||
final World dimensionWorld = DimensionManager.getWorld(receiverDimension);
|
||||
operator.apply(dimensionWorld, receiver, null);
|
||||
if (dimensionWorld != null) {
|
||||
operator.apply(dimensionWorld, receiver, null);
|
||||
}
|
||||
} else {
|
||||
operator.apply(getWorld(), receiver, null);
|
||||
}
|
||||
|
||||
@@ -12,9 +12,7 @@ import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -23,19 +21,15 @@ import javax.annotation.Nullable;
|
||||
public abstract class TileNode extends TileBase implements INetworkNode, IRedstoneConfigurable, IWrenchable, INetworkNeighborhoodAware {
|
||||
public static final TileDataParameter<Integer> REDSTONE_MODE = RedstoneMode.createParameter();
|
||||
|
||||
private static final String NBT_ACTIVE = "Connected";
|
||||
private static final String NBT_NETWORK = "Network";
|
||||
private static final String NBT_ACTIVE = "Active";
|
||||
|
||||
private RedstoneMode redstoneMode = RedstoneMode.IGNORE;
|
||||
private boolean active;
|
||||
private boolean update;
|
||||
|
||||
private BlockPos networkPos;
|
||||
private boolean rebuildNeighbors;
|
||||
protected boolean rebuildOnUpdateChange;
|
||||
|
||||
protected INetworkMaster network;
|
||||
|
||||
protected boolean rebuildOnUpdateChange;
|
||||
|
||||
public TileNode() {
|
||||
dataManager.addWatchedParameter(REDSTONE_MODE);
|
||||
}
|
||||
@@ -54,32 +48,20 @@ public abstract class TileNode extends TileBase implements INetworkNode, IRedsto
|
||||
@Override
|
||||
public void update() {
|
||||
if (!getWorld().isRemote) {
|
||||
if (networkPos != null) {
|
||||
TileEntity tile = getWorld().getTileEntity(networkPos);
|
||||
|
||||
if (tile instanceof INetworkMaster) {
|
||||
((INetworkMaster) tile).getNodeGraph().replace(this);
|
||||
|
||||
onConnected((INetworkMaster) tile);
|
||||
}
|
||||
|
||||
networkPos = null;
|
||||
}
|
||||
|
||||
if (update != canUpdate() && network != null) {
|
||||
update = canUpdate();
|
||||
|
||||
onConnectionChange(network, update);
|
||||
|
||||
if (rebuildOnUpdateChange) {
|
||||
network.getNodeGraph().rebuild();
|
||||
}
|
||||
}
|
||||
|
||||
boolean wasActive = active;
|
||||
active = hasNetwork() && canUpdate();
|
||||
if (active != wasActive && hasConnectivityState()) {
|
||||
updateBlock();
|
||||
if (active != wasActive) {
|
||||
if (hasConnectivityState()) {
|
||||
updateBlock();
|
||||
}
|
||||
|
||||
if (network != null) {
|
||||
onConnectionChange(network, active);
|
||||
|
||||
if (rebuildOnUpdateChange) {
|
||||
network.getNodeGraph().rebuild();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (active) {
|
||||
@@ -90,6 +72,15 @@ public abstract class TileNode extends TileBase implements INetworkNode, IRedsto
|
||||
super.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
super.invalidate();
|
||||
|
||||
if (getWorld() != null && !getWorld().isRemote && hasNetwork()) {
|
||||
network.getNodeGraph().rebuild();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(INetworkMaster network) {
|
||||
this.network = network;
|
||||
@@ -118,21 +109,24 @@ public abstract class TileNode extends TileBase implements INetworkNode, IRedsto
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing side) {
|
||||
if (capability == CapabilityNetworkNode.NETWORK_NODE_CAPABILITY && canConduct(side)) {
|
||||
if (capability == CapabilityNetworkNode.NETWORK_NODE_CAPABILITY) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.hasCapability(capability, side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing side) {
|
||||
if (capability == CapabilityNetworkNode.NETWORK_NODE_CAPABILITY && canConduct(side)) {
|
||||
if (capability == CapabilityNetworkNode.NETWORK_NODE_CAPABILITY) {
|
||||
return CapabilityNetworkNode.NETWORK_NODE_CAPABILITY.cast(this);
|
||||
}
|
||||
|
||||
return super.getCapability(capability, side);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public INetworkMaster getNetwork() {
|
||||
return network;
|
||||
}
|
||||
@@ -166,10 +160,6 @@ public abstract class TileNode extends TileBase implements INetworkNode, IRedsto
|
||||
super.read(tag);
|
||||
|
||||
readConfiguration(tag);
|
||||
|
||||
if (tag.hasKey(NBT_NETWORK)) {
|
||||
networkPos = BlockPos.fromLong(tag.getLong(NBT_NETWORK));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -178,10 +168,6 @@ public abstract class TileNode extends TileBase implements INetworkNode, IRedsto
|
||||
|
||||
writeConfiguration(tag);
|
||||
|
||||
if (network != null) {
|
||||
tag.setLong(NBT_NETWORK, network.getPosition().toLong());
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
@@ -222,7 +208,9 @@ public abstract class TileNode extends TileBase implements INetworkNode, IRedsto
|
||||
@Override
|
||||
public void walkNeighborhood(Operator operator) {
|
||||
for (EnumFacing facing : EnumFacing.VALUES) {
|
||||
operator.apply(getWorld(), pos.offset(facing), facing.getOpposite());
|
||||
if (canConduct(facing)) {
|
||||
operator.apply(getWorld(), pos.offset(facing), facing.getOpposite());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user