Finish the new system. Still some todo's

This commit is contained in:
Raoul Van den Berge
2016-07-05 14:03:52 +02:00
parent f895ffd7e1
commit e628b5f77f
8 changed files with 94 additions and 164 deletions

View File

@@ -42,11 +42,6 @@ public interface INetworkMaster {
*/
List<INetworkNode> getNodes();
/**
* @param nodes The nodes to set
*/
void setNodes(List<INetworkNode> nodes);
/**
* @return The {@link IGridHandler} for this network
*/
@@ -106,6 +101,11 @@ public interface INetworkMaster {
*/
void rebuildPatterns();
/**
* Rebuilds the network node list.
*/
void rebuildNodes();
/**
* Returns crafting patterns from an item stack.
*

View File

@@ -1,7 +1,6 @@
package refinedstorage.api.network;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
/**
* Represents a node in the storage network.
@@ -27,20 +26,6 @@ public interface INetworkNode {
*/
BlockPos getPosition();
/**
* Called when this node is placed in the world.
*
* @param world The world
*/
void onPlaced(World world);
/**
* Called when this node is removed from the world.
*
* @param world The world
*/
void onBreak(World world);
/**
* Called when this node is connected to a network.
*

View File

@@ -1,5 +1,6 @@
package refinedstorage.block;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.properties.PropertyInteger;
@@ -97,6 +98,8 @@ public class BlockController extends BlockBase {
if (tag != null && tag.hasKey(TileController.NBT_ENERGY)) {
controller.getEnergy().receiveEnergy(tag.getInteger(TileController.NBT_ENERGY), false);
}
controller.rebuildNodes();
}
super.onBlockPlacedBy(world, pos, state, player, stack);
@@ -111,6 +114,15 @@ public class BlockController extends BlockBase {
super.breakBlock(world, pos, state);
}
@Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block) {
super.neighborChanged(state, world, pos, block);
if (!world.isRemote) {
((TileController) world.getTileEntity(pos)).rebuildNodes();
}
}
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
List<ItemStack> drops = new ArrayList<ItemStack>();

View File

@@ -6,9 +6,12 @@ import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import refinedstorage.api.network.INetworkMaster;
import refinedstorage.tile.TileNode;
public abstract class BlockNode extends BlockBase {
@@ -42,16 +45,30 @@ public abstract class BlockNode extends BlockBase {
super.onBlockPlacedBy(world, pos, state, player, stack);
if (!world.isRemote) {
((TileNode) world.getTileEntity(pos)).onPlaced(world);
for (EnumFacing facing : EnumFacing.VALUES) {
TileEntity tile = world.getTileEntity(pos.offset(facing));
if (tile instanceof TileNode && ((TileNode) tile).isConnected()) {
((TileNode) tile).getNetwork().rebuildNodes();
break;
}
}
}
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
INetworkMaster network = null;
if (!world.isRemote) {
((TileNode) world.getTileEntity(pos)).onBreak(world);
network = ((TileNode) world.getTileEntity(pos)).getNetwork();
}
super.breakBlock(world, pos, state);
if (network != null) {
network.rebuildNodes();
}
}
}

View File

@@ -5,7 +5,6 @@ import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
@@ -107,18 +106,14 @@ public class TileDiskDrive extends TileNode implements IStorageProvider, IStorag
public void onConnectionChange(INetworkMaster network, boolean state) {
super.onConnectionChange(network, state);
network.getStorage().rebuild();
}
@Override
public void onBreak(World world) {
// @TODO
for (Storage storage : this.storages) {
if (storage != null) {
storage.writeToNBT();
}
}
super.onBreak(world);
network.getStorage().rebuild();
}
@Override

View File

@@ -2,10 +2,8 @@ package refinedstorage.tile;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import refinedstorage.RefinedStorageUtils;
import refinedstorage.api.RefinedStorageCapabilities;
@@ -14,8 +12,6 @@ import refinedstorage.api.network.INetworkNode;
import refinedstorage.tile.config.IRedstoneModeConfig;
import refinedstorage.tile.config.RedstoneMode;
import java.util.*;
public abstract class TileNode extends TileBase implements INetworkNode, ISynchronizedContainer, IRedstoneModeConfig {
private static final String NBT_CONNECTED = "Connected";
@@ -63,125 +59,6 @@ public abstract class TileNode extends TileBase implements INetworkNode, ISynchr
super.update();
}
@Override
public void onPlaced(World world) {
List<INetworkNode> nodes = new ArrayList<INetworkNode>();
Set<BlockPos> nodesPos = new HashSet<BlockPos>();
Queue<BlockPos> positions = new ArrayDeque<BlockPos>();
Set<BlockPos> checked = new HashSet<BlockPos>();
nodes.add(this);
positions.add(pos);
INetworkMaster master = null;
BlockPos currentPos;
while ((currentPos = positions.poll()) != null) {
TileEntity tile = world.getTileEntity(currentPos);
if (tile instanceof INetworkMaster) {
master = (INetworkMaster) tile;
continue;
}
if (tile == null || !tile.hasCapability(RefinedStorageCapabilities.NETWORK_NODE_CAPABILITY, null)) {
continue;
}
INetworkNode node = tile.getCapability(RefinedStorageCapabilities.NETWORK_NODE_CAPABILITY, null);
nodes.add(node);
nodesPos.add(node.getPosition());
for (EnumFacing sideOnCurrent : EnumFacing.VALUES) {
BlockPos sidePos = currentPos.offset(sideOnCurrent);
if (checked.add(sidePos)) {
positions.add(sidePos);
}
}
}
if (master != null) {
for (INetworkNode newNode : nodes) {
boolean isNew = false;
for (INetworkNode oldNode : master.getNodes()) {
if (oldNode.getPosition().equals(newNode.getPosition())) {
isNew = true;
break;
}
}
if (!isNew) {
newNode.onConnected(master);
}
}
master.setNodes(nodes);
}
}
@Override
public void onBreak(World world) {
if (network == null) {
return;
}
List<INetworkNode> nodes = new ArrayList<INetworkNode>();
Set<BlockPos> nodesPos = new HashSet<BlockPos>();
Queue<BlockPos> positions = new ArrayDeque<BlockPos>();
Set<BlockPos> checked = new HashSet<BlockPos>();
checked.add(pos);
for (EnumFacing side : EnumFacing.VALUES) {
BlockPos sidePos = pos.offset(side);
if (!checked.add(sidePos)) {
continue;
}
positions.add(sidePos);
BlockPos currentPos;
while ((currentPos = positions.poll()) != null) {
TileEntity tile = world.getTileEntity(currentPos);
if (tile == null || !tile.hasCapability(RefinedStorageCapabilities.NETWORK_NODE_CAPABILITY, null)) {
continue;
}
INetworkNode node = tile.getCapability(RefinedStorageCapabilities.NETWORK_NODE_CAPABILITY, null);
nodes.add(node);
nodesPos.add(currentPos);
for (EnumFacing sideOfCurrent : EnumFacing.VALUES) {
BlockPos sideOfCurrentPos = currentPos.offset(sideOfCurrent);
if (checked.add(sideOfCurrentPos)) {
positions.add(sideOfCurrentPos);
}
}
}
}
List<INetworkNode> oldNodes = network.getNodes();
network.setNodes(nodes);
for (INetworkNode oldNode : oldNodes) {
if (!nodesPos.contains(oldNode.getPosition())) {
oldNode.onDisconnected();
}
}
}
@Override
public void onConnected(INetworkMaster network) {
onConnectionChange(network, true);

View File

@@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.RefinedStorage;
@@ -85,16 +84,12 @@ public class TileStorage extends TileNode implements IStorageProvider, IStorageG
public void onConnectionChange(INetworkMaster network, boolean state) {
super.onConnectionChange(network, state);
network.getStorage().rebuild();
}
@Override
public void onBreak(World world) {
// @TODO
if (storage != null) {
storage.writeToNBT();
}
super.onBreak(world);
network.getStorage().rebuild();
}
@Override

View File

@@ -13,6 +13,7 @@ import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
@@ -23,6 +24,7 @@ import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.RefinedStorage;
import refinedstorage.RefinedStorageBlocks;
import refinedstorage.RefinedStorageUtils;
import refinedstorage.api.RefinedStorageCapabilities;
import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.ICraftingTask;
import refinedstorage.api.network.IGridHandler;
@@ -90,6 +92,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
};
private List<INetworkNode> nodes = new ArrayList<INetworkNode>();
private Set<BlockPos> nodesPos = new HashSet<BlockPos>();
private List<ICraftingPattern> patterns = new ArrayList<ICraftingPattern>();
@@ -231,11 +234,6 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
return nodes;
}
@Override
public void setNodes(List<INetworkNode> nodes) {
this.nodes = nodes;
}
public List<ClientNode> getClientNodes() {
return clientNodes;
}
@@ -376,6 +374,57 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
storage.rebuild();
}
@Override
public void rebuildNodes() {
List<INetworkNode> newNodes = new ArrayList<INetworkNode>();
Set<BlockPos> newNodesPos = new HashSet<BlockPos>();
Set<BlockPos> checked = new HashSet<BlockPos>();
Queue<BlockPos> toCheck = new ArrayDeque<BlockPos>();
for (EnumFacing facing : EnumFacing.VALUES) {
checked.add(pos.offset(facing));
toCheck.add(pos.offset(facing));
}
BlockPos currentPos;
while ((currentPos = toCheck.poll()) != null) {
TileEntity tile = worldObj.getTileEntity(currentPos);
if (tile == null || !tile.hasCapability(RefinedStorageCapabilities.NETWORK_NODE_CAPABILITY, null)) {
continue;
}
INetworkNode node = tile.getCapability(RefinedStorageCapabilities.NETWORK_NODE_CAPABILITY, null);
// @TODO: Care about relays
newNodes.add(node);
newNodesPos.add(node.getPosition());
for (EnumFacing facing : EnumFacing.VALUES) {
if (checked.add(currentPos.offset(facing))) {
toCheck.add(currentPos.offset(facing));
}
}
}
for (INetworkNode newNode : newNodes) {
if (!nodesPos.contains(newNode.getPosition())) {
newNode.onConnected(this);
}
}
for (INetworkNode oldNode : nodes) {
if (!newNodesPos.contains(oldNode.getPosition())) {
oldNode.onDisconnected();
}
}
this.nodes = newNodes;
this.nodesPos = newNodesPos;
}
@Override
public void sendStorageToClient() {
for (EntityPlayer player : worldObj.playerEntities) {