Skip to content

Commit 20e1f93

Browse files
committed
Fixed allocator not marking chunks before validation
1 parent 88915ec commit 20e1f93

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Plugin/src/main/java/dev/lrxh/neptune/game/arena/Arena.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import dev.lrxh.api.arena.IArena;
44
import dev.lrxh.blockChanger.snapshot.CuboidSnapshot;
5-
import dev.lrxh.neptune.Neptune;
65
import dev.lrxh.neptune.configs.impl.SettingsLocale;
76
import dev.lrxh.neptune.game.arena.allocator.Allocation;
87
import dev.lrxh.neptune.game.arena.allocator.SpatialAllocator;
@@ -98,6 +97,7 @@ public boolean isSetup() {
9897
}
9998

10099
public synchronized CompletableFuture<Arena> createDuplicate() {
100+
101101
if (snapshot == null) {
102102
CompletableFuture<Arena> failed = new CompletableFuture<>();
103103
failed.completeExceptionally(new IllegalStateException("CuboidSnapshot not ready"));
@@ -127,23 +127,22 @@ public synchronized CompletableFuture<Arena> createDuplicate() {
127127

128128
final int originalMinChunkX = min.getBlockX() >> 4;
129129
final int originalMinChunkZ = min.getBlockZ() >> 4;
130-
131130
final int chunkOffsetX = allocation.chunkX - originalMinChunkX;
132131
final int chunkOffsetZ = allocation.chunkZ - originalMinChunkZ;
133-
134132
final int offsetBlocksX = chunkOffsetX * 16;
135133
final int offsetBlocksZ = chunkOffsetZ * 16;
136134

137-
138135
Location newRedSpawn = (this.redSpawn != null ? LocationUtil.addOffset(this.redSpawn.clone(), offsetBlocksX, offsetBlocksZ) : null);
139136
Location newBlueSpawn = (this.blueSpawn != null ? LocationUtil.addOffset(this.blueSpawn.clone(), offsetBlocksX, offsetBlocksZ) : null);
140137
Location newMin = LocationUtil.addOffset(this.min.clone(), offsetBlocksX, offsetBlocksZ);
141138
Location newMax = LocationUtil.addOffset(this.max.clone(), offsetBlocksX, offsetBlocksZ);
142139

143140
CompletableFuture<Arena> future = new CompletableFuture<>();
141+
144142
snapshot.offset(offsetBlocksX, offsetBlocksZ)
145143
.thenApplyAsync(cuboidSnapshot -> {
146-
cuboidSnapshot.restore(true);
144+
cuboidSnapshot.restore(false);
145+
147146
return new Arena(
148147
this.name + "#" + currentIndex,
149148
this.displayName,
@@ -166,11 +165,13 @@ public synchronized CompletableFuture<Arena> createDuplicate() {
166165
} else {
167166
future.complete(arena);
168167
}
168+
169169
});
170170

171171
return future;
172172
}
173173

174+
174175
public List<String> getWhitelistedBlocksAsString() {
175176
List<String> result = new ArrayList<>();
176177
for (Material mat : whitelistedBlocks) {
@@ -200,7 +201,7 @@ public void remove() {
200201

201202
public void restore() {
202203
if (snapshot != null) {
203-
snapshot.restore(true);
204+
snapshot.restore(false);
204205
}
205206
}
206207

Plugin/src/main/java/dev/lrxh/neptune/game/arena/allocator/SpatialAllocator.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.lrxh.neptune.game.arena.allocator;
22

3-
import dev.lrxh.neptune.Neptune;
43
import dev.lrxh.neptune.configs.impl.SettingsLocale;
54

65
import java.util.concurrent.ConcurrentHashMap;
@@ -95,6 +94,14 @@ private Allocation reserveAt(int startChunkX, int startChunkZ, int widthChunks,
9594
try {
9695
allocations.put(id, alloc);
9796

97+
// ✅ Reserve the region first
98+
for (int x = startChunkX; x < startChunkX + widthChunks; x++) {
99+
for (int z = startChunkZ; z < startChunkZ + depthChunks; z++) {
100+
occupancy.put(keyFor(x, z), id);
101+
}
102+
}
103+
104+
// ✅ Now verify that all chunks were successfully marked
98105
int mismatches = 0;
99106
for (int x = startChunkX; x < startChunkX + widthChunks; x++) {
100107
for (int z = startChunkZ; z < startChunkZ + depthChunks; z++) {
@@ -104,7 +111,9 @@ private Allocation reserveAt(int startChunkX, int startChunkZ, int widthChunks,
104111
}
105112
}
106113
}
114+
107115
if (mismatches > 0) {
116+
// Rollback if verification fails
108117
for (int x = startChunkX; x < startChunkX + widthChunks; x++) {
109118
for (int z = startChunkZ; z < startChunkZ + depthChunks; z++) {
110119
occupancy.remove(keyFor(x, z), id);
@@ -116,6 +125,7 @@ private Allocation reserveAt(int startChunkX, int startChunkZ, int widthChunks,
116125

117126
return alloc;
118127
} catch (RuntimeException ex) {
128+
// Rollback if any unexpected exception occurs
119129
for (int x = startChunkX; x < startChunkX + widthChunks; x++) {
120130
for (int z = startChunkZ; z < startChunkZ + depthChunks; z++) {
121131
occupancy.remove(keyFor(x, z), id);
@@ -126,6 +136,7 @@ private Allocation reserveAt(int startChunkX, int startChunkZ, int widthChunks,
126136
}
127137
}
128138

139+
129140
public synchronized void free(long allocationId) {
130141
Allocation alloc = allocations.remove(allocationId);
131142
if (alloc == null)

0 commit comments

Comments
 (0)