2016-09-09 21:43:33 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityDistributorItem.java") is part of the Actually Additions mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
|
|
|
* © 2015-2016 Ellpeck
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-09-11 13:14:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2016-09-09 21:43:33 +02: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;
|
|
|
|
|
2016-09-11 12:57:20 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-09-09 21:43:33 +02:00
|
|
|
public class TileEntityDistributorItem extends TileEntityInventoryBase{
|
|
|
|
|
2016-09-11 12:57:20 +02:00
|
|
|
private final Map<EnumFacing, IItemHandler> handlersAround = new HashMap<EnumFacing, IItemHandler>();
|
2016-09-12 20:45:29 +02:00
|
|
|
private int putSide;
|
2016-09-09 21:43:33 +02:00
|
|
|
|
|
|
|
public TileEntityDistributorItem(){
|
|
|
|
super(1, "distributorItem");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
super.updateEntity();
|
|
|
|
|
|
|
|
if(!this.worldObj.isRemote){
|
2016-09-11 13:14:39 +02:00
|
|
|
boolean shouldMarkDirty = false;
|
|
|
|
|
|
|
|
IItemHandler handlerUp = this.handlersAround.get(EnumFacing.UP);
|
|
|
|
if(handlerUp != null){
|
|
|
|
for(int i = 0; i < handlerUp.getSlots(); i++){
|
|
|
|
|
2016-09-11 13:33:47 +02:00
|
|
|
ItemStack pullable = handlerUp.extractItem(i, 1, true);
|
2016-09-11 13:14:39 +02:00
|
|
|
if(pullable != null && (this.slots[0] == null || ItemUtil.canBeStacked(this.slots[0], pullable))){
|
2016-09-11 13:33:47 +02:00
|
|
|
ItemStack pulled = handlerUp.extractItem(i, 1, false);
|
2016-09-11 13:14:39 +02:00
|
|
|
if(pulled != null){
|
|
|
|
if(this.slots[0] == null){
|
|
|
|
this.slots[0] = pulled.copy();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.slots[0].stackSize += pulled.stackSize;
|
|
|
|
}
|
|
|
|
shouldMarkDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.slots[0] != null && this.slots[0].stackSize >= this.slots[0].getMaxStackSize()){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!this.handlersAround.isEmpty() && (!this.handlersAround.containsKey(EnumFacing.UP) || this.handlersAround.size() >= 2) && this.slots[0] != null){
|
2016-09-11 12:57:20 +02:00
|
|
|
EnumFacing[] allFacings = EnumFacing.values();
|
|
|
|
do{
|
|
|
|
this.putSide++;
|
|
|
|
|
|
|
|
if(this.putSide >= 6){
|
|
|
|
this.putSide = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 13:14:39 +02:00
|
|
|
while(allFacings[this.putSide] == EnumFacing.UP || !this.handlersAround.containsKey(allFacings[this.putSide]));
|
2016-09-11 12:57:20 +02:00
|
|
|
|
|
|
|
EnumFacing putFacing = allFacings[this.putSide];
|
|
|
|
IItemHandler handler = this.handlersAround.get(putFacing);
|
|
|
|
if(handler != null){
|
2016-09-11 13:14:39 +02:00
|
|
|
int aroundAmount = this.handlersAround.containsKey(EnumFacing.UP) ? this.handlersAround.size()-1 : this.handlersAround.size();
|
|
|
|
int amount = this.slots[0].stackSize/aroundAmount;
|
2016-09-11 12:57:20 +02:00
|
|
|
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;
|
2016-09-09 21:43:33 +02:00
|
|
|
}
|
2016-09-11 12:57:20 +02:00
|
|
|
else if(notInserted.stackSize != this.slots[0].stackSize){
|
|
|
|
this.slots[0].stackSize -= notInserted.stackSize;
|
|
|
|
toInsert = notInserted;
|
|
|
|
|
|
|
|
shouldMarkDirty = true;
|
2016-09-09 21:43:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 12:57:20 +02:00
|
|
|
if(this.slots[0].stackSize <= 0){
|
|
|
|
this.slots[0] = null;
|
|
|
|
shouldMarkDirty = true;
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 21:43:33 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-11 13:14:39 +02:00
|
|
|
|
|
|
|
if(shouldMarkDirty){
|
|
|
|
this.markDirty();
|
|
|
|
}
|
2016-09-11 12:57:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 21:43:33 +02:00
|
|
|
|
2016-09-11 12:57:20 +02:00
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart(){
|
2016-09-11 12:57:20 +02:00
|
|
|
this.handlersAround.clear();
|
|
|
|
|
|
|
|
for(EnumFacing side : EnumFacing.values()){
|
2016-09-11 13:14:39 +02:00
|
|
|
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);
|
2016-09-11 12:57:20 +02:00
|
|
|
}
|
2016-09-09 21:43:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldSyncSlots(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-11 12:57:20 +02:00
|
|
|
@Override
|
|
|
|
public void markDirty(){
|
|
|
|
super.markDirty();
|
|
|
|
this.sendUpdate();
|
|
|
|
}
|
|
|
|
|
2016-09-09 21:43:33 +02:00
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public boolean shouldSaveDataOnChangeOrWorldStart(){
|
2016-09-09 21:43:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction){
|
|
|
|
return direction == EnumFacing.UP && this.isItemValidForSlot(index, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|