ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java

486 lines
20 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityInputter.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-03-19 21:27:56 +01:00
2016-01-08 13:31:58 +01:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
import de.ellpeck.actuallyadditions.mod.network.gui.INumberReactor;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.entity.player.EntityPlayer;
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;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
2015-03-19 21:27:56 +01:00
public class TileEntityInputter extends TileEntityInventoryBase implements IButtonReactor, INumberReactor{
2015-06-28 03:12:32 +02:00
2015-10-03 10:16:18 +02:00
public static final int PUT_FILTER_START = 13;
public static final int PULL_FILTER_START = 1;
public static final int OKAY_BUTTON_ID = 133;
public int sideToPut = -1;
public int slotToPutStart;
public int slotToPutEnd;
public TileEntity placeToPut;
public int sideToPull = -1;
public int slotToPullStart;
public int slotToPullEnd;
public TileEntity placeToPull;
public boolean isAdvanced;
private int lastPutSide;
private int lastPutStart;
private int lastPutEnd;
private int lastPullSide;
private int lastPullStart;
private int lastPullEnd;
public FilterSettings leftFilter = new FilterSettings(PULL_FILTER_START, PULL_FILTER_START+12, true, true, false, 0, -1000);
public FilterSettings rightFilter = new FilterSettings(PUT_FILTER_START, PUT_FILTER_START+12, true, true, false, 0, -2000);
2015-10-03 10:16:18 +02:00
private boolean hasCheckedTilesAround;
2015-10-03 10:16:18 +02:00
public TileEntityInputter(int slots, String name){
super(slots, name);
}
public TileEntityInputter(){
super(1, "inputter");
this.isAdvanced = false;
}
2015-06-28 03:12:32 +02:00
@Override
public void onNumberReceived(int text, int textID, EntityPlayer player){
if(text != -1){
2016-02-01 21:14:17 +01:00
if(textID == 0){
this.slotToPutStart = Math.max(text, 0);
2016-02-01 21:14:17 +01:00
}
if(textID == 1){
this.slotToPutEnd = Math.max(text, 0);
}
2016-02-01 21:14:17 +01:00
if(textID == 2){
this.slotToPullStart = Math.max(text, 0);
2016-02-01 21:14:17 +01:00
}
if(textID == 3){
this.slotToPullEnd = Math.max(text, 0);
}
2015-06-28 03:12:32 +02:00
}
this.markDirty();
}
2015-03-19 21:27:56 +01:00
private boolean newPulling(){
for(EnumFacing side : EnumFacing.values()){
if(this.placeToPull.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)){
IItemHandler cap = this.placeToPull.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
if(cap != null){
for(int i = Math.max(this.slotToPullStart, 0); i < Math.min(this.slotToPullEnd, cap.getSlots()); i++){
if(this.checkBothFilters(cap.getStackInSlot(i), false)){
if(WorldUtil.doItemInteraction(i, 0, this.placeToPull, this, side, null)){
return true;
}
}
}
}
}
}
return false;
}
private boolean newPutting(){
if(this.checkBothFilters(this.slots[0], true)){
for(EnumFacing side : EnumFacing.values()){
if(this.placeToPut.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)){
IItemHandler cap = this.placeToPut.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
if(cap != null){
for(int i = Math.max(this.slotToPutStart, 0); i < Math.min(this.slotToPutEnd, cap.getSlots()); i++){
if(WorldUtil.doItemInteraction(0, i, this, this.placeToPut, null, side)){
return true;
}
}
}
}
}
}
return false;
}
/**
* Pulls Items from the specified Slots on the specified Side
*/
private void pull(){
if(this.newPulling() || !(this.placeToPull instanceof IInventory)){
return;
}
//The Inventory to pull from
2016-05-02 17:46:53 +02:00
IInventory theInventory = (IInventory)this.placeToPull;
//Does the Inventory even have Slots!?
2015-06-28 03:12:32 +02:00
if(theInventory.getSizeInventory() > 0){
//The slot currently pulling from (for later)
2015-06-28 03:12:32 +02:00
int theSlotToPull = this.slotToPullStart;
//The amount of Items that can fit into one slot of the inventory
2015-03-29 15:29:05 +02:00
int maxSize = theInventory.getInventoryStackLimit();
//If the Inventory is ISided, deal with that
2015-03-29 15:29:05 +02:00
ISidedInventory theSided = null;
2015-10-03 10:16:18 +02:00
if(theInventory instanceof ISidedInventory){
theSided = (ISidedInventory)theInventory;
}
//If can be pulled (for later)
2015-06-28 03:12:32 +02:00
boolean can = false;
2015-03-19 21:27:56 +01:00
//The Stack that is pulled (for later)
2015-03-19 21:27:56 +01:00
ItemStack theStack = null;
//Go through all of the specified Slots
2015-07-03 01:22:09 +02:00
for(int i = Math.max(theSlotToPull, 0); i < Math.min(this.slotToPullEnd, theInventory.getSizeInventory()); i++){
//Temporary Stack for storage
2015-03-29 15:29:05 +02:00
ItemStack tempStack = theInventory.getStackInSlot(i);
if(tempStack != null){
//Set maxSize to the max Size of the temporary stack if it's smaller than the Inventory's Max Size
2015-10-02 16:48:01 +02:00
if(tempStack.getMaxStackSize() < this.getInventoryStackLimit()){
maxSize = tempStack.getMaxStackSize();
}
else{
maxSize = this.getInventoryStackLimit();
}
2015-03-29 15:29:05 +02:00
}
//If ESD has enough Space & Item in question is on whitelist
if(tempStack != null && (this.slots[0] == null || (ItemUtil.canBeStacked(tempStack, this.slots[0]) && this.slots[0].stackSize < maxSize)) && this.checkBothFilters(tempStack, false)){
//Deal with ISided
2015-03-29 15:29:05 +02:00
if(theSided != null){
//Check if Item can be inserted from any Side (Because Sidedness gets ignored!)
for(int j = 0; j <= 5; j++){
if(theSided.canExtractItem(i, tempStack, EnumFacing.values()[j])){
2015-03-29 15:29:05 +02:00
theStack = tempStack;
theSlotToPull = i;
2015-06-28 03:12:32 +02:00
can = true;
2015-03-29 15:29:05 +02:00
break;
}
}
}
//Deal with IInventory
2015-03-29 15:29:05 +02:00
else{
2015-03-19 21:27:56 +01:00
theStack = tempStack;
theSlotToPull = i;
2015-06-28 03:12:32 +02:00
can = true;
2015-03-19 21:27:56 +01:00
}
}
//Stop if it can already pull
2015-10-03 10:16:18 +02:00
if(can){
break;
}
2015-03-19 21:27:56 +01:00
}
//If pull can be done
2015-06-28 03:12:32 +02:00
if(can){
//If ESD already has Items
2015-03-19 21:27:56 +01:00
if(this.slots[0] != null){
if(ItemUtil.canBeStacked(theStack, this.slots[0])){
//If the StackSize is smaller than the space the ESD has left
2015-10-02 16:48:01 +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);
}
//If the StackSize is bigger than what fits into the Inventory
2015-10-02 16:48:01 +02:00
else if(theStack.stackSize > maxSize-this.slots[0].stackSize){
theInventory.decrStackSize(theSlotToPull, maxSize-this.slots[0].stackSize);
2015-03-29 15:29:05 +02:00
this.slots[0].stackSize = maxSize;
2015-03-19 21:27:56 +01:00
}
}
}
//If ESD is empty
2015-03-19 21:27:56 +01:00
else{
2015-03-20 18:58:31 +01:00
ItemStack toBePut = theStack.copy();
2015-10-03 10:16:18 +02:00
if(maxSize < toBePut.stackSize){
toBePut.stackSize = maxSize;
}
//Actually puts the Item
2015-03-20 18:58:31 +01:00
this.setInventorySlotContents(0, toBePut);
//Removes the Item from the inventory getting pulled from
2015-10-02 16:48:01 +02:00
if(theStack.stackSize == toBePut.stackSize){
theInventory.setInventorySlotContents(theSlotToPull, null);
}
else{
theInventory.decrStackSize(theSlotToPull, toBePut.stackSize);
}
2015-03-19 21:27:56 +01:00
}
}
}
}
/**
* Puts Items into the specified Slots at the specified Side
* (Check pull() for Description, similar to this)
*/
private void put(){
if(this.newPutting() || !(this.placeToPut instanceof IInventory)){
return;
}
2016-05-02 17:46:53 +02:00
IInventory theInventory = (IInventory)this.placeToPut;
2015-06-28 03:12:32 +02:00
if(theInventory.getSizeInventory() > 0){
int theSlotToPut = this.slotToPutStart;
2015-03-29 15:29:05 +02:00
int maxSize = theInventory.getInventoryStackLimit();
ISidedInventory theSided = null;
2015-10-03 10:16:18 +02:00
if(theInventory instanceof ISidedInventory){
theSided = (ISidedInventory)theInventory;
}
2015-03-29 15:29:05 +02:00
boolean can = false;
2015-03-19 21:27:56 +01:00
if(this.slots[0] != null){
ItemStack theStack = null;
2015-07-03 01:22:09 +02:00
for(int i = Math.max(theSlotToPut, 0); i < Math.min(this.slotToPutEnd, theInventory.getSizeInventory()); i++){
2015-03-29 15:29:05 +02:00
ItemStack tempStack = theInventory.getStackInSlot(i);
if(tempStack != null){
2015-10-02 16:48:01 +02:00
if(tempStack.getMaxStackSize() < theInventory.getInventoryStackLimit()){
maxSize = tempStack.getMaxStackSize();
}
else{
maxSize = theInventory.getInventoryStackLimit();
}
2015-03-29 15:29:05 +02:00
}
if(theInventory.isItemValidForSlot(i, this.slots[0]) && (tempStack == null || (ItemUtil.canBeStacked(tempStack, this.slots[0]) && tempStack.stackSize < maxSize)) && this.checkBothFilters(this.slots[0], true)){
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], EnumFacing.values()[j])){
2015-03-29 15:29:05 +02:00
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
}
}
2015-10-03 10:16:18 +02:00
if(can){
break;
}
2015-03-19 21:27:56 +01:00
}
2015-03-29 15:29:05 +02:00
if(can){
2015-03-19 21:27:56 +01:00
if(theStack != null){
ItemStack copiedStack = theStack.copy();
if(ItemUtil.canBeStacked(copiedStack, this.slots[0])){
2015-10-02 16:48:01 +02:00
if(this.slots[0].stackSize <= maxSize-copiedStack.stackSize){
copiedStack.stackSize += this.slots[0].stackSize;
2015-03-19 21:27:56 +01:00
this.slots[0] = null;
theInventory.setInventorySlotContents(theSlotToPut, copiedStack);
2015-03-19 21:27:56 +01:00
}
2015-10-02 16:48:01 +02:00
else if(this.slots[0].stackSize > maxSize-copiedStack.stackSize){
this.decrStackSize(0, maxSize-copiedStack.stackSize);
copiedStack.stackSize = maxSize;
theInventory.setInventorySlotContents(theSlotToPut, copiedStack);
2015-03-19 21:27:56 +01:00
}
}
}
else{
2015-03-20 18:58:31 +01:00
ItemStack toBePut = this.slots[0].copy();
2015-10-03 10:16:18 +02:00
if(maxSize < toBePut.stackSize){
toBePut.stackSize = maxSize;
}
2015-03-20 18:58:31 +01:00
theInventory.setInventorySlotContents(theSlotToPut, toBePut);
2015-10-02 16:48:01 +02:00
if(this.slots[0].stackSize == toBePut.stackSize){
this.slots[0] = null;
}
else{
this.decrStackSize(0, toBePut.stackSize);
}
2015-03-19 21:27:56 +01:00
}
}
}
}
}
/**
* Checks if one of the filters contains the ItemStack
2015-10-02 16:48:01 +02:00
*
* @param stack The ItemStack
* @return If the Item is filtered correctly
*/
private boolean checkBothFilters(ItemStack stack, boolean output){
return !this.isAdvanced || (output ? this.rightFilter : this.leftFilter).check(stack, this.slots);
}
/**
* Sets all of the relevant variables
*/
2015-03-19 21:27:56 +01:00
public void initVars(){
if(this.sideToPull != -1){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPull);
this.placeToPull = this.worldObj.getTileEntity(this.pos.offset(side));
2015-05-04 17:26:50 +02:00
if(this.slotToPullEnd <= 0 && this.placeToPull != null){
if(this.placeToPull instanceof IInventory){
this.slotToPullEnd = ((IInventory)this.placeToPull).getSizeInventory();
}
else{
if(this.placeToPull.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){
IItemHandler cap = this.placeToPull.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if(cap != null){
this.slotToPullEnd = cap.getSlots();
}
}
}
2015-10-03 10:16:18 +02:00
}
2015-03-19 21:27:56 +01:00
}
if(this.sideToPut != -1){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPut);
this.placeToPut = this.worldObj.getTileEntity(this.pos.offset(side));
if(this.slotToPutEnd <= 0 && this.placeToPut != null){
if(this.placeToPut instanceof IInventory){
this.slotToPutEnd = ((IInventory)this.placeToPut).getSizeInventory();
}
else{
if(this.placeToPut.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){
IItemHandler cap = this.placeToPut.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if(cap != null){
this.slotToPutEnd = cap.getSlots();
}
}
}
2015-10-03 10:16:18 +02:00
}
2015-03-19 21:27:56 +01:00
}
}
@Override
public void onButtonPressed(int buttonID, EntityPlayer player){
this.leftFilter.onButtonPressed(buttonID);
this.rightFilter.onButtonPressed(buttonID);
//Reset the Slots
2015-06-28 03:12:32 +02:00
if(buttonID == 0 || buttonID == 1){
this.slotToPutStart = 0;
this.slotToPutEnd = 0;
}
if(buttonID == 2 || buttonID == 3){
this.slotToPullStart = 0;
this.slotToPullEnd = 0;
}
2015-10-03 10:16:18 +02:00
if(buttonID == 0){
this.sideToPut++;
}
if(buttonID == 1){
this.sideToPut--;
}
2015-03-19 21:27:56 +01:00
2015-10-03 10:16:18 +02:00
if(buttonID == 2){
this.sideToPull++;
}
if(buttonID == 3){
this.sideToPull--;
}
2015-03-19 21:27:56 +01:00
2015-10-02 16:48:01 +02:00
if(this.sideToPut >= 6){
this.sideToPut = -1;
}
else if(this.sideToPut < -1){
this.sideToPut = 5;
}
else if(this.sideToPull >= 6){
this.sideToPull = -1;
}
2015-10-03 10:16:18 +02:00
else if(this.sideToPull < -1){
this.sideToPull = 5;
}
2015-06-28 03:12:32 +02:00
this.markDirty();
this.initVars();
2015-03-19 21:27:56 +01:00
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
if(type != NBTType.SAVE_BLOCK){
compound.setInteger("SideToPut", this.sideToPut);
compound.setInteger("SlotToPut", this.slotToPutStart);
compound.setInteger("SlotToPutEnd", this.slotToPutEnd);
compound.setInteger("SideToPull", this.sideToPull);
compound.setInteger("SlotToPull", this.slotToPullStart);
compound.setInteger("SlotToPullEnd", this.slotToPullEnd);
this.leftFilter.writeToNBT(compound, "LeftFilter");
this.rightFilter.writeToNBT(compound, "RightFilter");
}
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
if(type != NBTType.SAVE_BLOCK){
this.sideToPut = compound.getInteger("SideToPut");
this.slotToPutStart = compound.getInteger("SlotToPut");
this.slotToPutEnd = compound.getInteger("SlotToPutEnd");
this.sideToPull = compound.getInteger("SideToPull");
this.slotToPullStart = compound.getInteger("SlotToPull");
this.slotToPullEnd = compound.getInteger("SlotToPullEnd");
this.leftFilter.readFromNBT(compound, "LeftFilter");
this.rightFilter.readFromNBT(compound, "RightFilter");
}
super.readSyncableNBT(compound, type);
}
2015-12-01 19:48:09 +01:00
@Override
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
if(!this.hasCheckedTilesAround){
this.initVars();
this.hasCheckedTilesAround = true;
}
2015-12-01 19:48:09 +01:00
//Is Block not powered by Redstone?
if(!this.isRedstonePowered){
2015-12-01 19:48:09 +01:00
if(!(this.sideToPull == this.sideToPut && this.slotToPullStart == this.slotToPutStart && this.slotToPullEnd == this.slotToPutEnd)){
if(this.sideToPull != -1 && this.placeToPull != null){
2015-12-01 19:48:09 +01:00
this.pull();
}
if(this.slots[0] != null && this.sideToPut != -1 && this.placeToPut != null){
2015-12-01 19:48:09 +01:00
this.put();
}
}
}
//Update the Client
if((this.sideToPut != this.lastPutSide || this.sideToPull != this.lastPullSide || this.slotToPullStart != this.lastPullStart || this.slotToPullEnd != this.lastPullEnd || this.slotToPutStart != this.lastPutStart || this.slotToPutEnd != this.lastPutEnd || this.leftFilter.needsUpdateSend() || this.rightFilter.needsUpdateSend()) && this.sendUpdateWithInterval()){
2015-12-01 19:48:09 +01:00
this.lastPutSide = this.sideToPut;
this.lastPullSide = this.sideToPull;
this.lastPullStart = this.slotToPullStart;
this.lastPullEnd = this.slotToPullEnd;
this.lastPutStart = this.slotToPutStart;
this.lastPutEnd = this.slotToPutEnd;
this.leftFilter.updateLasts();
this.rightFilter.updateLasts();
2015-12-01 19:48:09 +01:00
}
}
}
2015-03-19 21:27:56 +01:00
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
2016-02-01 17:49:55 +01:00
return i == 0;
2015-03-19 21:27:56 +01:00
}
2015-12-19 10:30:39 +01:00
@Override
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
2016-02-01 17:49:55 +01:00
return this.isItemValidForSlot(slot, stack);
2015-12-19 10:30:39 +01:00
}
2015-03-19 21:27:56 +01:00
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
return slot == 0;
2015-03-19 21:27:56 +01:00
}
2015-06-28 03:12:32 +02:00
}