2015-10-19 17:50:43 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("LaserRelayConnectionHandler.java") is part of the Actually Additions mod for Minecraft.
|
2015-10-19 17:50:43 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-10-19 17:50:43 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-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
|
|
|
|
2016-06-04 13:51:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.WorldData;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay;
|
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;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-06-04 14:38:20 +02:00
|
|
|
import net.minecraft.world.World;
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2016-06-17 23:50:38 +02:00
|
|
|
public final class LaserRelayConnectionHandler{
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2016-06-04 13:51:06 +02:00
|
|
|
public static NBTTagCompound writeNetworkToNBT(Network network){
|
2015-10-29 17:36:30 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-04 13:51:06 +02:00
|
|
|
public static Network readNetworkFromNBT(NBTTagCompound tag){
|
2015-10-29 17:36:30 +01:00
|
|
|
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-06-04 14:38:20 +02:00
|
|
|
public static ConcurrentSet<ConnectionPair> getConnectionsFor(BlockPos relay, World world){
|
2015-10-29 17:36:30 +01:00
|
|
|
ConcurrentSet<ConnectionPair> allPairs = new ConcurrentSet<ConnectionPair>();
|
2016-06-04 13:51:06 +02:00
|
|
|
for(Network aNetwork : WorldData.getDataForWorld(world).laserRelayNetworks){
|
2015-10-29 17:36:30 +01:00
|
|
|
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-06-04 14:38:20 +02:00
|
|
|
public static void removeRelayFromNetwork(BlockPos relay, World world){
|
2016-06-04 13:51:06 +02:00
|
|
|
Network network = getNetworkFor(relay, world);
|
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)
|
2016-06-04 13:51:06 +02:00
|
|
|
WorldData.getDataForWorld(world).laserRelayNetworks.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)){
|
2016-06-05 14:33:53 +02:00
|
|
|
addConnection(pair.positions[0], pair.positions[1], world);
|
2015-10-28 20:35:39 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Network for a Relay
|
|
|
|
*/
|
2016-06-04 14:38:20 +02:00
|
|
|
public static Network getNetworkFor(BlockPos relay, World world){
|
2016-06-04 13:51:06 +02:00
|
|
|
for(Network aNetwork : WorldData.getDataForWorld(world).laserRelayNetworks){
|
2015-10-29 17:36:30 +01:00
|
|
|
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-06-04 14:38:20 +02:00
|
|
|
public static boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, World world){
|
2016-07-04 20:15:41 +02:00
|
|
|
int distanceSq = (int)firstRelay.distanceSq(secondRelay);
|
|
|
|
if(distanceSq > TileEntityLaserRelay.MAX_DISTANCE*TileEntityLaserRelay.MAX_DISTANCE || firstRelay.equals(secondRelay)){
|
2015-10-21 18:31:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-04 13:51:06 +02:00
|
|
|
Network firstNetwork = getNetworkFor(firstRelay, world);
|
|
|
|
Network secondNetwork = getNetworkFor(secondRelay, world);
|
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();
|
2016-06-04 13:51:06 +02:00
|
|
|
WorldData.getDataForWorld(world).laserRelayNetworks.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
|
|
|
}
|
2016-06-04 13:51:06 +02:00
|
|
|
//Both relays have laserRelayNetworks
|
2015-10-20 00:22:36 +02:00
|
|
|
else if(firstNetwork != null && secondNetwork != null){
|
2016-06-04 13:51:06 +02:00
|
|
|
mergeNetworks(firstNetwork, secondNetwork, world);
|
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
|
2016-05-16 22:52:27 +02:00
|
|
|
else{
|
2015-10-29 17:36:30 +01:00
|
|
|
secondNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
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());
|
2016-06-04 13:51:06 +02:00
|
|
|
//System.out.println(laserRelayNetworks);
|
2015-10-20 18:06:06 +02:00
|
|
|
return true;
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-04 13:51:06 +02:00
|
|
|
* Merges two laserRelayNetworks together
|
2015-10-19 17:50:43 +02:00
|
|
|
* (Actually puts everything from the second network into the first one and removes the second one)
|
|
|
|
*/
|
2016-06-04 14:38:20 +02:00
|
|
|
public static void mergeNetworks(Network firstNetwork, Network secondNetwork, World world){
|
2015-10-29 17:36:30 +01:00
|
|
|
for(ConnectionPair secondPair : secondNetwork.connections){
|
|
|
|
firstNetwork.connections.add(secondPair);
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2016-06-04 13:51:06 +02:00
|
|
|
WorldData.getDataForWorld(world).laserRelayNetworks.remove(secondNetwork);
|
2015-10-29 21:03:40 +01:00
|
|
|
//System.out.println("Merged Two Networks!");
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 17:50:43 +02:00
|
|
|
public static class ConnectionPair{
|
|
|
|
|
2016-06-05 14:33:53 +02:00
|
|
|
public final BlockPos[] positions = new BlockPos[2];
|
2015-10-19 17:50:43 +02:00
|
|
|
|
2016-01-08 13:31:58 +01:00
|
|
|
public ConnectionPair(BlockPos firstRelay, BlockPos secondRelay){
|
2016-06-05 14:33:53 +02:00
|
|
|
this.positions[0] = firstRelay;
|
|
|
|
this.positions[1] = secondRelay;
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
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){
|
2016-06-05 14:33:53 +02:00
|
|
|
for(BlockPos position : this.positions){
|
2016-07-04 20:15:41 +02:00
|
|
|
if(position != null && position.equals(relay)){
|
2016-06-05 14:33:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-10-20 00:22:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString(){
|
2016-06-05 14:33:53 +02:00
|
|
|
return (this.positions[0] == null ? "-" : this.positions[0].toString())+" | "+(this.positions[1] == null ? "-" : this.positions[1].toString());
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
2015-10-21 00:22:50 +02:00
|
|
|
|
2015-10-29 17:36:30 +01:00
|
|
|
public NBTTagCompound writeToNBT(){
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
2016-06-05 14:33:53 +02:00
|
|
|
for(int i = 0; i < this.positions.length; i++){
|
|
|
|
BlockPos relay = this.positions[i];
|
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;
|
|
|
|
}
|
2016-06-05 12:51:13 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj){
|
|
|
|
if(obj instanceof ConnectionPair){
|
|
|
|
ConnectionPair pair = (ConnectionPair)obj;
|
2016-06-05 14:33:53 +02:00
|
|
|
for(int i = 0; i < this.positions.length; i++){
|
|
|
|
if(this.positions[i] == pair.positions[i] || (this.positions[i] != null && this.positions[i].equals(pair.positions[i]))){
|
2016-06-05 12:51:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.equals(obj);
|
|
|
|
}
|
2015-10-29 17:36:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class Network{
|
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
public final ConcurrentSet<ConnectionPair> connections = new ConcurrentSet<ConnectionPair>();
|
2015-10-29 17:36:30 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString(){
|
|
|
|
return this.connections.toString();
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2016-06-05 12:51:13 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj){
|
|
|
|
if(obj instanceof Network){
|
|
|
|
if(this.connections.equals(((Network)obj).connections)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.equals(obj);
|
|
|
|
}
|
2015-10-19 17:50:43 +02:00
|
|
|
}
|
2015-10-29 15:03:15 +01:00
|
|
|
}
|