/
PlotArea.java
1533 lines (1395 loc) · 54 KB
/
PlotArea.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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.plot;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.collection.QuadMap;
import com.plotsquared.core.configuration.ConfigurationNode;
import com.plotsquared.core.configuration.ConfigurationSection;
import com.plotsquared.core.configuration.ConfigurationUtil;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.configuration.file.YamlConfiguration;
import com.plotsquared.core.generator.GridPlotWorld;
import com.plotsquared.core.generator.IndependentPlotGenerator;
import com.plotsquared.core.inject.annotations.WorldConfig;
import com.plotsquared.core.location.BlockLoc;
import com.plotsquared.core.location.Direction;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.MetaDataAccess;
import com.plotsquared.core.player.PlayerMetaDataKeys;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.flag.FlagContainer;
import com.plotsquared.core.plot.flag.FlagParseException;
import com.plotsquared.core.plot.flag.GlobalFlagContainer;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import com.plotsquared.core.queue.GlobalBlockQueue;
import com.plotsquared.core.queue.QueueCoordinator;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.PlotExpression;
import com.plotsquared.core.util.RegionUtil;
import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.task.TaskTime;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
/**
* @author Jesse Boyd, Alexander Söderberg
*/
public abstract class PlotArea implements ComponentLike {
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + PlotArea.class.getSimpleName());
private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
private static final DecimalFormat FLAG_DECIMAL_FORMAT = new DecimalFormat("0");
static {
FLAG_DECIMAL_FORMAT.setMaximumFractionDigits(340);
}
protected final ConcurrentHashMap<PlotId, Plot> plots = new ConcurrentHashMap<>();
@NonNull
private final String worldName;
private final String id;
@NonNull
private final PlotManager plotManager;
private final int worldHash;
private final PlotId min;
private final PlotId max;
@NonNull
private final IndependentPlotGenerator generator;
/**
* Area flag container
*/
private final FlagContainer flagContainer =
new FlagContainer(GlobalFlagContainer.getInstance());
private final FlagContainer roadFlagContainer =
new FlagContainer(GlobalFlagContainer.getInstance());
private final YamlConfiguration worldConfiguration;
private final GlobalBlockQueue globalBlockQueue;
private boolean roadFlags = false;
private boolean autoMerge = false;
private boolean allowSigns = true;
private boolean miscSpawnUnowned = false;
private boolean mobSpawning = false;
private boolean mobSpawnerSpawning = false;
private BiomeType plotBiome = BiomeTypes.FOREST;
private boolean plotChat = true;
private boolean forcingPlotChat = false;
private boolean schematicClaimSpecify = false;
private boolean schematicOnClaim = false;
private String schematicFile = "null";
private boolean spawnEggs = false;
private boolean spawnCustom = true;
private boolean spawnBreeding = false;
private PlotAreaType type = PlotAreaType.NORMAL;
private PlotAreaTerrainType terrain = PlotAreaTerrainType.NONE;
private boolean homeAllowNonmember = false;
private BlockLoc nonmemberHome;
private BlockLoc defaultHome;
private int maxBuildHeight = PlotSquared.platform().versionMaxHeight() + 1; // Exclusive
private int minBuildHeight = PlotSquared.platform().versionMinHeight() + 1; // Inclusive
private int maxGenHeight = PlotSquared.platform().versionMaxHeight(); // Inclusive
private int minGenHeight = PlotSquared.platform().versionMinHeight(); // Inclusive
private GameMode gameMode = GameModes.CREATIVE;
private Map<String, PlotExpression> prices = new HashMap<>();
private List<String> schematics = new ArrayList<>();
private boolean worldBorder = false;
private int borderSize = 1;
private boolean useEconomy = false;
private int hash;
private CuboidRegion region;
private ConcurrentHashMap<String, Object> meta;
private QuadMap<PlotCluster> clusters;
private String signMaterial = "OAK_WALL_SIGN";
private String legacySignMaterial = "WALL_SIGN";
public PlotArea(
final @NonNull String worldName, final @Nullable String id,
@NonNull IndependentPlotGenerator generator, final @Nullable PlotId min,
final @Nullable PlotId max,
@WorldConfig final @Nullable YamlConfiguration worldConfiguration,
final @NonNull GlobalBlockQueue blockQueue
) {
this.worldName = worldName;
this.id = id;
this.plotManager = createManager();
this.generator = generator;
this.globalBlockQueue = blockQueue;
if (min == null || max == null) {
if (min != max) {
throw new IllegalArgumentException(
"None of the ids can be null for this constructor");
}
this.min = null;
this.max = null;
} else {
this.min = min;
this.max = max;
}
this.worldHash = worldName.hashCode();
this.worldConfiguration = worldConfiguration;
}
private static void parseFlags(FlagContainer flagContainer, List<String> flagStrings) {
for (final String key : flagStrings) {
final String[] split;
if (key.contains(";")) {
split = key.split(";");
} else {
split = key.split(":");
}
final PlotFlag<?, ?> flagInstance =
GlobalFlagContainer.getInstance().getFlagFromString(split[0]);
if (flagInstance != null) {
try {
flagContainer.addFlag(flagInstance.parse(split[1]));
} catch (final FlagParseException e) {
LOGGER.warn(
"Failed to parse default flag with key '{}' and value '{}'. "
+ "Reason: {}. This flag will not be added as a default flag.",
e.getFlag().getName(),
e.getValue(),
e.getErrorMessage()
);
e.printStackTrace();
}
} else {
flagContainer.addUnknownFlag(split[0], split[1]);
}
}
}
@NonNull
protected abstract PlotManager createManager();
public QueueCoordinator getQueue() {
return this.globalBlockQueue.getNewQueue(PlotSquared.platform().worldUtil().getWeWorld(worldName));
}
/**
* Returns the region for this PlotArea, or a CuboidRegion encompassing
* the whole world if none exists.
*
* @return CuboidRegion
*/
public CuboidRegion getRegion() {
this.region = getRegionAbs();
if (this.region == null) {
return new CuboidRegion(
BlockVector3.at(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE),
BlockVector3.at(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE)
);
}
return this.region;
}
/**
* Returns the region for this PlotArea.
*
* @return CuboidRegion or null if no applicable region
*/
private CuboidRegion getRegionAbs() {
if (this.region == null) {
if (this.min != null) {
Location bot = getPlotManager().getPlotBottomLocAbs(this.min);
Location top = getPlotManager().getPlotTopLocAbs(this.max);
BlockVector3 pos1 = bot.getBlockVector3().subtract(BlockVector3.ONE);
BlockVector3 pos2 = top.getBlockVector3().add(BlockVector3.ONE);
this.region = new CuboidRegion(pos1, pos2);
}
}
return this.region;
}
/**
* Returns the minimum value of a {@link PlotId}.
*
* @return the minimum value for a {@link PlotId}
*/
public @NonNull PlotId getMin() {
return this.min == null ? PlotId.of(Integer.MIN_VALUE, Integer.MIN_VALUE) : this.min;
}
/**
* Returns the max PlotId.
*
* @return the maximum value for a {@link PlotId}
*/
public @NonNull PlotId getMax() {
return this.max == null ? PlotId.of(Integer.MAX_VALUE, Integer.MAX_VALUE) : this.max;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
PlotArea plotarea = (PlotArea) obj;
return this.getWorldHash() == plotarea.getWorldHash() && this.getWorldName()
.equals(plotarea.getWorldName()) && StringMan.isEqual(this.getId(), plotarea.getId());
}
public Set<PlotCluster> getClusters() {
return this.clusters == null ? new HashSet<>() : this.clusters.getAll();
}
/**
* Check if a PlotArea is compatible (move/copy etc.).
*
* @param plotArea the {@link PlotArea} to compare
* @return {@code true} if both areas are compatible
*/
public boolean isCompatible(final @NonNull PlotArea plotArea) {
final ConfigurationSection section = this.worldConfiguration.getConfigurationSection("worlds");
for (ConfigurationNode setting : plotArea.getSettingNodes()) {
Object constant = section.get(plotArea.worldName + '.' + setting.getConstant());
if (constant == null || !constant
.equals(section.get(this.worldName + '.' + setting.getConstant()))) {
return false;
}
}
return true;
}
/**
* When a world is created, the following method will be called for each.
*
* @param config Configuration Section
*/
public void loadDefaultConfiguration(ConfigurationSection config) {
if ((this.min != null || this.max != null) && !(this instanceof GridPlotWorld)) {
throw new IllegalArgumentException("Must extend GridPlotWorld to provide");
}
if (config.contains("generator.terrain")) {
this.terrain = ConfigurationUtil.getTerrain(config);
this.type = ConfigurationUtil.getType(config);
}
this.mobSpawning = config.getBoolean("natural_mob_spawning");
this.miscSpawnUnowned = config.getBoolean("misc_spawn_unowned");
this.mobSpawnerSpawning = config.getBoolean("mob_spawner_spawning");
this.autoMerge = config.getBoolean("plot.auto_merge");
this.allowSigns = config.getBoolean("plot.create_signs");
if (PlotSquared.platform().serverVersion()[1] == 13) {
this.legacySignMaterial = config.getString("plot.legacy_sign_material");
} else {
this.signMaterial = config.getString("plot.sign_material");
}
String biomeString = config.getString("plot.biome");
if (!biomeString.startsWith("minecraft:")) {
biomeString = "minecraft:" + biomeString;
config.set("plot.biome", biomeString.toLowerCase());
}
this.plotBiome = ConfigurationUtil.BIOME.parseString(biomeString.toLowerCase());
this.schematicOnClaim = config.getBoolean("schematic.on_claim");
this.schematicFile = config.getString("schematic.file");
this.schematicClaimSpecify = config.getBoolean("schematic.specify_on_claim");
this.schematics = new ArrayList<>(config.getStringList("schematic.schematics"));
this.schematics.replaceAll(String::toLowerCase);
this.useEconomy = config.getBoolean("economy.use");
ConfigurationSection priceSection = config.getConfigurationSection("economy.prices");
if (this.useEconomy) {
this.prices = new HashMap<>();
for (String key : priceSection.getKeys(false)) {
String raw = priceSection.getString(key);
if (raw.contains("{arg}")) {
raw = raw.replace("{arg}", "plots");
priceSection.set(key, raw); // update if replaced
}
this.prices.put(key, PlotExpression.compile(raw, "plots"));
}
}
this.plotChat = config.getBoolean("chat.enabled");
this.forcingPlotChat = config.getBoolean("chat.forced");
this.worldBorder = config.getBoolean("world.border");
this.borderSize = config.getInt("world.border_size");
this.maxBuildHeight = config.getInt("world.max_height");
this.minBuildHeight = config.getInt("world.min_height");
this.minGenHeight = config.getInt("world.min_gen_height");
this.maxGenHeight = config.getInt("world.max_gen_height");
switch (config.getString("world.gamemode").toLowerCase()) {
case "creative", "c", "1" -> this.gameMode = GameModes.CREATIVE;
case "adventure", "a", "2" -> this.gameMode = GameModes.ADVENTURE;
case "spectator", "3" -> this.gameMode = GameModes.SPECTATOR;
default -> this.gameMode = GameModes.SURVIVAL;
}
String homeNonMembers = config.getString("home.nonmembers");
String homeDefault = config.getString("home.default");
this.defaultHome = BlockLoc.fromString(homeDefault);
this.homeAllowNonmember = homeNonMembers.equalsIgnoreCase(homeDefault);
if (this.homeAllowNonmember) {
this.nonmemberHome = defaultHome;
} else {
this.nonmemberHome = BlockLoc.fromString(homeNonMembers);
}
if ("side".equalsIgnoreCase(homeDefault)) {
this.defaultHome = null;
} else if (StringMan.isEqualIgnoreCaseToAny(homeDefault, "center", "middle", "centre")) {
this.defaultHome = new BlockLoc(Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
} else {
try {
/*String[] split = homeDefault.split(",");
this.DEFAULT_HOME =
new PlotLoc(Integer.parseInt(split[0]), Integer.parseInt(split[1]));*/
this.defaultHome = BlockLoc.fromString(homeDefault);
} catch (NumberFormatException ignored) {
this.defaultHome = null;
}
}
this.spawnEggs = config.getBoolean("event.spawn.egg");
this.spawnCustom = config.getBoolean("event.spawn.custom");
this.spawnBreeding = config.getBoolean("event.spawn.breeding");
if (PlotSquared.get().isWeInitialised()) {
loadFlags(config);
} else {
ConsolePlayer.getConsole().sendMessage(
TranslatableCaption.of("flags.delaying_loading_area_flags"),
TagResolver.resolver("area", Tag.inserting(Component.text(this.id == null ? this.worldName : this.id)))
);
TaskManager.runTaskLater(() -> loadFlags(config), TaskTime.ticks(1));
}
loadConfiguration(config);
}
private void loadFlags(ConfigurationSection config) {
ConsolePlayer.getConsole().sendMessage(
TranslatableCaption.of("flags.loading_area_flags"),
TagResolver.resolver("area", Tag.inserting(Component.text(this.id == null ? this.worldName : this.id)))
);
List<String> flags = config.getStringList("flags.default");
if (flags.isEmpty()) {
flags = config.getStringList("flags");
if (flags.isEmpty()) {
flags = new ArrayList<>();
ConfigurationSection section = config.getConfigurationSection("flags");
Set<String> keys = section.getKeys(false);
for (String key : keys) {
if (!"default".equals(key)) {
flags.add(key + ';' + section.get(key));
}
}
}
}
parseFlags(this.getFlagContainer(), flags);
ConsolePlayer.getConsole().sendMessage(
TranslatableCaption.of("flags.area_flags"),
TagResolver.resolver("flags", Tag.inserting(Component.text(flags.toString())))
);
List<String> roadflags = config.getStringList("road.flags");
if (roadflags.isEmpty()) {
roadflags = new ArrayList<>();
ConfigurationSection section = config.getConfigurationSection("road.flags");
Set<String> keys = section.getKeys(false);
for (String key : keys) {
if (!"default".equals(key)) {
roadflags.add(key + ';' + section.get(key));
}
}
}
this.roadFlags = !roadflags.isEmpty();
parseFlags(this.getRoadFlagContainer(), roadflags);
ConsolePlayer.getConsole().sendMessage(
TranslatableCaption.of("flags.road_flags"),
TagResolver.resolver("flags", Tag.inserting(Component.text(roadflags.toString())))
);
}
public abstract void loadConfiguration(ConfigurationSection config);
/**
* Saving core PlotArea settings.
*
* @param config Configuration Section
*/
public void saveConfiguration(ConfigurationSection config) {
HashMap<String, Object> options = new HashMap<>();
options.put("natural_mob_spawning", this.isMobSpawning());
options.put("misc_spawn_unowned", this.isMiscSpawnUnowned());
options.put("mob_spawner_spawning", this.isMobSpawnerSpawning());
options.put("plot.auto_merge", this.isAutoMerge());
options.put("plot.create_signs", this.allowSigns());
if (PlotSquared.platform().serverVersion()[1] == 13) {
options.put("plot.legacy_sign_material", this.legacySignMaterial);
} else {
options.put("plot.sign_material", this.signMaterial());
}
options.put("plot.biome", "minecraft:forest");
options.put("schematic.on_claim", this.isSchematicOnClaim());
options.put("schematic.file", this.getSchematicFile());
options.put("schematic.specify_on_claim", this.isSchematicClaimSpecify());
options.put("schematic.schematics", this.getSchematics());
options.put("economy.use", this.useEconomy());
options.put("economy.prices.claim", 100);
options.put("economy.prices.merge", 100);
options.put("economy.prices.sell", 100);
options.put("chat.enabled", this.isPlotChat());
options.put("chat.forced", this.isForcingPlotChat());
options.put("flags.default", null);
options.put("event.spawn.egg", this.isSpawnEggs());
options.put("event.spawn.custom", this.isSpawnCustom());
options.put("event.spawn.breeding", this.isSpawnBreeding());
options.put("world.border", this.hasWorldBorder());
options.put("world.border_size", this.getBorderSize());
options.put("home.default", "side");
String position = config.getString(
"home.nonmembers",
config.getBoolean("home.allow-nonmembers", false) ?
config.getString("home.default", "side") :
"side"
);
options.put("home.nonmembers", position);
options.put("world.max_height", this.getMaxBuildHeight());
options.put("world.min_height", this.getMinBuildHeight());
options.put("world.min_gen_height", this.getMinGenHeight());
options.put("world.max_gen_height", this.getMaxGenHeight());
options.put("world.gamemode", this.getGameMode().getName().toLowerCase());
options.put("road.flags.default", null);
if (this.getType() != PlotAreaType.NORMAL) {
options.put("generator.terrain", this.getTerrain());
options.put("generator.type", this.getType().toString());
}
ConfigurationNode[] settings = getSettingNodes();
/*
* Saving generator specific settings
*/
for (ConfigurationNode setting : settings) {
options.put(setting.getConstant(), setting.getValue());
}
for (Entry<String, Object> stringObjectEntry : options.entrySet()) {
if (!config.contains(stringObjectEntry.getKey())) {
config.set(stringObjectEntry.getKey(), stringObjectEntry.getValue());
}
}
if (!config.contains("flags")) {
config.set(
"flags.use",
"63,64,68,69,71,77,96,143,167,193,194,195,196,197,77,143,69,70,72,147,148,107,183,184,185,186,187,132"
);
}
if (!config.contains("road.flags")) {
config.set("road.flags.liquid-flow", false);
}
}
@NonNull
@Override
public String toString() {
if (this.getId() == null) {
return this.getWorldName();
} else {
return this.getWorldName() + ";" + this.getId();
}
}
@Override
public @NotNull Component asComponent() {
return Component.text(toString());
}
@Override
public int hashCode() {
if (this.hash != 0) {
return this.hash;
}
return this.hash = toString().hashCode();
}
/**
* Used for the <b>/plot setup</b> command Return null if you do not want to support this feature
*
* @return ConfigurationNode[]
*/
public abstract ConfigurationNode[] getSettingNodes();
/**
* Gets the {@link Plot} at a location.
*
* @param location the location
* @return the {@link Plot} or null if none exists
*/
public @Nullable Plot getPlotAbs(final @NonNull Location location) {
final PlotId pid =
this.getPlotManager().getPlotId(location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
}
return getPlotAbs(pid);
}
/**
* Gets the base plot at a location.
*
* @param location the location
* @return base Plot
*/
public @Nullable Plot getPlot(final @NonNull Location location) {
final PlotId pid =
this.getPlotManager().getPlotId(location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
}
return getPlot(pid);
}
/**
* Get the owned base plot at a location.
*
* @param location the location
* @return the base plot or null
*/
public @Nullable Plot getOwnedPlot(final @NonNull Location location) {
final PlotId pid =
this.getPlotManager().getPlotId(location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
}
Plot plot = this.plots.get(pid);
return plot == null ? null : plot.getBasePlot(false);
}
/**
* Get the owned plot at a location.
*
* @param location the location
* @return Plot or null
*/
public @Nullable Plot getOwnedPlotAbs(final @NonNull Location location) {
final PlotId pid =
this.getPlotManager().getPlotId(location.getX(), location.getY(), location.getZ());
if (pid == null) {
return null;
}
return this.plots.get(pid);
}
/**
* Get the owned Plot at a PlotId.
*
* @param id the {@link PlotId}
* @return the plot or null
*/
public @Nullable Plot getOwnedPlotAbs(final @NonNull PlotId id) {
return this.plots.get(id);
}
public @Nullable Plot getOwnedPlot(final @NonNull PlotId id) {
Plot plot = this.plots.get(id);
return plot == null ? null : plot.getBasePlot(false);
}
public boolean contains(final int x, final int z) {
return this.getType() != PlotAreaType.PARTIAL || RegionUtil.contains(getRegionAbs(), x, z);
}
public boolean contains(final @NonNull PlotId id) {
return this.min == null || (id.getX() >= this.min.getX() && id.getX() <= this.max.getX() &&
id.getY() >= this.min.getY() && id.getY() <= this.max.getY());
}
public boolean contains(final @NonNull Location location) {
return StringMan.isEqual(location.getWorldName(), this.getWorldName()) && (
getRegionAbs() == null || this.region.contains(location.getBlockVector3()));
}
/**
* Get if the {@code PlotArea}'s build range (min build height -> max build height) contains the given y value
*
* @param y y height
* @return if build height contains y
*/
public boolean buildRangeContainsY(int y) {
return y >= minBuildHeight && y < maxBuildHeight;
}
/**
* Utility method to check if the player is attempting to place blocks outside the build area, and notify of this if the
* player does not have permissions.
*
* @param player Player to check
* @param y y height to check
* @return true if outside build area with no permissions
* @since 6.9.1
*/
public boolean notifyIfOutsideBuildArea(PlotPlayer<?> player, int y) {
if (!buildRangeContainsY(y) && !player.hasPermission(Permission.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
player.sendMessage(
TranslatableCaption.of("height.height_limit"),
TagResolver.builder()
.tag("minheight", Tag.inserting(Component.text(minBuildHeight)))
.tag(
"maxheight",
Tag.inserting(Component.text(maxBuildHeight))
).build()
);
// Return true if "failed" as the method will always be inverted otherwise
return true;
}
return false;
}
public @NonNull Set<Plot> getPlotsAbs(final UUID uuid) {
if (uuid == null) {
return Collections.emptySet();
}
final HashSet<Plot> myPlots = new HashSet<>();
forEachPlotAbs(value -> {
if (uuid.equals(value.getOwnerAbs())) {
myPlots.add(value);
}
});
return myPlots;
}
public @NonNull Set<Plot> getPlots(final @NonNull UUID uuid) {
return getPlots().stream().filter(plot -> plot.isBasePlot() && plot.isOwner(uuid))
.collect(ImmutableSet.toImmutableSet());
}
/**
* A collection of the claimed plots in this {@link PlotArea}.
*
* @return a collection of claimed plots
*/
public Collection<Plot> getPlots() {
return this.plots.values();
}
public int getPlotCount(final @NonNull UUID uuid) {
if (!Settings.Done.COUNTS_TOWARDS_LIMIT) {
return (int) getPlotsAbs(uuid).stream().filter(plot -> !DoneFlag.isDone(plot)).count();
}
return getPlotsAbs(uuid).size();
}
/**
* Retrieves the plots for the player in this PlotArea.
*
* @param player player to get plots of
* @return set of player's plots
* @deprecated Use {@link #getPlots(UUID)}
*/
@Deprecated
public Set<Plot> getPlots(final @NonNull PlotPlayer<?> player) {
return getPlots(player.getUUID());
}
//todo check if this method is needed in this class
public boolean hasPlot(final @NonNull UUID uuid) {
return this.plots.entrySet().stream().anyMatch(entry -> entry.getValue().isOwner(uuid));
}
public int getPlotCount(final @Nullable PlotPlayer<?> player) {
return player != null ? getPlotCount(player.getUUID()) : 0;
}
public @Nullable Plot getPlotAbs(final @NonNull PlotId id) {
Plot plot = getOwnedPlotAbs(id);
if (plot == null) {
if (this.min != null && (id.getX() < this.min.getX() || id.getX() > this.max.getX() || id.getY() < this.min.getY()
|| id.getY() > this.max.getY())) {
return null;
}
return new Plot(this, id);
}
return plot;
}
public @Nullable Plot getPlot(final @NonNull PlotId id) {
final Plot plot = getOwnedPlotAbs(id);
if (plot == null) {
if (this.min != null && (id.getX() < this.min.getX() || id.getX() > this.max.getX() || id.getY() < this.min.getY()
|| id.getY() > this.max.getY())) {
return null;
}
return new Plot(this, id);
}
return plot.getBasePlot(false);
}
/**
* Retrieves the number of claimed plot in the {@link PlotArea}.
*
* @return the number of claimed plots
*/
public int getPlotCount() {
return this.plots.size();
}
public @Nullable PlotCluster getCluster(final @NonNull Location location) {
final Plot plot = getPlot(location);
if (plot == null) {
return null;
}
return this.clusters != null ? this.clusters.get(plot.getId().getX(), plot.getId().getY()) : null;
}
public @Nullable PlotCluster getFirstIntersectingCluster(
final @NonNull PlotId pos1,
final @NonNull PlotId pos2
) {
if (this.clusters == null) {
return null;
}
for (PlotCluster cluster : this.clusters.getAll()) {
if (cluster.intersects(pos1, pos2)) {
return cluster;
}
}
return null;
}
@Nullable PlotCluster getCluster(final @NonNull PlotId id) {
return this.clusters != null ? this.clusters.get(id.getX(), id.getY()) : null;
}
/**
* Session only plot metadata (session is until the server stops).
* <br>
* For persistent metadata use the flag system
*
* @param key metadata key
* @param value metadata value
*/
public void setMeta(final @NonNull String key, final @Nullable Object value) {
if (this.meta == null) {
this.meta = new ConcurrentHashMap<>();
}
this.meta.put(key, value);
}
public @NonNull <T> T getMeta(final @NonNull String key, final @NonNull T def) {
final Object v = getMeta(key);
return v == null ? def : (T) v;
}
/**
* Get the metadata for a key<br>
* <br>
* For persistent metadata use the flag system
*
* @param key metadata key to get value for
* @return metadata value
*/
public @Nullable Object getMeta(final @NonNull String key) {
if (this.meta != null) {
return this.meta.get(key);
}
return null;
}
@SuppressWarnings("unused")
public @NonNull Set<Plot> getBasePlots() {
final HashSet<Plot> myPlots = new HashSet<>(getPlots());
myPlots.removeIf(plot -> !plot.isBasePlot());
return myPlots;
}
private void forEachPlotAbs(Consumer<Plot> run) {
for (final Entry<PlotId, Plot> entry : this.plots.entrySet()) {
run.accept(entry.getValue());
}
}
public void forEachBasePlot(Consumer<Plot> run) {
for (final Plot plot : getPlots()) {
if (plot.isBasePlot()) {
run.accept(plot);
}
}
}
/**
* Returns an ImmutableMap of PlotId's and Plots in this PlotArea.
*
* @return map of PlotId against Plot for all plots in this area
* @deprecated Poorly implemented. May be removed in future.
*/
//todo eventually remove
@Deprecated
public @NonNull Map<PlotId, Plot> getPlotsRaw() {
return ImmutableMap.copyOf(plots);
}
public @NonNull Set<Entry<PlotId, Plot>> getPlotEntries() {
return this.plots.entrySet();
}
public boolean addPlot(final @NonNull Plot plot) {
for (final PlotPlayer<?> pp : plot.getPlayersInPlot()) {
try (final MetaDataAccess<Plot> metaDataAccess = pp.accessTemporaryMetaData(
PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
metaDataAccess.set(plot);
}
}
return this.plots.put(plot.getId(), plot) == null;
}
public Plot getNextFreePlot(final PlotPlayer<?> player, @Nullable PlotId start) {
int plots;
PlotId center;
PlotId min = getMin();
PlotId max = getMax();
if (getType() == PlotAreaType.PARTIAL) {
center = PlotId.of(MathMan.average(min.getX(), max.getX()), MathMan.average(min.getY(), max.getY()));
plots = Math.max(max.getX() - min.getX() + 1, max.getY() - min.getY() + 1) + 1;
if (start != null) {
start = PlotId.of(start.getX() - center.getX(), start.getY() - center.getY());
}
} else {
center = PlotId.of(0, 0);
plots = Integer.MAX_VALUE;
}
for (int i = 0; i < plots; i++) {
if (start == null) {
start = getMeta("lastPlot", PlotId.of(0, 0));
} else {
start = start.getNextId();
}
PlotId currentId = PlotId.of(center.getX() + start.getX(), center.getY() + start.getY());
Plot plot = getPlotAbs(currentId);
if (plot != null && plot.canClaim(player)) {
setMeta("lastPlot", start);
return plot;
}
}
return null;
}
public boolean addPlotIfAbsent(final @NonNull Plot plot) {
if (this.plots.putIfAbsent(plot.getId(), plot) == null) {
for (PlotPlayer<?> pp : plot.getPlayersInPlot()) {
try (final MetaDataAccess<Plot> metaDataAccess = pp.accessTemporaryMetaData(
PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
metaDataAccess.set(plot);
}
}
return true;
}
return false;
}
public boolean addPlotAbs(final @NonNull Plot plot) {
return this.plots.put(plot.getId(), plot) == null;
}
/**
* Get the plot border distance for a world<br>
*
* @return The border distance or Integer.MAX_VALUE if no border is set
* @deprecated Use {@link PlotArea#getBorder(boolean)}
*/
@Deprecated(forRemoval = true, since = "7.2.0")
public int getBorder() {
final Integer meta = (Integer) getMeta("worldBorder");
if (meta != null) {
int border = meta + 1;
if (border == 0) {
return Integer.MAX_VALUE;
} else {
return border;
}
}
return Integer.MAX_VALUE;
}
/**
* Get the plot border distance for a world, specifying whether the returned value should include the world.border-size
* value. This is a player-traversable area, where plots cannot be claimed
*
* @param getExtended If the extra border given by world.border-size should be included
* @return Border distance of Integer.MAX_VALUE if no border is set
* @since 7.2.0
*/
public int getBorder(boolean getExtended) {
final Integer meta = (Integer) getMeta("worldBorder");
if (meta != null) {
int border = meta + 1;
if (border == 0) {
return Integer.MAX_VALUE;
} else {
return getExtended ? border + borderSize : border;
}
}
return Integer.MAX_VALUE;
}
/**
* Setup the plot border for a world (usually done when the world is created).
*/
public void setupBorder() {
if (!this.hasWorldBorder()) {
return;
}
final Integer meta = (Integer) getMeta("worldBorder");
if (meta == null) {
setMeta("worldBorder", 1);
}
for (final Plot plot : getPlots()) {
plot.updateWorldBorder();
}
}
/**
* Delete the metadata for a key.
* - metadata is session only
* - deleting other plugin's metadata may cause issues
*