-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluidRegistry.java
More file actions
229 lines (202 loc) · 6.48 KB
/
Copy pathFluidRegistry.java
File metadata and controls
229 lines (202 loc) · 6.48 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.fluidphysics.core;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.fluids.FluidType;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Global registry for tracking active fluid blocks.
* Optimizes updates by only processing active fluid areas.
* Manages chunk loading/unloading for fluid data.
*/
public class FluidRegistry {
// Singleton instance
private static FluidRegistry instance;
// Active fluid blocks that need processing
private final Set<BlockPos> activeBlocks = new HashSet<>();
// Fluid data storage
private final Map<BlockPos, FluidLevelData> fluidDataMap = new HashMap<>();
// Configuration
private final FluidPhysicsConfig config;
/**
* Private constructor for singleton pattern.
*
* @param config The fluid physics configuration
*/
private FluidRegistry(FluidPhysicsConfig config) {
this.config = config;
}
/**
* Gets the singleton instance, creating it if necessary.
*
* @param config The fluid physics configuration
* @return The singleton instance
*/
public static synchronized FluidRegistry getInstance(FluidPhysicsConfig config) {
if (instance == null) {
instance = new FluidRegistry(config);
}
return instance;
}
/**
* Registers a fluid block for processing.
*
* @param pos The position of the fluid block
* @param state The block state
*/
public void registerFluidBlock(BlockPos pos, BlockState state) {
// Create fluid data from the block state
FluidLevelData fluidData = FluidLevelData.fromFluidState(state.getFluidState());
// Only register if it's not empty
if (!fluidData.isEmpty()) {
fluidDataMap.put(pos.immutable(), fluidData);
activeBlocks.add(pos.immutable());
}
}
/**
* Unregisters a fluid block.
*
* @param pos The position of the fluid block
*/
public void unregisterFluidBlock(BlockPos pos) {
fluidDataMap.remove(pos.immutable());
activeBlocks.remove(pos.immutable());
}
/**
* Gets the fluid data for a block.
*
* @param pos The position of the fluid block
* @return The fluid data, or null if not registered
*/
public FluidLevelData getFluidData(BlockPos pos) {
return fluidDataMap.get(pos.immutable());
}
/**
* Sets the fluid data for a block.
*
* @param pos The position of the fluid block
* @param fluidData The fluid data
*/
public void setFluidData(BlockPos pos, FluidLevelData fluidData) {
if (fluidData.isEmpty()) {
unregisterFluidBlock(pos);
} else {
fluidDataMap.put(pos.immutable(), fluidData);
activeBlocks.add(pos.immutable());
}
}
/**
* Gets all active fluid blocks.
*
* @return A set of active block positions
*/
public Set<BlockPos> getActiveBlocks() {
return new HashSet<>(activeBlocks);
}
/**
* Marks a block as active for processing.
*
* @param pos The position to mark
*/
public void markActive(BlockPos pos) {
activeBlocks.add(pos.immutable());
}
/**
* Marks a block as inactive.
*
* @param pos The position to mark
*/
public void markInactive(BlockPos pos) {
activeBlocks.remove(pos.immutable());
}
/**
* Checks if a block is active.
*
* @param pos The position to check
* @return True if the block is active
*/
public boolean isActive(BlockPos pos) {
return activeBlocks.contains(pos.immutable());
}
/**
* Handles chunk loading.
*
* @param world The Minecraft world
* @param chunkX The chunk X coordinate
* @param chunkZ The chunk Z coordinate
*/
public void onChunkLoad(Level world, int chunkX, int chunkZ) {
// Scan the chunk for fluid blocks
scanChunkForFluids(world, chunkX, chunkZ);
}
/**
* Handles chunk unloading.
*
* @param chunkX The chunk X coordinate
* @param chunkZ The chunk Z coordinate
*/
public void onChunkUnload(int chunkX, int chunkZ) {
// Remove all fluid data in this chunk
removeFluidDataInChunk(chunkX, chunkZ);
}
/**
* Scans a chunk for fluid blocks.
*
* @param world The Minecraft world
* @param chunkX The chunk X coordinate
* @param chunkZ The chunk Z coordinate
*/
private void scanChunkForFluids(Level world, int chunkX, int chunkZ) {
int startX = chunkX << 4;
int startZ = chunkZ << 4;
// Scan all blocks in the chunk
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = world.getMinBuildHeight(); y < world.getMaxBuildHeight(); y++) {
BlockPos pos = new BlockPos(startX + x, y, startZ + z);
BlockState state = world.getBlockState(pos);
// If this is a fluid block, register it
if (!state.getFluidState().isEmpty()) {
registerFluidBlock(pos, state);
}
}
}
}
}
/**
* Removes all fluid data in a chunk.
*
* @param chunkX The chunk X coordinate
* @param chunkZ The chunk Z coordinate
*/
private void removeFluidDataInChunk(int chunkX, int chunkZ) {
int startX = chunkX << 4;
int startZ = chunkZ << 4;
int endX = startX + 15;
int endZ = startZ + 15;
// Create a list of positions to remove
Set<BlockPos> toRemove = new HashSet<>();
// Find all positions in this chunk
for (BlockPos pos : fluidDataMap.keySet()) {
if (pos.getX() >= startX && pos.getX() <= endX &&
pos.getZ() >= startZ && pos.getZ() <= endZ) {
toRemove.add(pos);
}
}
// Remove all positions
for (BlockPos pos : toRemove) {
fluidDataMap.remove(pos);
activeBlocks.remove(pos);
}
}
/**
* Clears all fluid data.
*/
public void clear() {
fluidDataMap.clear();
activeBlocks.clear();
}
}