-
Notifications
You must be signed in to change notification settings - Fork 99
/
LaserRelayConnectionHandler.java
214 lines (191 loc) · 8.27 KB
/
LaserRelayConnectionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* 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
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.misc.apiimpl;
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;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public final class LaserRelayConnectionHandler implements ILaserRelayConnectionHandler {
public static NBTTagCompound writeNetworkToNBT(Network network) {
NBTTagList list = new NBTTagList();
for (IConnectionPair pair : network.connections) {
NBTTagCompound tag = new NBTTagCompound();
pair.writeToNBT(tag);
list.appendTag(tag);
}
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("Network", list);
return compound;
}
public static Network readNetworkFromNBT(NBTTagCompound tag) {
NBTTagList list = tag.getTagList("Network", 10);
Network network = new Network();
for (int i = 0; i < list.tagCount(); i++) {
ConnectionPair pair = new ConnectionPair();
pair.readFromNBT(list.getCompoundTagAt(i));
network.connections.add(pair);
}
return network;
}
/**
* 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 (IConnectionPair secondPair : secondNetwork.connections) {
firstNetwork.connections.add(secondPair);
}
WorldData data = WorldData.get(world);
secondNetwork.changeAmount++;
data.laserRelayNetworks.remove(secondNetwork);
data.markDirty();
//System.out.println("Merged Two Networks!");
}
/**
* Gets all Connections for a Relay
*/
@Override
public ConcurrentSet<IConnectionPair> getConnectionsFor(BlockPos relay, World world) {
ConcurrentSet<IConnectionPair> allPairs = new ConcurrentSet<>();
for (Network aNetwork : WorldData.get(world).laserRelayNetworks) {
for (IConnectionPair pair : aNetwork.connections) {
if (pair.contains(relay)) {
allPairs.add(pair);
}
}
}
return allPairs;
}
/**
* Removes a Relay from its Network
*/
@Override
public void removeRelayFromNetwork(BlockPos relay, World world) {
Network network = this.getNetworkFor(relay, world);
if (network != null) {
network.changeAmount++;
//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.markDirty();
for (IConnectionPair pair : network.connections) {
if (!pair.contains(relay)) {
this.addConnection(pair.getPositions()[0], pair.getPositions()[1], pair.getType(), world, pair.doesSuppressRender());
}
}
//System.out.println("Removing a Relay from the Network!");
}
}
/**
* Gets a Network for a Relay
*/
@Override
public Network getNetworkFor(BlockPos relay, World world) {
if (world != null) for (Network aNetwork : WorldData.get(world).laserRelayNetworks) {
for (IConnectionPair pair : aNetwork.connections) {
if (pair.contains(relay)) { return aNetwork; }
}
}
return null;
}
@Override
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, LaserType type, World world) {
return this.addConnection(firstRelay, secondRelay, type, world, false);
}
/**
* Adds a new connection between two relays
* (Puts it into the correct network!)
*/
@Override
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, LaserType type, World world, boolean suppressConnectionRender) {
return this.addConnection(firstRelay, secondRelay, type, world, suppressConnectionRender, false);
}
@Override
public boolean addConnection(BlockPos firstRelay, BlockPos secondRelay, LaserType type, World world, boolean suppressConnectionRender, boolean removeIfConnected) {
if (firstRelay == null || secondRelay == null || firstRelay == secondRelay || firstRelay.equals(secondRelay)) { return false; }
WorldData data = WorldData.get(world);
Network firstNetwork = this.getNetworkFor(firstRelay, world);
Network secondNetwork = this.getNetworkFor(secondRelay, world);
//No Network exists
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
else if (firstNetwork == secondNetwork) {
if (removeIfConnected) {
this.removeConnection(world, firstRelay, secondRelay);
return true;
} else {
return false;
}
}
//Both relays have laserRelayNetworks
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
else if (firstNetwork != null) {
firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay, type, suppressConnectionRender));
firstNetwork.changeAmount++;
}
//Only second network exists
else {
secondNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay, type, suppressConnectionRender));
secondNetwork.changeAmount++;
}
//System.out.println("Connected "+firstRelay.toString()+" to "+secondRelay.toString());
//System.out.println(firstNetwork == null ? secondNetwork.toString() : firstNetwork.toString());
//System.out.println(laserRelayNetworks);
data.markDirty();
return true;
}
@Override
public void removeConnection(World world, BlockPos firstRelay, BlockPos secondRelay) {
if (world != null && firstRelay != null && secondRelay != null) {
Network network = this.getNetworkFor(firstRelay, world);
if (network != null) {
network.changeAmount++;
WorldData data = WorldData.get(world);
data.laserRelayNetworks.remove(network);
data.markDirty();
for (IConnectionPair pair : network.connections) {
if (!pair.contains(firstRelay) || !pair.contains(secondRelay)) {
this.addConnection(pair.getPositions()[0], pair.getPositions()[1], pair.getType(), world, pair.doesSuppressRender());
}
}
}
}
}
@Override
public LaserType getTypeFromLaser(TileEntity tile) {
if (tile instanceof TileEntityLaserRelay) {
return ((TileEntityLaserRelay) tile).type;
} else {
return null;
}
}
@Override
public LaserType getTypeFromLaser(BlockPos pos, World world) {
return this.getTypeFromLaser(world.getTileEntity(pos));
}
}