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.IItemHandler;
import java.util.HashMap;
import java.util.Map;
public class TileEntityDistributorItem extends TileEntityInventoryBase{
private int sidePutTo;
private int lastSlotAmount;
private int putSide;
private final Map<EnumFacing, IItemHandler> handlersAround = new HashMap<EnumFacing, IItemHandler>();
public TileEntityDistributorItem(){
super(1, "distributorItem");
@ -31,47 +34,74 @@ public class TileEntityDistributorItem extends TileEntityInventoryBase{
super.updateEntity();
if(!this.worldObj.isRemote){
if(this.slots[0] != null){
TileEntity tile = this.tilesAround[this.sidePutTo];
if(!this.handlersAround.isEmpty() && this.slots[0] != null){
EnumFacing[] allFacings = EnumFacing.values();
do{
this.putSide++;
if(tile != null){
EnumFacing side = EnumFacing.values()[this.sidePutTo].getOpposite();
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;
if(this.putSide >= 6){
this.putSide = 0;
}
}
while(!this.handlersAround.containsKey(allFacings[this.putSide]));
for(int i = 0; i < cap.getSlots(); i++){
stackToPut = cap.insertItem(i, stackToPut.copy(), false);
if(stackToPut == null){
this.slots[0].stackSize -= amountPer;
break;
}
else{
this.slots[0].stackSize -= stackToPut.stackSize;
}
EnumFacing putFacing = allFacings[this.putSide];
IItemHandler handler = this.handlersAround.get(putFacing);
if(handler != null){
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;
}
else if(notInserted.stackSize != this.slots[0].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++;
if(this.sidePutTo == 1){
this.sidePutTo++;
}
else if(this.sidePutTo >= 6){
this.sidePutTo = 0;
}
}
}
}
int stackSize = this.slots[0] == null ? 0 : this.slots[0].stackSize;
if(stackSize != this.lastSlotAmount && this.sendUpdateWithInterval()){
this.lastSlotAmount = stackSize;
@Override
public void saveAllHandlersAround(){
this.handlersAround.clear();
for(EnumFacing side : EnumFacing.values()){
if(side != EnumFacing.UP){
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
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
compound.setInteger("PutSide", this.sidePutTo);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
this.sidePutTo = compound.getInteger("PutSide");
}
@Override
@ -93,6 +121,12 @@ public class TileEntityDistributorItem extends TileEntityInventoryBase{
return true;
}
@Override
public void markDirty(){
super.markDirty();
this.sendUpdate();
}
@Override
public boolean shouldSaveHandlersAround(){
return true;