@@ -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 ();
0 commit comments