Skip to content

Commit 04a0f0b

Browse files
committed
Fix plot merge
1 parent ff66cec commit 04a0f0b

5 files changed

Lines changed: 69 additions & 12 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
<dependency>
2626
<groupId>org.projectlombok</groupId>
2727
<artifactId>lombok</artifactId>
28-
<version>1.18.0</version>
28+
<version>1.18.22</version>
2929
</dependency>
3030
<dependency>
3131
<groupId>com.google.code.gson</groupId>
3232
<artifactId>gson</artifactId>
33-
<version>2.8.9</version>
33+
<version>2.9.0</version>
3434
</dependency>
3535
<dependency>
3636
<groupId>commons-io</groupId>

src/main/java/de/kcodeyt/plotplugin/command/defaults/MergeCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public boolean execute(Player player, String[] args) {
4848
if(player.hasPermission("plot.command.admin.merge") || (plot.isOwner(player.getUniqueId()) && rPlot.isOwner(player.getUniqueId()))) {
4949
final PlotPreMergeEvent plotPreMergeEvent = new PlotPreMergeEvent(player, plot, dir);
5050
this.plugin.getServer().getPluginManager().callEvent(plotPreMergeEvent);
51-
if(plotPreMergeEvent.isCancelled())
52-
return false;
51+
if(plotPreMergeEvent.isCancelled()) return false;
5352

5453
if(!plotManager.startMerge(plot, dir)) {
5554
player.sendMessage(this.translate("merge-failure-no-plots-found"));

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,16 @@ public Set<Plot> getConnectedPlots(Plot plot) {
318318
}
319319

320320
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) {
321325
if(!plot.hasOwner()) return false;
322326

323-
int max = 8;
327+
final PlotMerged plotMerged = new PlotMerged(plot, dir);
328+
if(mergedSet.contains(plotMerged)) return false;
329+
mergedSet.add(plotMerged);
330+
324331
final Set<Plot> visited = new HashSet<>();
325332
final Set<PlotVector> merged = new HashSet<>();
326333
final Set<Plot> connected = this.getConnectedPlots(plot);
@@ -340,17 +347,15 @@ public boolean startMerge(Plot plot, int dir) {
340347
});
341348

342349
boolean toReturn = false;
343-
while((current = frontier.poll()) != null && max >= 0) {
344-
if(visited.contains(current)) continue;
350+
while((current = frontier.poll()) != null) {
351+
if(!visited.add(current)) continue;
345352

346-
visited.add(current);
347-
Set<Plot> plots;
353+
Set<Plot> nextPlots;
348354
for(int iDir = 0; iDir < 4; iDir++) {
349355
if((dir == -1 || dir == iDir) && !current.isMerged(iDir)) {
350356
final Plot other = this.getPlotById(current.getRelative(iDir));
351-
if(other.isOwner(plot.getOwner()) && (other.getBasePlot().equals(current.getBasePlot())
352-
|| (plots = this.getConnectedPlots(other)).size() <= max && frontier.
353-
addAll(plots) && (max -= plots.size()) != -1)) {
357+
if(other.isOwner(plot.getOwner()) && (other.getBasePlot().equals(current.getBasePlot()) ||
358+
(nextPlots = this.getConnectedPlots(other)).size() > 0 && frontier.addAll(nextPlots))) {
354359
this.mergePlot(other, current, whenDone);
355360
merged.add(current.getPlotVector());
356361
merged.add(other.getPlotVector());
@@ -360,6 +365,21 @@ public boolean startMerge(Plot plot, int dir) {
360365
}
361366
}
362367

368+
int relativeDir;
369+
for(Plot visitedPlot0 : visited) {
370+
for(Plot visitedPlot1 : visited) {
371+
if(!visitedPlot0.equals(visitedPlot1)) continue;
372+
373+
relativeDir = visitedPlot0.getRelativeDir(visitedPlot1.getPlotVector());
374+
if(!visitedPlot0.isMerged(relativeDir))
375+
this.startMerge(visitedPlot0, relativeDir, mergedSet);
376+
377+
relativeDir = visitedPlot1.getRelativeDir(visitedPlot0.getPlotVector());
378+
if(!visitedPlot1.isMerged(relativeDir))
379+
this.startMerge(visitedPlot1, relativeDir, mergedSet);
380+
}
381+
}
382+
363383
whenDone.start();
364384
return toReturn;
365385
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ public PlotVector getRelative(int direction) {
189189
}
190190
}
191191

192+
public int getRelativeDir(PlotVector other) {
193+
final int x = this.plotVector.getX() - other.getX();
194+
final int z = this.plotVector.getZ() - other.getZ();
195+
196+
if(x == 0 && z == 1) return 0;
197+
if(x == -1 && z == 0) return 1;
198+
if(x == 0 && z == -1) return 2;
199+
if(x == 1 && z == 0) return 3;
200+
return -1;
201+
}
202+
192203
public void recalculateOrigin() {
193204
if(this.hasNoMerges()) {
194205
this.origin = this;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2022 KCodeYT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.kcodeyt.plotplugin.util;
18+
19+
import lombok.Value;
20+
21+
@Value
22+
public class PlotMerged {
23+
24+
Plot plot;
25+
int dir;
26+
27+
}

0 commit comments

Comments
 (0)