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

223 lines
8.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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.misc.apiimpl;
2016-09-12 16:13:39 +02:00
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.ILaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay;
import io.netty.util.internal.ConcurrentSet;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
2019-05-02 09:10:29 +02:00
public final class LaserRelayConnectionHandler implements ILaserRelayConnectionHandler {
2024-03-02 21:23:08 +01:00
public static CompoundTag writeNetworkToNBT(Network network) {
ListTag list = new ListTag();
2019-05-02 09:10:29 +02:00
for (IConnectionPair pair : network.connections) {
2024-03-02 21:23:08 +01:00
CompoundTag tag = new CompoundTag();
2016-09-12 16:13:39 +02:00
pair.writeToNBT(tag);
2021-02-26 22:15:48 +01:00
list.add(tag);
2015-10-21 00:22:50 +02:00
}
2024-03-02 21:23:08 +01:00
CompoundTag compound = new CompoundTag();
2021-02-26 22:15:48 +01:00
compound.put("Network", list);
return compound;
2015-10-21 00:22:50 +02:00
}
2024-03-02 21:23:08 +01:00
public static Network readNetworkFromNBT(CompoundTag tag) {
ListTag list = tag.getList("Network", 10);
Network network = new Network();
2021-02-26 22:15:48 +01:00
for (int i = 0; i < list.size(); i++) {
2016-09-12 16:13:39 +02:00
ConnectionPair pair = new ConnectionPair();
2021-02-26 22:15:48 +01:00
pair.readFromNBT(list.getCompound(i));
2016-09-12 16:13:39 +02:00
network.connections.add(pair);
2015-10-21 00:22:50 +02:00
}
return network;
}
2016-09-12 20:45:29 +02:00
/**
* Merges two laserRelayNetworks together
* (Actually puts everything from the second network into the first one and removes the second one)
*/
2024-03-02 21:23:08 +01:00
private static void mergeNetworks(Network firstNetwork, Network secondNetwork, Level world) {
2019-05-02 09:10:29 +02:00
for (IConnectionPair secondPair : secondNetwork.connections) {
2016-09-12 20:45:29 +02:00
firstNetwork.connections.add(secondPair);
}
WorldData data = WorldData.get(world);
2017-01-18 15:22:04 +01:00
secondNetwork.changeAmount++;
data.laserRelayNetworks.remove(secondNetwork);
data.setDirty();
2016-09-12 20:45:29 +02:00
//System.out.println("Merged Two Networks!");
}
/**
2015-10-23 16:54:33 +02:00
* Gets all Connections for a Relay
*/
@Override
2024-03-02 21:23:08 +01:00
public ConcurrentSet<IConnectionPair> getConnectionsFor(BlockPos relay, Level world) {
2019-02-27 19:53:05 +01:00
ConcurrentSet<IConnectionPair> allPairs = new ConcurrentSet<>();
2023-01-19 16:27:56 +01:00
if(!world.isClientSide) {
for (Network aNetwork : WorldData.get(world).laserRelayNetworks) {
for (IConnectionPair pair : aNetwork.connections) {
if (pair.contains(relay)) {
allPairs.add(pair);
}
}
}
2023-01-19 16:27:56 +01:00
} //TODO ohhh boy
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
2024-03-02 21:23:08 +01:00
public void removeRelayFromNetwork(BlockPos relay, Level world) {
Network network = this.getNetworkFor(relay, world);
2019-05-02 09:10:29 +02:00
if (network != null) {
network.changeAmount++;
2015-10-23 16:54:33 +02:00
//Setup new network (so that splitting a network will cause it to break into two)
WorldData data = WorldData.get(world);
data.laserRelayNetworks.remove(network);
data.setDirty();
2019-05-02 09:10:29 +02:00
for (IConnectionPair pair : network.connections) {
if (!pair.contains(relay)) {
2016-09-12 16:13:39 +02:00
this.addConnection(pair.getPositions()[0], pair.getPositions()[1], pair.getType(), world, pair.doesSuppressRender());
}
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
2024-03-02 21:23:08 +01:00
public Network getNetworkFor(BlockPos relay, Level world) {
2023-01-19 16:27:56 +01:00
if (world != null && !world.isClientSide) {
2021-02-26 22:15:48 +01:00
for (Network aNetwork : WorldData.get(world).laserRelayNetworks) {
for (IConnectionPair pair : aNetwork.connections) {
if (pair.contains(relay)) {
return aNetwork;
}
}
2015-10-21 00:22:50 +02:00
}
2019-05-02 09:10:29 +02:00
}
2015-10-23 16:54:33 +02:00
return null;
2015-10-21 00:22:50 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, LaserType type, Level world) {
return this.addConnection(firstRelay, secondRelay, type, world, false);
}
/**
* Adds a new connection between two relays
* (Puts it into the correct network!)
*/
@Override
2024-03-02 21:23:08 +01:00
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, LaserType type, Level world, boolean suppressConnectionRender) {
return this.addConnection(firstRelay, secondRelay, type, world, suppressConnectionRender, false);
}
@Override
2024-03-02 21:23:08 +01:00
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, LaserType type, Level world, boolean suppressConnectionRender, boolean removeIfConnected) {
2023-01-19 16:27:56 +01:00
if (world.isClientSide || firstRelay == null || secondRelay == null || firstRelay == secondRelay || firstRelay.equals(secondRelay)) {
2021-02-26 22:15:48 +01:00
return false;
}
WorldData data = WorldData.get(world);
Network firstNetwork = this.getNetworkFor(firstRelay, world);
Network secondNetwork = this.getNetworkFor(secondRelay, world);
//No Network exists
2019-05-02 09:10:29 +02:00
if (firstNetwork == null && secondNetwork == null) {
firstNetwork = new Network();
data.laserRelayNetworks.add(firstNetwork);
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay, type, suppressConnectionRender));
firstNetwork.changeAmount++;
}
//The same Network
2019-05-02 09:10:29 +02:00
else if (firstNetwork == secondNetwork) {
if (removeIfConnected) {
this.removeConnection(world, firstRelay, secondRelay);
return true;
2019-05-02 09:10:29 +02:00
} else {
return false;
}
}
//Both relays have laserRelayNetworks
2019-05-02 09:10:29 +02:00
else if (firstNetwork != null && secondNetwork != null) {
mergeNetworks(firstNetwork, secondNetwork, world);
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay, type, suppressConnectionRender));
firstNetwork.changeAmount++;
}
//Only first network exists
2019-05-02 09:10:29 +02:00
else if (firstNetwork != null) {
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay, type, suppressConnectionRender));
firstNetwork.changeAmount++;
}
//Only second network exists
2019-05-02 09:10:29 +02:00
else {
secondNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay, type, suppressConnectionRender));
secondNetwork.changeAmount++;
}
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);
data.setDirty();
2015-10-20 18:06:06 +02:00
return true;
}
2017-02-13 19:07:53 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void removeConnection(Level world, BlockPos firstRelay, BlockPos secondRelay) {
2023-01-19 16:27:56 +01:00
if (world != null && !world.isClientSide && firstRelay != null && secondRelay != null) {
2017-02-13 19:07:53 +01:00
Network network = this.getNetworkFor(firstRelay, world);
2019-05-02 09:10:29 +02:00
if (network != null) {
2017-02-13 19:07:53 +01:00
network.changeAmount++;
WorldData data = WorldData.get(world);
data.laserRelayNetworks.remove(network);
data.setDirty();
2017-02-13 19:07:53 +01:00
2019-05-02 09:10:29 +02:00
for (IConnectionPair pair : network.connections) {
if (!pair.contains(firstRelay) || !pair.contains(secondRelay)) {
2017-02-13 19:07:53 +01:00
this.addConnection(pair.getPositions()[0], pair.getPositions()[1], pair.getType(), world, pair.doesSuppressRender());
}
}
}
}
}
@Override
2024-03-02 21:23:08 +01:00
public LaserType getTypeFromLaser(BlockEntity tile) {
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityLaserRelay) {
return ((TileEntityLaserRelay) tile).type;
} else {
return null;
}
}
@Override
2024-03-02 21:23:08 +01:00
public LaserType getTypeFromLaser(BlockPos pos, Level world) {
return this.getTypeFromLaser(world.getBlockEntity(pos));
}
2021-02-26 22:15:48 +01:00
}