ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/misc/LaserRelayConnectionHandler.java

147 lines
5.5 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +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-05-16 22:52:27 +02:00
* 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-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.misc;
import de.ellpeck.actuallyadditions.api.laser.ConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.ILaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
import io.netty.util.internal.ConcurrentSet;
2015-10-21 00:22:50 +02:00
import net.minecraft.nbt.NBTTagCompound;
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;
public final class LaserRelayConnectionHandler implements ILaserRelayConnectionHandler{
public static 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
}
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("Network", list);
return compound;
2015-10-21 00:22:50 +02:00
}
public static 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-23 16:54:33 +02:00
* Gets all Connections for a Relay
*/
@Override
public ConcurrentSet<ConnectionPair> getConnectionsFor(BlockPos relay, World world){
ConcurrentSet<ConnectionPair> allPairs = new ConcurrentSet<ConnectionPair>();
for(Network aNetwork : WorldData.getDataForWorld(world).laserRelayNetworks){
for(ConnectionPair pair : aNetwork.connections){
if(pair.contains(relay)){
2015-10-23 16:54:33 +02:00
allPairs.add(pair);
}
}
}
2015-10-23 16:54:33 +02:00
return allPairs;
}
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
*/
@Override
public void removeRelayFromNetwork(BlockPos relay, World world){
Network network = this.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)
WorldData.getDataForWorld(world).laserRelayNetworks.remove(network);
for(ConnectionPair pair : network.connections){
if(!pair.contains(relay)){
this.addConnection(pair.positions[0], pair.positions[1], world);
}
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
*/
@Override
public Network getNetworkFor(BlockPos relay, World world){
for(Network aNetwork : WorldData.getDataForWorld(world).laserRelayNetworks){
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
}
/**
* Adds a new connection between two relays
* (Puts it into the correct network!)
*/
@Override
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, World world){
if(firstRelay == null || secondRelay == null || firstRelay == secondRelay || firstRelay.equals(secondRelay)){
return false;
}
Network firstNetwork = this.getNetworkFor(firstRelay, world);
Network secondNetwork = this.getNetworkFor(secondRelay, world);
//No Network exists
if(firstNetwork == null && secondNetwork == null){
firstNetwork = new Network();
WorldData.getDataForWorld(world).laserRelayNetworks.add(firstNetwork);
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
}
//The same Network
else if(firstNetwork == secondNetwork){
2015-10-20 18:06:06 +02:00
return false;
}
//Both relays have laserRelayNetworks
else if(firstNetwork != null && secondNetwork != null){
mergeNetworks(firstNetwork, secondNetwork, world);
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
}
//Only first network exists
else if(firstNetwork != null){
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
}
//Only second network exists
2016-05-16 22:52:27 +02:00
else{
secondNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay));
}
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(laserRelayNetworks);
2015-10-20 18:06:06 +02:00
return true;
}
/**
* Merges two laserRelayNetworks together
* (Actually puts everything from the second network into the first one and removes the second one)
*/
private static void mergeNetworks(Network firstNetwork, Network secondNetwork, World world){
for(ConnectionPair secondPair : secondNetwork.connections){
firstNetwork.connections.add(secondPair);
}
WorldData.getDataForWorld(world).laserRelayNetworks.remove(secondNetwork);
2015-10-29 21:03:40 +01:00
//System.out.println("Merged Two Networks!");
}
}