diff --git a/src/main/java/crazypants/enderio/machine/transceiver/ServerChannelRegister.java b/src/main/java/crazypants/enderio/machine/transceiver/ServerChannelRegister.java index b9cfe2f2..2c64b295 100644 --- a/src/main/java/crazypants/enderio/machine/transceiver/ServerChannelRegister.java +++ b/src/main/java/crazypants/enderio/machine/transceiver/ServerChannelRegister.java @@ -13,8 +13,11 @@ import com.google.gson.stream.JsonWriter; import crazypants.enderio.Log; import crazypants.enderio.config.Config; +import crazypants.enderio.machine.SlotDefinition; +import crazypants.util.ItemUtil; import crazypants.util.RoundRobinIterator; +import net.minecraft.item.ItemStack; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.Fluid; @@ -114,6 +117,15 @@ public class ServerChannelRegister extends ChannelRegister { iterators.remove(channel); } + private RoundRobinIterator getIterator(Channel channel) { + RoundRobinIterator res = iterators.get(channel); + if(res == null) { + res = new RoundRobinIterator(transceivers); + iterators.put(channel, res); + } + return res; + } + //Power public void sendPower(TileTransceiver sender, int canSend, Channel channel) { @@ -131,15 +143,6 @@ public class ServerChannelRegister extends ChannelRegister { } } - private RoundRobinIterator getIterator(Channel channel) { - RoundRobinIterator res = iterators.get(channel); - if(res == null) { - res = new RoundRobinIterator(transceivers); - iterators.put(channel, res); - } - return res; - } - //Fluid public FluidTankInfo[] getTankInfoForChannels(TileTransceiver tileTransceiver, List channels) { @@ -167,22 +170,78 @@ public class ServerChannelRegister extends ChannelRegister { if(resource == null || !from.hasPower()) { return 0; } - for(Channel channel : list) { + for (Channel channel : list) { RoundRobinIterator iter = getIterator(channel); - for(TileTransceiver trans : iter) { + for (TileTransceiver trans : iter) { if(trans != from) { int val = trans.recieveFluid(list, resource, doFill); if(val > 0) { - if(doFill && Config.transceiverBucketTransmissionCostRF > 0) { - int powerUsed = (int)Math.max(1, Config.transceiverBucketTransmissionCostRF * val/1000d); + if(doFill && Config.transceiverBucketTransmissionCostRF > 0) { + int powerUsed = (int) Math.max(1, Config.transceiverBucketTransmissionCostRF * val / 1000d); from.usePower(powerUsed); } return val; - } + } } } } return 0; } + //Item + + public void sendItem(TileTransceiver from, List channels, int slot, ItemStack contents) { + if(!from.hasPower()) { + return; + } + for (Channel channel : channels) { + RoundRobinIterator iter = getIterator(channel); + for (TileTransceiver trans : iter) { + if(trans != from && trans.getRecieveChannels(ChannelType.ITEM).contains(channel)) { + contents = sendItem(from, slot, contents, trans); + if(contents == null) { + return; + } + } + } + } + } + + private ItemStack sendItem(TileTransceiver from, int slot, ItemStack contents, TileTransceiver to) { + SlotDefinition sd = to.getSlotDefinition(); + //try merging into existing stacks + for (int i = sd.minOutputSlot; i <= sd.maxOutputSlot; i++) { + ItemStack existing = to.getStackInSlot(i); + if(ItemUtil.canMergeStacks(existing, contents)) { + int numCanMerge = existing.getMaxStackSize() - existing.stackSize; + ItemStack remaining; + if(numCanMerge >= contents.stackSize) { + remaining = null; + } else { + remaining = contents.copy(); + remaining.stackSize -= numCanMerge; + } + ItemStack destStack = existing.copy(); + destStack.stackSize += numCanMerge; + to.setInventorySlotContents(i, destStack); + from.setInventorySlotContents(slot, remaining); + if(remaining == null) { + return null; + } else { + contents = remaining.copy(); + } + } + } + //then fill empty stack + for (int i = sd.minOutputSlot; i <= sd.maxOutputSlot; i++) { + ItemStack existing = to.getStackInSlot(i); + if(existing == null) { + to.setInventorySlotContents(i, contents.copy()); + from.setInventorySlotContents(slot, null); + return null; + } + } + return contents; + } + } diff --git a/src/main/java/crazypants/enderio/machine/transceiver/TileTransceiver.java b/src/main/java/crazypants/enderio/machine/transceiver/TileTransceiver.java index a393e5b6..58a7132f 100644 --- a/src/main/java/crazypants/enderio/machine/transceiver/TileTransceiver.java +++ b/src/main/java/crazypants/enderio/machine/transceiver/TileTransceiver.java @@ -67,7 +67,9 @@ public class TileTransceiver extends AbstractPoweredTaskEntity implements IFluid return res; } + //NB: Fluid done synchronously processPower(); + processItems(); return res; } @@ -418,4 +420,18 @@ public class TileTransceiver extends AbstractPoweredTaskEntity implements IFluid return false; } + //item handling + private void processItems() { + List sendItemChannels = getSendChannels(ChannelType.ITEM); + if(!sendItemChannels.isEmpty()) { + for (int i = slotDefinition.minInputSlot; i <= slotDefinition.maxInputSlot; i++) { + ItemStack toSend = getStackInSlot(i); + if(toSend != null) { + ServerChannelRegister.instance.sendItem(this, sendItemChannels, i, toSend); + } + } + } + + } + } diff --git a/src/main/java/crazypants/util/ItemUtil.java b/src/main/java/crazypants/util/ItemUtil.java index a216460b..9d57c9a7 100644 --- a/src/main/java/crazypants/util/ItemUtil.java +++ b/src/main/java/crazypants/util/ItemUtil.java @@ -164,4 +164,20 @@ public class ItemUtil { return inv; } + /** + * Checks if items, damage and NBT are equal. + * @param s1 + * @param s2 + * @return + */ + public static boolean canMergeStacks(ItemStack s1, ItemStack s2) { + if(s1 == null || s2 == null || !s1.isStackable() || !s2.isStackable()) { + return false; + } + if(!s1.isItemEqual(s2)) { + return false; + } + return ItemStack.areItemStackTagsEqual(s1, s2); + } + }