Initial refactor of storage networks, not functional yet.
This commit is contained in:
		
							
								
								
									
										558
									
								
								src/main/java/refinedstorage/api/storagenet/StorageNetwork.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										558
									
								
								src/main/java/refinedstorage/api/storagenet/StorageNetwork.java
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,558 @@
 | 
				
			|||||||
 | 
					package refinedstorage.api.storagenet;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import cofh.api.energy.EnergyStorage;
 | 
				
			||||||
 | 
					import net.minecraft.entity.player.EntityPlayer;
 | 
				
			||||||
 | 
					import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			||||||
 | 
					import net.minecraft.item.ItemStack;
 | 
				
			||||||
 | 
					import net.minecraft.nbt.NBTTagCompound;
 | 
				
			||||||
 | 
					import net.minecraft.nbt.NBTTagList;
 | 
				
			||||||
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
 | 
					import net.minecraft.world.World;
 | 
				
			||||||
 | 
					import net.minecraftforge.common.util.Constants;
 | 
				
			||||||
 | 
					import net.minecraftforge.items.ItemHandlerHelper;
 | 
				
			||||||
 | 
					import refinedstorage.RefinedStorage;
 | 
				
			||||||
 | 
					import refinedstorage.RefinedStorageBlocks;
 | 
				
			||||||
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.RefinedStorageCapabilities;
 | 
				
			||||||
 | 
					import refinedstorage.api.storage.CompareFlags;
 | 
				
			||||||
 | 
					import refinedstorage.api.storage.IStorage;
 | 
				
			||||||
 | 
					import refinedstorage.autocrafting.CraftingPattern;
 | 
				
			||||||
 | 
					import refinedstorage.autocrafting.task.BasicCraftingTask;
 | 
				
			||||||
 | 
					import refinedstorage.autocrafting.task.ICraftingTask;
 | 
				
			||||||
 | 
					import refinedstorage.autocrafting.task.ProcessingCraftingTask;
 | 
				
			||||||
 | 
					import refinedstorage.block.EnumControllerType;
 | 
				
			||||||
 | 
					import refinedstorage.container.ContainerGrid;
 | 
				
			||||||
 | 
					import refinedstorage.item.ItemPattern;
 | 
				
			||||||
 | 
					import refinedstorage.network.MessageGridItems;
 | 
				
			||||||
 | 
					import refinedstorage.tile.TileCrafter;
 | 
				
			||||||
 | 
					import refinedstorage.tile.TileMachine;
 | 
				
			||||||
 | 
					import refinedstorage.tile.TileWirelessTransmitter;
 | 
				
			||||||
 | 
					import refinedstorage.tile.config.RedstoneMode;
 | 
				
			||||||
 | 
					import refinedstorage.tile.controller.StorageHandler;
 | 
				
			||||||
 | 
					import refinedstorage.tile.controller.WirelessGridHandler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class StorageNetwork {
 | 
				
			||||||
 | 
					    public static final int ENERGY_CAPACITY = 32000;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static final String NBT_CRAFTING_TASKS = "CraftingTasks";
 | 
				
			||||||
 | 
					    public static final String NBT_ENERGY = "Energy";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private StorageHandler storageHandler = new StorageHandler(this);
 | 
				
			||||||
 | 
					    private WirelessGridHandler wirelessGridHandler = new WirelessGridHandler(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private List<ItemStack> items = new ArrayList<ItemStack>();
 | 
				
			||||||
 | 
					    private List<ItemStack> combinedItems = new ArrayList<ItemStack>();
 | 
				
			||||||
 | 
					    private Set<Integer> combinedItemsIndices = new HashSet<Integer>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private List<IStorage> storages = new ArrayList<IStorage>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private List<TileMachine> machines = new ArrayList<TileMachine>();
 | 
				
			||||||
 | 
					    private List<TileMachine> machinesToAdd = new ArrayList<TileMachine>();
 | 
				
			||||||
 | 
					    private List<TileMachine> machinesToRemove = new ArrayList<TileMachine>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private List<CraftingPattern> patterns = new ArrayList<CraftingPattern>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Stack<ICraftingTask> craftingTasks = new Stack<ICraftingTask>();
 | 
				
			||||||
 | 
					    private List<ICraftingTask> craftingTasksToAddAsLast = new ArrayList<ICraftingTask>();
 | 
				
			||||||
 | 
					    private List<ICraftingTask> craftingTasksToAdd = new ArrayList<ICraftingTask>();
 | 
				
			||||||
 | 
					    private List<ICraftingTask> craftingTasksToCancel = new ArrayList<ICraftingTask>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private EnergyStorage energy = new EnergyStorage(ENERGY_CAPACITY);
 | 
				
			||||||
 | 
					    private int energyUsage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private int wirelessGridRange;
 | 
				
			||||||
 | 
					    private boolean couldRun;
 | 
				
			||||||
 | 
					    private long lastEnergyUpdate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private EnumControllerType type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private World world;
 | 
				
			||||||
 | 
					    private BlockPos pos;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private RedstoneMode redstoneMode = RedstoneMode.IGNORE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageNetwork(World world, BlockPos pos, EnumControllerType type) {
 | 
				
			||||||
 | 
					        this.world = world;
 | 
				
			||||||
 | 
					        this.pos = pos;
 | 
				
			||||||
 | 
					        this.type = type;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public RedstoneMode getRedstoneMode() {
 | 
				
			||||||
 | 
					        return redstoneMode;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setRedstoneMode(RedstoneMode mode) {
 | 
				
			||||||
 | 
					        this.redstoneMode = mode;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public EnergyStorage getEnergy() {
 | 
				
			||||||
 | 
					        return energy;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public BlockPos getPos() {
 | 
				
			||||||
 | 
					        return pos;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public World getWorld() {
 | 
				
			||||||
 | 
					        return world;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public boolean canRun() {
 | 
				
			||||||
 | 
					        return energy.getEnergyStored() > 0 && energy.getEnergyStored() >= energyUsage && redstoneMode.isEnabled(world, pos);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void update(int ticks) {
 | 
				
			||||||
 | 
					        for (TileMachine machine : machinesToAdd) {
 | 
				
			||||||
 | 
					            if (!machines.contains(machine)) {
 | 
				
			||||||
 | 
					                machines.add(machine);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        machinesToAdd.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        machines.removeAll(machinesToRemove);
 | 
				
			||||||
 | 
					        machinesToRemove.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int lastEnergy = energy.getEnergyStored();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (canRun()) {
 | 
				
			||||||
 | 
					            if (ticks % 20 == 0) {
 | 
				
			||||||
 | 
					                syncMachines();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (ICraftingTask taskToCancel : craftingTasksToCancel) {
 | 
				
			||||||
 | 
					                taskToCancel.onCancelled(this);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            craftingTasks.removeAll(craftingTasksToCancel);
 | 
				
			||||||
 | 
					            craftingTasksToCancel.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (ICraftingTask task : craftingTasksToAdd) {
 | 
				
			||||||
 | 
					                craftingTasks.push(task);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            craftingTasksToAdd.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (ICraftingTask task : craftingTasksToAddAsLast) {
 | 
				
			||||||
 | 
					                craftingTasks.add(0, task);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            craftingTasksToAddAsLast.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (!craftingTasks.empty()) {
 | 
				
			||||||
 | 
					                ICraftingTask top = craftingTasks.peek();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (ticks % top.getPattern().getCrafter(world).getSpeed() == 0 && top.update(this)) {
 | 
				
			||||||
 | 
					                    top.onDone(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    craftingTasks.pop();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else if (!machines.isEmpty()) {
 | 
				
			||||||
 | 
					            // Machine list should NOT be empty to trigger a disconnect
 | 
				
			||||||
 | 
					            // We need to sync machines again to reset energy usage etc
 | 
				
			||||||
 | 
					            disconnectAll();
 | 
				
			||||||
 | 
					            syncMachines();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (couldRun != canRun()) {
 | 
				
			||||||
 | 
					            couldRun = canRun();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            world.notifyNeighborsOfStateChange(pos, RefinedStorageBlocks.CONTROLLER);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        wirelessGridHandler.update();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (type == EnumControllerType.NORMAL && energyUsage > 0) {
 | 
				
			||||||
 | 
					            if (energy.getEnergyStored() - energyUsage >= 0) {
 | 
				
			||||||
 | 
					                energy.extractEnergy(energyUsage, false);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                energy.setEnergyStored(0);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else if (type == EnumControllerType.CREATIVE) {
 | 
				
			||||||
 | 
					            energy.setEnergyStored(energy.getMaxEnergyStored());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (energy.getEnergyStored() != lastEnergy) {
 | 
				
			||||||
 | 
					            world.updateComparatorOutputLevel(pos, RefinedStorageBlocks.CONTROLLER);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (System.currentTimeMillis() - lastEnergyUpdate > 5000) {
 | 
				
			||||||
 | 
					                lastEnergyUpdate = System.currentTimeMillis();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                RefinedStorageUtils.updateBlock(world, pos);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<TileMachine> getMachines() {
 | 
				
			||||||
 | 
					        return machines;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void addMachine(TileMachine machine) {
 | 
				
			||||||
 | 
					        machinesToAdd.add(machine);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void removeMachine(TileMachine machine) {
 | 
				
			||||||
 | 
					        machinesToRemove.add(machine);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageHandler getStorageHandler() {
 | 
				
			||||||
 | 
					        return storageHandler;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public WirelessGridHandler getWirelessGridHandler() {
 | 
				
			||||||
 | 
					        return wirelessGridHandler;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public int getWirelessGridRange() {
 | 
				
			||||||
 | 
					        return wirelessGridRange;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private void disconnectAll() {
 | 
				
			||||||
 | 
					        for (TileMachine machine : machines) {
 | 
				
			||||||
 | 
					            machine.onDisconnected(world);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        machines.clear();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<ItemStack> getItems() {
 | 
				
			||||||
 | 
					        return items;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<ICraftingTask> getCraftingTasks() {
 | 
				
			||||||
 | 
					        return craftingTasks;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void addCraftingTask(ICraftingTask task) {
 | 
				
			||||||
 | 
					        craftingTasksToAdd.add(task);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void addCraftingTaskAsLast(ICraftingTask task) {
 | 
				
			||||||
 | 
					        craftingTasksToAddAsLast.add(task);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ICraftingTask createCraftingTask(CraftingPattern pattern) {
 | 
				
			||||||
 | 
					        if (pattern.isProcessing()) {
 | 
				
			||||||
 | 
					            return new ProcessingCraftingTask(pattern);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            return new BasicCraftingTask(pattern);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void cancelCraftingTask(ICraftingTask task) {
 | 
				
			||||||
 | 
					        craftingTasksToCancel.add(task);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<CraftingPattern> getPatterns() {
 | 
				
			||||||
 | 
					        return patterns;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<CraftingPattern> getPattern(ItemStack pattern, int flags) {
 | 
				
			||||||
 | 
					        List<CraftingPattern> patterns = new ArrayList<CraftingPattern>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (CraftingPattern craftingPattern : getPatterns()) {
 | 
				
			||||||
 | 
					            for (ItemStack output : craftingPattern.getOutputs()) {
 | 
				
			||||||
 | 
					                if (RefinedStorageUtils.compareStack(output, pattern, flags)) {
 | 
				
			||||||
 | 
					                    patterns.add(craftingPattern);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return patterns;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public CraftingPattern getPatternWithBestScore(ItemStack pattern) {
 | 
				
			||||||
 | 
					        return getPatternWithBestScore(pattern, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public CraftingPattern getPatternWithBestScore(ItemStack pattern, int flags) {
 | 
				
			||||||
 | 
					        List<CraftingPattern> patterns = getPattern(pattern, flags);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (patterns.isEmpty()) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        } else if (patterns.size() == 1) {
 | 
				
			||||||
 | 
					            return patterns.get(0);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int highestScore = 0;
 | 
				
			||||||
 | 
					        int highestPattern = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 0; i < patterns.size(); ++i) {
 | 
				
			||||||
 | 
					            int score = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (ItemStack input : patterns.get(i).getInputs()) {
 | 
				
			||||||
 | 
					                ItemStack stored = getItem(input, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                score += stored != null ? stored.stackSize : 0;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (score > highestScore) {
 | 
				
			||||||
 | 
					                highestScore = score;
 | 
				
			||||||
 | 
					                highestPattern = i;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return patterns.get(highestPattern);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private void syncMachines() {
 | 
				
			||||||
 | 
					        this.wirelessGridRange = 0;
 | 
				
			||||||
 | 
					        this.energyUsage = 0;
 | 
				
			||||||
 | 
					        this.storages.clear();
 | 
				
			||||||
 | 
					        this.patterns.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (TileMachine machine : machines) {
 | 
				
			||||||
 | 
					            if (!machine.canUpdate()) {
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (machine instanceof TileWirelessTransmitter) {
 | 
				
			||||||
 | 
					                this.wirelessGridRange += ((TileWirelessTransmitter) machine).getRange();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (machine.hasCapability(RefinedStorageCapabilities.STORAGE_PROVIDER_CAPABILITY, null)) {
 | 
				
			||||||
 | 
					                machine.getCapability(RefinedStorageCapabilities.STORAGE_PROVIDER_CAPABILITY, null).provide(storages);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (machine instanceof TileCrafter) {
 | 
				
			||||||
 | 
					                TileCrafter crafter = (TileCrafter) machine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                for (int i = 0; i < crafter.getPatterns().getSlots(); ++i) {
 | 
				
			||||||
 | 
					                    ItemStack pattern = crafter.getPatterns().getStackInSlot(i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if (pattern != null && ItemPattern.isValid(pattern)) {
 | 
				
			||||||
 | 
					                        patterns.add(new CraftingPattern(crafter.getPos().getX(), crafter.getPos().getY(), crafter.getPos().getZ(), ItemPattern.isProcessing(pattern), ItemPattern.getInputs(pattern), ItemPattern.getOutputs(pattern), ItemPattern.getByproducts(pattern)));
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            this.energyUsage += machine.getEnergyUsage();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Collections.sort(storages, new Comparator<IStorage>() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public int compare(IStorage left, IStorage right) {
 | 
				
			||||||
 | 
					                int leftStored = left.getStored();
 | 
				
			||||||
 | 
					                int rightStored = right.getStored();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (leftStored == rightStored) {
 | 
				
			||||||
 | 
					                    return 0;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                return (leftStored > rightStored) ? -1 : 1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Collections.sort(storages, new Comparator<IStorage>() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public int compare(IStorage left, IStorage right) {
 | 
				
			||||||
 | 
					                if (left.getPriority() == right.getPriority()) {
 | 
				
			||||||
 | 
					                    return 0;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                return (left.getPriority() > right.getPriority()) ? -1 : 1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        syncItems();
 | 
				
			||||||
 | 
					        syncItemsWithClients();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private void syncItems() {
 | 
				
			||||||
 | 
					        items.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (IStorage storage : storages) {
 | 
				
			||||||
 | 
					            storage.addItems(items);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (CraftingPattern pattern : patterns) {
 | 
				
			||||||
 | 
					            for (ItemStack output : pattern.getOutputs()) {
 | 
				
			||||||
 | 
					                ItemStack patternStack = output.copy();
 | 
				
			||||||
 | 
					                patternStack.stackSize = 0;
 | 
				
			||||||
 | 
					                items.add(patternStack);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        combinedItems.clear();
 | 
				
			||||||
 | 
					        combinedItemsIndices.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 0; i < items.size(); ++i) {
 | 
				
			||||||
 | 
					            if (combinedItemsIndices.contains(i)) {
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            ItemStack stack = items.get(i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (int j = i + 1; j < items.size(); ++j) {
 | 
				
			||||||
 | 
					                if (combinedItemsIndices.contains(j)) {
 | 
				
			||||||
 | 
					                    continue;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                ItemStack otherStack = items.get(j);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (RefinedStorageUtils.compareStackNoQuantity(stack, otherStack)) {
 | 
				
			||||||
 | 
					                    // We copy here so we don't modify the quantity of the ItemStack IStorage uses.
 | 
				
			||||||
 | 
					                    // We re-get the ItemStack because the stack may change from a previous iteration in this loop
 | 
				
			||||||
 | 
					                    ItemStack newStack = items.get(i).copy();
 | 
				
			||||||
 | 
					                    newStack.stackSize += otherStack.stackSize;
 | 
				
			||||||
 | 
					                    items.set(i, newStack);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    combinedItems.add(otherStack);
 | 
				
			||||||
 | 
					                    combinedItemsIndices.add(j);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        items.removeAll(combinedItems);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void syncItemsWithClients() {
 | 
				
			||||||
 | 
					        for (EntityPlayer player : world.playerEntities) {
 | 
				
			||||||
 | 
					            if (player.openContainer.getClass() == ContainerGrid.class && pos.equals(((ContainerGrid) player.openContainer).getGrid().getControllerPos())) {
 | 
				
			||||||
 | 
					                syncItemsWithClient((EntityPlayerMP) player);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void syncItemsWithClient(EntityPlayerMP player) {
 | 
				
			||||||
 | 
					        RefinedStorage.NETWORK.sendTo(new MessageGridItems(this), player);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ItemStack push(ItemStack stack, int size, boolean simulate) {
 | 
				
			||||||
 | 
					        if (stack == null || stack.getItem() == null) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (storages.isEmpty()) {
 | 
				
			||||||
 | 
					            return ItemHandlerHelper.copyStackWithSize(stack, size);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        int orginalSize = size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ItemStack remainder = stack;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (IStorage storage : storages) {
 | 
				
			||||||
 | 
					            remainder = storage.push(remainder, size, simulate);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (remainder == null) {
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                size = remainder.stackSize;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!simulate) {
 | 
				
			||||||
 | 
					            syncItems();
 | 
				
			||||||
 | 
					            syncItemsWithClients();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            int sizePushed = remainder != null ? (orginalSize - remainder.stackSize) : orginalSize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (int i = 0; i < sizePushed; ++i) {
 | 
				
			||||||
 | 
					                if (!craftingTasks.empty()) {
 | 
				
			||||||
 | 
					                    ICraftingTask top = craftingTasks.peek();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if (top instanceof ProcessingCraftingTask) {
 | 
				
			||||||
 | 
					                        ((ProcessingCraftingTask) top).onPushed(stack);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return remainder;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ItemStack take(ItemStack stack, int size) {
 | 
				
			||||||
 | 
					        return take(stack, size, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ItemStack take(ItemStack stack, int size, int flags) {
 | 
				
			||||||
 | 
					        int requested = size;
 | 
				
			||||||
 | 
					        int received = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ItemStack newStack = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (IStorage storage : storages) {
 | 
				
			||||||
 | 
					            ItemStack took = storage.take(stack, requested - received, flags);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (took != null) {
 | 
				
			||||||
 | 
					                if (newStack == null) {
 | 
				
			||||||
 | 
					                    newStack = took;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    newStack.stackSize += took.stackSize;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                received += took.stackSize;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (requested == received) {
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (newStack != null) {
 | 
				
			||||||
 | 
					            syncItems();
 | 
				
			||||||
 | 
					            syncItemsWithClients();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return newStack;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ItemStack getItem(ItemStack stack, int flags) {
 | 
				
			||||||
 | 
					        for (ItemStack otherStack : items) {
 | 
				
			||||||
 | 
					            if (RefinedStorageUtils.compareStack(otherStack, stack, flags)) {
 | 
				
			||||||
 | 
					                return otherStack;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public NBTTagCompound write(NBTTagCompound tag) {
 | 
				
			||||||
 | 
					        energy.writeToNBT(tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        tag.setInteger(RedstoneMode.NBT, redstoneMode.id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        NBTTagList list = new NBTTagList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (ICraftingTask task : craftingTasks) {
 | 
				
			||||||
 | 
					            NBTTagCompound taskTag = new NBTTagCompound();
 | 
				
			||||||
 | 
					            task.writeToNBT(taskTag);
 | 
				
			||||||
 | 
					            list.appendTag(taskTag);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        tag.setTag(NBT_CRAFTING_TASKS, list);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return tag;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void read(NBTTagCompound nbt) {
 | 
				
			||||||
 | 
					        energy.readFromNBT(nbt);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (nbt.hasKey(RedstoneMode.NBT)) {
 | 
				
			||||||
 | 
					            redstoneMode = RedstoneMode.getById(nbt.getInteger(RedstoneMode.NBT));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (nbt.hasKey(NBT_CRAFTING_TASKS)) {
 | 
				
			||||||
 | 
					            NBTTagList taskList = nbt.getTagList(NBT_CRAFTING_TASKS, Constants.NBT.TAG_COMPOUND);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (int i = 0; i < taskList.tagCount(); ++i) {
 | 
				
			||||||
 | 
					                NBTTagCompound taskTag = taskList.getCompoundTagAt(i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                CraftingPattern pattern = CraftingPattern.readFromNBT(taskTag.getCompoundTag(CraftingPattern.NBT));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (pattern != null) {
 | 
				
			||||||
 | 
					                    switch (taskTag.getInteger("Type")) {
 | 
				
			||||||
 | 
					                        case BasicCraftingTask.ID:
 | 
				
			||||||
 | 
					                            addCraftingTask(new BasicCraftingTask(taskTag, pattern));
 | 
				
			||||||
 | 
					                            break;
 | 
				
			||||||
 | 
					                        case ProcessingCraftingTask.ID:
 | 
				
			||||||
 | 
					                            addCraftingTask(new ProcessingCraftingTask(taskTag, pattern));
 | 
				
			||||||
 | 
					                            break;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								src/main/java/refinedstorage/api/storagenet/StorageNetworkRegistry.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										10
									
								
								src/main/java/refinedstorage/api/storagenet/StorageNetworkRegistry.java
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					package refinedstorage.api.storagenet;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class StorageNetworkRegistry {
 | 
				
			||||||
 | 
					    public static final Map<BlockPos, StorageNetwork> NETWORKS = new HashMap<BlockPos, StorageNetwork>();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -3,7 +3,7 @@ package refinedstorage.autocrafting;
 | 
				
			|||||||
import net.minecraft.item.ItemStack;
 | 
					import net.minecraft.item.ItemStack;
 | 
				
			||||||
import net.minecraft.nbt.NBTTagCompound;
 | 
					import net.minecraft.nbt.NBTTagCompound;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class CraftingTaskScheduler {
 | 
					public class CraftingTaskScheduler {
 | 
				
			||||||
    public static String NBT_SCHEDULED = "CraftingTaskScheduled";
 | 
					    public static String NBT_SCHEDULED = "CraftingTaskScheduled";
 | 
				
			||||||
@@ -14,13 +14,13 @@ public class CraftingTaskScheduler {
 | 
				
			|||||||
        return scheduledItem == null || !RefinedStorageUtils.compareStack(scheduledItem, item, compare);
 | 
					        return scheduledItem == null || !RefinedStorageUtils.compareStack(scheduledItem, item, compare);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void schedule(TileController controller, int compare, ItemStack item) {
 | 
					    public void schedule(StorageNetwork network, int compare, ItemStack item) {
 | 
				
			||||||
        CraftingPattern pattern = controller.getPatternWithBestScore(item, compare);
 | 
					        CraftingPattern pattern = network.getPatternWithBestScore(item, compare);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (pattern != null) {
 | 
					        if (pattern != null) {
 | 
				
			||||||
            scheduledItem = item;
 | 
					            scheduledItem = item;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            controller.addCraftingTask(controller.createCraftingTask(pattern));
 | 
					            network.addCraftingTask(network.createCraftingTask(pattern));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,8 +5,8 @@ import net.minecraft.nbt.NBTTagCompound;
 | 
				
			|||||||
import net.minecraft.nbt.NBTTagList;
 | 
					import net.minecraft.nbt.NBTTagList;
 | 
				
			||||||
import net.minecraftforge.common.util.Constants;
 | 
					import net.minecraftforge.common.util.Constants;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.autocrafting.CraftingPattern;
 | 
					import refinedstorage.autocrafting.CraftingPattern;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
@@ -50,7 +50,7 @@ public class BasicCraftingTask implements ICraftingTask {
 | 
				
			|||||||
        return pattern;
 | 
					        return pattern;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public boolean update(TileController controller) {
 | 
					    public boolean update(StorageNetwork network) {
 | 
				
			||||||
        this.updatedOnce = true;
 | 
					        this.updatedOnce = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        boolean done = true;
 | 
					        boolean done = true;
 | 
				
			||||||
@@ -63,17 +63,17 @@ public class BasicCraftingTask implements ICraftingTask {
 | 
				
			|||||||
            if (!satisfied[i]) {
 | 
					            if (!satisfied[i]) {
 | 
				
			||||||
                done = false;
 | 
					                done = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                ItemStack took = controller.take(input, 1);
 | 
					                ItemStack took = network.take(input, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (took != null) {
 | 
					                if (took != null) {
 | 
				
			||||||
                    itemsTook.add(took);
 | 
					                    itemsTook.add(took);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    satisfied[i] = true;
 | 
					                    satisfied[i] = true;
 | 
				
			||||||
                } else if (!childTasks[i]) {
 | 
					                } else if (!childTasks[i]) {
 | 
				
			||||||
                    CraftingPattern pattern = controller.getPatternWithBestScore(input);
 | 
					                    CraftingPattern pattern = network.getPatternWithBestScore(input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (pattern != null) {
 | 
					                    if (pattern != null) {
 | 
				
			||||||
                        controller.addCraftingTask(controller.createCraftingTask(pattern));
 | 
					                        network.addCraftingTask(network.createCraftingTask(pattern));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        childTasks[i] = true;
 | 
					                        childTasks[i] = true;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
@@ -90,23 +90,23 @@ public class BasicCraftingTask implements ICraftingTask {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // @todo: handle no space
 | 
					    // @todo: handle no space
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onDone(TileController controller) {
 | 
					    public void onDone(StorageNetwork network) {
 | 
				
			||||||
        for (ItemStack output : pattern.getOutputs()) {
 | 
					        for (ItemStack output : pattern.getOutputs()) {
 | 
				
			||||||
            controller.push(output, output.stackSize, false);
 | 
					            network.push(output, output.stackSize, false);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (pattern.getByproducts() != null) {
 | 
					        if (pattern.getByproducts() != null) {
 | 
				
			||||||
            for (ItemStack byproduct : pattern.getByproducts()) {
 | 
					            for (ItemStack byproduct : pattern.getByproducts()) {
 | 
				
			||||||
                controller.push(byproduct, byproduct.stackSize, false);
 | 
					                network.push(byproduct, byproduct.stackSize, false);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // @todo: handle no space
 | 
					    // @todo: handle no space
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onCancelled(TileController controller) {
 | 
					    public void onCancelled(StorageNetwork network) {
 | 
				
			||||||
        for (ItemStack took : itemsTook) {
 | 
					        for (ItemStack took : itemsTook) {
 | 
				
			||||||
            controller.push(took, took.stackSize, false);
 | 
					            network.push(took, took.stackSize, false);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,17 +1,17 @@
 | 
				
			|||||||
package refinedstorage.autocrafting.task;
 | 
					package refinedstorage.autocrafting.task;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import net.minecraft.nbt.NBTTagCompound;
 | 
					import net.minecraft.nbt.NBTTagCompound;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.autocrafting.CraftingPattern;
 | 
					import refinedstorage.autocrafting.CraftingPattern;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public interface ICraftingTask {
 | 
					public interface ICraftingTask {
 | 
				
			||||||
    CraftingPattern getPattern();
 | 
					    CraftingPattern getPattern();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    boolean update(TileController controller);
 | 
					    boolean update(StorageNetwork network);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void onDone(TileController controller);
 | 
					    void onDone(StorageNetwork network);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void onCancelled(TileController controller);
 | 
					    void onCancelled(StorageNetwork network);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void writeToNBT(NBTTagCompound tag);
 | 
					    void writeToNBT(NBTTagCompound tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,9 +5,9 @@ import net.minecraft.nbt.NBTTagCompound;
 | 
				
			|||||||
import net.minecraftforge.items.IItemHandler;
 | 
					import net.minecraftforge.items.IItemHandler;
 | 
				
			||||||
import net.minecraftforge.items.ItemHandlerHelper;
 | 
					import net.minecraftforge.items.ItemHandlerHelper;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.autocrafting.CraftingPattern;
 | 
					import refinedstorage.autocrafting.CraftingPattern;
 | 
				
			||||||
import refinedstorage.tile.TileCrafter;
 | 
					import refinedstorage.tile.TileCrafter;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ProcessingCraftingTask implements ICraftingTask {
 | 
					public class ProcessingCraftingTask implements ICraftingTask {
 | 
				
			||||||
    public static final int ID = 1;
 | 
					    public static final int ID = 1;
 | 
				
			||||||
@@ -42,17 +42,17 @@ public class ProcessingCraftingTask implements ICraftingTask {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public boolean update(TileController controller) {
 | 
					    public boolean update(StorageNetwork network) {
 | 
				
			||||||
        this.updatedOnce = true;
 | 
					        this.updatedOnce = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        TileCrafter crafter = pattern.getCrafter(controller.getWorld());
 | 
					        TileCrafter crafter = pattern.getCrafter(network.getWorld());
 | 
				
			||||||
        IItemHandler handler = RefinedStorageUtils.getItemHandler(crafter.getFacingTile(), crafter.getDirection().getOpposite());
 | 
					        IItemHandler handler = RefinedStorageUtils.getItemHandler(crafter.getFacingTile(), crafter.getDirection().getOpposite());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (handler != null) {
 | 
					        if (handler != null) {
 | 
				
			||||||
            for (int i = 0; i < inserted.length; ++i) {
 | 
					            for (int i = 0; i < inserted.length; ++i) {
 | 
				
			||||||
                if (!inserted[i]) {
 | 
					                if (!inserted[i]) {
 | 
				
			||||||
                    ItemStack input = pattern.getInputs()[i];
 | 
					                    ItemStack input = pattern.getInputs()[i];
 | 
				
			||||||
                    ItemStack took = controller.take(input, 1);
 | 
					                    ItemStack took = network.take(input, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (took != null) {
 | 
					                    if (took != null) {
 | 
				
			||||||
                        if (ItemHandlerHelper.insertItem(handler, took, true) == null) {
 | 
					                        if (ItemHandlerHelper.insertItem(handler, took, true) == null) {
 | 
				
			||||||
@@ -60,15 +60,15 @@ public class ProcessingCraftingTask implements ICraftingTask {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                            inserted[i] = true;
 | 
					                            inserted[i] = true;
 | 
				
			||||||
                        } else {
 | 
					                        } else {
 | 
				
			||||||
                            controller.push(took, took.stackSize, false);
 | 
					                            network.push(took, took.stackSize, false);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    } else if (!childTasks[i]) {
 | 
					                    } else if (!childTasks[i]) {
 | 
				
			||||||
                        CraftingPattern pattern = controller.getPatternWithBestScore(input);
 | 
					                        CraftingPattern pattern = network.getPatternWithBestScore(input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if (pattern != null) {
 | 
					                        if (pattern != null) {
 | 
				
			||||||
                            childTasks[i] = true;
 | 
					                            childTasks[i] = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            controller.addCraftingTask(controller.createCraftingTask(pattern));
 | 
					                            network.addCraftingTask(network.createCraftingTask(pattern));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            break;
 | 
					                            break;
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
@@ -101,12 +101,12 @@ public class ProcessingCraftingTask implements ICraftingTask {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onDone(TileController controller) {
 | 
					    public void onDone(StorageNetwork network) {
 | 
				
			||||||
        // NO OP
 | 
					        // NO OP
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onCancelled(TileController controller) {
 | 
					    public void onCancelled(StorageNetwork network) {
 | 
				
			||||||
        // NO OP
 | 
					        // NO OP
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,8 @@ import net.minecraft.world.World;
 | 
				
			|||||||
import refinedstorage.RefinedStorage;
 | 
					import refinedstorage.RefinedStorage;
 | 
				
			||||||
import refinedstorage.RefinedStorageBlocks;
 | 
					import refinedstorage.RefinedStorageBlocks;
 | 
				
			||||||
import refinedstorage.RefinedStorageGui;
 | 
					import refinedstorage.RefinedStorageGui;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
import refinedstorage.item.ItemBlockController;
 | 
					import refinedstorage.item.ItemBlockController;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					import refinedstorage.tile.controller.TileController;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -91,12 +93,8 @@ public class BlockController extends BlockBase {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        NBTTagCompound tag = itemStack.getTagCompound();
 | 
					        NBTTagCompound tag = itemStack.getTagCompound();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tag != null && tag.hasKey(TileController.NBT_ENERGY)) {
 | 
					        if (tag != null && tag.hasKey(StorageNetwork.NBT_ENERGY)) {
 | 
				
			||||||
            TileEntity tile = world.getTileEntity(pos);
 | 
					            StorageNetworkRegistry.NETWORKS.get(pos).getEnergy().receiveEnergy(tag.getInteger(StorageNetwork.NBT_ENERGY), false);
 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (tile instanceof TileController) {
 | 
					 | 
				
			||||||
                ((TileController) tile).receiveEnergy(null, tag.getInteger(TileController.NBT_ENERGY), false);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -107,7 +105,7 @@ public class BlockController extends BlockBase {
 | 
				
			|||||||
        ItemStack stack = new ItemStack(RefinedStorageBlocks.CONTROLLER, 1, RefinedStorageBlocks.CONTROLLER.getMetaFromState(state));
 | 
					        ItemStack stack = new ItemStack(RefinedStorageBlocks.CONTROLLER, 1, RefinedStorageBlocks.CONTROLLER.getMetaFromState(state));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        NBTTagCompound tag = new NBTTagCompound();
 | 
					        NBTTagCompound tag = new NBTTagCompound();
 | 
				
			||||||
        tag.setInteger(TileController.NBT_ENERGY, ((TileController) world.getTileEntity(pos)).getEnergyStored(null));
 | 
					        tag.setInteger(StorageNetwork.NBT_ENERGY, ((TileController) world.getTileEntity(pos)).getEnergyStored(null));
 | 
				
			||||||
        stack.setTagCompound(tag);
 | 
					        stack.setTagCompound(tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        drops.add(stack);
 | 
					        drops.add(stack);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,8 +6,8 @@ import net.minecraft.item.ItemStack;
 | 
				
			|||||||
import net.minecraft.nbt.NBTTagCompound;
 | 
					import net.minecraft.nbt.NBTTagCompound;
 | 
				
			||||||
import net.minecraft.world.World;
 | 
					import net.minecraft.world.World;
 | 
				
			||||||
import refinedstorage.RefinedStorageBlocks;
 | 
					import refinedstorage.RefinedStorageBlocks;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.block.EnumControllerType;
 | 
					import refinedstorage.block.EnumControllerType;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -21,11 +21,11 @@ public class ItemBlockController extends ItemBlockBase {
 | 
				
			|||||||
        if (stack.getMetadata() != EnumControllerType.CREATIVE.getId()) {
 | 
					        if (stack.getMetadata() != EnumControllerType.CREATIVE.getId()) {
 | 
				
			||||||
            int energyStored = 0;
 | 
					            int energyStored = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (stack.getTagCompound() != null && stack.getTagCompound().hasKey(TileController.NBT_ENERGY)) {
 | 
					            if (stack.getTagCompound() != null && stack.getTagCompound().hasKey(StorageNetwork.NBT_ENERGY)) {
 | 
				
			||||||
                energyStored = stack.getTagCompound().getInteger(TileController.NBT_ENERGY);
 | 
					                energyStored = stack.getTagCompound().getInteger(StorageNetwork.NBT_ENERGY);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            list.add(I18n.format("misc.refinedstorage:energy_stored", energyStored, TileController.ENERGY_CAPACITY));
 | 
					            list.add(I18n.format("misc.refinedstorage:energy_stored", energyStored, StorageNetwork.ENERGY_CAPACITY));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -43,7 +43,7 @@ public class ItemBlockController extends ItemBlockBase {
 | 
				
			|||||||
            tag = new NBTTagCompound();
 | 
					            tag = new NBTTagCompound();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        tag.setInteger(TileController.NBT_ENERGY, stack.getMetadata() == EnumControllerType.CREATIVE.getId() ? TileController.ENERGY_CAPACITY : 0);
 | 
					        tag.setInteger(StorageNetwork.NBT_ENERGY, stack.getMetadata() == EnumControllerType.CREATIVE.getId() ? StorageNetwork.ENERGY_CAPACITY : 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return stack;
 | 
					        return stack;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,14 +10,14 @@ import net.minecraft.item.IItemPropertyGetter;
 | 
				
			|||||||
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.*;
 | 
					import net.minecraft.util.*;
 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraft.util.text.TextComponentTranslation;
 | 
					import net.minecraft.util.text.TextComponentTranslation;
 | 
				
			||||||
import net.minecraft.world.World;
 | 
					import net.minecraft.world.World;
 | 
				
			||||||
import refinedstorage.RefinedStorage;
 | 
					import refinedstorage.RefinedStorage;
 | 
				
			||||||
import refinedstorage.RefinedStorageBlocks;
 | 
					import refinedstorage.RefinedStorageBlocks;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
import refinedstorage.tile.grid.TileGrid;
 | 
					import refinedstorage.tile.grid.TileGrid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
@@ -134,10 +134,10 @@ public class ItemWirelessGrid extends ItemEnergyContainer {
 | 
				
			|||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand) {
 | 
					    public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand) {
 | 
				
			||||||
        if (!world.isRemote && hasValidNBT(stack) && getDimensionId(stack) == player.dimension) {
 | 
					        if (!world.isRemote && hasValidNBT(stack) && getDimensionId(stack) == player.dimension) {
 | 
				
			||||||
            TileEntity tile = world.getTileEntity(new BlockPos(getX(stack), getY(stack), getZ(stack)));
 | 
					            StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(getX(stack), getY(stack), getZ(stack)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (tile instanceof TileController) {
 | 
					            if (network != null) {
 | 
				
			||||||
                if (((TileController) tile).getWirelessGridHandler().handleOpen(player, hand)) {
 | 
					                if (network.getWirelessGridHandler().handleOpen(player, hand)) {
 | 
				
			||||||
                    return new ActionResult(EnumActionResult.SUCCESS, stack);
 | 
					                    return new ActionResult(EnumActionResult.SUCCESS, stack);
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    player.addChatComponentMessage(new TextComponentTranslation("misc.refinedstorage:wireless_grid.out_of_range"));
 | 
					                    player.addChatComponentMessage(new TextComponentTranslation("misc.refinedstorage:wireless_grid.out_of_range"));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,7 +47,7 @@ public class MessageCraftingMonitorCancel extends MessageHandlerPlayerToServer<M
 | 
				
			|||||||
            TileCraftingMonitor monitor = (TileCraftingMonitor) tile;
 | 
					            TileCraftingMonitor monitor = (TileCraftingMonitor) tile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (monitor.isConnected()) {
 | 
					            if (monitor.isConnected()) {
 | 
				
			||||||
                monitor.getController().getStorageHandler().onCraftingCancelRequested(message.id);
 | 
					                monitor.getNetwork().getStorageHandler().onCraftingCancelRequested(message.id);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,7 +50,7 @@ public class MessageGridCraftingClear extends MessageHandlerPlayerToServer<Messa
 | 
				
			|||||||
                        ItemStack slot = grid.getMatrix().getStackInSlot(i);
 | 
					                        ItemStack slot = grid.getMatrix().getStackInSlot(i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if (slot != null) {
 | 
					                        if (slot != null) {
 | 
				
			||||||
                            grid.getMatrix().setInventorySlotContents(i, grid.getController().push(slot, slot.stackSize, false));
 | 
					                            grid.getMatrix().setInventorySlotContents(i, grid.getNetwork().push(slot, slot.stackSize, false));
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                } else if (grid.getType() == EnumGridType.PATTERN) {
 | 
					                } else if (grid.getType() == EnumGridType.PATTERN) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,10 @@ package refinedstorage.network;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import io.netty.buffer.ByteBuf;
 | 
					import io.netty.buffer.ByteBuf;
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
					import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			||||||
import net.minecraft.tileentity.TileEntity;
 | 
					 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
				
			||||||
import refinedstorage.tile.grid.TileGrid;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MessageGridCraftingStart extends MessageHandlerPlayerToServer<MessageGridCraftingStart> implements IMessage {
 | 
					public class MessageGridCraftingStart extends MessageHandlerPlayerToServer<MessageGridCraftingStart> implements IMessage {
 | 
				
			||||||
    private int x;
 | 
					    private int x;
 | 
				
			||||||
@@ -45,10 +45,10 @@ public class MessageGridCraftingStart extends MessageHandlerPlayerToServer<Messa
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void handle(MessageGridCraftingStart message, EntityPlayerMP player) {
 | 
					    public void handle(MessageGridCraftingStart message, EntityPlayerMP player) {
 | 
				
			||||||
        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.x, message.y, message.z));
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.x, message.y, message.z));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileGrid && ((TileGrid) tile).isConnected()) {
 | 
					        if (network != null && network.canRun()) {
 | 
				
			||||||
            ((TileGrid) tile).getController().getStorageHandler().onCraftingRequested(message.id, message.quantity);
 | 
					            network.getStorageHandler().onCraftingRequested(message.id, message.quantity);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -44,7 +44,7 @@ public class MessageGridHeldItemPush extends MessageHandlerPlayerToServer<Messag
 | 
				
			|||||||
        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.x, message.y, message.z));
 | 
					        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.x, message.y, message.z));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileGrid && ((TileGrid) tile).isConnected()) {
 | 
					        if (tile instanceof TileGrid && ((TileGrid) tile).isConnected()) {
 | 
				
			||||||
            ((TileGrid) tile).getController().getStorageHandler().onHeldItemPush(message.one, player);
 | 
					            ((TileGrid) tile).getNetwork().getStorageHandler().onHeldItemPush(message.one, player);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,22 +8,22 @@ import net.minecraftforge.fml.common.network.ByteBufUtils;
 | 
				
			|||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.container.ContainerGrid;
 | 
					import refinedstorage.container.ContainerGrid;
 | 
				
			||||||
import refinedstorage.tile.ClientItem;
 | 
					import refinedstorage.tile.ClientItem;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MessageGridItems implements IMessage, IMessageHandler<MessageGridItems, IMessage> {
 | 
					public class MessageGridItems implements IMessage, IMessageHandler<MessageGridItems, IMessage> {
 | 
				
			||||||
    private TileController controller;
 | 
					    private StorageNetwork network;
 | 
				
			||||||
    private List<ClientItem> items = new ArrayList<ClientItem>();
 | 
					    private List<ClientItem> items = new ArrayList<ClientItem>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public MessageGridItems() {
 | 
					    public MessageGridItems() {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public MessageGridItems(TileController controller) {
 | 
					    public MessageGridItems(StorageNetwork network) {
 | 
				
			||||||
        this.controller = controller;
 | 
					        this.network = network;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
@@ -42,9 +42,9 @@ public class MessageGridItems implements IMessage, IMessageHandler<MessageGridIt
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void toBytes(ByteBuf buf) {
 | 
					    public void toBytes(ByteBuf buf) {
 | 
				
			||||||
        buf.writeInt(controller.getItems().size());
 | 
					        buf.writeInt(network.getItems().size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (ItemStack item : controller.getItems()) {
 | 
					        for (ItemStack item : network.getItems()) {
 | 
				
			||||||
            buf.writeInt(item.stackSize);
 | 
					            buf.writeInt(item.stackSize);
 | 
				
			||||||
            ByteBufUtils.writeItemStack(buf, item);
 | 
					            ByteBufUtils.writeItemStack(buf, item);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,10 @@ package refinedstorage.network;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import io.netty.buffer.ByteBuf;
 | 
					import io.netty.buffer.ByteBuf;
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
					import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			||||||
import net.minecraft.tileentity.TileEntity;
 | 
					 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
				
			||||||
import refinedstorage.tile.grid.TileGrid;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MessageGridStoragePull extends MessageHandlerPlayerToServer<MessageGridStoragePull> implements IMessage {
 | 
					public class MessageGridStoragePull extends MessageHandlerPlayerToServer<MessageGridStoragePull> implements IMessage {
 | 
				
			||||||
    private int x;
 | 
					    private int x;
 | 
				
			||||||
@@ -45,10 +45,10 @@ public class MessageGridStoragePull extends MessageHandlerPlayerToServer<Message
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void handle(MessageGridStoragePull message, EntityPlayerMP player) {
 | 
					    public void handle(MessageGridStoragePull message, EntityPlayerMP player) {
 | 
				
			||||||
        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.x, message.y, message.z));
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.x, message.y, message.z));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileGrid && ((TileGrid) tile).isConnected()) {
 | 
					        if (network != null && network.canRun()) {
 | 
				
			||||||
            ((TileGrid) tile).getController().getStorageHandler().onPull(message.id, message.flags, player);
 | 
					            network.getStorageHandler().onPull(message.id, message.flags, player);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,10 @@ package refinedstorage.network;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import io.netty.buffer.ByteBuf;
 | 
					import io.netty.buffer.ByteBuf;
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
					import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			||||||
import net.minecraft.tileentity.TileEntity;
 | 
					 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MessageWirelessGridCraftingStart extends MessageHandlerPlayerToServer<MessageWirelessGridCraftingStart> implements IMessage {
 | 
					public class MessageWirelessGridCraftingStart extends MessageHandlerPlayerToServer<MessageWirelessGridCraftingStart> implements IMessage {
 | 
				
			||||||
    private int controllerX;
 | 
					    private int controllerX;
 | 
				
			||||||
@@ -45,10 +45,10 @@ public class MessageWirelessGridCraftingStart extends MessageHandlerPlayerToServ
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void handle(MessageWirelessGridCraftingStart message, EntityPlayerMP player) {
 | 
					    public void handle(MessageWirelessGridCraftingStart message, EntityPlayerMP player) {
 | 
				
			||||||
        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileController && ((TileController) tile).canRun()) {
 | 
					        if (network != null && network.canRun()) {
 | 
				
			||||||
            ((TileController) tile).getStorageHandler().onCraftingRequested(message.id, message.quantity);
 | 
					            network.getStorageHandler().onCraftingRequested(message.id, message.quantity);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,10 @@ package refinedstorage.network;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import io.netty.buffer.ByteBuf;
 | 
					import io.netty.buffer.ByteBuf;
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
					import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			||||||
import net.minecraft.tileentity.TileEntity;
 | 
					 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MessageWirelessGridHeldItemPush extends MessageHandlerPlayerToServer<MessageWirelessGridHeldItemPush> implements IMessage {
 | 
					public class MessageWirelessGridHeldItemPush extends MessageHandlerPlayerToServer<MessageWirelessGridHeldItemPush> implements IMessage {
 | 
				
			||||||
    private int controllerX;
 | 
					    private int controllerX;
 | 
				
			||||||
@@ -41,10 +41,10 @@ public class MessageWirelessGridHeldItemPush extends MessageHandlerPlayerToServe
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void handle(MessageWirelessGridHeldItemPush message, EntityPlayerMP player) {
 | 
					    public void handle(MessageWirelessGridHeldItemPush message, EntityPlayerMP player) {
 | 
				
			||||||
        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileController && ((TileController) tile).canRun()) {
 | 
					        if (network != null && network.canRun()) {
 | 
				
			||||||
            ((TileController) tile).getStorageHandler().onHeldItemPush(message.one, player);
 | 
					            network.getStorageHandler().onHeldItemPush(message.one, player);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,10 @@ package refinedstorage.network;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import io.netty.buffer.ByteBuf;
 | 
					import io.netty.buffer.ByteBuf;
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
					import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			||||||
import net.minecraft.tileentity.TileEntity;
 | 
					 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
					import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MessageWirelessGridStoragePull extends MessageHandlerPlayerToServer<MessageWirelessGridStoragePull> implements IMessage {
 | 
					public class MessageWirelessGridStoragePull extends MessageHandlerPlayerToServer<MessageWirelessGridStoragePull> implements IMessage {
 | 
				
			||||||
    private int controllerX;
 | 
					    private int controllerX;
 | 
				
			||||||
@@ -45,10 +45,10 @@ public class MessageWirelessGridStoragePull extends MessageHandlerPlayerToServer
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void handle(MessageWirelessGridStoragePull message, EntityPlayerMP player) {
 | 
					    public void handle(MessageWirelessGridStoragePull message, EntityPlayerMP player) {
 | 
				
			||||||
        TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(new BlockPos(message.controllerX, message.controllerY, message.controllerZ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileController && ((TileController) tile).canRun()) {
 | 
					        if (network != null && network.canRun()) {
 | 
				
			||||||
            ((TileController) tile).getStorageHandler().onPull(message.id, message.flags, player);
 | 
					            network.getStorageHandler().onPull(message.id, message.flags, player);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -58,7 +58,7 @@ public class TileConstructor extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
            BlockPos front = pos.offset(getDirection());
 | 
					            BlockPos front = pos.offset(getDirection());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (worldObj.isAirBlock(front) && block.getBlock().canPlaceBlockAt(worldObj, front)) {
 | 
					            if (worldObj.isAirBlock(front) && block.getBlock().canPlaceBlockAt(worldObj, front)) {
 | 
				
			||||||
                ItemStack took = controller.take(filter.getStackInSlot(0), 1, compare);
 | 
					                ItemStack took = network.take(filter.getStackInSlot(0), 1, compare);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (took != null) {
 | 
					                if (took != null) {
 | 
				
			||||||
                    scheduler.resetSchedule();
 | 
					                    scheduler.resetSchedule();
 | 
				
			||||||
@@ -70,7 +70,7 @@ public class TileConstructor extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
                    ItemStack craft = filter.getStackInSlot(0);
 | 
					                    ItemStack craft = filter.getStackInSlot(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (scheduler.canSchedule(compare, craft)) {
 | 
					                    if (scheduler.canSchedule(compare, craft)) {
 | 
				
			||||||
                        scheduler.schedule(controller, compare, craft);
 | 
					                        scheduler.schedule(network, compare, craft);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -45,9 +45,9 @@ public class TileCrafter extends TileMachine {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onDisconnected(World world) {
 | 
					    public void onDisconnected(World world) {
 | 
				
			||||||
        for (ICraftingTask task : controller.getCraftingTasks()) {
 | 
					        for (ICraftingTask task : network.getCraftingTasks()) {
 | 
				
			||||||
            if (task.getPattern().getCrafter(worldObj) == this) {
 | 
					            if (task.getPattern().getCrafter(worldObj) == this) {
 | 
				
			||||||
                controller.cancelCraftingTask(task);
 | 
					                network.cancelCraftingTask(task);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,9 +28,9 @@ public class TileCraftingMonitor extends TileMachine {
 | 
				
			|||||||
        super.writeContainerData(buf);
 | 
					        super.writeContainerData(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (connected) {
 | 
					        if (connected) {
 | 
				
			||||||
            buf.writeInt(controller.getCraftingTasks().size());
 | 
					            buf.writeInt(network.getCraftingTasks().size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            for (ICraftingTask task : controller.getCraftingTasks()) {
 | 
					            for (ICraftingTask task : network.getCraftingTasks()) {
 | 
				
			||||||
                ByteBufUtils.writeUTF8String(buf, task.getInfo());
 | 
					                ByteBufUtils.writeUTF8String(buf, task.getInfo());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                buf.writeInt(task.getPattern().getOutputs().length);
 | 
					                buf.writeInt(task.getPattern().getOutputs().length);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -64,10 +64,10 @@ public class TileDestructor extends TileMachine implements ICompareConfig, IMode
 | 
				
			|||||||
                    for (ItemStack drop : drops) {
 | 
					                    for (ItemStack drop : drops) {
 | 
				
			||||||
                        // We check if the controller isn't null here because when a destructor faces a machine block and removes it
 | 
					                        // We check if the controller isn't null here because when a destructor faces a machine block and removes it
 | 
				
			||||||
                        // it will essentially remove this block itself from the network without knowing
 | 
					                        // it will essentially remove this block itself from the network without knowing
 | 
				
			||||||
                        if (controller == null) {
 | 
					                        if (network == null) {
 | 
				
			||||||
                            InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), drop);
 | 
					                            InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), drop);
 | 
				
			||||||
                        } else {
 | 
					                        } else {
 | 
				
			||||||
                            ItemStack remainder = controller.push(drop, drop.stackSize, false);
 | 
					                            ItemStack remainder = network.push(drop, drop.stackSize, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            if (remainder != null) {
 | 
					                            if (remainder != null) {
 | 
				
			||||||
                                InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), remainder);
 | 
					                                InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), remainder);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -53,7 +53,7 @@ public class TileDetector extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
            boolean wasPowered = powered;
 | 
					            boolean wasPowered = powered;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (slot != null) {
 | 
					            if (slot != null) {
 | 
				
			||||||
                ItemStack stack = controller.getItem(slot, compare);
 | 
					                ItemStack stack = network.getItem(slot, compare);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (stack != null) {
 | 
					                if (stack != null) {
 | 
				
			||||||
                    switch (mode) {
 | 
					                    switch (mode) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,7 +50,7 @@ public class TileExporter extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
                if (slot != null) {
 | 
					                if (slot != null) {
 | 
				
			||||||
                    int size = RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_STACK) ? 64 : 1;
 | 
					                    int size = RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_STACK) ? 64 : 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    ItemStack took = controller.take(slot, size, compare);
 | 
					                    ItemStack took = network.take(slot, size, compare);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (took != null) {
 | 
					                    if (took != null) {
 | 
				
			||||||
                        scheduler.resetSchedule();
 | 
					                        scheduler.resetSchedule();
 | 
				
			||||||
@@ -58,11 +58,11 @@ public class TileExporter extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
                        ItemStack remainder = ItemHandlerHelper.insertItem(handler, took, false);
 | 
					                        ItemStack remainder = ItemHandlerHelper.insertItem(handler, took, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if (remainder != null) {
 | 
					                        if (remainder != null) {
 | 
				
			||||||
                            controller.push(remainder, remainder.stackSize, false);
 | 
					                            network.push(remainder, remainder.stackSize, false);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    } else if (RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_CRAFTING)) {
 | 
					                    } else if (RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_CRAFTING)) {
 | 
				
			||||||
                        if (scheduler.canSchedule(compare, slot)) {
 | 
					                        if (scheduler.canSchedule(compare, slot)) {
 | 
				
			||||||
                            scheduler.schedule(controller, compare, slot);
 | 
					                            scheduler.schedule(network, compare, slot);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -63,8 +63,8 @@ public class TileImporter extends TileMachine implements ICompareConfig, IModeCo
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                ItemStack result = handler.extractItem(currentSlot, quantity, true);
 | 
					                ItemStack result = handler.extractItem(currentSlot, quantity, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (result != null && controller.push(result, result.stackSize, true) == null) {
 | 
					                if (result != null && network.push(result, result.stackSize, true) == null) {
 | 
				
			||||||
                    controller.push(result, result.stackSize, false);
 | 
					                    network.push(result, result.stackSize, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    handler.extractItem(currentSlot, quantity, false);
 | 
					                    handler.extractItem(currentSlot, quantity, false);
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -52,7 +52,7 @@ public class TileInterface extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
        } else if (ticks % RefinedStorageUtils.getSpeed(upgrades) == 0) {
 | 
					        } else if (ticks % RefinedStorageUtils.getSpeed(upgrades) == 0) {
 | 
				
			||||||
            int size = Math.min(slot.stackSize, RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_STACK) ? 64 : 1);
 | 
					            int size = Math.min(slot.stackSize, RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_STACK) ? 64 : 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            ItemStack remainder = controller.push(slot, size, false);
 | 
					            ItemStack remainder = network.push(slot, size, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (remainder == null) {
 | 
					            if (remainder == null) {
 | 
				
			||||||
                importItems.extractItem(currentSlot, size, false);
 | 
					                importItems.extractItem(currentSlot, size, false);
 | 
				
			||||||
@@ -67,13 +67,13 @@ public class TileInterface extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            if (wanted == null) {
 | 
					            if (wanted == null) {
 | 
				
			||||||
                if (got != null) {
 | 
					                if (got != null) {
 | 
				
			||||||
                    exportItems.setStackInSlot(i, controller.push(got, got.stackSize, false));
 | 
					                    exportItems.setStackInSlot(i, network.push(got, got.stackSize, false));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                int delta = got == null ? wanted.stackSize : (wanted.stackSize - got.stackSize);
 | 
					                int delta = got == null ? wanted.stackSize : (wanted.stackSize - got.stackSize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (delta > 0) {
 | 
					                if (delta > 0) {
 | 
				
			||||||
                    ItemStack result = controller.take(wanted, delta, compare);
 | 
					                    ItemStack result = network.take(wanted, delta, compare);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (result != null) {
 | 
					                    if (result != null) {
 | 
				
			||||||
                        if (got == null) {
 | 
					                        if (got == null) {
 | 
				
			||||||
@@ -83,7 +83,7 @@ public class TileInterface extends TileMachine implements ICompareConfig {
 | 
				
			|||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                } else if (delta < 0) {
 | 
					                } else if (delta < 0) {
 | 
				
			||||||
                    ItemStack remainder = controller.push(got, Math.abs(delta), false);
 | 
					                    ItemStack remainder = network.push(got, Math.abs(delta), false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (remainder == null) {
 | 
					                    if (remainder == null) {
 | 
				
			||||||
                        exportItems.extractItem(i, Math.abs(delta), false);
 | 
					                        exportItems.extractItem(i, Math.abs(delta), false);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ import net.minecraft.block.Block;
 | 
				
			|||||||
import net.minecraft.nbt.NBTTagCompound;
 | 
					import net.minecraft.nbt.NBTTagCompound;
 | 
				
			||||||
import net.minecraft.world.World;
 | 
					import net.minecraft.world.World;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
import refinedstorage.tile.config.IRedstoneModeConfig;
 | 
					import refinedstorage.tile.config.IRedstoneModeConfig;
 | 
				
			||||||
import refinedstorage.tile.config.RedstoneMode;
 | 
					import refinedstorage.tile.config.RedstoneMode;
 | 
				
			||||||
import refinedstorage.tile.controller.ControllerSearcher;
 | 
					import refinedstorage.tile.controller.ControllerSearcher;
 | 
				
			||||||
@@ -19,22 +21,18 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta
 | 
				
			|||||||
    protected boolean connected;
 | 
					    protected boolean connected;
 | 
				
			||||||
    protected boolean wasConnected;
 | 
					    protected boolean wasConnected;
 | 
				
			||||||
    protected RedstoneMode redstoneMode = RedstoneMode.IGNORE;
 | 
					    protected RedstoneMode redstoneMode = RedstoneMode.IGNORE;
 | 
				
			||||||
    protected TileController controller;
 | 
					    protected StorageNetwork network;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private Block block;
 | 
					    private Block block;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private Set<String> visited = new HashSet<String>();
 | 
					    private Set<String> visited = new HashSet<String>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public TileController getController() {
 | 
					 | 
				
			||||||
        return controller;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void searchController(World world) {
 | 
					    public void searchController(World world) {
 | 
				
			||||||
        visited.clear();
 | 
					        visited.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        TileController newController = ControllerSearcher.search(world, pos, visited);
 | 
					        TileController newController = ControllerSearcher.search(world, pos, visited);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (controller == null) {
 | 
					        if (network == null) {
 | 
				
			||||||
            if (newController != null) {
 | 
					            if (newController != null) {
 | 
				
			||||||
                onConnected(world, newController);
 | 
					                onConnected(world, newController);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -87,14 +85,16 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private boolean tryConnect(TileController controller) {
 | 
					    private boolean tryConnect(TileController controller) {
 | 
				
			||||||
        if (!controller.canRun()) {
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controller.getPos());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!network.canRun()) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.controller = controller;
 | 
					        this.network = network;
 | 
				
			||||||
        this.connected = true;
 | 
					        this.connected = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        controller.addMachine(this);
 | 
					        network.addMachine(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -102,14 +102,18 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta
 | 
				
			|||||||
    public void onDisconnected(World world) {
 | 
					    public void onDisconnected(World world) {
 | 
				
			||||||
        this.connected = false;
 | 
					        this.connected = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (this.controller != null) {
 | 
					        if (this.network != null) {
 | 
				
			||||||
            this.controller.removeMachine(this);
 | 
					            this.network.removeMachine(this);
 | 
				
			||||||
            this.controller = null;
 | 
					            this.network = null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        world.notifyNeighborsOfStateChange(pos, block);
 | 
					        world.notifyNeighborsOfStateChange(pos, block);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageNetwork getNetwork() {
 | 
				
			||||||
 | 
					        return network;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public boolean isConnected() {
 | 
					    public boolean isConnected() {
 | 
				
			||||||
        return connected;
 | 
					        return connected;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,7 @@ import net.minecraft.entity.player.EntityPlayerMP;
 | 
				
			|||||||
import net.minecraft.inventory.InventoryHelper;
 | 
					import net.minecraft.inventory.InventoryHelper;
 | 
				
			||||||
import net.minecraft.item.ItemStack;
 | 
					import net.minecraft.item.ItemStack;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.autocrafting.CraftingPattern;
 | 
					import refinedstorage.autocrafting.CraftingPattern;
 | 
				
			||||||
import refinedstorage.autocrafting.task.ICraftingTask;
 | 
					import refinedstorage.autocrafting.task.ICraftingTask;
 | 
				
			||||||
import refinedstorage.item.ItemWirelessGrid;
 | 
					import refinedstorage.item.ItemWirelessGrid;
 | 
				
			||||||
@@ -12,10 +13,10 @@ import refinedstorage.network.GridPullFlags;
 | 
				
			|||||||
public class StorageHandler {
 | 
					public class StorageHandler {
 | 
				
			||||||
    public static final int MAX_CRAFTING_PER_REQUEST = 500;
 | 
					    public static final int MAX_CRAFTING_PER_REQUEST = 500;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private TileController controller;
 | 
					    private StorageNetwork network;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public StorageHandler(TileController controller) {
 | 
					    public StorageHandler(StorageNetwork network) {
 | 
				
			||||||
        this.controller = controller;
 | 
					        this.network = network;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void onPull(int id, int flags, EntityPlayerMP player) {
 | 
					    public void onPull(int id, int flags, EntityPlayerMP player) {
 | 
				
			||||||
@@ -23,11 +24,11 @@ public class StorageHandler {
 | 
				
			|||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (id < 0 || id > controller.getItems().size() - 1) {
 | 
					        if (id < 0 || id > network.getItems().size() - 1) {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ItemStack stack = controller.getItems().get(id);
 | 
					        ItemStack stack = network.getItems().get(id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        int size = 64;
 | 
					        int size = 64;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -45,7 +46,7 @@ public class StorageHandler {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        size = Math.min(size, stack.getItem().getItemStackLimit(stack));
 | 
					        size = Math.min(size, stack.getItem().getItemStackLimit(stack));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ItemStack took = controller.take(stack, size);
 | 
					        ItemStack took = network.take(stack, size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (took != null) {
 | 
					        if (took != null) {
 | 
				
			||||||
            if (GridPullFlags.isPullingWithShift(flags)) {
 | 
					            if (GridPullFlags.isPullingWithShift(flags)) {
 | 
				
			||||||
@@ -57,7 +58,7 @@ public class StorageHandler {
 | 
				
			|||||||
                player.updateHeldItem();
 | 
					                player.updateHeldItem();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            controller.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PULL);
 | 
					            network.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PULL);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -70,8 +71,8 @@ public class StorageHandler {
 | 
				
			|||||||
        int size = one ? 1 : stack.stackSize;
 | 
					        int size = one ? 1 : stack.stackSize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (one) {
 | 
					        if (one) {
 | 
				
			||||||
            if (controller.push(stack, size, true) == null) {
 | 
					            if (network.push(stack, size, true) == null) {
 | 
				
			||||||
                controller.push(stack, size, false);
 | 
					                network.push(stack, size, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                stack.stackSize -= size;
 | 
					                stack.stackSize -= size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -80,21 +81,21 @@ public class StorageHandler {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            player.inventory.setItemStack(controller.push(stack, size, false));
 | 
					            player.inventory.setItemStack(network.push(stack, size, false));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        player.updateHeldItem();
 | 
					        player.updateHeldItem();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        controller.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH);
 | 
					        network.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void onCraftingRequested(int id, int quantity) {
 | 
					    public void onCraftingRequested(int id, int quantity) {
 | 
				
			||||||
        if (id >= 0 && id < controller.getItems().size() && quantity > 0 && quantity <= MAX_CRAFTING_PER_REQUEST) {
 | 
					        if (id >= 0 && id < network.getItems().size() && quantity > 0 && quantity <= MAX_CRAFTING_PER_REQUEST) {
 | 
				
			||||||
            ItemStack requested = controller.getItems().get(id);
 | 
					            ItemStack requested = network.getItems().get(id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            int quantityPerRequest = 0;
 | 
					            int quantityPerRequest = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            CraftingPattern pattern = controller.getPatternWithBestScore(requested);
 | 
					            CraftingPattern pattern = network.getPatternWithBestScore(requested);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (pattern != null) {
 | 
					            if (pattern != null) {
 | 
				
			||||||
                for (ItemStack output : pattern.getOutputs()) {
 | 
					                for (ItemStack output : pattern.getOutputs()) {
 | 
				
			||||||
@@ -108,7 +109,7 @@ public class StorageHandler {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                while (quantity > 0) {
 | 
					                while (quantity > 0) {
 | 
				
			||||||
                    controller.addCraftingTaskAsLast(controller.createCraftingTask(pattern));
 | 
					                    network.addCraftingTaskAsLast(network.createCraftingTask(pattern));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    quantity -= quantityPerRequest;
 | 
					                    quantity -= quantityPerRequest;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
@@ -117,11 +118,11 @@ public class StorageHandler {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void onCraftingCancelRequested(int id) {
 | 
					    public void onCraftingCancelRequested(int id) {
 | 
				
			||||||
        if (id >= 0 && id < controller.getCraftingTasks().size()) {
 | 
					        if (id >= 0 && id < network.getCraftingTasks().size()) {
 | 
				
			||||||
            controller.cancelCraftingTask(controller.getCraftingTasks().get(id));
 | 
					            network.cancelCraftingTask(network.getCraftingTasks().get(id));
 | 
				
			||||||
        } else if (id == -1) {
 | 
					        } else if (id == -1) {
 | 
				
			||||||
            for (ICraftingTask task : controller.getCraftingTasks()) {
 | 
					            for (ICraftingTask task : network.getCraftingTasks()) {
 | 
				
			||||||
                controller.cancelCraftingTask(task);
 | 
					                network.cancelCraftingTask(task);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,170 +1,111 @@
 | 
				
			|||||||
package refinedstorage.tile.controller;
 | 
					package refinedstorage.tile.controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cofh.api.energy.EnergyStorage;
 | 
					 | 
				
			||||||
import cofh.api.energy.IEnergyReceiver;
 | 
					import cofh.api.energy.IEnergyReceiver;
 | 
				
			||||||
import io.netty.buffer.ByteBuf;
 | 
					import io.netty.buffer.ByteBuf;
 | 
				
			||||||
import net.minecraft.block.state.IBlockState;
 | 
					import net.minecraft.block.state.IBlockState;
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayer;
 | 
					 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
					 | 
				
			||||||
import net.minecraft.inventory.Container;
 | 
					import net.minecraft.inventory.Container;
 | 
				
			||||||
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.util.EnumFacing;
 | 
					import net.minecraft.util.EnumFacing;
 | 
				
			||||||
import net.minecraftforge.common.util.Constants;
 | 
					 | 
				
			||||||
import net.minecraftforge.fml.common.network.ByteBufUtils;
 | 
					import net.minecraftforge.fml.common.network.ByteBufUtils;
 | 
				
			||||||
import net.minecraftforge.items.ItemHandlerHelper;
 | 
					 | 
				
			||||||
import refinedstorage.RefinedStorage;
 | 
					 | 
				
			||||||
import refinedstorage.RefinedStorageBlocks;
 | 
					import refinedstorage.RefinedStorageBlocks;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.api.RefinedStorageCapabilities;
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
import refinedstorage.api.storage.CompareFlags;
 | 
					 | 
				
			||||||
import refinedstorage.api.storage.IStorage;
 | 
					 | 
				
			||||||
import refinedstorage.autocrafting.CraftingPattern;
 | 
					 | 
				
			||||||
import refinedstorage.autocrafting.task.BasicCraftingTask;
 | 
					 | 
				
			||||||
import refinedstorage.autocrafting.task.ICraftingTask;
 | 
					 | 
				
			||||||
import refinedstorage.autocrafting.task.ProcessingCraftingTask;
 | 
					 | 
				
			||||||
import refinedstorage.block.BlockController;
 | 
					import refinedstorage.block.BlockController;
 | 
				
			||||||
import refinedstorage.block.EnumControllerType;
 | 
					import refinedstorage.block.EnumControllerType;
 | 
				
			||||||
import refinedstorage.container.ContainerController;
 | 
					import refinedstorage.container.ContainerController;
 | 
				
			||||||
import refinedstorage.container.ContainerGrid;
 | 
					import refinedstorage.tile.ISynchronizedContainer;
 | 
				
			||||||
import refinedstorage.item.ItemPattern;
 | 
					import refinedstorage.tile.TileBase;
 | 
				
			||||||
import refinedstorage.network.MessageGridItems;
 | 
					import refinedstorage.tile.TileMachine;
 | 
				
			||||||
import refinedstorage.tile.*;
 | 
					 | 
				
			||||||
import refinedstorage.tile.config.IRedstoneModeConfig;
 | 
					import refinedstorage.tile.config.IRedstoneModeConfig;
 | 
				
			||||||
import refinedstorage.tile.config.RedstoneMode;
 | 
					import refinedstorage.tile.config.RedstoneMode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class TileController extends TileBase implements IEnergyReceiver, ISynchronizedContainer, IRedstoneModeConfig {
 | 
					public class TileController extends TileBase implements IEnergyReceiver, ISynchronizedContainer, IRedstoneModeConfig {
 | 
				
			||||||
    public static final int ENERGY_CAPACITY = 32000;
 | 
					    private StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(pos);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static final String NBT_CRAFTING_TASKS = "CraftingTasks";
 | 
					 | 
				
			||||||
    public static final String NBT_ENERGY = "Energy";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private EnumControllerType type;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private StorageHandler storageHandler = new StorageHandler(this);
 | 
					 | 
				
			||||||
    private WirelessGridHandler wirelessGridHandler = new WirelessGridHandler(this);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private List<ItemStack> items = new ArrayList<ItemStack>();
 | 
					 | 
				
			||||||
    private List<ItemStack> combinedItems = new ArrayList<ItemStack>();
 | 
					 | 
				
			||||||
    private Set<Integer> combinedItemsIndices = new HashSet<Integer>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private List<IStorage> storages = new ArrayList<IStorage>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private RedstoneMode redstoneMode = RedstoneMode.IGNORE;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private List<TileMachine> machines = new ArrayList<TileMachine>();
 | 
					 | 
				
			||||||
    private List<TileMachine> machinesToAdd = new ArrayList<TileMachine>();
 | 
					 | 
				
			||||||
    private List<TileMachine> machinesToRemove = new ArrayList<TileMachine>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private List<ClientMachine> clientMachines = new ArrayList<ClientMachine>();
 | 
					    private List<ClientMachine> clientMachines = new ArrayList<ClientMachine>();
 | 
				
			||||||
 | 
					    private int energy;
 | 
				
			||||||
    private List<CraftingPattern> patterns = new ArrayList<CraftingPattern>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private Stack<ICraftingTask> craftingTasks = new Stack<ICraftingTask>();
 | 
					 | 
				
			||||||
    private List<ICraftingTask> craftingTasksToAddAsLast = new ArrayList<ICraftingTask>();
 | 
					 | 
				
			||||||
    private List<ICraftingTask> craftingTasksToAdd = new ArrayList<ICraftingTask>();
 | 
					 | 
				
			||||||
    private List<ICraftingTask> craftingTasksToCancel = new ArrayList<ICraftingTask>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private EnergyStorage energy = new EnergyStorage(ENERGY_CAPACITY);
 | 
					 | 
				
			||||||
    private int energyUsage;
 | 
					    private int energyUsage;
 | 
				
			||||||
 | 
					    private EnumControllerType type;
 | 
				
			||||||
    private int wirelessGridRange;
 | 
					    private RedstoneMode redstoneMode;
 | 
				
			||||||
    private boolean couldRun;
 | 
					 | 
				
			||||||
    private long lastEnergyUpdate;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void update() {
 | 
					    public NBTTagCompound write(NBTTagCompound tag) {
 | 
				
			||||||
        super.update();
 | 
					        super.write(tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!worldObj.isRemote) {
 | 
					        return network.write(tag);
 | 
				
			||||||
            for (TileMachine machine : machinesToAdd) {
 | 
					 | 
				
			||||||
                if (!machines.contains(machine)) {
 | 
					 | 
				
			||||||
                    machines.add(machine);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            machinesToAdd.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            machines.removeAll(machinesToRemove);
 | 
					 | 
				
			||||||
            machinesToRemove.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            int lastEnergy = energy.getEnergyStored();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (canRun()) {
 | 
					 | 
				
			||||||
                if (ticks % 20 == 0) {
 | 
					 | 
				
			||||||
                    syncMachines();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                for (ICraftingTask taskToCancel : craftingTasksToCancel) {
 | 
					 | 
				
			||||||
                    taskToCancel.onCancelled(this);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                craftingTasks.removeAll(craftingTasksToCancel);
 | 
					 | 
				
			||||||
                craftingTasksToCancel.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                for (ICraftingTask task : craftingTasksToAdd) {
 | 
					 | 
				
			||||||
                    craftingTasks.push(task);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                craftingTasksToAdd.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                for (ICraftingTask task : craftingTasksToAddAsLast) {
 | 
					 | 
				
			||||||
                    craftingTasks.add(0, task);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                craftingTasksToAddAsLast.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (!craftingTasks.empty()) {
 | 
					 | 
				
			||||||
                    ICraftingTask top = craftingTasks.peek();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    if (ticks % top.getPattern().getCrafter(worldObj).getSpeed() == 0 && top.update(this)) {
 | 
					 | 
				
			||||||
                        top.onDone(this);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                        craftingTasks.pop();
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            } else if (!machines.isEmpty()) {
 | 
					 | 
				
			||||||
                // Machine list should NOT be empty to trigger a disconnect
 | 
					 | 
				
			||||||
                // We need to sync machines again to reset energy usage etc
 | 
					 | 
				
			||||||
                disconnectAll();
 | 
					 | 
				
			||||||
                syncMachines();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (couldRun != canRun()) {
 | 
					 | 
				
			||||||
                couldRun = canRun();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                worldObj.notifyNeighborsOfStateChange(pos, RefinedStorageBlocks.CONTROLLER);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            wirelessGridHandler.update();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (getType() == EnumControllerType.NORMAL && energyUsage > 0) {
 | 
					 | 
				
			||||||
                if (energy.getEnergyStored() - energyUsage >= 0) {
 | 
					 | 
				
			||||||
                    energy.extractEnergy(energyUsage, false);
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    energy.setEnergyStored(0);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            } else if (getType() == EnumControllerType.CREATIVE) {
 | 
					 | 
				
			||||||
                energy.setEnergyStored(energy.getMaxEnergyStored());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (energy.getEnergyStored() != lastEnergy) {
 | 
					 | 
				
			||||||
                worldObj.updateComparatorOutputLevel(pos, RefinedStorageBlocks.CONTROLLER);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (System.currentTimeMillis() - lastEnergyUpdate > 5000) {
 | 
					 | 
				
			||||||
                    lastEnergyUpdate = System.currentTimeMillis();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    RefinedStorageUtils.updateBlock(worldObj, pos);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void addMachine(TileMachine machine) {
 | 
					    @Override
 | 
				
			||||||
        machinesToAdd.add(machine);
 | 
					    public void read(NBTTagCompound tag) {
 | 
				
			||||||
 | 
					        super.read(tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        network.read(tag);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void removeMachine(TileMachine machine) {
 | 
					    @Override
 | 
				
			||||||
        machinesToRemove.add(machine);
 | 
					    public NBTTagCompound writeUpdate(NBTTagCompound tag) {
 | 
				
			||||||
 | 
					        super.writeUpdate(tag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        tag.setInteger(StorageNetwork.NBT_ENERGY, network.getEnergy().getEnergyStored());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return tag;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void readUpdate(NBTTagCompound tag) {
 | 
				
			||||||
 | 
					        energy = tag.getInteger(StorageNetwork.NBT_ENERGY);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        super.readUpdate(tag);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
 | 
				
			||||||
 | 
					        return network.getEnergy().receiveEnergy(maxReceive, simulate);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public int getEnergyStored(EnumFacing from) {
 | 
				
			||||||
 | 
					        return network.getEnergy().getEnergyStored();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public int getEnergyScaled(int i) {
 | 
				
			||||||
 | 
					        return (int) ((float) network.getEnergy().getEnergyStored() / (float) network.getEnergy().getMaxEnergyStored() * (float) i);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public int getMaxEnergyStored(EnumFacing from) {
 | 
				
			||||||
 | 
					        return network.getEnergy().getMaxEnergyStored();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public boolean canConnectEnergy(EnumFacing from) {
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public RedstoneMode getRedstoneMode() {
 | 
				
			||||||
 | 
					        return network.getRedstoneMode();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void setRedstoneMode(RedstoneMode mode) {
 | 
				
			||||||
 | 
					        network.setRedstoneMode(mode);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<ClientMachine> getClientMachines() {
 | 
				
			||||||
 | 
					        return clientMachines;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public int getEnergy() {
 | 
				
			||||||
 | 
					        return energy;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public int getEnergyUsage() {
 | 
				
			||||||
 | 
					        return energyUsage;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public EnumControllerType getType() {
 | 
					    public EnumControllerType getType() {
 | 
				
			||||||
@@ -175,458 +116,12 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
 | 
				
			|||||||
        return type == null ? EnumControllerType.NORMAL : type;
 | 
					        return type == null ? EnumControllerType.NORMAL : type;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public StorageHandler getStorageHandler() {
 | 
					 | 
				
			||||||
        return storageHandler;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public WirelessGridHandler getWirelessGridHandler() {
 | 
					 | 
				
			||||||
        return wirelessGridHandler;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public int getWirelessGridRange() {
 | 
					 | 
				
			||||||
        return wirelessGridRange;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void disconnectAll() {
 | 
					 | 
				
			||||||
        for (TileMachine machine : machines) {
 | 
					 | 
				
			||||||
            machine.onDisconnected(worldObj);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        machines.clear();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<ItemStack> getItems() {
 | 
					 | 
				
			||||||
        return items;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<ICraftingTask> getCraftingTasks() {
 | 
					 | 
				
			||||||
        return craftingTasks;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void addCraftingTask(ICraftingTask task) {
 | 
					 | 
				
			||||||
        craftingTasksToAdd.add(task);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        markDirty();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void addCraftingTaskAsLast(ICraftingTask task) {
 | 
					 | 
				
			||||||
        craftingTasksToAddAsLast.add(task);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        markDirty();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public ICraftingTask createCraftingTask(CraftingPattern pattern) {
 | 
					 | 
				
			||||||
        if (pattern.isProcessing()) {
 | 
					 | 
				
			||||||
            return new ProcessingCraftingTask(pattern);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            return new BasicCraftingTask(pattern);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void cancelCraftingTask(ICraftingTask task) {
 | 
					 | 
				
			||||||
        craftingTasksToCancel.add(task);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        markDirty();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<CraftingPattern> getPatterns() {
 | 
					 | 
				
			||||||
        return patterns;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<CraftingPattern> getPattern(ItemStack pattern) {
 | 
					 | 
				
			||||||
        return getPattern(pattern, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<CraftingPattern> getPattern(ItemStack pattern, int flags) {
 | 
					 | 
				
			||||||
        List<CraftingPattern> patterns = new ArrayList<CraftingPattern>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (CraftingPattern craftingPattern : getPatterns()) {
 | 
					 | 
				
			||||||
            for (ItemStack output : craftingPattern.getOutputs()) {
 | 
					 | 
				
			||||||
                if (RefinedStorageUtils.compareStack(output, pattern, flags)) {
 | 
					 | 
				
			||||||
                    patterns.add(craftingPattern);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return patterns;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public CraftingPattern getPatternWithBestScore(ItemStack pattern) {
 | 
					 | 
				
			||||||
        return getPatternWithBestScore(pattern, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public CraftingPattern getPatternWithBestScore(ItemStack pattern, int flags) {
 | 
					 | 
				
			||||||
        List<CraftingPattern> patterns = getPattern(pattern, flags);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (patterns.isEmpty()) {
 | 
					 | 
				
			||||||
            return null;
 | 
					 | 
				
			||||||
        } else if (patterns.size() == 1) {
 | 
					 | 
				
			||||||
            return patterns.get(0);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        int highestScore = 0;
 | 
					 | 
				
			||||||
        int highestPattern = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (int i = 0; i < patterns.size(); ++i) {
 | 
					 | 
				
			||||||
            int score = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for (ItemStack input : patterns.get(i).getInputs()) {
 | 
					 | 
				
			||||||
                ItemStack stored = getItem(input, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                score += stored != null ? stored.stackSize : 0;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (score > highestScore) {
 | 
					 | 
				
			||||||
                highestScore = score;
 | 
					 | 
				
			||||||
                highestPattern = i;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return patterns.get(highestPattern);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void syncMachines() {
 | 
					 | 
				
			||||||
        this.wirelessGridRange = 0;
 | 
					 | 
				
			||||||
        this.energyUsage = 0;
 | 
					 | 
				
			||||||
        this.storages.clear();
 | 
					 | 
				
			||||||
        this.patterns.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (TileMachine machine : machines) {
 | 
					 | 
				
			||||||
            if (!machine.canUpdate()) {
 | 
					 | 
				
			||||||
                continue;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (machine instanceof TileWirelessTransmitter) {
 | 
					 | 
				
			||||||
                this.wirelessGridRange += ((TileWirelessTransmitter) machine).getRange();
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (machine.hasCapability(RefinedStorageCapabilities.STORAGE_PROVIDER_CAPABILITY, null)) {
 | 
					 | 
				
			||||||
                machine.getCapability(RefinedStorageCapabilities.STORAGE_PROVIDER_CAPABILITY, null).provide(storages);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (machine instanceof TileCrafter) {
 | 
					 | 
				
			||||||
                TileCrafter crafter = (TileCrafter) machine;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                for (int i = 0; i < crafter.getPatterns().getSlots(); ++i) {
 | 
					 | 
				
			||||||
                    ItemStack pattern = crafter.getPatterns().getStackInSlot(i);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    if (pattern != null && ItemPattern.isValid(pattern)) {
 | 
					 | 
				
			||||||
                        patterns.add(new CraftingPattern(crafter.getPos().getX(), crafter.getPos().getY(), crafter.getPos().getZ(), ItemPattern.isProcessing(pattern), ItemPattern.getInputs(pattern), ItemPattern.getOutputs(pattern), ItemPattern.getByproducts(pattern)));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            this.energyUsage += machine.getEnergyUsage();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Collections.sort(storages, new Comparator<IStorage>() {
 | 
					 | 
				
			||||||
            @Override
 | 
					 | 
				
			||||||
            public int compare(IStorage left, IStorage right) {
 | 
					 | 
				
			||||||
                int leftStored = left.getStored();
 | 
					 | 
				
			||||||
                int rightStored = right.getStored();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (leftStored == rightStored) {
 | 
					 | 
				
			||||||
                    return 0;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return (leftStored > rightStored) ? -1 : 1;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Collections.sort(storages, new Comparator<IStorage>() {
 | 
					 | 
				
			||||||
            @Override
 | 
					 | 
				
			||||||
            public int compare(IStorage left, IStorage right) {
 | 
					 | 
				
			||||||
                if (left.getPriority() == right.getPriority()) {
 | 
					 | 
				
			||||||
                    return 0;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return (left.getPriority() > right.getPriority()) ? -1 : 1;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        syncItems();
 | 
					 | 
				
			||||||
        syncItemsWithClients();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void syncItems() {
 | 
					 | 
				
			||||||
        items.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (IStorage storage : storages) {
 | 
					 | 
				
			||||||
            storage.addItems(items);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (CraftingPattern pattern : patterns) {
 | 
					 | 
				
			||||||
            for (ItemStack output : pattern.getOutputs()) {
 | 
					 | 
				
			||||||
                ItemStack patternStack = output.copy();
 | 
					 | 
				
			||||||
                patternStack.stackSize = 0;
 | 
					 | 
				
			||||||
                items.add(patternStack);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        combinedItems.clear();
 | 
					 | 
				
			||||||
        combinedItemsIndices.clear();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (int i = 0; i < items.size(); ++i) {
 | 
					 | 
				
			||||||
            if (combinedItemsIndices.contains(i)) {
 | 
					 | 
				
			||||||
                continue;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            ItemStack stack = items.get(i);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for (int j = i + 1; j < items.size(); ++j) {
 | 
					 | 
				
			||||||
                if (combinedItemsIndices.contains(j)) {
 | 
					 | 
				
			||||||
                    continue;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                ItemStack otherStack = items.get(j);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (RefinedStorageUtils.compareStackNoQuantity(stack, otherStack)) {
 | 
					 | 
				
			||||||
                    // We copy here so we don't modify the quantity of the ItemStack IStorage uses.
 | 
					 | 
				
			||||||
                    // We re-get the ItemStack because the stack may change from a previous iteration in this loop
 | 
					 | 
				
			||||||
                    ItemStack newStack = items.get(i).copy();
 | 
					 | 
				
			||||||
                    newStack.stackSize += otherStack.stackSize;
 | 
					 | 
				
			||||||
                    items.set(i, newStack);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    combinedItems.add(otherStack);
 | 
					 | 
				
			||||||
                    combinedItemsIndices.add(j);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        items.removeAll(combinedItems);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void syncItemsWithClients() {
 | 
					 | 
				
			||||||
        for (EntityPlayer player : worldObj.playerEntities) {
 | 
					 | 
				
			||||||
            if (player.openContainer.getClass() == ContainerGrid.class && pos.equals(((ContainerGrid) player.openContainer).getGrid().getControllerPos())) {
 | 
					 | 
				
			||||||
                syncItemsWithClient((EntityPlayerMP) player);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void syncItemsWithClient(EntityPlayerMP player) {
 | 
					 | 
				
			||||||
        RefinedStorage.NETWORK.sendTo(new MessageGridItems(this), player);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public ItemStack push(ItemStack stack, int size, boolean simulate) {
 | 
					 | 
				
			||||||
        if (stack == null || stack.getItem() == null) {
 | 
					 | 
				
			||||||
            return null;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (storages.isEmpty()) {
 | 
					 | 
				
			||||||
            return ItemHandlerHelper.copyStackWithSize(stack, size);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        int orginalSize = size;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        ItemStack remainder = stack;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (IStorage storage : storages) {
 | 
					 | 
				
			||||||
            remainder = storage.push(remainder, size, simulate);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (remainder == null) {
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                size = remainder.stackSize;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!simulate) {
 | 
					 | 
				
			||||||
            syncItems();
 | 
					 | 
				
			||||||
            syncItemsWithClients();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            int sizePushed = remainder != null ? (orginalSize - remainder.stackSize) : orginalSize;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for (int i = 0; i < sizePushed; ++i) {
 | 
					 | 
				
			||||||
                if (!craftingTasks.empty()) {
 | 
					 | 
				
			||||||
                    ICraftingTask top = craftingTasks.peek();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    if (top instanceof ProcessingCraftingTask) {
 | 
					 | 
				
			||||||
                        ((ProcessingCraftingTask) top).onPushed(stack);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return remainder;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public ItemStack take(ItemStack stack, int size) {
 | 
					 | 
				
			||||||
        return take(stack, size, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public ItemStack take(ItemStack stack, int size, int flags) {
 | 
					 | 
				
			||||||
        int requested = size;
 | 
					 | 
				
			||||||
        int received = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        ItemStack newStack = null;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (IStorage storage : storages) {
 | 
					 | 
				
			||||||
            ItemStack took = storage.take(stack, requested - received, flags);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (took != null) {
 | 
					 | 
				
			||||||
                if (newStack == null) {
 | 
					 | 
				
			||||||
                    newStack = took;
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    newStack.stackSize += took.stackSize;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                received += took.stackSize;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (requested == received) {
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (newStack != null) {
 | 
					 | 
				
			||||||
            syncItems();
 | 
					 | 
				
			||||||
            syncItemsWithClients();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return newStack;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public ItemStack getItem(ItemStack stack, int flags) {
 | 
					 | 
				
			||||||
        for (ItemStack otherStack : items) {
 | 
					 | 
				
			||||||
            if (RefinedStorageUtils.compareStack(otherStack, stack, flags)) {
 | 
					 | 
				
			||||||
                return otherStack;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return null;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void setEnergyStored(int amount) {
 | 
					 | 
				
			||||||
        energy.setEnergyStored(amount);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public NBTTagCompound write(NBTTagCompound tag) {
 | 
					 | 
				
			||||||
        super.write(tag);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        energy.writeToNBT(tag);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        tag.setInteger(RedstoneMode.NBT, redstoneMode.id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        NBTTagList list = new NBTTagList();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (ICraftingTask task : craftingTasks) {
 | 
					 | 
				
			||||||
            NBTTagCompound taskTag = new NBTTagCompound();
 | 
					 | 
				
			||||||
            task.writeToNBT(taskTag);
 | 
					 | 
				
			||||||
            list.appendTag(taskTag);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        tag.setTag(NBT_CRAFTING_TASKS, list);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return tag;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public void read(NBTTagCompound nbt) {
 | 
					 | 
				
			||||||
        super.read(nbt);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        energy.readFromNBT(nbt);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (nbt.hasKey(RedstoneMode.NBT)) {
 | 
					 | 
				
			||||||
            redstoneMode = RedstoneMode.getById(nbt.getInteger(RedstoneMode.NBT));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (nbt.hasKey(NBT_CRAFTING_TASKS)) {
 | 
					 | 
				
			||||||
            NBTTagList taskList = nbt.getTagList(NBT_CRAFTING_TASKS, Constants.NBT.TAG_COMPOUND);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for (int i = 0; i < taskList.tagCount(); ++i) {
 | 
					 | 
				
			||||||
                NBTTagCompound taskTag = taskList.getCompoundTagAt(i);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                CraftingPattern pattern = CraftingPattern.readFromNBT(taskTag.getCompoundTag(CraftingPattern.NBT));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (pattern != null) {
 | 
					 | 
				
			||||||
                    switch (taskTag.getInteger("Type")) {
 | 
					 | 
				
			||||||
                        case BasicCraftingTask.ID:
 | 
					 | 
				
			||||||
                            addCraftingTask(new BasicCraftingTask(taskTag, pattern));
 | 
					 | 
				
			||||||
                            break;
 | 
					 | 
				
			||||||
                        case ProcessingCraftingTask.ID:
 | 
					 | 
				
			||||||
                            addCraftingTask(new ProcessingCraftingTask(taskTag, pattern));
 | 
					 | 
				
			||||||
                            break;
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public NBTTagCompound writeUpdate(NBTTagCompound tag) {
 | 
					 | 
				
			||||||
        super.writeUpdate(tag);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        tag.setInteger(NBT_ENERGY, getEnergyStored(null));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return tag;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public void readUpdate(NBTTagCompound tag) {
 | 
					 | 
				
			||||||
        setEnergyStored(tag.getInteger(NBT_ENERGY));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        super.readUpdate(tag);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
 | 
					 | 
				
			||||||
        return energy.receiveEnergy(maxReceive, simulate);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public int getEnergyStored(EnumFacing from) {
 | 
					 | 
				
			||||||
        return energy.getEnergyStored();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public int getEnergyScaled(int i) {
 | 
					 | 
				
			||||||
        return (int) ((float) energy.getEnergyStored() / (float) energy.getMaxEnergyStored() * (float) i);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public int getMaxEnergyStored(EnumFacing from) {
 | 
					 | 
				
			||||||
        return energy.getMaxEnergyStored();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public int getEnergyUsage() {
 | 
					 | 
				
			||||||
        return energyUsage;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public boolean canConnectEnergy(EnumFacing from) {
 | 
					 | 
				
			||||||
        return true;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public boolean canRun() {
 | 
					 | 
				
			||||||
        return energy.getEnergyStored() > 0 && energy.getEnergyStored() >= energyUsage && redstoneMode.isEnabled(worldObj, pos);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public RedstoneMode getRedstoneMode() {
 | 
					 | 
				
			||||||
        return redstoneMode;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					 | 
				
			||||||
    public void setRedstoneMode(RedstoneMode mode) {
 | 
					 | 
				
			||||||
        this.redstoneMode = mode;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        markDirty();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public List<ClientMachine> getClientMachines() {
 | 
					 | 
				
			||||||
        return clientMachines;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void readContainerData(ByteBuf buf) {
 | 
					    public void readContainerData(ByteBuf buf) {
 | 
				
			||||||
        setEnergyStored(buf.readInt());
 | 
					        this.energy = buf.readInt();
 | 
				
			||||||
        energyUsage = buf.readInt();
 | 
					        this.energyUsage = buf.readInt();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        redstoneMode = RedstoneMode.getById(buf.readInt());
 | 
					        this.redstoneMode = RedstoneMode.getById(buf.readInt());
 | 
				
			||||||
 | 
					 | 
				
			||||||
        machines.clear();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        List<ClientMachine> machines = new ArrayList<ClientMachine>();
 | 
					        List<ClientMachine> machines = new ArrayList<ClientMachine>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -641,7 +136,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
 | 
				
			|||||||
            machines.add(machine);
 | 
					            machines.add(machine);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        clientMachines = machines;
 | 
					        this.clientMachines = machines;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
@@ -653,7 +148,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        List<ClientMachine> m = new ArrayList<ClientMachine>();
 | 
					        List<ClientMachine> m = new ArrayList<ClientMachine>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (TileMachine machine : machines) {
 | 
					        for (TileMachine machine : network.getMachines()) {
 | 
				
			||||||
            if (machine.canUpdate()) {
 | 
					            if (machine.canUpdate()) {
 | 
				
			||||||
                IBlockState state = worldObj.getBlockState(machine.getPos());
 | 
					                IBlockState state = worldObj.getBlockState(machine.getPos());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,7 @@ import refinedstorage.RefinedStorage;
 | 
				
			|||||||
import refinedstorage.RefinedStorageGui;
 | 
					import refinedstorage.RefinedStorageGui;
 | 
				
			||||||
import refinedstorage.RefinedStorageItems;
 | 
					import refinedstorage.RefinedStorageItems;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
import refinedstorage.item.ItemWirelessGrid;
 | 
					import refinedstorage.item.ItemWirelessGrid;
 | 
				
			||||||
import refinedstorage.tile.grid.WirelessGridConsumer;
 | 
					import refinedstorage.tile.grid.WirelessGridConsumer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -16,13 +17,13 @@ import java.util.Iterator;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class WirelessGridHandler {
 | 
					public class WirelessGridHandler {
 | 
				
			||||||
    private TileController controller;
 | 
					    private StorageNetwork network;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private List<WirelessGridConsumer> consumers = new ArrayList<WirelessGridConsumer>();
 | 
					    private List<WirelessGridConsumer> consumers = new ArrayList<WirelessGridConsumer>();
 | 
				
			||||||
    private List<WirelessGridConsumer> consumersToRemove = new ArrayList<WirelessGridConsumer>();
 | 
					    private List<WirelessGridConsumer> consumersToRemove = new ArrayList<WirelessGridConsumer>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public WirelessGridHandler(TileController controller) {
 | 
					    public WirelessGridHandler(StorageNetwork network) {
 | 
				
			||||||
        this.controller = controller;
 | 
					        this.network = network;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void update() {
 | 
					    public void update() {
 | 
				
			||||||
@@ -41,17 +42,17 @@ public class WirelessGridHandler {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public boolean handleOpen(EntityPlayer player, EnumHand hand) {
 | 
					    public boolean handleOpen(EntityPlayer player, EnumHand hand) {
 | 
				
			||||||
        int distance = (int) Math.sqrt(Math.pow(controller.getPos().getX() - player.posX, 2) + Math.pow(controller.getPos().getY() - player.posY, 2) + Math.pow(controller.getPos().getZ() - player.posZ, 2));
 | 
					        int distance = (int) Math.sqrt(Math.pow(network.getPos().getX() - player.posX, 2) + Math.pow(network.getPos().getY() - player.posY, 2) + Math.pow(network.getPos().getZ() - player.posZ, 2));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (distance > controller.getWirelessGridRange()) {
 | 
					        if (distance > network.getWirelessGridRange()) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        consumers.add(new WirelessGridConsumer(player, hand, player.getHeldItem(hand)));
 | 
					        consumers.add(new WirelessGridConsumer(player, hand, player.getHeldItem(hand)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        player.openGui(RefinedStorage.INSTANCE, RefinedStorageGui.WIRELESS_GRID, controller.getWorld(), RefinedStorageUtils.getIdFromHand(hand), 0, 0);
 | 
					        player.openGui(RefinedStorage.INSTANCE, RefinedStorageGui.WIRELESS_GRID, player.worldObj, RefinedStorageUtils.getIdFromHand(hand), 0, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        controller.syncItemsWithClient((EntityPlayerMP) player);
 | 
					        network.syncItemsWithClient((EntityPlayerMP) player);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        drainEnergy(player, ItemWirelessGrid.USAGE_OPEN);
 | 
					        drainEnergy(player, ItemWirelessGrid.USAGE_OPEN);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -109,18 +109,18 @@ public class TileGrid extends TileMachine implements IGrid {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public BlockPos getControllerPos() {
 | 
					    public BlockPos getControllerPos() {
 | 
				
			||||||
        return controller != null ? controller.getPos() : null;
 | 
					        return network != null ? network.getPos() : null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void onGridOpened(EntityPlayer player) {
 | 
					    public void onGridOpened(EntityPlayer player) {
 | 
				
			||||||
        if (isConnected()) {
 | 
					        if (isConnected()) {
 | 
				
			||||||
            controller.syncItemsWithClient((EntityPlayerMP) player);
 | 
					            network.syncItemsWithClient((EntityPlayerMP) player);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public ItemStack onItemPush(EntityPlayer player, ItemStack stack) {
 | 
					    public ItemStack onItemPush(EntityPlayer player, ItemStack stack) {
 | 
				
			||||||
        return isConnected() ? controller.push(stack, stack.stackSize, false) : stack;
 | 
					        return isConnected() ? network.push(stack, stack.stackSize, false) : stack;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
@@ -162,7 +162,7 @@ public class TileGrid extends TileMachine implements IGrid {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                if (slot != null) {
 | 
					                if (slot != null) {
 | 
				
			||||||
                    if (slot.stackSize == 1 && isConnected()) {
 | 
					                    if (slot.stackSize == 1 && isConnected()) {
 | 
				
			||||||
                        matrix.setInventorySlotContents(i, controller.take(slot, 1));
 | 
					                        matrix.setInventorySlotContents(i, network.take(slot, 1));
 | 
				
			||||||
                    } else {
 | 
					                    } else {
 | 
				
			||||||
                        matrix.decrStackSize(i, 1);
 | 
					                        matrix.decrStackSize(i, 1);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
@@ -239,10 +239,10 @@ public class TileGrid extends TileMachine implements IGrid {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                if (slot != null) {
 | 
					                if (slot != null) {
 | 
				
			||||||
                    if (getType() == EnumGridType.CRAFTING) {
 | 
					                    if (getType() == EnumGridType.CRAFTING) {
 | 
				
			||||||
                        if (controller.push(slot, slot.stackSize, true) != null) {
 | 
					                        if (network.push(slot, slot.stackSize, true) != null) {
 | 
				
			||||||
                            return;
 | 
					                            return;
 | 
				
			||||||
                        } else {
 | 
					                        } else {
 | 
				
			||||||
                            controller.push(slot, slot.stackSize, false);
 | 
					                            network.push(slot, slot.stackSize, false);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -256,7 +256,7 @@ public class TileGrid extends TileMachine implements IGrid {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                    if (getType() == EnumGridType.CRAFTING) {
 | 
					                    if (getType() == EnumGridType.CRAFTING) {
 | 
				
			||||||
                        for (ItemStack possibility : possibilities) {
 | 
					                        for (ItemStack possibility : possibilities) {
 | 
				
			||||||
                            ItemStack took = controller.take(possibility, 1);
 | 
					                            ItemStack took = network.take(possibility, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            if (took != null) {
 | 
					                            if (took != null) {
 | 
				
			||||||
                                matrix.setInventorySlotContents(i, possibility);
 | 
					                                matrix.setInventorySlotContents(i, possibility);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,12 +2,13 @@ package refinedstorage.tile.grid;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import net.minecraft.entity.player.EntityPlayer;
 | 
					import net.minecraft.entity.player.EntityPlayer;
 | 
				
			||||||
import net.minecraft.item.ItemStack;
 | 
					import net.minecraft.item.ItemStack;
 | 
				
			||||||
import net.minecraft.tileentity.TileEntity;
 | 
					 | 
				
			||||||
import net.minecraft.util.EnumHand;
 | 
					import net.minecraft.util.EnumHand;
 | 
				
			||||||
import net.minecraft.util.math.BlockPos;
 | 
					import net.minecraft.util.math.BlockPos;
 | 
				
			||||||
import net.minecraft.world.World;
 | 
					import net.minecraft.world.World;
 | 
				
			||||||
import refinedstorage.RefinedStorage;
 | 
					import refinedstorage.RefinedStorage;
 | 
				
			||||||
import refinedstorage.RefinedStorageUtils;
 | 
					import refinedstorage.RefinedStorageUtils;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetwork;
 | 
				
			||||||
 | 
					import refinedstorage.api.storagenet.StorageNetworkRegistry;
 | 
				
			||||||
import refinedstorage.block.EnumGridType;
 | 
					import refinedstorage.block.EnumGridType;
 | 
				
			||||||
import refinedstorage.item.ItemWirelessGrid;
 | 
					import refinedstorage.item.ItemWirelessGrid;
 | 
				
			||||||
import refinedstorage.network.MessageWirelessGridCraftingStart;
 | 
					import refinedstorage.network.MessageWirelessGridCraftingStart;
 | 
				
			||||||
@@ -16,7 +17,6 @@ import refinedstorage.network.MessageWirelessGridSettingsUpdate;
 | 
				
			|||||||
import refinedstorage.network.MessageWirelessGridStoragePull;
 | 
					import refinedstorage.network.MessageWirelessGridStoragePull;
 | 
				
			||||||
import refinedstorage.tile.ClientItem;
 | 
					import refinedstorage.tile.ClientItem;
 | 
				
			||||||
import refinedstorage.tile.config.IRedstoneModeConfig;
 | 
					import refinedstorage.tile.config.IRedstoneModeConfig;
 | 
				
			||||||
import refinedstorage.tile.controller.TileController;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
@@ -63,16 +63,12 @@ public class WirelessGrid implements IGrid {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public ItemStack onItemPush(EntityPlayer player, ItemStack stack) {
 | 
					    public ItemStack onItemPush(EntityPlayer player, ItemStack stack) {
 | 
				
			||||||
        TileEntity tile = world.getTileEntity(controllerPos);
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controllerPos);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileController) {
 | 
					        if (network != null && network.canRun()) {
 | 
				
			||||||
            TileController controller = (TileController) tile;
 | 
					            network.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (controller.canRun()) {
 | 
					            return network.push(stack, stack.stackSize, false);
 | 
				
			||||||
                controller.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return controller.push(stack, stack.stackSize, false);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return stack;
 | 
					        return stack;
 | 
				
			||||||
@@ -89,10 +85,10 @@ public class WirelessGrid implements IGrid {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void onClose(EntityPlayer player) {
 | 
					    public void onClose(EntityPlayer player) {
 | 
				
			||||||
        TileEntity tile = player.worldObj.getTileEntity(controllerPos);
 | 
					        StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controllerPos);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (tile instanceof TileController) {
 | 
					        if (network != null) {
 | 
				
			||||||
            ((TileController) tile).getWirelessGridHandler().handleClose(player);
 | 
					            network.getWirelessGridHandler().handleClose(player);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user