First cut of items working.

This commit is contained in:
CrazyPants committed 2014-10-11 20:05:30 +08:00
1 parent f76961ad43
commit ae89cd4af8
3 files changed
+105 -14

No files matched your search

@@ -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<TileTransceiver> getIterator(Channel channel) {
RoundRobinIterator<TileTransceiver> res = iterators.get(channel);
if(res == null) {
res = new RoundRobinIterator<TileTransceiver>(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<TileTransceiver> getIterator(Channel channel) {
RoundRobinIterator<TileTransceiver> res = iterators.get(channel);
if(res == null) {
res = new RoundRobinIterator<TileTransceiver>(transceivers);
iterators.put(channel, res);
}
return res;
}
//Fluid
public FluidTankInfo[] getTankInfoForChannels(TileTransceiver tileTransceiver, List<Channel> 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<TileTransceiver> 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<Channel> channels, int slot, ItemStack contents) {
if(!from.hasPower()) {
return;
}
for (Channel channel : channels) {
RoundRobinIterator<TileTransceiver> 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;
}
}
@@ -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<Channel> 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);
}
}
}
}
}
@@ -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);
}
}