Skip to content

Commit 988c8ab

Browse files
committed
Optimize plot merging
1 parent 04a0f0b commit 988c8ab

5 files changed

Lines changed: 38 additions & 147 deletions

File tree

src/main/java/de/kcodeyt/plotplugin/generator/PlotGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public void regenerateChunk(PlotManager plotManager, FullChunk fullChunk, boolea
101101
for(Entity entity : new ArrayList<>(fullChunk.getEntities().values())) {
102102
if(entity instanceof Player) continue;
103103

104-
if(!REGENERATE_ALLOWED.isAllowed(shapes[((entity.getFloorZ() & 15) << 4) | (entity.getFloorX() & 15)]))
104+
if(REGENERATE_ALLOWED.isDisallowed(shapes[((entity.getFloorZ() & 15) << 4) | (entity.getFloorX() & 15)]))
105105
continue;
106106
toClose0.add(entity);
107107
}
108108

109109
for(BlockEntity blockEntity : new ArrayList<>(fullChunk.getBlockEntities().values())) {
110-
if(!REGENERATE_ALLOWED.isAllowed(shapes[((blockEntity.getFloorZ() & 15) << 4) | (blockEntity.getFloorX() & 15)]))
110+
if(REGENERATE_ALLOWED.isDisallowed(shapes[((blockEntity.getFloorZ() & 15) << 4) | (blockEntity.getFloorX() & 15)]))
111111
continue;
112112
toClose1.add(blockEntity);
113113
}
@@ -143,7 +143,7 @@ private void preGenerateChunk(PlotManager plotManager, FullChunk fullChunk, Shap
143143
for(int xBlock = 0; xBlock < 16; ++xBlock) {
144144
for(int zBlock = 0; zBlock < 16; ++zBlock) {
145145
final ShapeType shapeType = shapes[(zBlock << 4) | xBlock];
146-
if(!allowedShapes.isAllowed(shapeType)) continue;
146+
if(allowedShapes.isDisallowed(shapeType)) continue;
147147

148148
if(shapeType == ShapeType.PLOT) fullChunk.setBiomeId(xBlock, zBlock, levelSettings.getPlotBiome());
149149
else fullChunk.setBiomeId(xBlock, zBlock, levelSettings.getRoadBiome());

src/main/java/de/kcodeyt/plotplugin/manager/PlotManager.java

Lines changed: 26 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -118,71 +118,22 @@ public Plot getMergedPlot(int x, int z) {
118118
Plot plot;
119119
if((plot = this.getPlot(x, z)) != null) return plot;
120120

121-
plot = this.getDiffPlot(x, z);
122-
123-
final Plot plotNearby = this.getPlotNN(x, z);
124-
final int[] directions = this.getDirections(plot, plotNearby);
125-
126-
Plot current = plot;
127-
boolean isMerged = true;
128-
for(int direction : directions) {
129-
if(!current.isMerged(direction)) {
130-
isMerged = false;
131-
break;
132-
}
133-
134-
current = this.getPlotById(plot.getRelative(direction));
135-
}
121+
final int roadSize = this.levelSettings.getRoadSize();
122+
if((plot = this.getPlot(x - roadSize, z)) != null && plot.isMerged(1)) return plot;
123+
if((plot = this.getPlot(x, z - roadSize)) != null && plot.isMerged(2)) return plot;
124+
if((plot = this.getPlot(x - roadSize, z - roadSize)) != null && plot.isMerged(5))
125+
return plot;
136126

137-
if(isMerged) return plotNearby;
138127
return null;
139128
}
140129

141130
public Plot getPlot(int x, int z) {
142131
final PlotVector plotVector = this.getPlotVectorByPos(x, z);
143-
if(plotVector == null)
144-
return null;
145-
return this.getPlotById(plotVector.getX(), plotVector.getZ());
146-
}
132+
if(plotVector == null) return null;
147133

148-
private Plot getPlotNN(int x, int z) {
149-
final PlotVector plotVector = this.getPlotVectorByPosNN(x, z);
150134
return this.getPlotById(plotVector.getX(), plotVector.getZ());
151135
}
152136

153-
private Plot getDiffPlot(int x, int z) {
154-
final int plotSize = this.levelSettings.getPlotSize();
155-
final int totalSize = plotSize + this.levelSettings.getRoadSize();
156-
final int posX, posZ, difX, difZ;
157-
158-
if(x >= 0) {
159-
posX = (int) Math.floor((float) x / totalSize);
160-
difX = x % totalSize;
161-
} else {
162-
posX = (int) Math.ceil((float) (x - plotSize + 1) / totalSize);
163-
difX = Math.abs((x - plotSize + 1) % totalSize);
164-
}
165-
166-
if(z >= 0) {
167-
posZ = (int) Math.floor((float) z / totalSize);
168-
difZ = z % totalSize;
169-
} else {
170-
posZ = (int) Math.ceil((float) (z - plotSize + 1) / totalSize);
171-
difZ = Math.abs((z - plotSize + 1) % totalSize);
172-
}
173-
174-
final int addX = x >= 0 ? 1 : -1;
175-
final int addZ = z >= 0 ? 1 : -1;
176-
177-
final Plot plot;
178-
if(difX > plotSize - 1 && difZ > plotSize - 1) plot = this.getPlotByPlotPos(posX + addX, posZ + addZ);
179-
else if(difX > plotSize - 1) plot = this.getPlotByPlotPos(posX + addX, posZ);
180-
else if(difZ > plotSize - 1) plot = this.getPlotByPlotPos(posX, posZ + addZ);
181-
else plot = this.getPlotByPlotPos(posX, posZ);
182-
183-
return plot;
184-
}
185-
186137
private PlotVector getPlotVectorByPos(int x, int z) {
187138
final int plotSize = this.levelSettings.getPlotSize();
188139
final int totalSize = plotSize + this.levelSettings.getRoadSize();
@@ -211,20 +162,6 @@ private PlotVector getPlotVectorByPos(int x, int z) {
211162
return new PlotVector(posX, posZ);
212163
}
213164

214-
private PlotVector getPlotVectorByPosNN(int x, int z) {
215-
final int plotSize = this.levelSettings.getPlotSize();
216-
final int totalSize = plotSize + this.levelSettings.getRoadSize();
217-
final int posX, posZ;
218-
219-
if(x >= 0) posX = (int) Math.floor((float) x / totalSize);
220-
else posX = (int) Math.ceil((float) (x - plotSize + 1) / totalSize);
221-
222-
if(z >= 0) posZ = (int) Math.floor((float) z / totalSize);
223-
else posZ = (int) Math.ceil((float) (z - plotSize + 1) / totalSize);
224-
225-
return new PlotVector(posX, posZ);
226-
}
227-
228165
public Plot getPlotById(PlotVector plotVector) {
229166
return this.getPlotByPlotPos(plotVector.getX(), plotVector.getZ());
230167
}
@@ -314,74 +251,46 @@ public Set<Plot> getConnectedPlots(Plot plot) {
314251
}
315252
}
316253
}
254+
317255
return tmpSet;
318256
}
319257

320258
public boolean startMerge(Plot plot, int dir) {
321-
return this.startMerge(plot, dir, new HashSet<>());
322-
}
323-
324-
public boolean startMerge(Plot plot, int dir, Set<PlotMerged> mergedSet) {
325259
if(!plot.hasOwner()) return false;
326260

327-
final PlotMerged plotMerged = new PlotMerged(plot, dir);
328-
if(mergedSet.contains(plotMerged)) return false;
329-
mergedSet.add(plotMerged);
261+
final Plot relativePlot = this.getPlotById(plot.getRelative(dir));
330262

331-
final Set<Plot> visited = new HashSet<>();
332-
final Set<PlotVector> merged = new HashSet<>();
333-
final Set<Plot> connected = this.getConnectedPlots(plot);
334-
for(Plot current : connected) merged.add(current.getPlotVector());
263+
final List<Plot> plots = new ArrayList<>();
264+
plots.addAll(this.getConnectedPlots(plot));
265+
plots.addAll(this.getConnectedPlots(relativePlot));
335266

336-
final Queue<Plot> frontier = new ArrayDeque<>(connected);
337-
Plot current;
338267
final WhenDone whenDone = new WhenDone(() -> {
339-
this.finishPlotMerge(merged);
268+
this.finishPlotMerge(plots);
340269
plot.recalculateOrigin();
341270

342-
for(PlotVector plotVector : merged)
343-
if(!plotVector.equals(plot.getPlotVector()))
344-
this.mergePlotData(plot, this.getPlotById(plotVector));
271+
for(Plot other : plots)
272+
if(!other.equals(plot)) this.mergePlotData(plot, other);
345273

346274
this.savePlots();
347275
});
348276

349-
boolean toReturn = false;
350-
while((current = frontier.poll()) != null) {
351-
if(!visited.add(current)) continue;
352-
353-
Set<Plot> nextPlots;
354-
for(int iDir = 0; iDir < 4; iDir++) {
355-
if((dir == -1 || dir == iDir) && !current.isMerged(iDir)) {
356-
final Plot other = this.getPlotById(current.getRelative(iDir));
357-
if(other.isOwner(plot.getOwner()) && (other.getBasePlot().equals(current.getBasePlot()) ||
358-
(nextPlots = this.getConnectedPlots(other)).size() > 0 && frontier.addAll(nextPlots))) {
359-
this.mergePlot(other, current, whenDone);
360-
merged.add(current.getPlotVector());
361-
merged.add(other.getPlotVector());
362-
toReturn = true;
363-
}
364-
}
365-
}
366-
}
367-
368277
int relativeDir;
369-
for(Plot visitedPlot0 : visited) {
370-
for(Plot visitedPlot1 : visited) {
371-
if(!visitedPlot0.equals(visitedPlot1)) continue;
278+
for(Plot toMerge0 : plots) {
279+
for(Plot toMerge1 : plots) {
280+
if(toMerge0.equals(toMerge1)) continue;
372281

373-
relativeDir = visitedPlot0.getRelativeDir(visitedPlot1.getPlotVector());
374-
if(!visitedPlot0.isMerged(relativeDir))
375-
this.startMerge(visitedPlot0, relativeDir, mergedSet);
282+
relativeDir = toMerge0.getRelativeDir(toMerge1.getPlotVector());
283+
if(relativeDir != -1 && !toMerge0.isMerged(relativeDir))
284+
this.mergePlot(toMerge0, toMerge1, whenDone);
376285

377-
relativeDir = visitedPlot1.getRelativeDir(visitedPlot0.getPlotVector());
378-
if(!visitedPlot1.isMerged(relativeDir))
379-
this.startMerge(visitedPlot1, relativeDir, mergedSet);
286+
relativeDir = toMerge1.getRelativeDir(toMerge0.getPlotVector());
287+
if(relativeDir != -1 && !toMerge1.isMerged(relativeDir))
288+
this.mergePlot(toMerge1, toMerge0, whenDone);
380289
}
381290
}
382291

383292
whenDone.start();
384-
return toReturn;
293+
return true;
385294
}
386295

387296
private void mergePlotData(Plot plotA, Plot plotB) {
@@ -409,13 +318,12 @@ private void mergePlotData(Plot plotA, Plot plotB) {
409318
}
410319
}
411320

412-
private void finishPlotMerge(Set<PlotVector> plots) {
321+
private void finishPlotMerge(List<Plot> plots) {
413322
final BlockState claimBlock = this.levelSettings.getClaimPlotState();
414323
final BlockState wallBlock = this.levelSettings.getWallPlotState();
415324
final BlockState wallFillingBlock = this.levelSettings.getWallFillingState();
416325

417-
for(final PlotVector plotVector : plots) {
418-
final Plot plot = this.getPlotById(plotVector);
326+
for(Plot plot : plots) {
419327
this.changeBorder(plot, plot.hasOwner() ? claimBlock : wallBlock);
420328
this.changeWall(plot, wallFillingBlock);
421329
}
@@ -1012,23 +920,6 @@ else if(y == this.levelSettings.getGroundHeight())
1012920
});
1013921
}
1014922

1015-
private int[] getDirections(Plot plotA, Plot plotB) {
1016-
final int xA = plotA.getPlotVector().getX();
1017-
final int zA = plotA.getPlotVector().getZ();
1018-
final int xB = plotB.getPlotVector().getX();
1019-
final int zB = plotB.getPlotVector().getZ();
1020-
1021-
final List<Integer> directions = new ArrayList<>();
1022-
if(xA < xB) directions.add(1);
1023-
else if(xA > xB) directions.add(3);
1024-
if(zA < zB) directions.add(2);
1025-
else if(zA > zB) directions.add(0);
1026-
1027-
final int[] intArray = new int[directions.size()];
1028-
for(int i = 0; i < directions.size(); i++) intArray[i] = directions.get(i);
1029-
return intArray;
1030-
}
1031-
1032923
public ShapeType[] getShapes(int x, int z) {
1033924
final int totalSize = this.levelSettings.getTotalSize();
1034925
final int plotSize = this.levelSettings.getPlotSize();

src/main/java/de/kcodeyt/plotplugin/schematic/Schematic.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import java.util.Arrays;
3737
import java.util.HashMap;
3838
import java.util.Map;
39-
import java.util.Objects;
40-
import java.util.stream.Collectors;
4139
import java.util.zip.DataFormatException;
4240
import java.util.zip.Deflater;
4341
import java.util.zip.Inflater;
@@ -83,7 +81,7 @@ public void buildInChunk(TaskHelper taskHelper, Vector3 vector3, FullChunk fullC
8381
final int bX = x & 15;
8482
final int bZ = z & 15;
8583
final ShapeType shapeType = shapes[(bZ << 4) | bX];
86-
if(!allowedShapes.isAllowed(shapeType)) continue;
84+
if(allowedShapes.isDisallowed(shapeType)) continue;
8785

8886
fullChunk.setBlockStateAtLayer(bX, y, bZ, 0, schematicBlock.getLayer0());
8987
fullChunk.setBlockStateAtLayer(bX, y, bZ, 1, schematicBlock.getLayer1());
@@ -102,7 +100,7 @@ public void buildInChunk(TaskHelper taskHelper, Vector3 vector3, FullChunk fullC
102100
final int bX = x & 15;
103101
final int bZ = z & 15;
104102
final ShapeType shapeType = shapes[(bZ << 4) | bX];
105-
if(!allowedShapes.isAllowed(shapeType)) continue;
103+
if(allowedShapes.isDisallowed(shapeType)) continue;
106104

107105
taskHelper.addSyncTask(() -> {
108106
try {

src/main/java/de/kcodeyt/plotplugin/util/Allowed.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public Allowed(C... alloweds) {
2727
this.alloweds = alloweds;
2828
}
2929

30-
public boolean isAllowed(C c) {
31-
for(final Object allowed : this.alloweds)
30+
public boolean isDisallowed(C c) {
31+
for(Object allowed : this.alloweds)
3232
if(Objects.equals(allowed, c))
33-
return true;
34-
return false;
33+
return false;
34+
return true;
3535
}
3636

3737
}

src/main/java/de/kcodeyt/plotplugin/util/Plot.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package de.kcodeyt.plotplugin.util;
1818

1919
import de.kcodeyt.plotplugin.manager.PlotManager;
20+
import lombok.EqualsAndHashCode;
2021
import lombok.Getter;
2122
import lombok.Setter;
2223

@@ -25,6 +26,7 @@
2526

2627
@Setter
2728
@Getter
29+
@EqualsAndHashCode(exclude = {"plotManager", "origin"})
2830
public class Plot {
2931

3032
public static Plot fromConfig(PlotManager plotManager, Map<String, Object> plotMap) {
@@ -165,8 +167,8 @@ public boolean isMerged(int direction) {
165167
i = direction - 4;
166168
i2 = direction - 3;
167169
return this.isMerged(i2) && this.isMerged(i) && this.plotManager.getPlotById(this.getRelative(i)).isMerged(i2) && this.plotManager.getPlotById(this.getRelative(i2)).isMerged(i);
168-
169170
}
171+
170172
return false;
171173
}
172174

0 commit comments

Comments
 (0)