Improvements
This commit is contained in:
@@ -11,7 +11,6 @@ import refinedstorage.api.autocrafting.ICraftingTask;
|
|||||||
import refinedstorage.api.storage.CompareFlags;
|
import refinedstorage.api.storage.CompareFlags;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface INetworkMaster {
|
public interface INetworkMaster {
|
||||||
@@ -54,9 +53,9 @@ public interface INetworkMaster {
|
|||||||
void update();
|
void update();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return A iterator with all network slaves
|
* @return A list with all network slaves, do NOT modify
|
||||||
*/
|
*/
|
||||||
Iterator<INetworkSlave> getSlaves();
|
List<INetworkSlave> getSlaves();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param slave The slave to add
|
* @param slave The slave to add
|
||||||
|
@@ -32,7 +32,7 @@ public interface INetworkSlave {
|
|||||||
*
|
*
|
||||||
* @param world The world
|
* @param world The world
|
||||||
*/
|
*/
|
||||||
void onNeighborChanged(World world);
|
void refreshConnection(World world);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a connection is found to the network
|
* Called when a connection is found to the network
|
||||||
|
@@ -6,6 +6,7 @@ import net.minecraft.entity.player.EntityPlayerMP;
|
|||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.nbt.NBTTagList;
|
import net.minecraft.nbt.NBTTagList;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.Constants;
|
import net.minecraftforge.common.util.Constants;
|
||||||
@@ -58,6 +59,7 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
private List<IStorage> storages = new ArrayList<IStorage>();
|
private List<IStorage> storages = new ArrayList<IStorage>();
|
||||||
|
|
||||||
private List<BlockPos> slaves = new ArrayList<BlockPos>();
|
private List<BlockPos> slaves = new ArrayList<BlockPos>();
|
||||||
|
private List<INetworkSlave> actualSlaves = new ArrayList<INetworkSlave>();
|
||||||
private Map<BlockPos, Boolean> slaveConnectivity = new HashMap<BlockPos, Boolean>();
|
private Map<BlockPos, Boolean> slaveConnectivity = new HashMap<BlockPos, Boolean>();
|
||||||
private List<BlockPos> slavesToAdd = new ArrayList<BlockPos>();
|
private List<BlockPos> slavesToAdd = new ArrayList<BlockPos>();
|
||||||
private List<BlockPos> slavesToLoad = new ArrayList<BlockPos>();
|
private List<BlockPos> slavesToLoad = new ArrayList<BlockPos>();
|
||||||
@@ -150,11 +152,14 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
|
boolean forceUpdate = !slavesToAdd.isEmpty() || !slavesToRemove.isEmpty();
|
||||||
|
|
||||||
for (BlockPos slave : slavesToAdd) {
|
for (BlockPos slave : slavesToAdd) {
|
||||||
if (!slaves.contains(slave)) {
|
if (!slaves.contains(slave)) {
|
||||||
slaves.add(slave);
|
slaves.add(slave);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slavesToAdd.clear();
|
slavesToAdd.clear();
|
||||||
|
|
||||||
slaves.removeAll(slavesToRemove);
|
slaves.removeAll(slavesToRemove);
|
||||||
@@ -163,14 +168,11 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
int lastEnergy = energy.getEnergyStored();
|
int lastEnergy = energy.getEnergyStored();
|
||||||
|
|
||||||
if (canRun()) {
|
if (canRun()) {
|
||||||
if (ticks % 20 == 0) {
|
if (ticks % 20 == 0 || forceUpdate) {
|
||||||
updateSlaves();
|
updateSlaves();
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator<INetworkSlave> slaves = getSlaves();
|
for (INetworkSlave slave : getSlaves()) {
|
||||||
while (slaves.hasNext()) {
|
|
||||||
INetworkSlave slave = slaves.next();
|
|
||||||
|
|
||||||
if (slave.canUpdate()) {
|
if (slave.canUpdate()) {
|
||||||
slave.updateSlave();
|
slave.updateSlave();
|
||||||
}
|
}
|
||||||
@@ -196,11 +198,13 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
for (ICraftingTask task : craftingTasksToAdd) {
|
for (ICraftingTask task : craftingTasksToAdd) {
|
||||||
craftingTasks.push(task);
|
craftingTasks.push(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
craftingTasksToAdd.clear();
|
craftingTasksToAdd.clear();
|
||||||
|
|
||||||
for (ICraftingTask task : craftingTasksToAddAsLast) {
|
for (ICraftingTask task : craftingTasksToAddAsLast) {
|
||||||
craftingTasks.add(0, task);
|
craftingTasks.add(0, task);
|
||||||
}
|
}
|
||||||
|
|
||||||
craftingTasksToAddAsLast.clear();
|
craftingTasksToAddAsLast.clear();
|
||||||
|
|
||||||
if (!craftingTasks.empty()) {
|
if (!craftingTasks.empty()) {
|
||||||
@@ -249,25 +253,8 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<INetworkSlave> getSlaves() {
|
public List<INetworkSlave> getSlaves() {
|
||||||
return new Iterator<INetworkSlave>() {
|
return actualSlaves;
|
||||||
private int index;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasNext() {
|
|
||||||
return index < slaves.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public INetworkSlave next() {
|
|
||||||
return world.getTileEntity(slaves.get(index++)).getCapability(RefinedStorageCapabilities.NETWORK_SLAVE_CAPABILITY, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void remove() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -295,13 +282,11 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void disconnectAll() {
|
public void disconnectAll() {
|
||||||
Iterator<INetworkSlave> slaves = getSlaves();
|
for (INetworkSlave slave : getSlaves()) {
|
||||||
|
slave.disconnect(world);
|
||||||
while (slaves.hasNext()) {
|
|
||||||
slaves.next().disconnect(world);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.slaves.clear();
|
slaves.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -402,14 +387,24 @@ public class NetworkMaster implements INetworkMaster {
|
|||||||
|
|
||||||
private void updateSlaves() {
|
private void updateSlaves() {
|
||||||
this.energyUsage = 0;
|
this.energyUsage = 0;
|
||||||
|
this.actualSlaves.clear();
|
||||||
this.storages.clear();
|
this.storages.clear();
|
||||||
this.patterns.clear();
|
this.patterns.clear();
|
||||||
|
|
||||||
int range = 0;
|
int range = 0;
|
||||||
|
|
||||||
Iterator<INetworkSlave> slaves = getSlaves();
|
for (BlockPos slavePos : slaves) {
|
||||||
while (slaves.hasNext()) {
|
TileEntity tile = world.getTileEntity(slavePos);
|
||||||
INetworkSlave slave = slaves.next();
|
|
||||||
|
if (tile == null || !tile.hasCapability(RefinedStorageCapabilities.NETWORK_SLAVE_CAPABILITY, null)) {
|
||||||
|
removeSlave(slavePos);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
INetworkSlave slave = tile.getCapability(RefinedStorageCapabilities.NETWORK_SLAVE_CAPABILITY, null);
|
||||||
|
|
||||||
|
actualSlaves.add(slave);
|
||||||
|
|
||||||
if (!slave.canUpdate()) {
|
if (!slave.canUpdate()) {
|
||||||
continue;
|
continue;
|
||||||
|
@@ -43,7 +43,7 @@ public abstract class BlockSlave extends BlockBase {
|
|||||||
super.onBlockPlacedBy(world, pos, state, player, stack);
|
super.onBlockPlacedBy(world, pos, state, player, stack);
|
||||||
|
|
||||||
if (!world.isRemote) {
|
if (!world.isRemote) {
|
||||||
((TileSlave) world.getTileEntity(pos)).onNeighborChanged(world);
|
((TileSlave) world.getTileEntity(pos)).refreshConnection(world);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ public abstract class BlockSlave extends BlockBase {
|
|||||||
super.neighborChanged(state, world, pos, block);
|
super.neighborChanged(state, world, pos, block);
|
||||||
|
|
||||||
if (!world.isRemote) {
|
if (!world.isRemote) {
|
||||||
((TileSlave) world.getTileEntity(pos)).onNeighborChanged(world);
|
((TileSlave) world.getTileEntity(pos)).refreshConnection(world);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -82,7 +82,7 @@ public abstract class TileSlave extends TileBase implements INetworkSlave, ISync
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNeighborChanged(World world) {
|
public void refreshConnection(World world) {
|
||||||
TileController controller = searchController(world, pos, new HashSet<Long>());
|
TileController controller = searchController(world, pos, new HashSet<Long>());
|
||||||
|
|
||||||
if (network == null) {
|
if (network == null) {
|
||||||
|
@@ -22,7 +22,6 @@ import refinedstorage.tile.config.IRedstoneModeConfig;
|
|||||||
import refinedstorage.tile.config.RedstoneMode;
|
import refinedstorage.tile.config.RedstoneMode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TileController extends TileBase implements IEnergyReceiver, ISynchronizedContainer, IRedstoneModeConfig {
|
public class TileController extends TileBase implements IEnergyReceiver, ISynchronizedContainer, IRedstoneModeConfig {
|
||||||
@@ -151,10 +150,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
|
|||||||
if (getNetwork() != null) {
|
if (getNetwork() != null) {
|
||||||
List<ClientSlave> clientSlaves = new ArrayList<ClientSlave>();
|
List<ClientSlave> clientSlaves = new ArrayList<ClientSlave>();
|
||||||
|
|
||||||
Iterator<INetworkSlave> slaves = getNetwork().getSlaves();
|
for (INetworkSlave slave : getNetwork().getSlaves()) {
|
||||||
while (slaves.hasNext()) {
|
|
||||||
INetworkSlave slave = slaves.next();
|
|
||||||
|
|
||||||
if (slave.canUpdate()) {
|
if (slave.canUpdate()) {
|
||||||
IBlockState state = worldObj.getBlockState(slave.getPosition());
|
IBlockState state = worldObj.getBlockState(slave.getPosition());
|
||||||
|
|
||||||
@@ -164,10 +160,11 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
|
|||||||
clientSlave.amount = 1;
|
clientSlave.amount = 1;
|
||||||
clientSlave.stack = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state));
|
clientSlave.stack = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state));
|
||||||
|
|
||||||
if (clientSlaves.contains(clientSlave)) {
|
if (clientSlave.stack.getItem() != null && clientSlaves.contains(clientSlave)) {
|
||||||
for (ClientSlave other : clientSlaves) {
|
for (ClientSlave other : clientSlaves) {
|
||||||
if (other.equals(clientSlave)) {
|
if (other.equals(clientSlave)) {
|
||||||
other.amount++;
|
other.amount++;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user