2016-05-08 21:09:58 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
|
2016-05-09 19:05:20 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay.TileEntityLaserRelayItem;
|
2016-05-11 20:59:38 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
|
2016-05-08 21:09:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
|
2016-05-09 19:05:20 +02:00
|
|
|
import java.util.ArrayList;
|
2016-05-08 21:09:58 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TileEntityItemViewer extends TileEntityInventoryBase{
|
|
|
|
|
|
|
|
public TileEntityItemViewer(){
|
|
|
|
super(0, "itemViewer");
|
|
|
|
}
|
|
|
|
|
2016-05-09 19:05:20 +02:00
|
|
|
private List<GenericItemHandlerInfo> getItemHandlerInfos(){
|
|
|
|
TileEntityLaserRelayItem relay = this.getConnectedRelay();
|
2016-05-08 21:09:58 +02:00
|
|
|
if(relay != null){
|
|
|
|
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(relay.getPos());
|
|
|
|
if(network != null){
|
|
|
|
return relay.getItemHandlersInNetwork(network);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-09 19:05:20 +02:00
|
|
|
private SpecificItemHandlerInfo getSwitchedIndexHandler(int i){
|
|
|
|
List<GenericItemHandlerInfo> infos = this.getItemHandlerInfos();
|
2016-05-08 21:09:58 +02:00
|
|
|
int currentI = 0;
|
2016-05-09 19:05:20 +02:00
|
|
|
if(infos != null && !infos.isEmpty()){
|
|
|
|
for(GenericItemHandlerInfo info : infos){
|
|
|
|
for(IItemHandler handler : info.handlers){
|
|
|
|
int slotAmount = handler.getSlots();
|
|
|
|
if(currentI+slotAmount > i){
|
|
|
|
return new SpecificItemHandlerInfo(handler, i-currentI, info.relayInQuestion);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
currentI += slotAmount;
|
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-09 19:05:20 +02:00
|
|
|
private TileEntityLaserRelayItem getConnectedRelay(){
|
|
|
|
TileEntityLaserRelayItem tileFound = null;
|
2016-05-08 21:09:58 +02:00
|
|
|
if(this.worldObj != null){ //Why is that even possible..?
|
|
|
|
for(int i = 0; i <= 5; i++){
|
|
|
|
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
|
|
|
|
BlockPos pos = WorldUtil.getCoordsFromSide(side, this.getPos(), 0);
|
|
|
|
TileEntity tile = this.worldObj.getTileEntity(pos);
|
|
|
|
|
2016-05-09 19:05:20 +02:00
|
|
|
if(tile instanceof TileEntityLaserRelayItem){
|
2016-05-08 21:09:58 +02:00
|
|
|
if(tileFound != null){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else{
|
2016-05-09 19:05:20 +02:00
|
|
|
tileFound = (TileEntityLaserRelayItem)tile;
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tileFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction){
|
|
|
|
return this.isItemValidForSlot(index, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){
|
2016-05-09 19:05:20 +02:00
|
|
|
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(handler != null){
|
2016-05-11 20:55:29 +02:00
|
|
|
if(this.isWhitelisted(handler, stack)){
|
2016-05-10 22:19:15 +02:00
|
|
|
if(ItemStack.areItemsEqual(handler.handler.getStackInSlot(handler.switchedIndex), stack)){
|
|
|
|
ItemStack gaveBack = handler.handler.extractItem(handler.switchedIndex, stack.stackSize, true);
|
|
|
|
return gaveBack != null;
|
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-11 20:55:29 +02:00
|
|
|
private boolean isWhitelisted(SpecificItemHandlerInfo handler, ItemStack stack){
|
2016-05-11 20:59:38 +02:00
|
|
|
boolean whitelisted = handler.relayInQuestion.isWhitelisted(stack);
|
|
|
|
TileEntityLaserRelayItem connected = this.getConnectedRelay();
|
2016-05-11 21:00:26 +02:00
|
|
|
if(connected != handler.relayInQuestion){
|
2016-05-11 20:59:38 +02:00
|
|
|
return whitelisted && connected.isWhitelisted(stack);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return whitelisted;
|
|
|
|
}
|
2016-05-11 20:55:29 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack){
|
2016-05-09 19:05:20 +02:00
|
|
|
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(handler != null){
|
2016-05-11 20:55:29 +02:00
|
|
|
if(this.isWhitelisted(handler, stack)){
|
2016-05-10 22:19:15 +02:00
|
|
|
ItemStack gaveBack = handler.handler.insertItem(handler.switchedIndex, stack, true);
|
|
|
|
return !ItemStack.areItemStacksEqual(gaveBack, stack);
|
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear(){
|
|
|
|
for(int i = 0; i < this.getSizeInventory(); i++){
|
|
|
|
this.removeStackFromSlot(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int i, ItemStack stack){
|
|
|
|
if(stack != null){
|
2016-05-09 19:05:20 +02:00
|
|
|
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(handler != null){
|
|
|
|
ItemStack toInsert = stack.copy();
|
2016-05-09 19:05:20 +02:00
|
|
|
ItemStack inSlot = handler.handler.getStackInSlot(handler.switchedIndex);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(inSlot != null){
|
|
|
|
toInsert.stackSize -= inSlot.stackSize;
|
|
|
|
}
|
2016-05-09 19:05:20 +02:00
|
|
|
handler.handler.insertItem(handler.switchedIndex, toInsert, false);
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.removeStackFromSlot(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory(){
|
|
|
|
int size = 0;
|
2016-05-09 19:05:20 +02:00
|
|
|
List<GenericItemHandlerInfo> infos = this.getItemHandlerInfos();
|
|
|
|
if(infos != null){
|
|
|
|
for(GenericItemHandlerInfo info : infos){
|
|
|
|
for(IItemHandler handler : info.handlers){
|
|
|
|
size += handler.getSlots();
|
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int i){
|
2016-05-09 19:05:20 +02:00
|
|
|
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(handler != null){
|
2016-05-09 19:05:20 +02:00
|
|
|
return handler.handler.getStackInSlot(handler.switchedIndex);
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int i, int j){
|
2016-05-09 19:05:20 +02:00
|
|
|
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(handler != null){
|
2016-05-09 19:05:20 +02:00
|
|
|
return handler.handler.extractItem(handler.switchedIndex, j, false);
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack removeStackFromSlot(int index){
|
2016-05-09 19:05:20 +02:00
|
|
|
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(handler != null){
|
2016-05-09 19:05:20 +02:00
|
|
|
ItemStack stackInSlot = handler.handler.getStackInSlot(handler.switchedIndex);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(stackInSlot != null){
|
2016-05-09 19:05:20 +02:00
|
|
|
handler.handler.extractItem(handler.switchedIndex, stackInSlot.stackSize, false);
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
return stackInSlot;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-09 19:05:20 +02:00
|
|
|
|
|
|
|
private static class SpecificItemHandlerInfo{
|
|
|
|
|
|
|
|
public IItemHandler handler;
|
|
|
|
public int switchedIndex;
|
|
|
|
public TileEntityLaserRelayItem relayInQuestion;
|
|
|
|
|
|
|
|
public SpecificItemHandlerInfo(IItemHandler handler, int switchedIndex, TileEntityLaserRelayItem relayInQuestion){
|
|
|
|
this.handler = handler;
|
|
|
|
this.switchedIndex = switchedIndex;
|
|
|
|
this.relayInQuestion = relayInQuestion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class GenericItemHandlerInfo{
|
|
|
|
|
|
|
|
public List<IItemHandler> handlers = new ArrayList<IItemHandler>();
|
|
|
|
public TileEntityLaserRelayItem relayInQuestion;
|
|
|
|
|
|
|
|
public GenericItemHandlerInfo(TileEntityLaserRelayItem relayInQuestion){
|
|
|
|
this.relayInQuestion = relayInQuestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean containsHandler(List<GenericItemHandlerInfo> infos, IItemHandler handler){
|
|
|
|
for(GenericItemHandlerInfo info : infos){
|
|
|
|
if(info.handlers.contains(handler)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean containsTile(List<GenericItemHandlerInfo> infos, TileEntityLaserRelayItem tile){
|
|
|
|
for(GenericItemHandlerInfo info : infos){
|
|
|
|
if(info.relayInQuestion == tile){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|