2016-05-16 22:52:27 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityItemViewer.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
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-07-30 17:07:32 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
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-10-01 23:35:39 +02:00
|
|
|
import net.minecraftforge.items.wrapper.SidedInvWrapper;
|
2016-05-08 21:09:58 +02:00
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
import java.util.*;
|
2016-05-08 21:09:58 +02:00
|
|
|
|
|
|
|
public class TileEntityItemViewer extends TileEntityInventoryBase{
|
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
private final List<GenericItemHandlerInfo> genericInfos = new ArrayList<GenericItemHandlerInfo>();
|
|
|
|
private final Map<Integer, SpecificItemHandlerInfo> specificInfos = new HashMap<Integer, SpecificItemHandlerInfo>();
|
2016-10-31 19:05:29 +01:00
|
|
|
public TileEntityLaserRelayItem connectedRelay;
|
2016-11-03 10:54:28 +01:00
|
|
|
|
|
|
|
private Network oldNetwork;
|
2016-10-29 12:00:00 +02:00
|
|
|
private int lastNetworkChangeAmount = -1;
|
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
public TileEntityItemViewer(){
|
|
|
|
super(0, "itemViewer");
|
|
|
|
}
|
|
|
|
|
2016-10-01 23:35:39 +02:00
|
|
|
@Override
|
|
|
|
protected void getInvWrappers(SidedInvWrapper[] wrappers){
|
|
|
|
for(int i = 0; i < wrappers.length; i++){
|
2016-10-11 14:24:18 +02:00
|
|
|
final EnumFacing direction = EnumFacing.values()[i];
|
|
|
|
wrappers[i] = new SidedInvWrapper(this, direction){
|
2016-10-01 23:35:39 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate){
|
2016-10-11 14:24:18 +02:00
|
|
|
if(TileEntityItemViewer.this.canInsertItem(slot, stack, direction)){
|
|
|
|
SpecificItemHandlerInfo info = TileEntityItemViewer.this.getSwitchedIndexHandler(slot);
|
|
|
|
if(info != null){
|
|
|
|
return info.handler.insertItem(info.switchedIndex, stack, simulate);
|
|
|
|
}
|
2016-10-01 23:35:39 +02:00
|
|
|
}
|
2016-10-11 14:24:18 +02:00
|
|
|
return super.insertItem(slot, stack, simulate);
|
2016-10-01 23:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack extractItem(int slot, int amount, boolean simulate){
|
2016-10-11 14:24:18 +02:00
|
|
|
ItemStack stackIn = TileEntityItemViewer.this.getStackInSlot(slot);
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stackIn)){
|
2016-10-11 14:24:18 +02:00
|
|
|
if(TileEntityItemViewer.this.canExtractItem(slot, stackIn, direction)){
|
|
|
|
SpecificItemHandlerInfo info = TileEntityItemViewer.this.getSwitchedIndexHandler(slot);
|
|
|
|
if(info != null){
|
|
|
|
return info.handler.extractItem(info.switchedIndex, amount, simulate);
|
|
|
|
}
|
|
|
|
}
|
2016-10-01 23:35:39 +02:00
|
|
|
}
|
2016-10-11 14:24:18 +02:00
|
|
|
return super.extractItem(slot, amount, simulate);
|
2016-10-01 23:35:39 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 19:05:20 +02:00
|
|
|
private List<GenericItemHandlerInfo> getItemHandlerInfos(){
|
2016-10-29 12:00:00 +02:00
|
|
|
this.queryAndSaveData();
|
|
|
|
return this.genericInfos;
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
private void queryAndSaveData(){
|
|
|
|
if(this.connectedRelay != null){
|
|
|
|
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.connectedRelay.getPos(), this.worldObj);
|
2016-11-03 10:54:28 +01:00
|
|
|
if(network != null && (this.oldNetwork != network || this.lastNetworkChangeAmount != network.changeAmount)){
|
2016-10-29 12:00:00 +02:00
|
|
|
this.genericInfos.clear();
|
|
|
|
this.specificInfos.clear();
|
|
|
|
|
|
|
|
this.connectedRelay.getItemHandlersInNetwork(network, this.genericInfos);
|
|
|
|
if(!this.genericInfos.isEmpty()){
|
|
|
|
Collections.sort(this.genericInfos);
|
|
|
|
|
|
|
|
int slotsQueried = 0;
|
|
|
|
for(GenericItemHandlerInfo info : this.genericInfos){
|
|
|
|
for(IItemHandler handler : info.handlers){
|
|
|
|
for(int i = 0; i < handler.getSlots(); i++){
|
|
|
|
this.specificInfos.put(slotsQueried, new SpecificItemHandlerInfo(handler, i, info.relayInQuestion));
|
|
|
|
slotsQueried++;
|
|
|
|
}
|
2016-05-16 22:52:27 +02:00
|
|
|
}
|
2016-05-09 19:05:20 +02:00
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
|
2016-11-03 10:54:28 +01:00
|
|
|
this.oldNetwork = network;
|
2016-10-29 12:00:00 +02:00
|
|
|
this.lastNetworkChangeAmount = network.changeAmount;
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private SpecificItemHandlerInfo getSwitchedIndexHandler(int i){
|
|
|
|
this.queryAndSaveData();
|
|
|
|
return this.specificInfos.get(i);
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:40:09 +02:00
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public boolean shouldSaveDataOnChangeOrWorldStart(){
|
2016-09-09 18:40:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart(){
|
2016-05-09 19:05:20 +02:00
|
|
|
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);
|
2016-07-04 20:15:41 +02:00
|
|
|
BlockPos pos = this.getPos().offset(side);
|
2016-05-08 21:09:58 +02:00
|
|
|
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){
|
2016-06-11 15:33:44 +02:00
|
|
|
this.connectedRelay = null;
|
|
|
|
return;
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
else{
|
2016-05-09 19:05:20 +02:00
|
|
|
tileFound = (TileEntityLaserRelayItem)tile;
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 15:33:44 +02:00
|
|
|
this.connectedRelay = tileFound;
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canInsertItem(int index, ItemStack stack, EnumFacing direction){
|
2016-05-08 21:09:58 +02:00
|
|
|
return this.isItemValidForSlot(index, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
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-06-06 17:59:01 +02:00
|
|
|
if(this.isWhitelisted(handler, stack, true)){
|
2016-05-10 22:19:15 +02:00
|
|
|
if(ItemStack.areItemsEqual(handler.handler.getStackInSlot(handler.switchedIndex), stack)){
|
2016-11-16 16:59:00 +01:00
|
|
|
ItemStack gaveBack = handler.handler.extractItem(handler.switchedIndex, StackUtil.getStackSize(stack), true);
|
|
|
|
return StackUtil.isValid(gaveBack);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:59:01 +02:00
|
|
|
private boolean isWhitelisted(SpecificItemHandlerInfo handler, ItemStack stack, boolean output){
|
|
|
|
boolean whitelisted = handler.relayInQuestion.isWhitelisted(stack, output);
|
2016-06-11 15:33:44 +02:00
|
|
|
TileEntityLaserRelayItem connected = this.connectedRelay;
|
2016-05-16 22:52:27 +02:00
|
|
|
if(connected != null && connected != handler.relayInQuestion){
|
2016-10-11 14:41:26 +02:00
|
|
|
return whitelisted && connected.isWhitelisted(stack, output);
|
2016-05-11 20:59:38 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return whitelisted;
|
|
|
|
}
|
2016-05-11 20:55:29 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
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-06-06 17:59:01 +02:00
|
|
|
if(this.isWhitelisted(handler, stack, false)){
|
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){
|
2016-11-16 20:31:16 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
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-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(inSlot)){
|
|
|
|
toInsert = StackUtil.addStackSize(toInsert, -StackUtil.getStackSize(inSlot));
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|
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-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stackInSlot)){
|
|
|
|
handler.handler.extractItem(handler.switchedIndex, StackUtil.getStackSize(stackInSlot), 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{
|
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
public final IItemHandler handler;
|
|
|
|
public final int switchedIndex;
|
|
|
|
public final TileEntityLaserRelayItem relayInQuestion;
|
2016-05-09 19:05:20 +02:00
|
|
|
|
|
|
|
public SpecificItemHandlerInfo(IItemHandler handler, int switchedIndex, TileEntityLaserRelayItem relayInQuestion){
|
|
|
|
this.handler = handler;
|
|
|
|
this.switchedIndex = switchedIndex;
|
|
|
|
this.relayInQuestion = relayInQuestion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:00:42 +02:00
|
|
|
public static class GenericItemHandlerInfo implements Comparable<GenericItemHandlerInfo>{
|
2016-05-09 19:05:20 +02:00
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
public final List<IItemHandler> handlers = new ArrayList<IItemHandler>();
|
|
|
|
public final TileEntityLaserRelayItem relayInQuestion;
|
2016-05-09 19:05:20 +02:00
|
|
|
|
|
|
|
public GenericItemHandlerInfo(TileEntityLaserRelayItem relayInQuestion){
|
|
|
|
this.relayInQuestion = relayInQuestion;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:00:42 +02:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public int compareTo(GenericItemHandlerInfo other){
|
2016-11-16 18:51:23 +01:00
|
|
|
int thisPrio = this.relayInQuestion.getPriority();
|
|
|
|
int otherPrio = other.relayInQuestion.getPriority();
|
2016-05-12 19:00:42 +02:00
|
|
|
|
2016-11-16 18:51:23 +01:00
|
|
|
if(thisPrio == otherPrio){
|
|
|
|
return 0;
|
2016-05-16 22:52:27 +02:00
|
|
|
}
|
2016-11-16 18:51:23 +01:00
|
|
|
else if(thisPrio > otherPrio){
|
2016-05-16 22:52:27 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else{
|
2016-11-16 18:51:23 +01:00
|
|
|
return 1;
|
2016-05-12 19:00:42 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-09 19:05:20 +02:00
|
|
|
}
|
2016-05-08 21:09:58 +02:00
|
|
|
}
|