-
-
Notifications
You must be signed in to change notification settings - Fork 684
/
Area.java
903 lines (891 loc) · 51 KB
/
Area.java
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.command;
import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.ConfigurationSection;
import com.plotsquared.core.configuration.ConfigurationUtil;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.CaptionHolder;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.configuration.file.YamlConfiguration;
import com.plotsquared.core.events.TeleportCause;
import com.plotsquared.core.generator.AugmentedUtils;
import com.plotsquared.core.generator.HybridPlotWorld;
import com.plotsquared.core.inject.annotations.WorldConfig;
import com.plotsquared.core.inject.annotations.WorldFile;
import com.plotsquared.core.inject.factory.HybridPlotWorldFactory;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.PlotAreaTerrainType;
import com.plotsquared.core.plot.PlotAreaType;
import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotArea;
import com.plotsquared.core.queue.GlobalBlockQueue;
import com.plotsquared.core.queue.QueueCoordinator;
import com.plotsquared.core.setup.PlotAreaBuilder;
import com.plotsquared.core.util.FileUtils;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.RegionUtil;
import com.plotsquared.core.util.SchematicHandler;
import com.plotsquared.core.util.SetupUtils;
import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.TabCompletions;
import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.task.RunnableVal3;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.EditSessionBuilder;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.World;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@CommandDeclaration(command = "area",
permission = "plots.area",
category = CommandCategory.ADMINISTRATION,
requiredType = RequiredType.NONE,
aliases = "world",
usage = "/plot area <create | info | list | tp | regen>",
confirmation = true)
public class Area extends SubCommand {
private final PlotAreaManager plotAreaManager;
private final YamlConfiguration worldConfiguration;
private final File worldFile;
private final HybridPlotWorldFactory hybridPlotWorldFactory;
private final SetupUtils setupUtils;
private final WorldUtil worldUtil;
private final GlobalBlockQueue blockQueue;
private final Map<UUID, Map<String, Object>> metaData = new HashMap<>();
@Inject
public Area(
final @NonNull PlotAreaManager plotAreaManager,
@WorldConfig final @NonNull YamlConfiguration worldConfiguration,
@WorldFile final @NonNull File worldFile,
final @NonNull HybridPlotWorldFactory hybridPlotWorldFactory,
final @NonNull SetupUtils setupUtils,
final @NonNull WorldUtil worldUtil,
final @NonNull GlobalBlockQueue blockQueue
) {
this.plotAreaManager = plotAreaManager;
this.worldConfiguration = worldConfiguration;
this.worldFile = worldFile;
this.hybridPlotWorldFactory = hybridPlotWorldFactory;
this.setupUtils = setupUtils;
this.worldUtil = worldUtil;
this.blockQueue = blockQueue;
}
@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
if (args.length == 0) {
sendUsage(player);
return false;
}
switch (args[0].toLowerCase()) {
case "single" -> {
if (player instanceof ConsolePlayer) {
player.sendMessage(RequiredType.CONSOLE.getErrorMessage());
return false;
}
if (!player.hasPermission(Permission.PERMISSION_AREA_CREATE)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver(
"node",
Tag.inserting(Permission.PERMISSION_AREA_CREATE)
)
);
return false;
}
if (args.length < 2) {
player.sendMessage(
TranslatableCaption.of("single.single_area_needs_name"),
TagResolver.resolver("command", Tag.inserting(Component.text("/plot area single <name>")))
);
return false;
}
final PlotArea existingArea = this.plotAreaManager.getPlotArea(player.getLocation().getWorldName(), args[1]);
if (existingArea != null && existingArea.getId().equalsIgnoreCase(args[1])) {
player.sendMessage(TranslatableCaption.of("single.single_area_name_taken"));
return false;
}
final LocalSession localSession = WorldEdit.getInstance().getSessionManager().getIfPresent(player.toActor());
if (localSession == null) {
player.sendMessage(TranslatableCaption.of("single.single_area_missing_selection"));
return false;
}
Region playerSelectedRegion = null;
try {
playerSelectedRegion = localSession.getSelection(((Player) player.toActor()).getWorld());
} catch (final Exception ignored) {
}
if (playerSelectedRegion == null) {
player.sendMessage(TranslatableCaption.of("single.single_area_missing_selection"));
return false;
}
if (playerSelectedRegion.getWidth() != playerSelectedRegion.getLength()) {
player.sendMessage(TranslatableCaption.of("single.single_area_not_square"));
return false;
}
if (this.plotAreaManager.getPlotAreas(
Objects.requireNonNull(playerSelectedRegion.getWorld()).getName(),
CuboidRegion.makeCuboid(playerSelectedRegion)
).length != 0) {
player.sendMessage(TranslatableCaption.of("single.single_area_overlapping"));
}
// Alter the region
final BlockVector3 playerSelectionMin = playerSelectedRegion.getMinimumPoint();
final BlockVector3 playerSelectionMax = playerSelectedRegion.getMaximumPoint();
// Create a new selection that spans the entire vertical range of the world
World world = playerSelectedRegion.getWorld();
final CuboidRegion selectedRegion =
new CuboidRegion(
playerSelectedRegion.getWorld(),
BlockVector3.at(playerSelectionMin.getX(), world.getMinY(), playerSelectionMin.getZ()),
BlockVector3.at(playerSelectionMax.getX(), world.getMaxY(), playerSelectionMax.getZ())
);
// There's only one plot in the area...
final PlotId plotId = PlotId.of(1, 1);
final HybridPlotWorld hybridPlotWorld = this.hybridPlotWorldFactory
.create(
player.getLocation().getWorldName(),
args[1],
Objects.requireNonNull(PlotSquared.platform()).defaultGenerator(),
plotId,
plotId
);
// Plot size is the same as the region width
hybridPlotWorld.PLOT_WIDTH = hybridPlotWorld.SIZE = (short) selectedRegion.getWidth();
// We use a schematic generator
hybridPlotWorld.setTerrain(PlotAreaTerrainType.NONE);
// It is always a partial plot world
hybridPlotWorld.setType(PlotAreaType.PARTIAL);
// We save the schematic :D
hybridPlotWorld.PLOT_SCHEMATIC = true;
// Set the road width to 0
hybridPlotWorld.ROAD_WIDTH = hybridPlotWorld.ROAD_OFFSET_X = hybridPlotWorld.ROAD_OFFSET_Z = 0;
// Set the plot height to the selection height
hybridPlotWorld.PLOT_HEIGHT = hybridPlotWorld.ROAD_HEIGHT = hybridPlotWorld.WALL_HEIGHT = playerSelectionMin.getBlockY();
// No sign plz
hybridPlotWorld.setAllowSigns(false);
final File parentFile = FileUtils.getFile(
PlotSquared.platform().getDirectory(),
Settings.Paths.SCHEMATICS + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + hybridPlotWorld.getWorldName() + File.separator
+ hybridPlotWorld.getId()
);
if (!parentFile.exists() && !parentFile.mkdirs()) {
player.sendMessage(TranslatableCaption.of("single.single_area_could_not_make_directories"));
return false;
}
final File file = new File(parentFile, "plot.schem");
try (final ClipboardWriter clipboardWriter = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(
file))) {
final BlockArrayClipboard clipboard = new BlockArrayClipboard(selectedRegion);
EditSessionBuilder editSessionBuilder = WorldEdit.getInstance().newEditSessionBuilder();
editSessionBuilder.world(selectedRegion.getWorld());
final EditSession editSession = editSessionBuilder.build();
final ForwardExtentCopy forwardExtentCopy =
new ForwardExtentCopy(editSession, selectedRegion, clipboard, selectedRegion.getMinimumPoint());
forwardExtentCopy.setCopyingBiomes(true);
forwardExtentCopy.setCopyingEntities(true);
Operations.complete(forwardExtentCopy);
clipboardWriter.write(clipboard);
} catch (final Exception e) {
player.sendMessage(TranslatableCaption.of("single.single_area_failed_to_save"));
e.printStackTrace();
return false;
}
// Setup schematic
try {
hybridPlotWorld.setupSchematics();
} catch (final SchematicHandler.UnsupportedFormatException e) {
e.printStackTrace();
}
// Calculate the offset
final BlockVector3 singlePos1 = selectedRegion.getMinimumPoint();
// Now the schematic is saved, which is wonderful!
PlotAreaBuilder singleBuilder = PlotAreaBuilder.ofPlotArea(hybridPlotWorld).plotManager(PlotSquared
.platform()
.pluginName())
.generatorName(PlotSquared.platform().pluginName()).maximumId(plotId).minimumId(plotId);
Runnable singleRun = () -> {
final String path =
"worlds." + hybridPlotWorld.getWorldName() + ".areas." + hybridPlotWorld.getId() + '-' + singleBuilder
.minimumId() + '-'
+ singleBuilder.maximumId();
final int offsetX = singlePos1.getX();
final int offsetZ = singlePos1.getZ();
if (offsetX != 0) {
this.worldConfiguration.set(path + ".road.offset.x", offsetX);
}
if (offsetZ != 0) {
this.worldConfiguration.set(path + ".road.offset.z", offsetZ);
}
final String worldName = this.setupUtils.setupWorld(singleBuilder);
if (this.worldUtil.isWorld(worldName)) {
PlotSquared.get().loadWorld(worldName, null);
player.sendMessage(TranslatableCaption.of("single.single_area_created"));
} else {
player.sendMessage(
TranslatableCaption.of("errors.error_create"),
TagResolver.resolver("world", Tag.inserting(Component.text(hybridPlotWorld.getWorldName())))
);
}
};
singleRun.run();
return true;
}
case "c", "setup", "create" -> {
if (!player.hasPermission(Permission.PERMISSION_AREA_CREATE)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver(
"node",
Tag.inserting(Permission.PERMISSION_AREA_CREATE)
)
);
return false;
}
switch (args.length) {
case 1:
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax"),
TagResolver.resolver(
"value",
Tag.inserting(Component.text("/plot area create [world[:id]] [<modifier>=<value>]..."))
)
);
return false;
case 2:
switch (args[1].toLowerCase()) {
case "pos1" -> { // Set position 1
HybridPlotWorld area = (HybridPlotWorld) metaData.computeIfAbsent(
player.getUUID(),
missingUUID -> new HashMap<>()
)
.get("area_create_area");
if (area == null) {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax"),
TagResolver.resolver(
"value",
Tag.inserting(Component.text(
"/plot area create [world[:id]] [<modifier>=<value>]..."))
)
);
return false;
}
Location location = player.getLocation();
metaData.computeIfAbsent(player.getUUID(), missingUUID -> new HashMap<>()).put(
"area_pos1",
location
);
player.sendMessage(
TranslatableCaption.of("set.set_attribute"),
TagResolver.builder()
.tag("attribute", Tag.inserting(Component.text("area_pos1")))
.tag("value", Tag.inserting(
Component.text(location.getX())
.append(Component.text(","))
.append(Component.text(location.getZ()))
))
.build()
);
player.sendMessage(
TranslatableCaption.of("area.set_pos2"),
TagResolver.resolver("command", Tag.inserting(Component.text("/plot area create pos2")))
);
return true;
}
case "pos2" -> { // Set position 2 and finish creation for type=2 (partial)
final HybridPlotWorld area =
(HybridPlotWorld) metaData.computeIfAbsent(
player.getUUID(),
missingUUID -> new HashMap<>()
)
.get("area_create_area");
if (area == null) {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax"),
TagResolver.resolver(
"value",
Tag.inserting(Component.text(
"/plot area create [world[:id]] [<modifier>=<value>]..."))
)
);
return false;
}
Location pos1 = player.getLocation();
Location pos2 =
(Location) metaData.computeIfAbsent(player.getUUID(), missingUUID -> new HashMap<>()).get(
"area_pos1");
int dx = Math.abs(pos1.getX() - pos2.getX());
int dz = Math.abs(pos1.getZ() - pos2.getZ());
int numX = Math.max(1, (dx + 1 + area.ROAD_WIDTH + area.SIZE / 2) / area.SIZE);
int numZ = Math.max(1, (dz + 1 + area.ROAD_WIDTH + area.SIZE / 2) / area.SIZE);
int ddx = dx - (numX * area.SIZE - area.ROAD_WIDTH);
int ddz = dz - (numZ * area.SIZE - area.ROAD_WIDTH);
int bx = Math.min(pos1.getX(), pos2.getX()) + ddx;
int bz = Math.min(pos1.getZ(), pos2.getZ()) + ddz;
int tx = Math.max(pos1.getX(), pos2.getX()) - ddx;
int tz = Math.max(pos1.getZ(), pos2.getZ()) - ddz;
int lower = (area.ROAD_WIDTH & 1) == 0 ? area.ROAD_WIDTH / 2 - 1 : area.ROAD_WIDTH / 2;
final int offsetX = bx - (area.ROAD_WIDTH == 0 ? 0 : lower);
final int offsetZ = bz - (area.ROAD_WIDTH == 0 ? 0 : lower);
// Height doesn't matter for this region
final CuboidRegion region = RegionUtil.createRegion(bx, tx, 0, 0, bz, tz);
final Set<PlotArea> areas = this.plotAreaManager.getPlotAreasSet(area.getWorldName(), region);
if (!areas.isEmpty()) {
player.sendMessage(
TranslatableCaption.of("cluster.cluster_intersection"),
TagResolver.resolver(
"cluster",
Tag.inserting(areas.iterator().next())
)
);
return false;
}
PlotAreaBuilder builder = PlotAreaBuilder.ofPlotArea(area).plotManager(PlotSquared
.platform()
.pluginName())
.generatorName(PlotSquared.platform().pluginName()).minimumId(PlotId.of(1, 1))
.maximumId(PlotId.of(numX, numZ));
final String path =
"worlds." + area.getWorldName() + ".areas." + area.getId() + '-' + builder.minimumId() + '-' + builder
.maximumId();
Runnable run = () -> {
if (offsetX != 0) {
this.worldConfiguration.set(path + ".road.offset.x", offsetX);
}
if (offsetZ != 0) {
this.worldConfiguration.set(path + ".road.offset.z", offsetZ);
}
final String world = this.setupUtils.setupWorld(builder);
if (this.worldUtil.isWorld(world)) {
PlotSquared.get().loadWorld(world, null);
player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND_AREA_CREATE);
player.sendMessage(TranslatableCaption.of("setup.setup_finished"));
if (area.getTerrain() != PlotAreaTerrainType.ALL) {
QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(world));
queue.setChunkConsumer(chunk -> AugmentedUtils.generateChunk(
world,
chunk.getX(),
chunk.getZ(),
null
));
queue.addReadChunks(region.getChunks());
queue.enqueue();
}
} else {
player.sendMessage(
TranslatableCaption.of("errors.error_create"),
TagResolver.resolver("world", Tag.inserting(Component.text(area.getWorldName())))
);
}
};
if (hasConfirmation(player)) {
CmdConfirm.addPending(player, getCommandString() + " create pos2 (Creates world)", run);
} else {
run.run();
}
return true;
}
}
default: // Start creation
String[] split = args[1].split(":");
String id;
if (split.length == 2) {
id = split[1];
} else {
id = null;
}
PlotAreaBuilder builder = PlotAreaBuilder.newBuilder();
builder.worldName(split[0]);
final HybridPlotWorld pa =
this.hybridPlotWorldFactory.create(
builder.worldName(),
id,
PlotSquared.platform().defaultGenerator(),
null,
null
);
PlotArea other = this.plotAreaManager.getPlotArea(pa.getWorldName(), id);
if (other != null && Objects.equals(pa.getId(), other.getId())) {
player.sendMessage(
TranslatableCaption.of("setup.setup_world_taken"),
TagResolver.resolver("value", Tag.inserting(Component.text(pa.getId())))
);
return false;
}
Set<PlotArea> areas = this.plotAreaManager.getPlotAreasSet(pa.getWorldName());
if (!areas.isEmpty()) {
PlotArea area = areas.iterator().next();
pa.setType(area.getType());
}
pa.SIZE = (short) (pa.PLOT_WIDTH + pa.ROAD_WIDTH);
for (int i = 2; i < args.length; i++) {
String[] pair = args[i].split("=");
if (pair.length != 2) {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax_extended"),
TagResolver.builder()
.tag("value1", Tag.inserting(Component.text(getCommandString())))
.tag(
"value2",
Tag.inserting(Component.text("create [world[:id]] [<modifier>=<value>]..."))
)
.build()
);
return false;
}
switch (pair[0].toLowerCase()) {
case "s", "size" -> {
pa.PLOT_WIDTH = Integer.parseInt(pair[1]);
pa.SIZE = (short) (pa.PLOT_WIDTH + pa.ROAD_WIDTH);
}
case "g", "gap" -> {
pa.ROAD_WIDTH = Integer.parseInt(pair[1]);
pa.SIZE = (short) (pa.PLOT_WIDTH + pa.ROAD_WIDTH);
}
case "h", "height" -> {
int value = Integer.parseInt(pair[1]);
pa.PLOT_HEIGHT = value;
pa.ROAD_HEIGHT = value;
pa.WALL_HEIGHT = value;
}
case "f", "floor" -> pa.TOP_BLOCK = ConfigurationUtil.BLOCK_BUCKET.parseString(pair[1]);
case "m", "main" -> pa.MAIN_BLOCK = ConfigurationUtil.BLOCK_BUCKET.parseString(pair[1]);
case "w", "wall" -> pa.WALL_FILLING = ConfigurationUtil.BLOCK_BUCKET.parseString(pair[1]);
case "b", "border" -> pa.WALL_BLOCK = ConfigurationUtil.BLOCK_BUCKET.parseString(pair[1]);
case "terrain" -> {
pa.setTerrain(PlotAreaTerrainType.fromString(pair[1])
.orElseThrow(() -> new IllegalArgumentException(pair[1] + " is not a valid terrain.")));
builder.terrainType(pa.getTerrain());
}
case "type" -> {
pa.setType(PlotAreaType.fromString(pair[1])
.orElseThrow(() -> new IllegalArgumentException(pair[1] + " is not a valid type.")));
builder.plotAreaType(pa.getType());
}
default -> {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax_extended"),
TagResolver.builder()
.tag("value1", Tag.inserting(Component.text(getCommandString())))
.tag(
"value2",
Tag.inserting(Component.text(
" create [world[:id]] [<modifier>=<value>]..."))
)
.build()
);
return false;
}
}
}
if (pa.getType() != PlotAreaType.PARTIAL) {
if (this.worldUtil.isWorld(pa.getWorldName())) {
player.sendMessage(
TranslatableCaption.of("setup.setup_world_taken"),
TagResolver.resolver("value", Tag.inserting(Component.text(pa.getWorldName())))
);
return false;
}
Runnable run = () -> {
String path = "worlds." + pa.getWorldName();
if (!this.worldConfiguration.contains(path)) {
this.worldConfiguration.createSection(path);
}
ConfigurationSection section = this.worldConfiguration.getConfigurationSection(path);
pa.saveConfiguration(section);
pa.loadConfiguration(section);
builder.plotManager(PlotSquared.platform().pluginName());
builder.generatorName(PlotSquared.platform().pluginName());
String world = this.setupUtils.setupWorld(builder);
if (this.worldUtil.isWorld(world)) {
player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND_AREA_CREATE);
player.sendMessage(TranslatableCaption.of("setup.setup_finished"));
} else {
player.sendMessage(
TranslatableCaption.of("errors.error_create"),
TagResolver.resolver("world", Tag.inserting(Component.text(pa.getWorldName())))
);
}
try {
this.worldConfiguration.save(this.worldFile);
} catch (IOException e) {
e.printStackTrace();
}
};
if (hasConfirmation(player)) {
CmdConfirm.addPending(player, getCommandString() + ' ' + StringMan.join(args, " "), run);
} else {
run.run();
}
return true;
}
if (pa.getId() == null) {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax"),
TagResolver.resolver("value", Tag.inserting(Component.text(getUsage())))
);
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax_extended"),
TagResolver.builder()
.tag("value1", Tag.inserting(Component.text(getCommandString())))
.tag(
"value2",
Tag.inserting(Component.text(
" create [world[:id]] [<modifier>=<value>]..."))
)
.build()
);
return false;
}
if (this.worldUtil.isWorld(pa.getWorldName())) {
if (!player.getLocation().getWorldName().equals(pa.getWorldName())) {
player.teleport(this.worldUtil.getSpawn(pa.getWorldName()), TeleportCause.COMMAND_AREA_CREATE);
}
} else {
builder.terrainType(PlotAreaTerrainType.NONE);
builder.plotAreaType(PlotAreaType.NORMAL);
this.setupUtils.setupWorld(builder);
player.teleport(this.worldUtil.getSpawn(pa.getWorldName()), TeleportCause.COMMAND_AREA_CREATE);
}
metaData.computeIfAbsent(player.getUUID(), missingUUID -> new HashMap<>()).put("area_create_area", pa);
player.sendMessage(
TranslatableCaption.of("single.get_position"),
TagResolver.resolver("command", Tag.inserting(Component.text(getCommandString())))
);
break;
}
return true;
}
case "i", "info" -> {
if (!player.hasPermission(Permission.PERMISSION_AREA_INFO)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver(
"node",
Tag.inserting(Permission.PERMISSION_AREA_INFO)
)
);
return false;
}
PlotArea area;
switch (args.length) {
case 1 -> area = player.getApplicablePlotArea();
case 2 -> area = this.plotAreaManager.getPlotAreaByString(args[1]);
default -> {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax_extended"),
TagResolver.builder()
.tag("value1", Tag.inserting(Component.text(getCommandString())))
.tag("value2", Tag.inserting(Component.text(" info [area]")))
.build()
);
return false;
}
}
if (area == null) {
if (args.length == 2) {
player.sendMessage(
TranslatableCaption.of("errors.not_valid_plot_world"),
TagResolver.resolver("value", Tag.inserting(Component.text(args[1])))
);
} else {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
}
return false;
}
String name;
double percent;
int claimed = area.getPlotCount();
int clusters = area.getClusters().size();
String region;
String generator = String.valueOf(area.getGenerator());
if (area.getType() == PlotAreaType.PARTIAL) {
PlotId min = area.getMin();
PlotId max = area.getMax();
name = area.getWorldName() + ';' + area.getId() + ';' + min + ';' + max;
int size = (max.getX() - min.getX() + 1) * (max.getY() - min.getY() + 1);
percent = claimed == 0 ? 0 : size / (double) claimed;
region = area.getRegion().toString();
} else {
name = area.getWorldName();
percent = claimed == 0 ? 0 : 100d * claimed / Integer.MAX_VALUE;
region = "N/A";
}
TagResolver resolver = TagResolver.builder()
.tag(
"header",
Tag.inserting(TranslatableCaption.of("info.plot_info_header").toComponent(player))
)
.tag("name", Tag.inserting(Component.text(name)))
.tag("type", Tag.inserting(Component.text(area.getType().name())))
.tag("terrain", Tag.inserting(Component.text(area.getTerrain().name())))
.tag("usage", Tag.inserting(Component.text(String.format("%.2f", percent))))
.tag("claimed", Tag.inserting(Component.text(claimed)))
.tag("clusters", Tag.inserting(Component.text(clusters)))
.tag("region", Tag.inserting(Component.text(region)))
.tag("generator", Tag.inserting(Component.text(generator)))
.tag(
"footer",
Tag.inserting(TranslatableCaption.of("info.plot_info_footer").toComponent(player))
)
.build();
player.sendMessage(TranslatableCaption.of("info.area_info_format"), resolver);
return true;
}
case "l", "list" -> {
if (!player.hasPermission(Permission.PERMISSION_AREA_LIST)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver(
"node",
Tag.inserting(Permission.PERMISSION_AREA_LIST)
)
);
return false;
}
int page;
switch (args.length) {
case 1:
page = 0;
break;
case 2:
if (MathMan.isInteger(args[1])) {
page = Integer.parseInt(args[1]) - 1;
break;
}
default:
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax_extended"),
TagResolver.builder()
.tag("value1", Tag.inserting(Component.text(getCommandString())))
.tag("value2", Tag.inserting(Component.text(" list [#]")))
.build()
);
return false;
}
final List<PlotArea> areas = new ArrayList<>(Arrays.asList(this.plotAreaManager.getAllPlotAreas()));
paginate(player, areas, 8, page, new RunnableVal3<Integer, PlotArea, CaptionHolder>() {
@Override
public void run(Integer i, PlotArea area, CaptionHolder caption) {
String name;
double percent;
int claimed = area.getPlotCount();
int clusters = area.getClusters().size();
String region;
String generator = String.valueOf(area.getGenerator());
if (area.getType() == PlotAreaType.PARTIAL) {
PlotId min = area.getMin();
PlotId max = area.getMax();
name = area.getWorldName() + ';' + area.getId() + ';' + min + ';' + max;
int size = (max.getX() - min.getX() + 1) * (max.getY() - min.getY() + 1);
percent = claimed == 0 ? 0 : claimed / (double) size;
region = area.getRegion().toString();
} else {
name = area.getWorldName();
percent = claimed == 0 ? 0 : (double) claimed / Short.MAX_VALUE * Short.MAX_VALUE;
region = "N/A";
}
Component tooltip = MINI_MESSAGE.deserialize(
TranslatableCaption.of("info.area_list_tooltip").getComponent(player),
TagResolver.builder()
.tag("claimed", Tag.inserting(Component.text(claimed)))
.tag("usage", Tag.inserting(Component.text(String.format("%.2f", percent) + "%")))
.tag("clusters", Tag.inserting(Component.text(clusters)))
.tag("region", Tag.inserting(Component.text(region)))
.tag("generator", Tag.inserting(Component.text(generator)))
.build()
);
TagResolver resolver = TagResolver.builder()
.tag("hover_info", Tag.inserting(tooltip))
.tag("command_tp", Tag.preProcessParsed("/plot area tp " + name))
.tag("command_info", Tag.preProcessParsed("/plot area info " + name))
.tag("number", Tag.inserting(Component.text(i)))
.tag("area_name", Tag.inserting(Component.text(name)))
.tag("area_type", Tag.inserting(Component.text(area.getType().name())))
.tag("area_terrain", Tag.inserting(Component.text(area.getTerrain().name())))
.build();
caption.set(TranslatableCaption.of("info.area_list_item"));
caption.setTagResolvers(resolver);
}
}, "/plot area list", TranslatableCaption.of("list.area_list_header_paged"));
return true;
}
case "regen", "clear", "reset", "regenerate" -> {
if (!player.hasPermission(Permission.PERMISSION_AREA_REGEN)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver(
"node",
Tag.inserting(Permission.PERMISSION_AREA_REGEN)
)
);
return false;
}
final PlotArea area = player.getApplicablePlotArea();
if (area == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
return false;
}
if (area.getType() != PlotAreaType.PARTIAL) {
player.sendMessage(
TranslatableCaption.of("single.delete_world_region"),
TagResolver.resolver("world", Tag.inserting(Component.text(area.getWorldName())))
);
return false;
}
QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(area.getWorldName()));
queue.setChunkConsumer(chunk -> AugmentedUtils.generateChunk(
area.getWorldName(),
chunk.getX(),
chunk.getZ(),
null
));
queue.addReadChunks(area.getRegion().getChunks());
queue.setCompleteTask(() -> player.sendMessage(TranslatableCaption.of("single.regeneration_complete")));
queue.enqueue();
return true;
}
case "goto", "v", "teleport", "visit", "tp" -> {
if (!player.hasPermission(Permission.PERMISSION_AREA_TP)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver("node", Tag.inserting(Permission.PERMISSION_AREA_TP))
);
return false;
}
if (args.length != 2) {
player.sendMessage(
TranslatableCaption.of("commandconfig.command_syntax"),
TagResolver.resolver("value", Tag.inserting(Component.text("/plot area tp [area]")))
);
return false;
}
PlotArea area = this.plotAreaManager.getPlotAreaByString(args[1]);
if (area == null) {
player.sendMessage(
TranslatableCaption.of("errors.not_valid_plot_world"),
TagResolver.resolver("value", Tag.inserting(Component.text(args[1])))
);
return false;
}
Location center;
if (area instanceof SinglePlotArea) {
((SinglePlotArea) area).loadWorld(PlotId.of(0, 0));
center = this.worldUtil.getSpawn(PlotId.of(0, 0).toUnderscoreSeparatedString());
player.teleport(center, TeleportCause.COMMAND_AREA_TELEPORT);
} else if (area.getType() != PlotAreaType.PARTIAL) {
center = this.worldUtil.getSpawn(area.getWorldName());
player.teleport(center, TeleportCause.COMMAND_AREA_TELEPORT);
} else {
CuboidRegion region = area.getRegion();
center = Location.at(area.getWorldName(),
region.getMinimumPoint().getX() + (region.getMaximumPoint().getX() - region
.getMinimumPoint()
.getX()) / 2, 0,
region.getMinimumPoint().getZ() + (region.getMaximumPoint().getZ() - region
.getMinimumPoint()
.getZ()) / 2
);
this.worldUtil.getHighestBlock(area.getWorldName(), center.getX(), center.getZ(),
y -> player.teleport(center.withY(1 + y), TeleportCause.COMMAND_AREA_TELEPORT)
);
}
return true;
}
case "delete", "remove" -> {
player.sendMessage(TranslatableCaption.of("single.worldcreation_location"));
return true;
}
}
sendUsage(player);
return false;
}
@Override
public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
if (args.length == 1) {
final List<String> completions = new LinkedList<>();
if (player.hasPermission(Permission.PERMISSION_AREA_CREATE)) {
completions.add("create");
}
if (player.hasPermission(Permission.PERMISSION_AREA_CREATE)) {
completions.add("single");
}
if (player.hasPermission(Permission.PERMISSION_AREA_LIST)) {
completions.add("list");
}
if (player.hasPermission(Permission.PERMISSION_AREA_INFO)) {
completions.add("info");
}
if (player.hasPermission(Permission.PERMISSION_AREA_TP)) {
completions.add("tp");
}
final List<Command> commands = completions.stream().filter(completion -> completion
.toLowerCase()
.startsWith(args[0].toLowerCase()))
.map(completion -> new Command(
null,
true,
completion,
"",
RequiredType.NONE,
CommandCategory.ADMINISTRATION
) {
}).collect(Collectors.toCollection(LinkedList::new));
if (player.hasPermission(Permission.PERMISSION_AREA) && args[0].length() > 0) {
commands.addAll(TabCompletions.completePlayers(player, args[0], Collections.emptyList()));
}
return commands;
}
return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList());
}
}