2015-10-19 17:50:43 +02:00
|
|
|
/*
|
|
|
|
* This file ("LaserRelayConnectionHandler.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
|
2016-01-03 16:05:51 +01:00
|
|
|
* http://ellpeck.de/actaddlicense/
|
2015-10-19 17:50:43 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-01-03 16:05:51 +01:00
|
|
|
* © 2016 Ellpeck
|
2015-10-19 17:50:43 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.misc;
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2015-10-20 00:22:36 +02:00
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay;
|
2016-01-08 13:31:58 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2015-10-29 17:36:30 +01:00
|
|
|
import io.netty.util.internal.ConcurrentSet;
|
2015-10-21 00:22:50 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-10-29 17:36:30 +01:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2015-10-20 00:22:36 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-01-08 13:31:58 +01:00
|
|
|
import net.minecraft.util.BlockPos;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2015-12-23 01:43:49 +01:00
|
|
|
import net.minecraft.world.World;
|
2015-10-19 17:50:43 +02:00
|
|
|
|
|
|
|
public class LaserRelayConnectionHandler{
|
|
|
|
|
|
|
|
private static LaserRelayConnectionHandler instance;
|
|
|
|
|
|
|
|
/**
|
2015-10-29 17:36:30 +01:00
|
|
|
* All of the Networks
|
2015-10-19 17:50:43 +02:00
|
|
|
*/
|
2015-10-29 17:36:30 +01:00
|
|
|
public ConcurrentSet<Network> networks = new ConcurrentSet<Network>();
|
2015-10-19 17:50:43 +02:00
|
|
|
|
|
|
|
public static LaserRelayConnectionHandler getInstance(){
|
2016-03-18 16:13:09 +01:00
|
|
|
if(instance == null){
|
|
|
|
instance = new LaserRelayConnectionHandler();
|
|
|
|
}
|
2015-10-19 17:50:43 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:36:30 +01:00
|
|
|
public NBTTagCompound writeNetworkToNBT(Network network){
|
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
for(ConnectionPair pair : network.connections){
|
|
|
|
list.appendTag(pair.writeToNBT());
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-10-29 17:36:30 +01:00
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
compound.setTag("Network", list);
|
|
|
|
return compound;
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
|
|
|
|
2015-10-29 17:36:30 +01:00
|
|
|
public Network readNetworkFromNBT(NBTTagCompound tag){
|
|
|
|
NBTTagList list = tag.getTagList("Network", 10);
|
|
|
|
Network network = new Network();
|
|
|
|
for(int i = 0; i < list.tagCount(); i++){
|
|
|
|
network.connections.add(ConnectionPair.readFromNBT(list.getCompoundTagAt(i)));
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:50:43 +02:00
|
|
|
/**
|
2015-10-23 16:54:33 +02:00
|
|
|
* Gets all Connections for a Relay
|
2015-10-19 17:50:43 +02:00
|
|
|
*/
|
2016-01-08 13:31:58 +01:00
|
|
|
public ConcurrentSet<ConnectionPair> getConnectionsFor(BlockPos relay){
|
2015-10-29 17:36:30 +01:00
|
|
|
ConcurrentSet<ConnectionPair> allPairs = new ConcurrentSet<ConnectionPair>();
|
|
|
|
for(Network aNetwork : this.networks){
|
|
|
|
for(ConnectionPair pair : aNetwork.connections){
|
2015-10-19 17:50:43 +02:00
|
|
|
if(pair.contains(relay)){
|
2015-10-23 16:54:33 +02:00
|
|
|
allPairs.add(pair);
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 16:54:33 +02:00
|
|
|
return allPairs;
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
2015-10-21 00:22:50 +02:00
|
|
|
/**
|
2015-10-23 16:54:33 +02:00
|
|
|
* Removes a Relay from its Network
|
2015-10-21 00:22:50 +02:00
|
|
|
*/
|
2016-01-08 13:31:58 +01:00
|
|
|
public void removeRelayFromNetwork(BlockPos relay){
|
2015-10-29 17:36:30 +01:00
|
|
|
Network network = this.getNetworkFor(relay);
|
2015-10-23 16:54:33 +02:00
|
|
|
if(network != null){
|
|
|
|
//Setup new network (so that splitting a network will cause it to break into two)
|
|
|
|
this.networks.remove(network);
|
2015-10-29 17:36:30 +01:00
|
|
|
for(ConnectionPair pair : network.connections){
|
2015-10-28 20:35:39 +01:00
|
|
|
if(!pair.contains(relay)){
|
|
|
|
this.addConnection(pair.firstRelay, pair.secondRelay);
|
|
|
|
}
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
2015-10-29 21:03:40 +01:00
|
|
|
//System.out.println("Removing a Relay from the Network!");
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
2015-11-15 13:26:14 +01:00
|
|
|
WorldData.makeDirty();
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Network for a Relay
|
|
|
|
*/
|
2016-01-08 13:31:58 +01:00
|
|
|
public Network getNetworkFor(BlockPos relay){
|
2015-10-29 17:36:30 +01:00
|
|
|
for(Network aNetwork : this.networks){
|
|
|
|
for(ConnectionPair pair : aNetwork.connections){
|
2015-10-21 00:22:50 +02:00
|
|
|
if(pair.contains(relay)){
|
2015-10-23 16:54:33 +02:00
|
|
|
return aNetwork;
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 16:54:33 +02:00
|
|
|
return null;
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 17:50:43 +02:00
|
|
|
/**
|
|
|
|
* Adds a new connection between two relays
|
|
|
|
* (Puts it into the correct network!)
|
|
|
|
*/
|
2016-01-08 13:31:58 +01:00
|
|
|
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay){
|
|
|
|
int distance = (int)PosUtil.toVec(firstRelay).distanceTo(PosUtil.toVec(secondRelay));
|
|
|
|
if(distance > TileEntityLaserRelay.MAX_DISTANCE || PosUtil.areSamePos(firstRelay, secondRelay)){
|
2015-10-21 18:31:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:36:30 +01:00
|
|
|
Network firstNetwork = this.getNetworkFor(firstRelay);
|
|
|
|
Network secondNetwork = this.getNetworkFor(secondRelay);
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2015-10-20 00:22:36 +02:00
|
|
|
//No Network exists
|
|
|
|
if(firstNetwork == null && secondNetwork == null){
|
2015-10-29 17:36:30 +01:00
|
|
|
firstNetwork = new Network();
|
2015-10-20 00:22:36 +02:00
|
|
|
this.networks.add(firstNetwork);
|
2015-10-29 17:36:30 +01:00
|
|
|
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
//The same Network
|
|
|
|
else if(firstNetwork == secondNetwork){
|
2015-10-20 18:06:06 +02:00
|
|
|
return false;
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
//Both relays have networks
|
|
|
|
else if(firstNetwork != null && secondNetwork != null){
|
2015-10-19 17:50:43 +02:00
|
|
|
this.mergeNetworks(firstNetwork, secondNetwork);
|
2015-10-29 17:36:30 +01:00
|
|
|
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-10-20 00:22:36 +02:00
|
|
|
//Only first network exists
|
2015-10-19 17:50:43 +02:00
|
|
|
else if(firstNetwork != null){
|
2015-10-29 17:36:30 +01:00
|
|
|
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-10-20 00:22:36 +02:00
|
|
|
//Only second network exists
|
2015-10-19 17:50:43 +02:00
|
|
|
else if(secondNetwork != null){
|
2015-10-29 17:36:30 +01:00
|
|
|
secondNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-11-15 13:26:14 +01:00
|
|
|
WorldData.makeDirty();
|
2015-10-29 21:03:40 +01:00
|
|
|
//System.out.println("Connected "+firstRelay.toString()+" to "+secondRelay.toString());
|
|
|
|
//System.out.println(firstNetwork == null ? secondNetwork.toString() : firstNetwork.toString());
|
|
|
|
//System.out.println(this.networks);
|
2015-10-20 18:06:06 +02:00
|
|
|
return true;
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges two networks together
|
|
|
|
* (Actually puts everything from the second network into the first one and removes the second one)
|
|
|
|
*/
|
2015-10-29 17:36:30 +01:00
|
|
|
public void mergeNetworks(Network firstNetwork, Network secondNetwork){
|
|
|
|
for(ConnectionPair secondPair : secondNetwork.connections){
|
|
|
|
firstNetwork.connections.add(secondPair);
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
this.networks.remove(secondNetwork);
|
2015-11-15 13:26:14 +01:00
|
|
|
WorldData.makeDirty();
|
2015-10-29 21:03:40 +01:00
|
|
|
//System.out.println("Merged Two Networks!");
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
public int transferEnergyToReceiverInNeed(World world, BlockPos energyGottenFrom, Network network, int maxTransfer, boolean simulate){
|
2015-10-21 19:14:57 +02:00
|
|
|
int transmitted = 0;
|
2015-10-20 00:22:36 +02:00
|
|
|
//Go through all of the connections in the network
|
2015-10-29 17:36:30 +01:00
|
|
|
for(ConnectionPair pair : network.connections){
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos[] relays = new BlockPos[]{pair.firstRelay, pair.secondRelay};
|
2015-10-20 00:22:36 +02:00
|
|
|
//Go through both relays in the connection
|
2016-01-08 13:31:58 +01:00
|
|
|
for(BlockPos relay : relays){
|
2015-10-20 00:22:36 +02:00
|
|
|
if(relay != null){
|
|
|
|
//Get every side of the relay
|
|
|
|
for(int i = 0; i <= 5; i++){
|
2016-01-07 18:20:59 +01:00
|
|
|
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
|
2015-10-29 21:02:10 +01:00
|
|
|
//Get the Position at the side
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0);
|
|
|
|
if(!PosUtil.areSamePos(pos, energyGottenFrom)){
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2015-10-29 21:02:10 +01:00
|
|
|
if(tile instanceof IEnergyReceiver && !(tile instanceof TileEntityLaserRelay)){
|
|
|
|
IEnergyReceiver receiver = (IEnergyReceiver)tile;
|
|
|
|
if(receiver.canConnectEnergy(side.getOpposite())){
|
|
|
|
//Transfer the energy (with the energy loss!)
|
|
|
|
int theoreticalReceived = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), maxTransfer-transmitted, true);
|
|
|
|
//The amount of energy lost during a transfer
|
|
|
|
int deduct = (int)(theoreticalReceived*((double)ConfigIntValues.LASER_RELAY_LOSS.getValue()/100));
|
|
|
|
|
|
|
|
transmitted += ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), theoreticalReceived-deduct, simulate);
|
|
|
|
transmitted += deduct;
|
2015-11-12 17:56:57 +01:00
|
|
|
|
|
|
|
//If everything that could be transmitted was transmitted
|
|
|
|
if(transmitted >= maxTransfer){
|
|
|
|
return transmitted;
|
|
|
|
}
|
2015-10-29 21:02:10 +01:00
|
|
|
}
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 19:14:57 +02:00
|
|
|
return transmitted;
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class ConnectionPair{
|
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
public BlockPos firstRelay;
|
|
|
|
public BlockPos secondRelay;
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
public ConnectionPair(BlockPos firstRelay, BlockPos secondRelay){
|
2015-10-19 17:50:43 +02:00
|
|
|
this.firstRelay = firstRelay;
|
|
|
|
this.secondRelay = secondRelay;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:36:30 +01:00
|
|
|
public static ConnectionPair readFromNBT(NBTTagCompound compound){
|
2015-12-23 01:43:49 +01:00
|
|
|
if(compound != null){
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos[] pos = new BlockPos[2];
|
2015-12-23 01:43:49 +01:00
|
|
|
for(int i = 0; i < pos.length; i++){
|
|
|
|
int anX = compound.getInteger("x"+i);
|
|
|
|
int aY = compound.getInteger("y"+i);
|
|
|
|
int aZ = compound.getInteger("z"+i);
|
2016-01-08 13:31:58 +01:00
|
|
|
pos[i] = new BlockPos(anX, aY, aZ);
|
2015-12-23 01:43:49 +01:00
|
|
|
}
|
|
|
|
return new ConnectionPair(pos[0], pos[1]);
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
2015-12-23 01:43:49 +01:00
|
|
|
return null;
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
public boolean contains(BlockPos relay){
|
|
|
|
return (this.firstRelay != null && PosUtil.areSamePos(firstRelay, relay)) || (this.secondRelay != null && PosUtil.areSamePos(secondRelay, relay));
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-10-20 00:22:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString(){
|
|
|
|
return (this.firstRelay == null ? "-" : this.firstRelay.toString())+" | "+(this.secondRelay == null ? "-" : this.secondRelay.toString());
|
|
|
|
}
|
2015-10-21 00:22:50 +02:00
|
|
|
|
2015-10-29 17:36:30 +01:00
|
|
|
public NBTTagCompound writeToNBT(){
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
2015-10-21 00:22:50 +02:00
|
|
|
for(int i = 0; i < 2; i++){
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos relay = i == 0 ? this.firstRelay : this.secondRelay;
|
2015-10-29 17:36:30 +01:00
|
|
|
compound.setInteger("x"+i, relay.getX());
|
|
|
|
compound.setInteger("y"+i, relay.getY());
|
|
|
|
compound.setInteger("z"+i, relay.getZ());
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-10-29 17:36:30 +01:00
|
|
|
return compound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Network{
|
|
|
|
|
|
|
|
public ConcurrentSet<ConnectionPair> connections = new ConcurrentSet<ConnectionPair>();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString(){
|
|
|
|
return this.connections.toString();
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-10-29 15:03:15 +01:00
|
|
|
}
|