2015-03-19 21:27:56 +01:00
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
2015-04-19 01:50:02 +02:00
|
|
|
import ellpeck.actuallyadditions.util.WorldUtil;
|
2015-03-19 21:27:56 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
2015-03-29 15:29:05 +02:00
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2015-03-19 21:27:56 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
|
|
|
|
public class TileEntityInputter extends TileEntityInventoryBase{
|
|
|
|
|
2015-05-04 17:26:50 +02:00
|
|
|
public static class TileEntityInputterAdvanced extends TileEntityInputter{
|
|
|
|
|
|
|
|
public TileEntityInputterAdvanced(){
|
|
|
|
super(13, "inputterAdvanced");
|
|
|
|
this.isAdvanced = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-04 05:20:19 +02:00
|
|
|
public static final int PUT_FILTER_START = 1;
|
|
|
|
public static final int PULL_FILTER_START = 7;
|
|
|
|
|
2015-03-19 21:27:56 +01:00
|
|
|
public int sideToPut = -1;
|
|
|
|
public int slotToPut = -1;
|
|
|
|
public int placeToPutSlotAmount;
|
|
|
|
public TileEntity placeToPut;
|
|
|
|
|
|
|
|
public int sideToPull = -1;
|
|
|
|
public int slotToPull = -1;
|
|
|
|
public int placeToPullSlotAmount;
|
|
|
|
public TileEntity placeToPull;
|
|
|
|
|
2015-04-04 05:20:19 +02:00
|
|
|
public boolean isAdvanced;
|
|
|
|
|
2015-05-04 17:26:50 +02:00
|
|
|
public TileEntityInputter(int slots, String name){
|
|
|
|
super(slots, name);
|
2015-04-04 05:20:19 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:26:50 +02:00
|
|
|
public TileEntityInputter(){
|
|
|
|
super(1, "inputter");
|
|
|
|
this.isAdvanced = false;
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
if(!worldObj.isRemote){
|
|
|
|
this.initVars();
|
|
|
|
|
2015-04-04 05:20:19 +02:00
|
|
|
if(!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)){
|
|
|
|
if(!(this.sideToPull == this.sideToPut && this.slotToPull == this.slotToPut)){
|
|
|
|
if(sideToPull != -1) this.pull();
|
|
|
|
if(sideToPut != -1) this.put();
|
|
|
|
}
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void pull(){
|
|
|
|
if(this.placeToPullSlotAmount > 0){
|
|
|
|
IInventory theInventory = (IInventory)placeToPull;
|
|
|
|
int theSlotToPull = this.slotToPull;
|
2015-03-29 15:29:05 +02:00
|
|
|
int maxSize = theInventory.getInventoryStackLimit();
|
|
|
|
ISidedInventory theSided = null;
|
|
|
|
if(theInventory instanceof ISidedInventory) theSided = (ISidedInventory)theInventory;
|
2015-03-19 21:27:56 +01:00
|
|
|
|
|
|
|
ItemStack theStack = null;
|
2015-03-29 15:29:05 +02:00
|
|
|
for(int i = (theSlotToPull != -1 ? theSlotToPull : 0); i < (theSlotToPull != -1 ? theSlotToPull+1 : placeToPullSlotAmount); i++){
|
|
|
|
ItemStack tempStack = theInventory.getStackInSlot(i);
|
|
|
|
if(tempStack != null){
|
|
|
|
if(tempStack.getMaxStackSize() < this.getInventoryStackLimit()) maxSize = tempStack.getMaxStackSize();
|
|
|
|
else maxSize = this.getInventoryStackLimit();
|
|
|
|
}
|
2015-04-04 05:20:19 +02:00
|
|
|
if(tempStack != null && (this.slots[0] == null || (tempStack.isItemEqual(this.slots[0]) && this.slots[0].stackSize < maxSize)) && this.checkFilters(tempStack, true)){
|
2015-03-29 15:29:05 +02:00
|
|
|
if(theSided != null){
|
|
|
|
for(int j = 0; j < 5; j++){
|
|
|
|
if(theSided.canExtractItem(i, tempStack, j)){
|
|
|
|
theStack = tempStack;
|
|
|
|
theSlotToPull = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2015-03-19 21:27:56 +01:00
|
|
|
theStack = tempStack;
|
|
|
|
theSlotToPull = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
if(theStack != null){
|
2015-03-19 21:27:56 +01:00
|
|
|
if(this.slots[0] != null){
|
|
|
|
if(theStack.isItemEqual(this.slots[0])){
|
2015-03-29 15:29:05 +02:00
|
|
|
if(theStack.stackSize <= maxSize - this.slots[0].stackSize){
|
2015-03-19 21:27:56 +01:00
|
|
|
this.slots[0].stackSize += theStack.stackSize;
|
|
|
|
theInventory.setInventorySlotContents(theSlotToPull, null);
|
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
else if(theStack.stackSize > maxSize - this.slots[0].stackSize){
|
|
|
|
theInventory.decrStackSize(theSlotToPull, maxSize - this.slots[0].stackSize);
|
|
|
|
this.slots[0].stackSize = maxSize;
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2015-03-20 18:58:31 +01:00
|
|
|
ItemStack toBePut = theStack.copy();
|
2015-03-29 15:29:05 +02:00
|
|
|
if(maxSize < toBePut.stackSize) toBePut.stackSize = maxSize;
|
2015-03-20 18:58:31 +01:00
|
|
|
this.setInventorySlotContents(0, toBePut);
|
|
|
|
if(theStack.stackSize == toBePut.stackSize) theInventory.setInventorySlotContents(theSlotToPull, null);
|
2015-03-29 15:29:05 +02:00
|
|
|
else theInventory.decrStackSize(theSlotToPull, toBePut.stackSize);
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void put(){
|
|
|
|
if(this.placeToPutSlotAmount > 0){
|
|
|
|
IInventory theInventory = (IInventory)placeToPut;
|
|
|
|
int theSlotToPut = this.slotToPut;
|
2015-03-29 15:29:05 +02:00
|
|
|
int maxSize = theInventory.getInventoryStackLimit();
|
|
|
|
ISidedInventory theSided = null;
|
|
|
|
if(theInventory instanceof ISidedInventory) theSided = (ISidedInventory)theInventory;
|
|
|
|
boolean can = false;
|
2015-03-19 21:27:56 +01:00
|
|
|
|
|
|
|
if(this.slots[0] != null){
|
|
|
|
ItemStack theStack = null;
|
2015-03-29 15:29:05 +02:00
|
|
|
for(int i = (theSlotToPut != -1 ? theSlotToPut : 0); i < (theSlotToPut != -1 ? theSlotToPut+1 : placeToPutSlotAmount); i++){
|
|
|
|
ItemStack tempStack = theInventory.getStackInSlot(i);
|
|
|
|
if(tempStack != null){
|
|
|
|
if(tempStack.getMaxStackSize() < theInventory.getInventoryStackLimit()) maxSize = tempStack.getMaxStackSize();
|
|
|
|
else maxSize = theInventory.getInventoryStackLimit();
|
|
|
|
}
|
2015-04-04 05:20:19 +02:00
|
|
|
if((tempStack == null || (theInventory.isItemValidForSlot(i, this.slots[0]) && tempStack.isItemEqual(this.slots[0]) && tempStack.stackSize < maxSize)) && this.checkFilters(this.slots[0], false)){
|
2015-03-29 15:29:05 +02:00
|
|
|
if(theSided != null){
|
|
|
|
for(int j = 0; j < 5; j++){
|
|
|
|
if(theSided.canInsertItem(i, this.slots[0], j)){
|
|
|
|
theStack = tempStack;
|
|
|
|
theSlotToPut = i;
|
|
|
|
can = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2015-03-19 21:27:56 +01:00
|
|
|
theStack = tempStack;
|
|
|
|
theSlotToPut = i;
|
2015-03-29 15:29:05 +02:00
|
|
|
can = true;
|
2015-03-19 21:27:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
if(can){
|
2015-03-19 21:27:56 +01:00
|
|
|
if(theStack != null){
|
|
|
|
if(theStack.isItemEqual(this.slots[0])){
|
2015-03-29 15:29:05 +02:00
|
|
|
if(this.slots[0].stackSize <= maxSize - theStack.stackSize){
|
2015-03-19 21:27:56 +01:00
|
|
|
theStack.stackSize += this.slots[0].stackSize;
|
|
|
|
this.slots[0] = null;
|
|
|
|
}
|
2015-03-29 15:29:05 +02:00
|
|
|
else if(this.slots[0].stackSize > maxSize - theStack.stackSize){
|
|
|
|
this.decrStackSize(0, maxSize - theStack.stackSize);
|
|
|
|
theStack.stackSize = maxSize;
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2015-03-20 18:58:31 +01:00
|
|
|
ItemStack toBePut = this.slots[0].copy();
|
2015-03-29 15:29:05 +02:00
|
|
|
if(maxSize < toBePut.stackSize) toBePut.stackSize = maxSize;
|
2015-03-20 18:58:31 +01:00
|
|
|
theInventory.setInventorySlotContents(theSlotToPut, toBePut);
|
|
|
|
if(this.slots[0].stackSize == toBePut.stackSize) this.slots[0] = null;
|
2015-03-29 15:29:05 +02:00
|
|
|
else this.decrStackSize(0, toBePut.stackSize);
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 05:20:19 +02:00
|
|
|
public boolean checkFilters(ItemStack stack, boolean isPull){
|
|
|
|
if(!this.isAdvanced) return true;
|
|
|
|
|
|
|
|
int slotStart = isPull ? PULL_FILTER_START : PUT_FILTER_START;
|
|
|
|
int slotStop = slotStart+6;
|
|
|
|
|
|
|
|
for(int i = slotStart; i < slotStop; i++){
|
|
|
|
if(this.slots[i] != null && this.slots[i].isItemEqual(stack)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:27:56 +01:00
|
|
|
public void initVars(){
|
2015-05-04 17:26:50 +02:00
|
|
|
|
|
|
|
this.placeToPull = WorldUtil.getTileEntityFromSide(WorldUtil.getDirectionByRotatingSide(this.sideToPull), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
|
|
|
|
this.placeToPut = WorldUtil.getTileEntityFromSide(WorldUtil.getDirectionByRotatingSide(this.sideToPut), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
|
2015-03-19 21:27:56 +01:00
|
|
|
|
|
|
|
if(this.placeToPull != null && this.placeToPull instanceof IInventory){
|
|
|
|
this.placeToPullSlotAmount = ((IInventory)this.placeToPull).getSizeInventory();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.placeToPullSlotAmount = 0;
|
|
|
|
this.slotToPull = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.placeToPut != null && this.placeToPut instanceof IInventory){
|
|
|
|
this.placeToPutSlotAmount = ((IInventory)this.placeToPut).getSizeInventory();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.placeToPutSlotAmount = 0;
|
|
|
|
this.slotToPut = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onButtonPressed(int buttonID){
|
|
|
|
if(buttonID == 0) this.sideToPut++;
|
|
|
|
if(buttonID == 1) this.sideToPut--;
|
|
|
|
if(buttonID == 2) this.slotToPut++;
|
|
|
|
if(buttonID == 3) this.slotToPut--;
|
|
|
|
|
|
|
|
if(buttonID == 4) this.sideToPull++;
|
|
|
|
if(buttonID == 5) this.sideToPull--;
|
|
|
|
if(buttonID == 6) this.slotToPull++;
|
|
|
|
if(buttonID == 7) this.slotToPull--;
|
|
|
|
|
|
|
|
if(this.sideToPut >= 6) this.sideToPut = -1;
|
|
|
|
else if(this.sideToPut < -1) this.sideToPut = 5;
|
|
|
|
else if(this.sideToPull >= 6) this.sideToPull = -1;
|
|
|
|
else if(this.sideToPull < -1) this.sideToPull = 5;
|
|
|
|
else if(this.slotToPut >= this.placeToPutSlotAmount) this.slotToPut = -1;
|
|
|
|
else if(this.slotToPut < -1) this.slotToPut = this.placeToPutSlotAmount-1;
|
|
|
|
else if(this.slotToPull >= this.placeToPullSlotAmount) this.slotToPull = -1;
|
|
|
|
else if(this.slotToPull < -1) this.slotToPull = this.placeToPullSlotAmount-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
|
|
|
super.writeToNBT(compound);
|
|
|
|
compound.setInteger("SideToPut", this.sideToPut);
|
|
|
|
compound.setInteger("SlotToPut", this.slotToPut);
|
|
|
|
compound.setInteger("SideToPull", this.sideToPull);
|
|
|
|
compound.setInteger("SlotToPull", this.slotToPull);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
|
|
|
this.sideToPut = compound.getInteger("SideToPut");
|
|
|
|
this.slotToPut = compound.getInteger("SlotToPut");
|
|
|
|
this.sideToPull = compound.getInteger("SideToPull");
|
|
|
|
this.slotToPull = compound.getInteger("SlotToPull");
|
2015-04-04 05:20:19 +02:00
|
|
|
super.readFromNBT(compound);
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
2015-04-04 05:20:19 +02:00
|
|
|
|
2015-03-19 21:27:56 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2015-04-04 05:20:19 +02:00
|
|
|
return i == 0;
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, int side){
|
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, int side){
|
2015-04-04 05:20:19 +02:00
|
|
|
return slot == 0;
|
2015-03-19 21:27:56 +01:00
|
|
|
}
|
|
|
|
}
|