Rewrote the item distributor to be less broken

This commit is contained in:
Ellpeck 2016-09-11 12:57:20 +02:00
parent 890a4b91ff
commit 9cac45a514

View file

@ -17,10 +17,13 @@ import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import java.util.HashMap;
import java.util.Map;
public class TileEntityDistributorItem extends TileEntityInventoryBase{ public class TileEntityDistributorItem extends TileEntityInventoryBase{
private int sidePutTo; private int putSide;
private int lastSlotAmount; private final Map<EnumFacing, IItemHandler> handlersAround = new HashMap<EnumFacing, IItemHandler>();
public TileEntityDistributorItem(){ public TileEntityDistributorItem(){
super(1, "distributorItem"); super(1, "distributorItem");
@ -31,47 +34,74 @@ public class TileEntityDistributorItem extends TileEntityInventoryBase{
super.updateEntity(); super.updateEntity();
if(!this.worldObj.isRemote){ if(!this.worldObj.isRemote){
if(this.slots[0] != null){ if(!this.handlersAround.isEmpty() && this.slots[0] != null){
TileEntity tile = this.tilesAround[this.sidePutTo]; EnumFacing[] allFacings = EnumFacing.values();
do{
this.putSide++;
if(tile != null){ if(this.putSide >= 6){
EnumFacing side = EnumFacing.values()[this.sidePutTo].getOpposite(); this.putSide = 0;
if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)){
IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
if(cap != null){
int amountPer = this.slots[0].stackSize/5;
if(amountPer <= 0){
amountPer = this.slots[0].stackSize;
} }
ItemStack stackToPut = this.slots[0].copy(); }
stackToPut.stackSize = amountPer; while(!this.handlersAround.containsKey(allFacings[this.putSide]));
for(int i = 0; i < cap.getSlots(); i++){ EnumFacing putFacing = allFacings[this.putSide];
stackToPut = cap.insertItem(i, stackToPut.copy(), false); IItemHandler handler = this.handlersAround.get(putFacing);
if(stackToPut == null){ if(handler != null){
this.slots[0].stackSize -= amountPer; boolean shouldMarkDirty = false;
int amount = this.slots[0].stackSize/this.handlersAround.size();
if(amount <= 0){
amount = this.slots[0].stackSize;
}
if(amount > 0){
ItemStack toInsert = this.slots[0].copy();
toInsert.stackSize = amount;
for(int i = 0; i < handler.getSlots(); i++){
ItemStack notInserted = handler.insertItem(i, toInsert.copy(), false);
if(notInserted == null){
this.slots[0].stackSize -= amount;
shouldMarkDirty = true;
break; break;
} }
else{ else if(notInserted.stackSize != this.slots[0].stackSize){
this.slots[0].stackSize -= stackToPut.stackSize; this.slots[0].stackSize -= notInserted.stackSize;
toInsert = notInserted;
shouldMarkDirty = true;
}
}
if(this.slots[0].stackSize <= 0){
this.slots[0] = null;
shouldMarkDirty = true;
}
if(shouldMarkDirty){
this.markDirty();
}
} }
} }
} }
} }
} }
this.sidePutTo++; @Override
if(this.sidePutTo == 1){ public void saveAllHandlersAround(){
this.sidePutTo++; this.handlersAround.clear();
}
else if(this.sidePutTo >= 6){
this.sidePutTo = 0;
}
}
int stackSize = this.slots[0] == null ? 0 : this.slots[0].stackSize; for(EnumFacing side : EnumFacing.values()){
if(stackSize != this.lastSlotAmount && this.sendUpdateWithInterval()){ if(side != EnumFacing.UP){
this.lastSlotAmount = stackSize; TileEntity tile = this.worldObj.getTileEntity(this.pos.offset(side));
if(tile != null && tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite())){
IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
if(cap != null){
this.handlersAround.put(side, cap);
}
}
} }
} }
} }
@ -79,13 +109,11 @@ public class TileEntityDistributorItem extends TileEntityInventoryBase{
@Override @Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type); super.writeSyncableNBT(compound, type);
compound.setInteger("PutSide", this.sidePutTo);
} }
@Override @Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){ public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type); super.readSyncableNBT(compound, type);
this.sidePutTo = compound.getInteger("PutSide");
} }
@Override @Override
@ -93,6 +121,12 @@ public class TileEntityDistributorItem extends TileEntityInventoryBase{
return true; return true;
} }
@Override
public void markDirty(){
super.markDirty();
this.sendUpdate();
}
@Override @Override
public boolean shouldSaveHandlersAround(){ public boolean shouldSaveHandlersAround(){
return true; return true;