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