diff --git a/src/main/java/refinedstorage/api/storagenet/StorageNetwork.java b/src/main/java/refinedstorage/api/storagenet/StorageNetwork.java new file mode 100755 index 000000000..5df7087d2 --- /dev/null +++ b/src/main/java/refinedstorage/api/storagenet/StorageNetwork.java @@ -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 items = new ArrayList(); + private List combinedItems = new ArrayList(); + private Set combinedItemsIndices = new HashSet(); + + private List storages = new ArrayList(); + + private List machines = new ArrayList(); + private List machinesToAdd = new ArrayList(); + private List machinesToRemove = new ArrayList(); + + private List patterns = new ArrayList(); + + private Stack craftingTasks = new Stack(); + private List craftingTasksToAddAsLast = new ArrayList(); + private List craftingTasksToAdd = new ArrayList(); + private List craftingTasksToCancel = new ArrayList(); + + 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 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 getItems() { + return items; + } + + public List 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 getPatterns() { + return patterns; + } + + public List getPattern(ItemStack pattern, int flags) { + List patterns = new ArrayList(); + + 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 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() { + @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() { + @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; + } + } + } + } + } +} diff --git a/src/main/java/refinedstorage/api/storagenet/StorageNetworkRegistry.java b/src/main/java/refinedstorage/api/storagenet/StorageNetworkRegistry.java new file mode 100755 index 000000000..e900af1b2 --- /dev/null +++ b/src/main/java/refinedstorage/api/storagenet/StorageNetworkRegistry.java @@ -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 NETWORKS = new HashMap(); +} diff --git a/src/main/java/refinedstorage/autocrafting/CraftingTaskScheduler.java b/src/main/java/refinedstorage/autocrafting/CraftingTaskScheduler.java index 63bb0687f..8741478cc 100755 --- a/src/main/java/refinedstorage/autocrafting/CraftingTaskScheduler.java +++ b/src/main/java/refinedstorage/autocrafting/CraftingTaskScheduler.java @@ -3,7 +3,7 @@ package refinedstorage.autocrafting; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import refinedstorage.RefinedStorageUtils; -import refinedstorage.tile.controller.TileController; +import refinedstorage.api.storagenet.StorageNetwork; public class CraftingTaskScheduler { public static String NBT_SCHEDULED = "CraftingTaskScheduled"; @@ -14,13 +14,13 @@ public class CraftingTaskScheduler { return scheduledItem == null || !RefinedStorageUtils.compareStack(scheduledItem, item, compare); } - public void schedule(TileController controller, int compare, ItemStack item) { - CraftingPattern pattern = controller.getPatternWithBestScore(item, compare); + public void schedule(StorageNetwork network, int compare, ItemStack item) { + CraftingPattern pattern = network.getPatternWithBestScore(item, compare); if (pattern != null) { scheduledItem = item; - controller.addCraftingTask(controller.createCraftingTask(pattern)); + network.addCraftingTask(network.createCraftingTask(pattern)); } } diff --git a/src/main/java/refinedstorage/autocrafting/task/BasicCraftingTask.java b/src/main/java/refinedstorage/autocrafting/task/BasicCraftingTask.java index 113eb1f83..6ad2b8e25 100755 --- a/src/main/java/refinedstorage/autocrafting/task/BasicCraftingTask.java +++ b/src/main/java/refinedstorage/autocrafting/task/BasicCraftingTask.java @@ -5,8 +5,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; import refinedstorage.RefinedStorageUtils; +import refinedstorage.api.storagenet.StorageNetwork; import refinedstorage.autocrafting.CraftingPattern; -import refinedstorage.tile.controller.TileController; import java.util.ArrayList; import java.util.List; @@ -50,7 +50,7 @@ public class BasicCraftingTask implements ICraftingTask { return pattern; } - public boolean update(TileController controller) { + public boolean update(StorageNetwork network) { this.updatedOnce = true; boolean done = true; @@ -63,17 +63,17 @@ public class BasicCraftingTask implements ICraftingTask { if (!satisfied[i]) { done = false; - ItemStack took = controller.take(input, 1); + ItemStack took = network.take(input, 1); if (took != null) { itemsTook.add(took); satisfied[i] = true; } else if (!childTasks[i]) { - CraftingPattern pattern = controller.getPatternWithBestScore(input); + CraftingPattern pattern = network.getPatternWithBestScore(input); if (pattern != null) { - controller.addCraftingTask(controller.createCraftingTask(pattern)); + network.addCraftingTask(network.createCraftingTask(pattern)); childTasks[i] = true; } @@ -90,23 +90,23 @@ public class BasicCraftingTask implements ICraftingTask { // @todo: handle no space @Override - public void onDone(TileController controller) { + public void onDone(StorageNetwork network) { for (ItemStack output : pattern.getOutputs()) { - controller.push(output, output.stackSize, false); + network.push(output, output.stackSize, false); } if (pattern.getByproducts() != null) { for (ItemStack byproduct : pattern.getByproducts()) { - controller.push(byproduct, byproduct.stackSize, false); + network.push(byproduct, byproduct.stackSize, false); } } } // @todo: handle no space @Override - public void onCancelled(TileController controller) { + public void onCancelled(StorageNetwork network) { for (ItemStack took : itemsTook) { - controller.push(took, took.stackSize, false); + network.push(took, took.stackSize, false); } } diff --git a/src/main/java/refinedstorage/autocrafting/task/ICraftingTask.java b/src/main/java/refinedstorage/autocrafting/task/ICraftingTask.java index 8dba0752e..05496adba 100755 --- a/src/main/java/refinedstorage/autocrafting/task/ICraftingTask.java +++ b/src/main/java/refinedstorage/autocrafting/task/ICraftingTask.java @@ -1,17 +1,17 @@ package refinedstorage.autocrafting.task; import net.minecraft.nbt.NBTTagCompound; +import refinedstorage.api.storagenet.StorageNetwork; import refinedstorage.autocrafting.CraftingPattern; -import refinedstorage.tile.controller.TileController; public interface ICraftingTask { 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); diff --git a/src/main/java/refinedstorage/autocrafting/task/ProcessingCraftingTask.java b/src/main/java/refinedstorage/autocrafting/task/ProcessingCraftingTask.java index 8abfbaed7..02ab9699a 100755 --- a/src/main/java/refinedstorage/autocrafting/task/ProcessingCraftingTask.java +++ b/src/main/java/refinedstorage/autocrafting/task/ProcessingCraftingTask.java @@ -5,9 +5,9 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import refinedstorage.RefinedStorageUtils; +import refinedstorage.api.storagenet.StorageNetwork; import refinedstorage.autocrafting.CraftingPattern; import refinedstorage.tile.TileCrafter; -import refinedstorage.tile.controller.TileController; public class ProcessingCraftingTask implements ICraftingTask { public static final int ID = 1; @@ -42,17 +42,17 @@ public class ProcessingCraftingTask implements ICraftingTask { } @Override - public boolean update(TileController controller) { + public boolean update(StorageNetwork network) { this.updatedOnce = true; - TileCrafter crafter = pattern.getCrafter(controller.getWorld()); + TileCrafter crafter = pattern.getCrafter(network.getWorld()); IItemHandler handler = RefinedStorageUtils.getItemHandler(crafter.getFacingTile(), crafter.getDirection().getOpposite()); if (handler != null) { for (int i = 0; i < inserted.length; ++i) { if (!inserted[i]) { ItemStack input = pattern.getInputs()[i]; - ItemStack took = controller.take(input, 1); + ItemStack took = network.take(input, 1); if (took != null) { if (ItemHandlerHelper.insertItem(handler, took, true) == null) { @@ -60,15 +60,15 @@ public class ProcessingCraftingTask implements ICraftingTask { inserted[i] = true; } else { - controller.push(took, took.stackSize, false); + network.push(took, took.stackSize, false); } } else if (!childTasks[i]) { - CraftingPattern pattern = controller.getPatternWithBestScore(input); + CraftingPattern pattern = network.getPatternWithBestScore(input); if (pattern != null) { childTasks[i] = true; - controller.addCraftingTask(controller.createCraftingTask(pattern)); + network.addCraftingTask(network.createCraftingTask(pattern)); break; } @@ -101,12 +101,12 @@ public class ProcessingCraftingTask implements ICraftingTask { } @Override - public void onDone(TileController controller) { + public void onDone(StorageNetwork network) { // NO OP } @Override - public void onCancelled(TileController controller) { + public void onCancelled(StorageNetwork network) { // NO OP } diff --git a/src/main/java/refinedstorage/block/BlockController.java b/src/main/java/refinedstorage/block/BlockController.java index 9d32a195d..1dbedffa9 100755 --- a/src/main/java/refinedstorage/block/BlockController.java +++ b/src/main/java/refinedstorage/block/BlockController.java @@ -20,6 +20,8 @@ import net.minecraft.world.World; import refinedstorage.RefinedStorage; import refinedstorage.RefinedStorageBlocks; import refinedstorage.RefinedStorageGui; +import refinedstorage.api.storagenet.StorageNetwork; +import refinedstorage.api.storagenet.StorageNetworkRegistry; import refinedstorage.item.ItemBlockController; import refinedstorage.tile.controller.TileController; @@ -91,12 +93,8 @@ public class BlockController extends BlockBase { NBTTagCompound tag = itemStack.getTagCompound(); - if (tag != null && tag.hasKey(TileController.NBT_ENERGY)) { - TileEntity tile = world.getTileEntity(pos); - - if (tile instanceof TileController) { - ((TileController) tile).receiveEnergy(null, tag.getInteger(TileController.NBT_ENERGY), false); - } + if (tag != null && tag.hasKey(StorageNetwork.NBT_ENERGY)) { + StorageNetworkRegistry.NETWORKS.get(pos).getEnergy().receiveEnergy(tag.getInteger(StorageNetwork.NBT_ENERGY), false); } } @@ -107,7 +105,7 @@ public class BlockController extends BlockBase { ItemStack stack = new ItemStack(RefinedStorageBlocks.CONTROLLER, 1, RefinedStorageBlocks.CONTROLLER.getMetaFromState(state)); 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); drops.add(stack); diff --git a/src/main/java/refinedstorage/item/ItemBlockController.java b/src/main/java/refinedstorage/item/ItemBlockController.java index 76eb8abe1..5417f6751 100755 --- a/src/main/java/refinedstorage/item/ItemBlockController.java +++ b/src/main/java/refinedstorage/item/ItemBlockController.java @@ -6,8 +6,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import refinedstorage.RefinedStorageBlocks; +import refinedstorage.api.storagenet.StorageNetwork; import refinedstorage.block.EnumControllerType; -import refinedstorage.tile.controller.TileController; import java.util.List; @@ -21,11 +21,11 @@ public class ItemBlockController extends ItemBlockBase { if (stack.getMetadata() != EnumControllerType.CREATIVE.getId()) { int energyStored = 0; - if (stack.getTagCompound() != null && stack.getTagCompound().hasKey(TileController.NBT_ENERGY)) { - energyStored = stack.getTagCompound().getInteger(TileController.NBT_ENERGY); + if (stack.getTagCompound() != null && stack.getTagCompound().hasKey(StorageNetwork.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.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; } diff --git a/src/main/java/refinedstorage/item/ItemWirelessGrid.java b/src/main/java/refinedstorage/item/ItemWirelessGrid.java index f4d7d5923..0ce8bd30e 100755 --- a/src/main/java/refinedstorage/item/ItemWirelessGrid.java +++ b/src/main/java/refinedstorage/item/ItemWirelessGrid.java @@ -10,14 +10,14 @@ import net.minecraft.item.IItemPropertyGetter; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.World; import refinedstorage.RefinedStorage; import refinedstorage.RefinedStorageBlocks; -import refinedstorage.tile.controller.TileController; +import refinedstorage.api.storagenet.StorageNetwork; +import refinedstorage.api.storagenet.StorageNetworkRegistry; import refinedstorage.tile.grid.TileGrid; import java.util.List; @@ -134,10 +134,10 @@ public class ItemWirelessGrid extends ItemEnergyContainer { @Override public ActionResult onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand) { 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 (((TileController) tile).getWirelessGridHandler().handleOpen(player, hand)) { + if (network != null) { + if (network.getWirelessGridHandler().handleOpen(player, hand)) { return new ActionResult(EnumActionResult.SUCCESS, stack); } else { player.addChatComponentMessage(new TextComponentTranslation("misc.refinedstorage:wireless_grid.out_of_range")); diff --git a/src/main/java/refinedstorage/network/MessageCraftingMonitorCancel.java b/src/main/java/refinedstorage/network/MessageCraftingMonitorCancel.java index 1d6e7d1a5..1691f6967 100755 --- a/src/main/java/refinedstorage/network/MessageCraftingMonitorCancel.java +++ b/src/main/java/refinedstorage/network/MessageCraftingMonitorCancel.java @@ -47,7 +47,7 @@ public class MessageCraftingMonitorCancel extends MessageHandlerPlayerToServer implements IMessage { private int x; @@ -45,10 +45,10 @@ public class MessageGridCraftingStart extends MessageHandlerPlayerToServer { - private TileController controller; + private StorageNetwork network; private List items = new ArrayList(); public MessageGridItems() { } - public MessageGridItems(TileController controller) { - this.controller = controller; + public MessageGridItems(StorageNetwork network) { + this.network = network; } @Override @@ -42,9 +42,9 @@ public class MessageGridItems implements IMessage, IMessageHandler implements IMessage { private int x; @@ -45,10 +45,10 @@ public class MessageGridStoragePull extends MessageHandlerPlayerToServer implements IMessage { private int controllerX; @@ -45,10 +45,10 @@ public class MessageWirelessGridCraftingStart extends MessageHandlerPlayerToServ @Override 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()) { - ((TileController) tile).getStorageHandler().onCraftingRequested(message.id, message.quantity); + if (network != null && network.canRun()) { + network.getStorageHandler().onCraftingRequested(message.id, message.quantity); } } } diff --git a/src/main/java/refinedstorage/network/MessageWirelessGridHeldItemPush.java b/src/main/java/refinedstorage/network/MessageWirelessGridHeldItemPush.java index f1d2ce188..28e99721a 100755 --- a/src/main/java/refinedstorage/network/MessageWirelessGridHeldItemPush.java +++ b/src/main/java/refinedstorage/network/MessageWirelessGridHeldItemPush.java @@ -2,10 +2,10 @@ package refinedstorage.network; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; 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 implements IMessage { private int controllerX; @@ -41,10 +41,10 @@ public class MessageWirelessGridHeldItemPush extends MessageHandlerPlayerToServe @Override 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()) { - ((TileController) tile).getStorageHandler().onHeldItemPush(message.one, player); + if (network != null && network.canRun()) { + network.getStorageHandler().onHeldItemPush(message.one, player); } } } diff --git a/src/main/java/refinedstorage/network/MessageWirelessGridStoragePull.java b/src/main/java/refinedstorage/network/MessageWirelessGridStoragePull.java index 5a96f7539..27c2b9aa8 100755 --- a/src/main/java/refinedstorage/network/MessageWirelessGridStoragePull.java +++ b/src/main/java/refinedstorage/network/MessageWirelessGridStoragePull.java @@ -2,10 +2,10 @@ package refinedstorage.network; import io.netty.buffer.ByteBuf; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; 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 implements IMessage { private int controllerX; @@ -45,10 +45,10 @@ public class MessageWirelessGridStoragePull extends MessageHandlerPlayerToServer @Override 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()) { - ((TileController) tile).getStorageHandler().onPull(message.id, message.flags, player); + if (network != null && network.canRun()) { + network.getStorageHandler().onPull(message.id, message.flags, player); } } } diff --git a/src/main/java/refinedstorage/tile/TileConstructor.java b/src/main/java/refinedstorage/tile/TileConstructor.java index b74f64b72..2b8940793 100755 --- a/src/main/java/refinedstorage/tile/TileConstructor.java +++ b/src/main/java/refinedstorage/tile/TileConstructor.java @@ -58,7 +58,7 @@ public class TileConstructor extends TileMachine implements ICompareConfig { BlockPos front = pos.offset(getDirection()); 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) { scheduler.resetSchedule(); @@ -70,7 +70,7 @@ public class TileConstructor extends TileMachine implements ICompareConfig { ItemStack craft = filter.getStackInSlot(0); if (scheduler.canSchedule(compare, craft)) { - scheduler.schedule(controller, compare, craft); + scheduler.schedule(network, compare, craft); } } } diff --git a/src/main/java/refinedstorage/tile/TileCrafter.java b/src/main/java/refinedstorage/tile/TileCrafter.java index 6fe9a0b7f..215ad2b1c 100755 --- a/src/main/java/refinedstorage/tile/TileCrafter.java +++ b/src/main/java/refinedstorage/tile/TileCrafter.java @@ -45,9 +45,9 @@ public class TileCrafter extends TileMachine { @Override public void onDisconnected(World world) { - for (ICraftingTask task : controller.getCraftingTasks()) { + for (ICraftingTask task : network.getCraftingTasks()) { if (task.getPattern().getCrafter(worldObj) == this) { - controller.cancelCraftingTask(task); + network.cancelCraftingTask(task); } } diff --git a/src/main/java/refinedstorage/tile/TileCraftingMonitor.java b/src/main/java/refinedstorage/tile/TileCraftingMonitor.java index 956c7561d..8b3a340a8 100755 --- a/src/main/java/refinedstorage/tile/TileCraftingMonitor.java +++ b/src/main/java/refinedstorage/tile/TileCraftingMonitor.java @@ -28,9 +28,9 @@ public class TileCraftingMonitor extends TileMachine { super.writeContainerData(buf); 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()); buf.writeInt(task.getPattern().getOutputs().length); diff --git a/src/main/java/refinedstorage/tile/TileDestructor.java b/src/main/java/refinedstorage/tile/TileDestructor.java index f5dba54ae..aeea68440 100755 --- a/src/main/java/refinedstorage/tile/TileDestructor.java +++ b/src/main/java/refinedstorage/tile/TileDestructor.java @@ -64,10 +64,10 @@ public class TileDestructor extends TileMachine implements ICompareConfig, IMode for (ItemStack drop : drops) { // 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 - if (controller == null) { + if (network == null) { InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), drop); } else { - ItemStack remainder = controller.push(drop, drop.stackSize, false); + ItemStack remainder = network.push(drop, drop.stackSize, false); if (remainder != null) { InventoryHelper.spawnItemStack(worldObj, front.getX(), front.getY(), front.getZ(), remainder); diff --git a/src/main/java/refinedstorage/tile/TileDetector.java b/src/main/java/refinedstorage/tile/TileDetector.java index 773a478c4..5d8c5cb71 100755 --- a/src/main/java/refinedstorage/tile/TileDetector.java +++ b/src/main/java/refinedstorage/tile/TileDetector.java @@ -53,7 +53,7 @@ public class TileDetector extends TileMachine implements ICompareConfig { boolean wasPowered = powered; if (slot != null) { - ItemStack stack = controller.getItem(slot, compare); + ItemStack stack = network.getItem(slot, compare); if (stack != null) { switch (mode) { diff --git a/src/main/java/refinedstorage/tile/TileExporter.java b/src/main/java/refinedstorage/tile/TileExporter.java index 6b6c33901..9e5db5c51 100755 --- a/src/main/java/refinedstorage/tile/TileExporter.java +++ b/src/main/java/refinedstorage/tile/TileExporter.java @@ -50,7 +50,7 @@ public class TileExporter extends TileMachine implements ICompareConfig { if (slot != null) { 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) { scheduler.resetSchedule(); @@ -58,11 +58,11 @@ public class TileExporter extends TileMachine implements ICompareConfig { ItemStack remainder = ItemHandlerHelper.insertItem(handler, took, false); if (remainder != null) { - controller.push(remainder, remainder.stackSize, false); + network.push(remainder, remainder.stackSize, false); } } else if (RefinedStorageUtils.hasUpgrade(upgrades, ItemUpgrade.TYPE_CRAFTING)) { if (scheduler.canSchedule(compare, slot)) { - scheduler.schedule(controller, compare, slot); + scheduler.schedule(network, compare, slot); } } } diff --git a/src/main/java/refinedstorage/tile/TileImporter.java b/src/main/java/refinedstorage/tile/TileImporter.java index 547306e02..0c1db934b 100755 --- a/src/main/java/refinedstorage/tile/TileImporter.java +++ b/src/main/java/refinedstorage/tile/TileImporter.java @@ -63,8 +63,8 @@ public class TileImporter extends TileMachine implements ICompareConfig, IModeCo ItemStack result = handler.extractItem(currentSlot, quantity, true); - if (result != null && controller.push(result, result.stackSize, true) == null) { - controller.push(result, result.stackSize, false); + if (result != null && network.push(result, result.stackSize, true) == null) { + network.push(result, result.stackSize, false); handler.extractItem(currentSlot, quantity, false); } else { diff --git a/src/main/java/refinedstorage/tile/TileInterface.java b/src/main/java/refinedstorage/tile/TileInterface.java index 8832bcbb2..97fcffcf1 100755 --- a/src/main/java/refinedstorage/tile/TileInterface.java +++ b/src/main/java/refinedstorage/tile/TileInterface.java @@ -52,7 +52,7 @@ public class TileInterface extends TileMachine implements ICompareConfig { } else if (ticks % RefinedStorageUtils.getSpeed(upgrades) == 0) { 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) { importItems.extractItem(currentSlot, size, false); @@ -67,13 +67,13 @@ public class TileInterface extends TileMachine implements ICompareConfig { if (wanted == null) { if (got != null) { - exportItems.setStackInSlot(i, controller.push(got, got.stackSize, false)); + exportItems.setStackInSlot(i, network.push(got, got.stackSize, false)); } } else { int delta = got == null ? wanted.stackSize : (wanted.stackSize - got.stackSize); if (delta > 0) { - ItemStack result = controller.take(wanted, delta, compare); + ItemStack result = network.take(wanted, delta, compare); if (result != null) { if (got == null) { @@ -83,7 +83,7 @@ public class TileInterface extends TileMachine implements ICompareConfig { } } } 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) { exportItems.extractItem(i, Math.abs(delta), false); diff --git a/src/main/java/refinedstorage/tile/TileMachine.java b/src/main/java/refinedstorage/tile/TileMachine.java index 1d539697f..d47855951 100755 --- a/src/main/java/refinedstorage/tile/TileMachine.java +++ b/src/main/java/refinedstorage/tile/TileMachine.java @@ -5,6 +5,8 @@ import net.minecraft.block.Block; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import refinedstorage.RefinedStorageUtils; +import refinedstorage.api.storagenet.StorageNetwork; +import refinedstorage.api.storagenet.StorageNetworkRegistry; import refinedstorage.tile.config.IRedstoneModeConfig; import refinedstorage.tile.config.RedstoneMode; import refinedstorage.tile.controller.ControllerSearcher; @@ -19,22 +21,18 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta protected boolean connected; protected boolean wasConnected; protected RedstoneMode redstoneMode = RedstoneMode.IGNORE; - protected TileController controller; + protected StorageNetwork network; private Block block; private Set visited = new HashSet(); - public TileController getController() { - return controller; - } - public void searchController(World world) { visited.clear(); TileController newController = ControllerSearcher.search(world, pos, visited); - if (controller == null) { + if (network == null) { if (newController != null) { onConnected(world, newController); } @@ -87,14 +85,16 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta } private boolean tryConnect(TileController controller) { - if (!controller.canRun()) { + StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controller.getPos()); + + if (!network.canRun()) { return false; } - this.controller = controller; + this.network = network; this.connected = true; - controller.addMachine(this); + network.addMachine(this); return true; } @@ -102,14 +102,18 @@ public abstract class TileMachine extends TileBase implements ISynchronizedConta public void onDisconnected(World world) { this.connected = false; - if (this.controller != null) { - this.controller.removeMachine(this); - this.controller = null; + if (this.network != null) { + this.network.removeMachine(this); + this.network = null; } world.notifyNeighborsOfStateChange(pos, block); } + public StorageNetwork getNetwork() { + return network; + } + public boolean isConnected() { return connected; } diff --git a/src/main/java/refinedstorage/tile/controller/StorageHandler.java b/src/main/java/refinedstorage/tile/controller/StorageHandler.java index ec51596a3..cfdac7d8f 100755 --- a/src/main/java/refinedstorage/tile/controller/StorageHandler.java +++ b/src/main/java/refinedstorage/tile/controller/StorageHandler.java @@ -4,6 +4,7 @@ import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.InventoryHelper; import net.minecraft.item.ItemStack; import refinedstorage.RefinedStorageUtils; +import refinedstorage.api.storagenet.StorageNetwork; import refinedstorage.autocrafting.CraftingPattern; import refinedstorage.autocrafting.task.ICraftingTask; import refinedstorage.item.ItemWirelessGrid; @@ -12,10 +13,10 @@ import refinedstorage.network.GridPullFlags; public class StorageHandler { public static final int MAX_CRAFTING_PER_REQUEST = 500; - private TileController controller; + private StorageNetwork network; - public StorageHandler(TileController controller) { - this.controller = controller; + public StorageHandler(StorageNetwork network) { + this.network = network; } public void onPull(int id, int flags, EntityPlayerMP player) { @@ -23,11 +24,11 @@ public class StorageHandler { return; } - if (id < 0 || id > controller.getItems().size() - 1) { + if (id < 0 || id > network.getItems().size() - 1) { return; } - ItemStack stack = controller.getItems().get(id); + ItemStack stack = network.getItems().get(id); int size = 64; @@ -45,7 +46,7 @@ public class StorageHandler { size = Math.min(size, stack.getItem().getItemStackLimit(stack)); - ItemStack took = controller.take(stack, size); + ItemStack took = network.take(stack, size); if (took != null) { if (GridPullFlags.isPullingWithShift(flags)) { @@ -57,7 +58,7 @@ public class StorageHandler { 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; if (one) { - if (controller.push(stack, size, true) == null) { - controller.push(stack, size, false); + if (network.push(stack, size, true) == null) { + network.push(stack, size, false); stack.stackSize -= size; @@ -80,21 +81,21 @@ public class StorageHandler { } } } else { - player.inventory.setItemStack(controller.push(stack, size, false)); + player.inventory.setItemStack(network.push(stack, size, false)); } player.updateHeldItem(); - controller.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH); + network.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH); } public void onCraftingRequested(int id, int quantity) { - if (id >= 0 && id < controller.getItems().size() && quantity > 0 && quantity <= MAX_CRAFTING_PER_REQUEST) { - ItemStack requested = controller.getItems().get(id); + if (id >= 0 && id < network.getItems().size() && quantity > 0 && quantity <= MAX_CRAFTING_PER_REQUEST) { + ItemStack requested = network.getItems().get(id); int quantityPerRequest = 0; - CraftingPattern pattern = controller.getPatternWithBestScore(requested); + CraftingPattern pattern = network.getPatternWithBestScore(requested); if (pattern != null) { for (ItemStack output : pattern.getOutputs()) { @@ -108,7 +109,7 @@ public class StorageHandler { } while (quantity > 0) { - controller.addCraftingTaskAsLast(controller.createCraftingTask(pattern)); + network.addCraftingTaskAsLast(network.createCraftingTask(pattern)); quantity -= quantityPerRequest; } @@ -117,11 +118,11 @@ public class StorageHandler { } public void onCraftingCancelRequested(int id) { - if (id >= 0 && id < controller.getCraftingTasks().size()) { - controller.cancelCraftingTask(controller.getCraftingTasks().get(id)); + if (id >= 0 && id < network.getCraftingTasks().size()) { + network.cancelCraftingTask(network.getCraftingTasks().get(id)); } else if (id == -1) { - for (ICraftingTask task : controller.getCraftingTasks()) { - controller.cancelCraftingTask(task); + for (ICraftingTask task : network.getCraftingTasks()) { + network.cancelCraftingTask(task); } } } diff --git a/src/main/java/refinedstorage/tile/controller/TileController.java b/src/main/java/refinedstorage/tile/controller/TileController.java index 5fda6de23..4b0d45e9e 100755 --- a/src/main/java/refinedstorage/tile/controller/TileController.java +++ b/src/main/java/refinedstorage/tile/controller/TileController.java @@ -1,170 +1,111 @@ package refinedstorage.tile.controller; -import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyReceiver; import io.netty.buffer.ByteBuf; 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.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import net.minecraft.util.EnumFacing; -import net.minecraftforge.common.util.Constants; import net.minecraftforge.fml.common.network.ByteBufUtils; -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.api.storagenet.StorageNetwork; +import refinedstorage.api.storagenet.StorageNetworkRegistry; import refinedstorage.block.BlockController; import refinedstorage.block.EnumControllerType; import refinedstorage.container.ContainerController; -import refinedstorage.container.ContainerGrid; -import refinedstorage.item.ItemPattern; -import refinedstorage.network.MessageGridItems; -import refinedstorage.tile.*; +import refinedstorage.tile.ISynchronizedContainer; +import refinedstorage.tile.TileBase; +import refinedstorage.tile.TileMachine; import refinedstorage.tile.config.IRedstoneModeConfig; 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 static final int ENERGY_CAPACITY = 32000; - - 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 items = new ArrayList(); - private List combinedItems = new ArrayList(); - private Set combinedItemsIndices = new HashSet(); - - private List storages = new ArrayList(); - - private RedstoneMode redstoneMode = RedstoneMode.IGNORE; - - private List machines = new ArrayList(); - private List machinesToAdd = new ArrayList(); - private List machinesToRemove = new ArrayList(); + private StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(pos); private List clientMachines = new ArrayList(); - - private List patterns = new ArrayList(); - - private Stack craftingTasks = new Stack(); - private List craftingTasksToAddAsLast = new ArrayList(); - private List craftingTasksToAdd = new ArrayList(); - private List craftingTasksToCancel = new ArrayList(); - - private EnergyStorage energy = new EnergyStorage(ENERGY_CAPACITY); + private int energy; private int energyUsage; - - private int wirelessGridRange; - private boolean couldRun; - private long lastEnergyUpdate; + private EnumControllerType type; + private RedstoneMode redstoneMode; @Override - public void update() { - super.update(); + public NBTTagCompound write(NBTTagCompound tag) { + super.write(tag); - if (!worldObj.isRemote) { - 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); - } - } - } + return network.write(tag); } - public void addMachine(TileMachine machine) { - machinesToAdd.add(machine); + @Override + public void read(NBTTagCompound tag) { + super.read(tag); + + network.read(tag); } - public void removeMachine(TileMachine machine) { - machinesToRemove.add(machine); + @Override + 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 getClientMachines() { + return clientMachines; + } + + public int getEnergy() { + return energy; + } + + public int getEnergyUsage() { + return energyUsage; } public EnumControllerType getType() { @@ -175,458 +116,12 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr 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 getItems() { - return items; - } - - public List 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 getPatterns() { - return patterns; - } - - public List getPattern(ItemStack pattern) { - return getPattern(pattern, CompareFlags.COMPARE_DAMAGE | CompareFlags.COMPARE_NBT); - } - - public List getPattern(ItemStack pattern, int flags) { - List patterns = new ArrayList(); - - 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 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() { - @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() { - @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 getClientMachines() { - return clientMachines; - } - @Override public void readContainerData(ByteBuf buf) { - setEnergyStored(buf.readInt()); - energyUsage = buf.readInt(); + this.energy = buf.readInt(); + this.energyUsage = buf.readInt(); - redstoneMode = RedstoneMode.getById(buf.readInt()); - - machines.clear(); + this.redstoneMode = RedstoneMode.getById(buf.readInt()); List machines = new ArrayList(); @@ -641,7 +136,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr machines.add(machine); } - clientMachines = machines; + this.clientMachines = machines; } @Override @@ -653,7 +148,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr List m = new ArrayList(); - for (TileMachine machine : machines) { + for (TileMachine machine : network.getMachines()) { if (machine.canUpdate()) { IBlockState state = worldObj.getBlockState(machine.getPos()); diff --git a/src/main/java/refinedstorage/tile/controller/WirelessGridHandler.java b/src/main/java/refinedstorage/tile/controller/WirelessGridHandler.java index acba8a7de..557b61dab 100755 --- a/src/main/java/refinedstorage/tile/controller/WirelessGridHandler.java +++ b/src/main/java/refinedstorage/tile/controller/WirelessGridHandler.java @@ -8,6 +8,7 @@ import refinedstorage.RefinedStorage; import refinedstorage.RefinedStorageGui; import refinedstorage.RefinedStorageItems; import refinedstorage.RefinedStorageUtils; +import refinedstorage.api.storagenet.StorageNetwork; import refinedstorage.item.ItemWirelessGrid; import refinedstorage.tile.grid.WirelessGridConsumer; @@ -16,13 +17,13 @@ import java.util.Iterator; import java.util.List; public class WirelessGridHandler { - private TileController controller; + private StorageNetwork network; private List consumers = new ArrayList(); private List consumersToRemove = new ArrayList(); - public WirelessGridHandler(TileController controller) { - this.controller = controller; + public WirelessGridHandler(StorageNetwork network) { + this.network = network; } public void update() { @@ -41,17 +42,17 @@ public class WirelessGridHandler { } 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; } 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); diff --git a/src/main/java/refinedstorage/tile/grid/TileGrid.java b/src/main/java/refinedstorage/tile/grid/TileGrid.java index fb67e30d6..7db1ca641 100755 --- a/src/main/java/refinedstorage/tile/grid/TileGrid.java +++ b/src/main/java/refinedstorage/tile/grid/TileGrid.java @@ -109,18 +109,18 @@ public class TileGrid extends TileMachine implements IGrid { @Override public BlockPos getControllerPos() { - return controller != null ? controller.getPos() : null; + return network != null ? network.getPos() : null; } public void onGridOpened(EntityPlayer player) { if (isConnected()) { - controller.syncItemsWithClient((EntityPlayerMP) player); + network.syncItemsWithClient((EntityPlayerMP) player); } } @Override 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 @@ -162,7 +162,7 @@ public class TileGrid extends TileMachine implements IGrid { if (slot != null) { if (slot.stackSize == 1 && isConnected()) { - matrix.setInventorySlotContents(i, controller.take(slot, 1)); + matrix.setInventorySlotContents(i, network.take(slot, 1)); } else { matrix.decrStackSize(i, 1); } @@ -239,10 +239,10 @@ public class TileGrid extends TileMachine implements IGrid { if (slot != null) { if (getType() == EnumGridType.CRAFTING) { - if (controller.push(slot, slot.stackSize, true) != null) { + if (network.push(slot, slot.stackSize, true) != null) { return; } 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) { for (ItemStack possibility : possibilities) { - ItemStack took = controller.take(possibility, 1); + ItemStack took = network.take(possibility, 1); if (took != null) { matrix.setInventorySlotContents(i, possibility); diff --git a/src/main/java/refinedstorage/tile/grid/WirelessGrid.java b/src/main/java/refinedstorage/tile/grid/WirelessGrid.java index dd138ea14..2d5345ce5 100755 --- a/src/main/java/refinedstorage/tile/grid/WirelessGrid.java +++ b/src/main/java/refinedstorage/tile/grid/WirelessGrid.java @@ -2,12 +2,13 @@ package refinedstorage.tile.grid; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import refinedstorage.RefinedStorage; import refinedstorage.RefinedStorageUtils; +import refinedstorage.api.storagenet.StorageNetwork; +import refinedstorage.api.storagenet.StorageNetworkRegistry; import refinedstorage.block.EnumGridType; import refinedstorage.item.ItemWirelessGrid; import refinedstorage.network.MessageWirelessGridCraftingStart; @@ -16,7 +17,6 @@ import refinedstorage.network.MessageWirelessGridSettingsUpdate; import refinedstorage.network.MessageWirelessGridStoragePull; import refinedstorage.tile.ClientItem; import refinedstorage.tile.config.IRedstoneModeConfig; -import refinedstorage.tile.controller.TileController; import java.util.ArrayList; import java.util.List; @@ -63,16 +63,12 @@ public class WirelessGrid implements IGrid { @Override public ItemStack onItemPush(EntityPlayer player, ItemStack stack) { - TileEntity tile = world.getTileEntity(controllerPos); + StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controllerPos); - if (tile instanceof TileController) { - TileController controller = (TileController) tile; + if (network != null && network.canRun()) { + network.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH); - if (controller.canRun()) { - controller.getWirelessGridHandler().drainEnergy(player, ItemWirelessGrid.USAGE_PUSH); - - return controller.push(stack, stack.stackSize, false); - } + return network.push(stack, stack.stackSize, false); } return stack; @@ -89,10 +85,10 @@ public class WirelessGrid implements IGrid { } public void onClose(EntityPlayer player) { - TileEntity tile = player.worldObj.getTileEntity(controllerPos); + StorageNetwork network = StorageNetworkRegistry.NETWORKS.get(controllerPos); - if (tile instanceof TileController) { - ((TileController) tile).getWirelessGridHandler().handleClose(player); + if (network != null) { + network.getWirelessGridHandler().handleClose(player); } }