Added round robin mode to item conduits.

This commit is contained in:
CrazyPants committed 2014-06-03 00:39:09 +08:00
1 parent f2798cf460
commit 4ebb438153
14 files changed
+486 -362

No files matched your search

@@ -40,7 +40,7 @@ public class ExternalConnectionContainer extends Container {
// add players inventory
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
int x = 8 + j * 18;
int x = 23 + j * 18;
int y = topY + i * 18;
addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, x, y));
slotLocations.add(new Point(x, y));
@@ -49,7 +49,7 @@ public class ExternalConnectionContainer extends Container {
int y = 171;
for (int i = 0; i < 9; ++i) {
int x = 8 + i * 18;
int x = 23 + i * 18;
addSlotToContainer(new Slot(playerInv, i, x, y));
slotLocations.add(new Point(x, y));
}
@@ -61,7 +61,7 @@ public class ExternalConnectionContainer extends Container {
inputFilter = itemConduit.getInputFilter(dir);
int topY = 67;
int leftX = 16;
int leftX = 23;
int index = 0;
for (int row = 0; row < 2; ++row) {
@@ -81,7 +81,7 @@ public class ExternalConnectionContainer extends Container {
outputFilter = itemConduit.getOutputFilter(dir);
leftX = 16;
leftX = 23;
index = 0;
for (int row = 0; row < 2; ++row) {
@@ -38,9 +38,14 @@ public class GuiExternalConnection extends GuiContainerBase {
this.playerInv = playerInv;
this.bundle = bundle;
this.dir = dir;
ySize = 166 + 29;
xSize = 206;
container.setInputSlotsVisible(false);
container.setOutputSlotsVisible(false);
container.setInventorySlotsVisible(false);
for (IConduit con : bundle.getConduits()) {
if(con.containsExternalConnection(dir) || con.canConnectToExternal(dir, true)) {
ISettingsPanel tab = TabFactory.instance.createPanelForConduit(this, con);
@@ -51,7 +56,6 @@ public class GuiExternalConnection extends GuiContainerBase {
}
}
ySize = 166 + 29;
}
@Override
@@ -38,6 +38,7 @@ public class ItemSettings extends BaseSettingsPanel {
private static final int ID_STICKY = 21;
private static final int ID_LOOP = 22;
private static final int ID_CHANNEL = 23;
private static final int ID_ROUND_ROBIN = 24;
private IItemConduit itemConduit;
@@ -54,6 +55,7 @@ public class ItemSettings extends BaseSettingsPanel {
private ColorButton channelB;
private ToggleButtonEIO loopB;
private ToggleButtonEIO roundRobinB;
private RedstoneModeButton rsB;
private ColorButton colorB;
@@ -101,8 +103,22 @@ public class ItemSettings extends BaseSettingsPanel {
colorB = new ColorButton(gui, ID_COLOR_BUTTON, x, y);
colorB.setColorIndex(itemConduit.getExtractionSignalColor(gui.dir).ordinal());
colorB.setToolTipHeading(Lang.localize("gui.conduit.item.sigCol"));
x += gap + colorB.getWidth();
roundRobinB = new ToggleButtonEIO(gui, ID_ROUND_ROBIN, x, y, IconEIO.ROUND_ROBIN_OFF, IconEIO.ROUND_ROBIN);
roundRobinB.setSelectedToolTip(Lang.localize("gui.conduit.item.roundRobinEnabled"));
roundRobinB.setUnselectedToolTip(Lang.localize("gui.conduit.item.roundRobinDisabled"));
roundRobinB.setPaintSelectedBorder(false);
x += gap + roundRobinB.getWidth();
loopB = new ToggleButtonEIO(gui, ID_LOOP, x, y, IconEIO.LOOP_OFF, IconEIO.LOOP);
loopB.setSelectedToolTip(Lang.localize("gui.conduit.item.selfFeedEnabled"));
loopB.setUnselectedToolTip(Lang.localize("gui.conduit.item.selfFeedDisabled"));
loopB.setPaintSelectedBorder(false);
x = 112;
x = 115;
y = 66;
whiteListB = new IconButtonEIO(gui, ID_WHITELIST, x, y, IconEIO.FILTER_WHITELIST);
whiteListB.setToolTip(Lang.localize("gui.conduit.item.whitelist"));
@@ -121,7 +137,7 @@ public class ItemSettings extends BaseSettingsPanel {
stickyB.setPaintSelectedBorder(false);
y += 20;
x = 112;
x = 115;
channelB = new ColorButton(gui, ID_CHANNEL, x, y);
channelB.setColorIndex(0);
@@ -139,13 +155,6 @@ public class ItemSettings extends BaseSettingsPanel {
useOreDictB.setUnselectedToolTip(Lang.localize("gui.conduit.item.oreDicDisabled"));
useOreDictB.setPaintSelectedBorder(false);
//x += 20;
y = customTop;
loopB = new ToggleButtonEIO(gui, ID_LOOP, x, y, IconEIO.LOOP_OFF, IconEIO.LOOP);
loopB.setSelectedToolTip(Lang.localize("gui.conduit.item.selfFeedEnabled"));
loopB.setUnselectedToolTip(Lang.localize("gui.conduit.item.selfFeedDisabled"));
loopB.setPaintSelectedBorder(false);
}
private String getHeading() {
@@ -266,7 +275,11 @@ public class ItemSettings extends BaseSettingsPanel {
if(mode == ConnectionMode.IN_OUT) {
loopB.onGuiInit();
loopB.setSelected(itemConduit.isSelfFeedEnabled(gui.dir));
loopB.setSelected(itemConduit.isSelfFeedEnabled(gui.dir));
}
if(!outputActive) {
roundRobinB.onGuiInit();
roundRobinB.setSelected(itemConduit.isRoundRobinEnabled(gui.dir));
}
}
@@ -305,6 +318,10 @@ public class ItemSettings extends BaseSettingsPanel {
} else if(guiButton.id == ID_LOOP) {
itemConduit.setSelfFeedEnabled(gui.dir, !itemConduit.isSelfFeedEnabled(gui.dir));
EnderIO.packetPipeline.sendToServer(new PacketItemConduitFilter(itemConduit, gui.dir));
} else if(guiButton.id == ID_ROUND_ROBIN) {
itemConduit.setRoundRobinEnabled(gui.dir, !itemConduit.isRoundRobinEnabled(gui.dir));
EnderIO.packetPipeline.sendToServer(new PacketItemConduitFilter(itemConduit, gui.dir));
} else if(guiButton.id == ID_CHANNEL) {
@@ -43,6 +43,10 @@ public interface IItemConduit extends IConduit, IItemDuct, IExtractor {
boolean isSelfFeedEnabled(ForgeDirection dir);
void setSelfFeedEnabled(ForgeDirection dir, boolean enabled);
boolean isRoundRobinEnabled(ForgeDirection dir);
void setRoundRobinEnabled(ForgeDirection dir, boolean enabled);
DyeColor getInputColor(ForgeDirection dir);
@@ -92,6 +92,8 @@ public class ItemConduit extends AbstractConduit implements IItemConduit {
protected final EnumMap<ForgeDirection, ItemFilter> inputFilters = new EnumMap<ForgeDirection, ItemFilter>(ForgeDirection.class);
protected final EnumMap<ForgeDirection, Boolean> selfFeed = new EnumMap<ForgeDirection, Boolean>(ForgeDirection.class);
protected final EnumMap<ForgeDirection, Boolean> roundRobin = new EnumMap<ForgeDirection, Boolean>(ForgeDirection.class);
protected final EnumMap<ForgeDirection, DyeColor> outputColors = new EnumMap<ForgeDirection, DyeColor>(ForgeDirection.class);
protected final EnumMap<ForgeDirection, DyeColor> inputColors = new EnumMap<ForgeDirection, DyeColor>(ForgeDirection.class);
@@ -363,6 +365,23 @@ public class ItemConduit extends AbstractConduit implements IItemConduit {
network.routesChanged();
}
}
@Override
public boolean isRoundRobinEnabled(ForgeDirection dir) {
Boolean val = roundRobin.get(dir);
if(val == null) {
return false;
}
return val;
}
@Override
public void setRoundRobinEnabled(ForgeDirection dir, boolean enabled) {
roundRobin.put(dir, enabled);
if(network != null) {
network.routesChanged();
}
}
@Override
public boolean canConnectToExternal(ForgeDirection direction, boolean ignoreDisabled) {
@@ -494,6 +513,12 @@ public class ItemConduit extends AbstractConduit implements IItemConduit {
nbtRoot.setBoolean("selfFeed." + entry.getKey().name(), entry.getValue());
}
}
for (Entry<ForgeDirection, Boolean> entry : roundRobin.entrySet()) {
if(entry.getValue() != null) {
nbtRoot.setBoolean("roundRobin." + entry.getKey().name(), entry.getValue());
}
}
for (Entry<ForgeDirection, DyeColor> entry : inputColors.entrySet()) {
if(entry.getValue() != null) {
@@ -552,6 +577,12 @@ public class ItemConduit extends AbstractConduit implements IItemConduit {
boolean val = nbtRoot.getBoolean(key);
selfFeed.put(dir, val);
}
key = "roundRobin." + dir.name();
if(nbtRoot.hasKey(key)) {
boolean val = nbtRoot.getBoolean(key);
roundRobin.put(dir, val);
}
key = "inSC." + dir.name();
if(nbtRoot.hasKey(key)) {
@@ -1,39 +1,32 @@
package crazypants.enderio.conduit.item;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import crazypants.enderio.Config;
import crazypants.enderio.conduit.AbstractConduitNetwork;
import crazypants.enderio.conduit.ConduitNetworkTickHandler;
import crazypants.enderio.conduit.ConduitNetworkTickHandler.TickListener;
import crazypants.enderio.conduit.ConnectionMode;
import crazypants.enderio.conduit.IConduit;
import crazypants.enderio.conduit.item.ItemConduitNetwork.NetworkedInventory.Target;
import crazypants.enderio.conduit.item.NetworkedInventory.Target;
import crazypants.util.BlockCoord;
import crazypants.util.InventoryWrapper;
import crazypants.util.ItemUtil;
import crazypants.util.Lang;
public class ItemConduitNetwork extends AbstractConduitNetwork<IItemConduit, IItemConduit> {
private long timeAtLastApply;
private final List<NetworkedInventory> inventories = new ArrayList<ItemConduitNetwork.NetworkedInventory>();
private final Map<BlockCoord, List<NetworkedInventory>> invMap = new HashMap<BlockCoord, List<ItemConduitNetwork.NetworkedInventory>>();
final List<NetworkedInventory> inventories = new ArrayList<NetworkedInventory>();
private final Map<BlockCoord, List<NetworkedInventory>> invMap = new HashMap<BlockCoord, List<NetworkedInventory>>();
private final Map<BlockCoord, IItemConduit> conMap = new HashMap<BlockCoord, IItemConduit>();
final Map<BlockCoord, IItemConduit> conMap = new HashMap<BlockCoord, IItemConduit>();
private boolean requiresSort = true;
@@ -68,7 +61,7 @@ public class ItemConduitNetwork extends AbstractConduitNetwork<IItemConduit, IIt
public void inventoryAdded(IItemConduit itemConduit, ForgeDirection direction, int x, int y, int z, IInventory externalInventory) {
BlockCoord bc = new BlockCoord(x, y, z);
NetworkedInventory inv = new NetworkedInventory(externalInventory, itemConduit, direction, bc);
NetworkedInventory inv = new NetworkedInventory(this, externalInventory, itemConduit, direction, bc);
inventories.add(inv);
getOrCreate(bc).add(inv);
requiresSort = true;
@@ -77,7 +70,7 @@ public class ItemConduitNetwork extends AbstractConduitNetwork<IItemConduit, IIt
private List<NetworkedInventory> getOrCreate(BlockCoord bc) {
List<NetworkedInventory> res = invMap.get(bc);
if(res == null) {
res = new ArrayList<ItemConduitNetwork.NetworkedInventory>();
res = new ArrayList<NetworkedInventory>();
invMap.put(bc, res);
}
return res;
@@ -164,7 +157,7 @@ public class ItemConduitNetwork extends AbstractConduitNetwork<IItemConduit, IIt
if(inv.hasTarget(con, dir)) {
ItemFilter f = inv.con.getInputFilter(inv.conDir);
if(input == null || f == null || f.doesItemPassFilter(input)) {
result.add(Lang.localize(inv.inv.getInventoryName(), false) + " " + inv.location);
result.add(Lang.localize(inv.inv.getInventoryName(), false) + " " + inv.location);
}
}
}
@@ -212,337 +205,7 @@ public class ItemConduitNetwork extends AbstractConduitNetwork<IItemConduit, IIt
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
private static int MAX_SLOT_CHECK_PER_TICK = 64;
class NetworkedInventory {
ISidedInventory inv;
IItemConduit con;
ForgeDirection conDir;
BlockCoord location;
int inventorySide;
List<Target> sendPriority = new ArrayList<Target>();
private int extractFromSlot = -1;
int tickDeficit;
//work around for a vanilla chest changing into a double chest without doing unneeded checks all the time
boolean recheckInv = false;
World world;
NetworkedInventory(IInventory inv, IItemConduit con, ForgeDirection conDir, BlockCoord location) {
inventorySide = conDir.getOpposite().ordinal();
this.con = con;
this.conDir = conDir;
this.location = location;
world = con.getBundle().getWorld();
TileEntity te = world.getTileEntity(location.x, location.y, location.z);
if(te.getClass().getName().contains("cpw.mods.ironchest")) {
recheckInv = true;
} else if(te instanceof TileEntityChest) {
recheckInv = true;
}
updateInventory();
}
public boolean hasTarget(IItemConduit conduit, ForgeDirection dir) {
for (Target t : sendPriority) {
if(t.inv.con == conduit && t.inv.conDir == dir) {
return true;
}
}
return false;
}
boolean canExtract() {
ConnectionMode mode = con.getConectionMode(conDir);
return mode == ConnectionMode.INPUT || mode == ConnectionMode.IN_OUT;
}
boolean canInsert() {
ConnectionMode mode = con.getConectionMode(conDir);
return mode == ConnectionMode.OUTPUT || mode == ConnectionMode.IN_OUT;
}
boolean isSticky() {
return con.getOutputFilter(conDir).isValid() && con.getOutputFilter(conDir).isSticky();
}
public void onTick(long tick) {
int transfered;
if(tickDeficit > 0 || !canExtract() || !con.isExtractionRedstoneConditionMet(conDir)) {
//do nothing
} else {
transferItems();
}
tickDeficit--;
if(tickDeficit < -1) {
//Sleep for a second before checking again.
tickDeficit = 20;
}
}
private boolean canExtractThisTick(long tick) {
if(!con.isExtractionRedstoneConditionMet(conDir)) {
return false;
}
return true;
}
private int nextSlot(int numSlots) {
++extractFromSlot;
if(extractFromSlot >= numSlots || extractFromSlot < 0) {
extractFromSlot = 0;
}
return extractFromSlot;
}
private void setNextStartingSlot(int slot) {
extractFromSlot = slot;
extractFromSlot--;
}
private boolean transferItems() {
if(recheckInv) {
updateInventory();
}
int[] slotIndices = inv.getAccessibleSlotsFromSide(inventorySide);
if(slotIndices == null) {
return false;
}
int numSlots = slotIndices.length;
ItemStack extractItem = null;
int maxExtracted = con.getMaximumExtracted();
int slot = -1;
int slotChecksPerTick = Math.min(numSlots, MAX_SLOT_CHECK_PER_TICK);
for (int i = 0; i < slotChecksPerTick; i++) {
int index = nextSlot(numSlots);
slot = slotIndices[index];
ItemStack item = inv.getStackInSlot(slot);
if(canExtractItem(item)) {
extractItem = item.copy();
if(inv.canExtractItem(slot, extractItem, inventorySide)) {
if(doTransfer(extractItem, slot, maxExtracted)) {
setNextStartingSlot(slot);
return true;
}
}
}
}
return false;
}
private boolean canExtractItem(ItemStack itemStack) {
if(itemStack == null) {
return false;
}
ItemFilter filter = con.getInputFilter(conDir);
if(filter == null) {
return true;
}
return filter.doesItemPassFilter(itemStack);
}
private boolean doTransfer(ItemStack extractedItem, int slot, int maxExtract) {
if(extractedItem == null) {
return false;
}
ItemStack toExtract = extractedItem.copy();
toExtract.stackSize = Math.min(maxExtract, toExtract.stackSize);
int numInserted = insertIntoTargets(toExtract);
if(numInserted <= 0) {
return false;
}
ItemStack curStack = inv.getStackInSlot(slot);
if(curStack != null) {
curStack = curStack.copy();
curStack.stackSize -= numInserted;
if(curStack.stackSize > 0) {
inv.setInventorySlotContents(slot, curStack);
inv.markDirty();
} else {
inv.setInventorySlotContents(slot, null);
inv.markDirty();
}
}
con.itemsExtracted(numInserted, slot);
tickDeficit = Math.round(numInserted * con.getTickTimePerItem());
return true;
}
private int insertIntoTargets(ItemStack toExtract) {
if(toExtract == null) {
return 0;
}
int totalToInsert = toExtract.stackSize;
int leftToInsert = totalToInsert;
boolean matchedStickyInput = false;
for (Target target : sendPriority) {
if(target.stickyInput && !matchedStickyInput) {
ItemFilter of = target.inv.con.getOutputFilter(target.inv.conDir);
matchedStickyInput = of.isValid() && of.doesItemPassFilter(toExtract);
}
if(target.stickyInput || !matchedStickyInput) {
if(target.inv.recheckInv) {
target.inv.updateInventory();
}
int inserted = target.inv.insertItem(toExtract);
if(inserted > 0) {
toExtract.stackSize -= inserted;
leftToInsert -= inserted;
}
if(leftToInsert <= 0) {
return totalToInsert;
}
}
}
return totalToInsert - leftToInsert;
}
private void updateInventory() {
TileEntity te = world.getTileEntity(location.x, location.y, location.z);
if(te instanceof ISidedInventory) {
this.inv = (ISidedInventory) te;
} else if(te instanceof IInventory) {
inv = new InventoryWrapper((IInventory) te);
}
}
private int insertItem(ItemStack item) {
if(!canInsert() || item == null) {
return 0;
}
ItemFilter filter = con.getOutputFilter(conDir);
if(filter != null) {
if(!filter.doesItemPassFilter(item)) {
return 0;
}
}
return ItemUtil.doInsertItem(inv, item, ForgeDirection.values()[inventorySide]);
}
void updateInsertOrder() {
sendPriority.clear();
if(!canExtract()) {
return;
}
List<Target> result = new ArrayList<ItemConduitNetwork.NetworkedInventory.Target>();
for (NetworkedInventory other : inventories) {
if((con.isSelfFeedEnabled(conDir) || (other != this))
&& other.canInsert()
&& con.getInputColor(conDir) == other.con.getOutputColor(other.conDir)) {
if(Config.itemConduitUsePhyscialDistance) {
sendPriority.add(new Target(other, distanceTo(other), other.isSticky()));
} else {
result.add(new Target(other, 9999999, other.isSticky()));
}
}
}
if(Config.itemConduitUsePhyscialDistance) {
Collections.sort(sendPriority);
} else {
if(!result.isEmpty()) {
Map<BlockCoord, Integer> visited = new HashMap<BlockCoord, Integer>();
List<BlockCoord> steps = new ArrayList<BlockCoord>();
steps.add(con.getLocation());
calculateDistances(result, visited, steps, 0);
sendPriority.addAll(result);
Collections.sort(sendPriority);
}
}
}
private void calculateDistances(List<Target> targets, Map<BlockCoord, Integer> visited, List<BlockCoord> steps, int distance) {
if(steps == null || steps.isEmpty()) {
return;
}
ArrayList<BlockCoord> nextSteps = new ArrayList<BlockCoord>();
for (BlockCoord bc : steps) {
IItemConduit con = conMap.get(bc);
if(con != null) {
for (ForgeDirection dir : con.getExternalConnections()) {
Target target = getTarget(targets, con, dir);
if(target != null && target.distance > distance) {
target.distance = distance;
}
}
if(!visited.containsKey(bc)) {
visited.put(bc, distance);
} else {
int prevDist = visited.get(bc);
if(prevDist <= distance) {
continue;
}
visited.put(bc, distance);
}
for (ForgeDirection dir : con.getConduitConnections()) {
nextSteps.add(bc.getLocation(dir));
}
}
}
calculateDistances(targets, visited, nextSteps, distance + 1);
}
private Target getTarget(List<Target> targets, IItemConduit con, ForgeDirection dir) {
for (Target target : targets) {
if(target.inv.conDir == dir && target.inv.con.getLocation().equals(con.getLocation())) {
return target;
}
}
return null;
}
private int distanceTo(NetworkedInventory other) {
return con.getLocation().distanceSquared(other.con.getLocation());
}
class Target implements Comparable<Target> {
NetworkedInventory inv;
int distance;
boolean stickyInput;
Target(NetworkedInventory inv, int distance, boolean stickyInput) {
this.inv = inv;
this.distance = distance;
this.stickyInput = stickyInput;
}
@Override
public int compareTo(Target o) {
if(stickyInput && !o.stickyInput) {
return -1;
}
if(!stickyInput && o.stickyInput) {
return 1;
}
return compare(distance, o.distance);
}
}
}
static int MAX_SLOT_CHECK_PER_TICK = 64;
private class InnerTickHandler implements TickListener {
@@ -0,0 +1,395 @@
package crazypants.enderio.conduit.item;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import crazypants.enderio.Config;
import crazypants.enderio.conduit.ConnectionMode;
import crazypants.util.BlockCoord;
import crazypants.util.InventoryWrapper;
import crazypants.util.ItemUtil;
class NetworkedInventory {
ISidedInventory inv;
IItemConduit con;
ForgeDirection conDir;
BlockCoord location;
int inventorySide;
List<Target> sendPriority = new ArrayList<Target>();
RoundRobinIterable rrIter = new RoundRobinIterable();
private int extractFromSlot = -1;
int tickDeficit;
//work around for a vanilla chest changing into a double chest without doing unneeded checks all the time
boolean recheckInv = false;
World world;
ItemConduitNetwork network;
NetworkedInventory(ItemConduitNetwork network, IInventory inv, IItemConduit con, ForgeDirection conDir, BlockCoord location) {
this.network = network;
inventorySide = conDir.getOpposite().ordinal();
this.con = con;
this.conDir = conDir;
this.location = location;
world = con.getBundle().getWorld();
TileEntity te = world.getTileEntity(location.x, location.y, location.z);
if(te.getClass().getName().contains("cpw.mods.ironchest")) {
recheckInv = true;
} else if(te instanceof TileEntityChest) {
recheckInv = true;
}
updateInventory();
}
public boolean hasTarget(IItemConduit conduit, ForgeDirection dir) {
for (Target t : sendPriority) {
if(t.inv.con == conduit && t.inv.conDir == dir) {
return true;
}
}
return false;
}
boolean canExtract() {
ConnectionMode mode = con.getConectionMode(conDir);
return mode == ConnectionMode.INPUT || mode == ConnectionMode.IN_OUT;
}
boolean canInsert() {
ConnectionMode mode = con.getConectionMode(conDir);
return mode == ConnectionMode.OUTPUT || mode == ConnectionMode.IN_OUT;
}
boolean isSticky() {
return con.getOutputFilter(conDir).isValid() && con.getOutputFilter(conDir).isSticky();
}
public void onTick(long tick) {
int transfered;
if(tickDeficit > 0 || !canExtract() || !con.isExtractionRedstoneConditionMet(conDir)) {
//do nothing
} else {
transferItems();
}
tickDeficit--;
if(tickDeficit < -1) {
//Sleep for a second before checking again.
tickDeficit = 20;
}
}
private boolean canExtractThisTick(long tick) {
if(!con.isExtractionRedstoneConditionMet(conDir)) {
return false;
}
return true;
}
private int nextSlot(int numSlots) {
++extractFromSlot;
if(extractFromSlot >= numSlots || extractFromSlot < 0) {
extractFromSlot = 0;
}
return extractFromSlot;
}
private void setNextStartingSlot(int slot) {
extractFromSlot = slot;
extractFromSlot--;
}
private boolean transferItems() {
if(recheckInv) {
updateInventory();
}
int[] slotIndices = inv.getAccessibleSlotsFromSide(inventorySide);
if(slotIndices == null) {
return false;
}
int numSlots = slotIndices.length;
ItemStack extractItem = null;
int maxExtracted = con.getMaximumExtracted();
int slot = -1;
int slotChecksPerTick = Math.min(numSlots, ItemConduitNetwork.MAX_SLOT_CHECK_PER_TICK);
for (int i = 0; i < slotChecksPerTick; i++) {
int index = nextSlot(numSlots);
slot = slotIndices[index];
ItemStack item = inv.getStackInSlot(slot);
if(canExtractItem(item)) {
extractItem = item.copy();
if(inv.canExtractItem(slot, extractItem, inventorySide)) {
if(doTransfer(extractItem, slot, maxExtracted)) {
setNextStartingSlot(slot);
return true;
}
}
}
}
return false;
}
private boolean canExtractItem(ItemStack itemStack) {
if(itemStack == null) {
return false;
}
ItemFilter filter = con.getInputFilter(conDir);
if(filter == null) {
return true;
}
return filter.doesItemPassFilter(itemStack);
}
private boolean doTransfer(ItemStack extractedItem, int slot, int maxExtract) {
if(extractedItem == null) {
return false;
}
ItemStack toExtract = extractedItem.copy();
toExtract.stackSize = Math.min(maxExtract, toExtract.stackSize);
int numInserted = insertIntoTargets(toExtract);
if(numInserted <= 0) {
return false;
}
ItemStack curStack = inv.getStackInSlot(slot);
if(curStack != null) {
curStack = curStack.copy();
curStack.stackSize -= numInserted;
if(curStack.stackSize > 0) {
inv.setInventorySlotContents(slot, curStack);
inv.markDirty();
} else {
inv.setInventorySlotContents(slot, null);
inv.markDirty();
}
}
con.itemsExtracted(numInserted, slot);
tickDeficit = Math.round(numInserted * con.getTickTimePerItem());
return true;
}
int insertIntoTargets(ItemStack toExtract) {
if(toExtract == null) {
return 0;
}
int totalToInsert = toExtract.stackSize;
int leftToInsert = totalToInsert;
boolean matchedStickyInput = false;
Iterable<Target> targets = getTargetIterator();
//for (Target target : sendPriority) {
for (Target target : targets) {
if(target.stickyInput && !matchedStickyInput) {
ItemFilter of = target.inv.con.getOutputFilter(target.inv.conDir);
matchedStickyInput = of.isValid() && of.doesItemPassFilter(toExtract);
}
if(target.stickyInput || !matchedStickyInput) {
if(target.inv.recheckInv) {
target.inv.updateInventory();
}
int inserted = target.inv.insertItem(toExtract);
if(inserted > 0) {
toExtract.stackSize -= inserted;
leftToInsert -= inserted;
}
if(leftToInsert <= 0) {
return totalToInsert;
}
}
}
return totalToInsert - leftToInsert;
}
private Iterable<Target> getTargetIterator() {
if(con.isRoundRobinEnabled(conDir)) {
return rrIter;
}
return sendPriority;
}
private void updateInventory() {
TileEntity te = world.getTileEntity(location.x, location.y, location.z);
if(te instanceof ISidedInventory) {
this.inv = (ISidedInventory) te;
} else if(te instanceof IInventory) {
inv = new InventoryWrapper((IInventory) te);
}
}
private int insertItem(ItemStack item) {
if(!canInsert() || item == null) {
return 0;
}
ItemFilter filter = con.getOutputFilter(conDir);
if(filter != null) {
if(!filter.doesItemPassFilter(item)) {
return 0;
}
}
return ItemUtil.doInsertItem(inv, item, ForgeDirection.values()[inventorySide]);
}
void updateInsertOrder() {
sendPriority.clear();
if(!canExtract()) {
return;
}
List<Target> result = new ArrayList<NetworkedInventory.Target>();
for (NetworkedInventory other : network.inventories) {
if((con.isSelfFeedEnabled(conDir) || (other != this))
&& other.canInsert()
&& con.getInputColor(conDir) == other.con.getOutputColor(other.conDir)) {
if(Config.itemConduitUsePhyscialDistance) {
sendPriority.add(new Target(other, distanceTo(other), other.isSticky()));
} else {
result.add(new Target(other, 9999999, other.isSticky()));
}
}
}
if(Config.itemConduitUsePhyscialDistance) {
Collections.sort(sendPriority);
} else {
if(!result.isEmpty()) {
Map<BlockCoord, Integer> visited = new HashMap<BlockCoord, Integer>();
List<BlockCoord> steps = new ArrayList<BlockCoord>();
steps.add(con.getLocation());
calculateDistances(result, visited, steps, 0);
sendPriority.addAll(result);
Collections.sort(sendPriority);
}
}
}
private void calculateDistances(List<Target> targets, Map<BlockCoord, Integer> visited, List<BlockCoord> steps, int distance) {
if(steps == null || steps.isEmpty()) {
return;
}
ArrayList<BlockCoord> nextSteps = new ArrayList<BlockCoord>();
for (BlockCoord bc : steps) {
IItemConduit con = network.conMap.get(bc);
if(con != null) {
for (ForgeDirection dir : con.getExternalConnections()) {
Target target = getTarget(targets, con, dir);
if(target != null && target.distance > distance) {
target.distance = distance;
}
}
if(!visited.containsKey(bc)) {
visited.put(bc, distance);
} else {
int prevDist = visited.get(bc);
if(prevDist <= distance) {
continue;
}
visited.put(bc, distance);
}
for (ForgeDirection dir : con.getConduitConnections()) {
nextSteps.add(bc.getLocation(dir));
}
}
}
calculateDistances(targets, visited, nextSteps, distance + 1);
}
private Target getTarget(List<Target> targets, IItemConduit con, ForgeDirection dir) {
for (Target target : targets) {
if(target.inv.conDir == dir && target.inv.con.getLocation().equals(con.getLocation())) {
return target;
}
}
return null;
}
private int distanceTo(NetworkedInventory other) {
return con.getLocation().distanceSquared(other.con.getLocation());
}
class RoundRobinIterable implements Iterable<Target>, Iterator<Target> {
int index = -1;
int currentCount = 0;
@Override
public Iterator<Target> iterator() {
currentCount = 0;
return this;
}
@Override
public boolean hasNext() {
return currentCount <= sendPriority.size();
}
@Override
public Target next() {
currentCount++;
index++;
if(index >= sendPriority.size()) {
index = 0;
}
return sendPriority.get(index);
}
@Override
public void remove() {
}
}
static class Target implements Comparable<Target> {
NetworkedInventory inv;
int distance;
boolean stickyInput;
Target(NetworkedInventory inv, int distance, boolean stickyInput) {
this.inv = inv;
this.distance = distance;
this.stickyInput = stickyInput;
}
@Override
public int compareTo(Target o) {
if(stickyInput && !o.stickyInput) {
return -1;
}
if(!stickyInput && o.stickyInput) {
return 1;
}
return ItemConduitNetwork.compare(distance, o.distance);
}
}
}
@@ -14,6 +14,7 @@ public class PacketItemConduitFilter extends AbstractConduitPacket<IItemConduit>
private ForgeDirection dir;
private boolean loopMode;
private boolean roundRobin;
private DyeColor colIn;
private DyeColor colOut;
@@ -27,6 +28,7 @@ public class PacketItemConduitFilter extends AbstractConduitPacket<IItemConduit>
super(con.getBundle().getEntity(), ConTypeEnum.ITEM);
this.dir = dir;
loopMode = con.isSelfFeedEnabled(dir);
roundRobin = con.isRoundRobinEnabled(dir);
colIn = con.getInputColor(dir);
colOut = con.getOutputColor(dir);
@@ -39,6 +41,7 @@ public class PacketItemConduitFilter extends AbstractConduitPacket<IItemConduit>
super.encode(ctx, buf);
buf.writeShort(dir.ordinal());
buf.writeBoolean(loopMode);
buf.writeBoolean(roundRobin);
buf.writeShort(colIn.ordinal());
buf.writeShort(colOut.ordinal());
writeFilter(buf, inputFilter);
@@ -58,6 +61,7 @@ public class PacketItemConduitFilter extends AbstractConduitPacket<IItemConduit>
super.decode(ctx, buf);
dir = ForgeDirection.values()[buf.readShort()];
loopMode = buf.readBoolean();
roundRobin = buf.readBoolean();
colIn = DyeColor.values()[buf.readShort()];
colOut = DyeColor.values()[buf.readShort()];
inputFilter = readFilter(buf);
@@ -78,6 +82,7 @@ public class PacketItemConduitFilter extends AbstractConduitPacket<IItemConduit>
@Override
protected void handleServerSide(EntityPlayer player, World worldObj, IConduitBundle tile, IItemConduit conduit) {
conduit.setSelfFeedEnabled(dir, loopMode);
conduit.setRoundRobinEnabled(dir, roundRobin);
conduit.setInputColor(dir, colIn);
conduit.setOutputColor(dir, colOut);
@@ -74,8 +74,11 @@ public final class IconEIO {
public static final IconEIO OUTPUT_OFF = new IconEIO(188, 197 + 32, 30, 14);
public static final IconEIO INPUT_OUTPUT_OFF = new IconEIO(218 + 32, 180, 30, 8);
public static final IconEIO LOOP = new IconEIO(86, 236, 20, 20);
public static final IconEIO LOOP_OFF = new IconEIO(106, 236, 20, 20);
public static final IconEIO LOOP = new IconEIO(217, 236, 20, 20);
public static final IconEIO LOOP_OFF = new IconEIO(236, 236, 20, 20);
public static final IconEIO ROUND_ROBIN = new IconEIO(86, 236, 20, 20);
public static final IconEIO ROUND_ROBIN_OFF = new IconEIO(106, 236, 20, 20);
public static final IconEIO IO_CONFIG_UP = new IconEIO(80, 116, 24, 24);
public static final IconEIO IO_CONFIG_DOWN = new IconEIO(104, 116, 24, 24);