-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathWorldPipeNet.java
executable file
·166 lines (143 loc) · 5.87 KB
/
WorldPipeNet.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
package gregtech.api.pipenet;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.storage.WorldSavedData;
import net.minecraftforge.common.util.Constants.NBT;
import java.lang.ref.WeakReference;
import java.util.*;
public abstract class WorldPipeNet<NodeDataType, T extends PipeNet<NodeDataType>> extends WorldSavedData {
private WeakReference<World> worldRef = new WeakReference<>(null);
protected boolean isFirstTick = true;
protected List<T> pipeNets = new ArrayList<>();
protected Map<ChunkPos, List<T>> pipeNetsByChunk = new HashMap<>();
// Used to do the old processing where a singleton is maintained
protected boolean old = false;
public static String getDataID(final String baseID, final World world) {
if (world == null || world.isRemote)
throw new RuntimeException("WorldPipeNet should only be created on the server!");
final int dimension = world.provider.getDimension();
return baseID + '.' + dimension;
}
public WorldPipeNet(String name) {
super(name);
}
public World getWorld() {
return this.worldRef.get();
}
protected void setWorldAndInit(World world) {
// The original way was to setup one WorldPipeNet
if (old) {
if (this.isFirstTick) {
this.worldRef = new WeakReference<World>(world);
this.isFirstTick = false;
onWorldSet();
}
}
// The correct way is to have to a WorldPipeNet per dimension
// which will change as the dimensions are loaded/unloaded
else if (world != this.worldRef.get()) {
this.worldRef = new WeakReference<World>(world);
onWorldSet();
}
}
protected void onWorldSet() {
this.pipeNets.forEach(PipeNet::onConnectionsUpdate);
}
public void addNode(BlockPos nodePos, NodeDataType nodeData, int mark, int blockedConnections, boolean isActive) {
T myPipeNet = null;
Node<NodeDataType> node = new Node<>(nodeData, blockedConnections, mark, isActive);
for (EnumFacing facing : EnumFacing.VALUES) {
BlockPos offsetPos = nodePos.offset(facing);
T pipeNet = getNetFromPos(offsetPos);
Node<NodeDataType> secondNode = pipeNet == null ? null : pipeNet.getAllNodes().get(offsetPos);
if (pipeNet != null && pipeNet.canAttachNode(nodeData) &&
pipeNet.canNodesConnect(secondNode, facing.getOpposite(), node, null)) {
if (myPipeNet == null) {
myPipeNet = pipeNet;
myPipeNet.addNode(nodePos, node);
} else if (myPipeNet != pipeNet) {
myPipeNet.uniteNetworks(pipeNet);
}
}
}
if (myPipeNet == null) {
myPipeNet = createNetInstance();
myPipeNet.addNode(nodePos, node);
addPipeNet(myPipeNet);
markDirty();
}
}
protected void addPipeNetToChunk(ChunkPos chunkPos, T pipeNet) {
this.pipeNetsByChunk.computeIfAbsent(chunkPos, any -> new ArrayList<>()).add(pipeNet);
}
protected void removePipeNetFromChunk(ChunkPos chunkPos, T pipeNet) {
List<T> list = this.pipeNetsByChunk.get(chunkPos);
if (list != null) list.remove(pipeNet);
if (list.isEmpty()) this.pipeNetsByChunk.remove(chunkPos);
}
public void removeNode(BlockPos nodePos) {
T pipeNet = getNetFromPos(nodePos);
if (pipeNet != null) {
pipeNet.removeNode(nodePos);
}
}
public void updateBlockedConnections(BlockPos nodePos, EnumFacing side, boolean isBlocked) {
T pipeNet = getNetFromPos(nodePos);
if (pipeNet != null) {
pipeNet.updateBlockedConnections(nodePos, side, isBlocked);
}
}
public void updateMark(BlockPos nodePos, int newMark) {
T pipeNet = getNetFromPos(nodePos);
if (pipeNet != null) {
pipeNet.updateMark(nodePos, newMark);
}
}
public T getNetFromPos(BlockPos blockPos) {
List<T> pipeNetsInChunk = pipeNetsByChunk.getOrDefault(new ChunkPos(blockPos), Collections.emptyList());
for (T pipeNet : pipeNetsInChunk) {
if (pipeNet.containsNode(blockPos))
return pipeNet;
}
return null;
}
protected void addPipeNet(T pipeNet) {
addPipeNetSilently(pipeNet);
}
protected void addPipeNetSilently(T pipeNet) {
this.pipeNets.add(pipeNet);
pipeNet.getContainedChunks().forEach(chunkPos -> addPipeNetToChunk(chunkPos, pipeNet));
pipeNet.isValid = true;
}
protected void removePipeNet(T pipeNet) {
this.pipeNets.remove(pipeNet);
pipeNet.getContainedChunks().forEach(chunkPos -> removePipeNetFromChunk(chunkPos, pipeNet));
pipeNet.isValid = false;
}
protected abstract T createNetInstance();
@Override
public void readFromNBT(NBTTagCompound nbt) {
this.pipeNets = new ArrayList<>();
NBTTagList allEnergyNets = nbt.getTagList("PipeNets", NBT.TAG_COMPOUND);
for (int i = 0; i < allEnergyNets.tagCount(); i++) {
NBTTagCompound pNetTag = allEnergyNets.getCompoundTagAt(i);
T pipeNet = createNetInstance();
pipeNet.deserializeNBT(pNetTag);
addPipeNetSilently(pipeNet);
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
NBTTagList allPipeNets = new NBTTagList();
for (T pipeNet : pipeNets) {
NBTTagCompound pNetTag = pipeNet.serializeNBT();
allPipeNets.appendTag(pNetTag);
}
compound.setTag("PipeNets", allPipeNets);
return compound;
}
}