2016-05-16 22:52:27 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityLaserRelayItem.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-14 17:50:10 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2016-09-12 16:13:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
|
2016-09-01 18:02:03 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.LaserType;
|
2016-07-30 17:07:32 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2016-06-11 15:33:44 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityItemViewer.GenericItemHandlerInfo;
|
2016-05-14 17:50:10 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-11-16 18:51:23 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraft.util.text.TextFormatting;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-11-03 10:49:41 +01:00
|
|
|
import java.util.HashMap;
|
2016-05-14 17:50:10 +02:00
|
|
|
import java.util.List;
|
2016-08-06 02:17:11 +02:00
|
|
|
import java.util.Map;
|
2016-08-07 12:37:08 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2016-05-14 17:50:10 +02:00
|
|
|
|
|
|
|
public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
|
|
|
|
|
2016-08-07 12:37:08 +02:00
|
|
|
public final Map<BlockPos, IItemHandler> handlersAround = new ConcurrentHashMap<BlockPos, IItemHandler>();
|
2016-11-16 20:31:16 +01:00
|
|
|
public int priority;
|
2016-06-11 15:33:44 +02:00
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
public TileEntityLaserRelayItem(String name){
|
2016-09-01 18:02:03 +02:00
|
|
|
super(name, LaserType.ITEM);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public TileEntityLaserRelayItem(){
|
|
|
|
this("laserRelayItem");
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:51:23 +01:00
|
|
|
public int getPriority(){
|
|
|
|
return this.priority;
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:59:01 +02:00
|
|
|
public boolean isWhitelisted(ItemStack stack, boolean output){
|
2016-05-14 17:50:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-11 15:33:44 +02:00
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart(){
|
2016-11-08 20:04:36 +01:00
|
|
|
Map<BlockPos, IItemHandler> old = new HashMap<BlockPos, IItemHandler>(this.handlersAround);
|
2016-11-03 10:49:41 +01:00
|
|
|
boolean change = false;
|
2016-06-11 15:33:44 +02:00
|
|
|
|
2016-11-03 10:49:41 +01:00
|
|
|
this.handlersAround.clear();
|
2016-05-14 17:50:10 +02:00
|
|
|
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-11-26 21:32:27 +01:00
|
|
|
TileEntity tile = this.world.getTileEntity(pos);
|
2016-08-06 02:17:11 +02:00
|
|
|
if(tile != null && !(tile instanceof TileEntityItemViewer) && !(tile instanceof TileEntityLaserRelay)){
|
2016-05-14 17:50:10 +02:00
|
|
|
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
|
|
|
|
if(handler != null){
|
2016-11-03 10:49:41 +01:00
|
|
|
this.handlersAround.put(pos, handler);
|
|
|
|
|
|
|
|
IItemHandler oldHandler = old.get(pos);
|
|
|
|
if(oldHandler == null || !handler.equals(oldHandler)){
|
|
|
|
change = true;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
|
2016-12-04 00:10:52 +01:00
|
|
|
if(change || old.size() != this.handlersAround.size()){
|
2016-11-03 10:49:41 +01:00
|
|
|
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.getPos(), this.getWorld());
|
|
|
|
if(network != null){
|
|
|
|
network.changeAmount++;
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
public void getItemHandlersInNetwork(Network network, List<GenericItemHandlerInfo> storeList){
|
2016-08-06 02:17:11 +02:00
|
|
|
//Keeps track of all the Laser Relays and Item Handlers that have been checked already to make nothing run multiple times
|
|
|
|
List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
|
|
|
|
|
2016-09-12 16:13:39 +02:00
|
|
|
for(IConnectionPair pair : network.connections){
|
|
|
|
for(BlockPos relay : pair.getPositions()){
|
2016-08-06 02:17:11 +02:00
|
|
|
if(relay != null && !alreadyChecked.contains(relay)){
|
|
|
|
alreadyChecked.add(relay);
|
2016-11-26 21:32:27 +01:00
|
|
|
TileEntity aRelayTile = this.world.getTileEntity(relay);
|
2016-06-11 15:33:44 +02:00
|
|
|
if(aRelayTile instanceof TileEntityLaserRelayItem){
|
|
|
|
TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem)aRelayTile;
|
2016-08-06 02:17:11 +02:00
|
|
|
GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-08-06 02:17:11 +02:00
|
|
|
Map<BlockPos, IItemHandler> handlersAroundTile = relayTile.handlersAround;
|
|
|
|
for(Map.Entry<BlockPos, IItemHandler> handler : handlersAroundTile.entrySet()){
|
|
|
|
if(!alreadyChecked.contains(handler.getKey())){
|
|
|
|
alreadyChecked.add(handler.getKey());
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-08-06 02:17:11 +02:00
|
|
|
info.handlers.add(handler.getValue());
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2016-08-06 02:17:11 +02:00
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
storeList.add(info);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 18:51:23 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
compound.setInteger("Priority", this.priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 15:09:37 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public String getExtraDisplayString(){
|
|
|
|
return "Priority: "+TextFormatting.DARK_RED+this.getPriority()+TextFormatting.RESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public String getCompassDisplayString(){
|
|
|
|
return TextFormatting.GREEN+"Right-Click to increase! \nSneak-Right-Click to decrease!";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCompassAction(EntityPlayer player){
|
|
|
|
if(player.isSneaking()){
|
|
|
|
this.priority--;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.priority++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:51:23 +01:00
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
this.priority = compound.getInteger("Priority");
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|