Encapsulation and Magic Values (#2711)

* Encapsulate attributes of PlotArea using lombok Getters/Setters

* Limit access to collections, don't expose implementation details

* Better naming for some boolean attributes

* Replace magic type value with PlotAreaType enum

* Replace magic terrain value with PlotAreaTerrainType enum

* Fix remaining rebase conflicts

* Reduce code duplication

Co-authored-by: Alexander Söderberg <Sauilitired@users.noreply.github.com>
This commit is contained in:
Hannes Greule
2020-04-02 14:34:38 +02:00
committed by GitHub
parent 9764d4d226
commit 099a680c85
53 changed files with 600 additions and 436 deletions

View File

@ -39,6 +39,8 @@ import com.github.intellectualsites.plotsquared.plot.generator.HybridUtils;
import com.github.intellectualsites.plotsquared.plot.generator.IndependentPlotGenerator;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotAreaTerrainType;
import com.github.intellectualsites.plotsquared.plot.object.PlotAreaType;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.SetupObject;
@ -318,7 +320,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
@Override @SuppressWarnings("deprecation") public void runEntityTask() {
PlotSquared.log(Captions.PREFIX + "KillAllEntities started.");
TaskManager.runTaskRepeat(() -> PlotSquared.get().forEachPlotArea(plotArea -> {
final World world = Bukkit.getWorld(plotArea.worldname);
final World world = Bukkit.getWorld(plotArea.getWorldName());
try {
if (world == null) {
return;
@ -728,8 +730,8 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
SetupObject setup = new SetupObject();
setup.plotManager = manager;
setup.setupGenerator = worldConfig.getString("generator.init", manager);
setup.type = worldConfig.getInt("generator.type");
setup.terrain = worldConfig.getInt("generator.terrain");
setup.type = MainUtil.getType(worldConfig);
setup.terrain = MainUtil.getTerrain(worldConfig);
setup.step = new ConfigurationNode[0];
setup.world = worldName;
SetupUtils.manager.setupWorld(setup);

View File

@ -63,7 +63,7 @@ public class BukkitPlotGenerator extends ChunkGenerator
}
@Override public void augment(PlotArea area) {
BukkitAugmentedGenerator.get(BukkitUtil.getWorld(area.worldname));
BukkitAugmentedGenerator.get(BukkitUtil.getWorld(area.getWorldName()));
}
@Override public boolean isFull() {
@ -88,8 +88,8 @@ public class BukkitPlotGenerator extends ChunkGenerator
Set<PlotArea> areas = PlotSquared.get().getPlotAreas(name);
if (!areas.isEmpty()) {
PlotArea area = areas.iterator().next();
if (!area.MOB_SPAWNING) {
if (!area.SPAWN_EGGS) {
if (!area.isMobSpawning()) {
if (!area.isSpawnEggs()) {
world.setSpawnFlags(false, false);
}
world.setAmbientSpawnLimit(0);

View File

@ -28,7 +28,7 @@ final class BlockStatePopulator extends BlockPopulator {
this.queue = GlobalBlockQueue.IMP.getNewQueue(world.getName(), false);
}
final PlotArea area = PlotSquared.get().getPlotArea(world.getName(), null);
final ChunkWrapper wrap = new ChunkWrapper(area.worldname, source.getX(), source.getZ());
final ChunkWrapper wrap = new ChunkWrapper(area.getWorldName(), source.getX(), source.getZ());
final ScopedLocalBlockQueue chunk = this.queue.getForChunk(wrap.x, wrap.z);
if (this.plotGenerator.populateChunk(chunk, area)) {
this.queue.flush();

View File

@ -101,7 +101,7 @@ public class EntitySpawnListener implements Listener {
}
Plot plot = location.getOwnedPlotAbs();
if (plot == null) {
if (!area.MOB_SPAWNING) {
if (!area.isMobSpawning()) {
EntityType type = entity.getType();
switch (type) {
case DROPPED_ITEM:
@ -112,7 +112,7 @@ public class EntitySpawnListener implements Listener {
case PLAYER:
return;
}
if (type.isAlive() || !area.MISC_SPAWN_UNOWNED) {
if (type.isAlive() || !area.isMiscSpawnUnowned()) {
event.setCancelled(true);
}
}

View File

@ -996,7 +996,7 @@ public class PlayerEvents extends PlotListener implements Listener {
PlotPlayer plotPlayer = BukkitUtil.getPlayer(event.getPlayer());
Location location = plotPlayer.getLocation();
PlotArea area = location.getPlotArea();
if (area == null || (area.PLOT_CHAT == plotPlayer.getAttribute("chat"))) {
if (area == null || (area.isPlotChat() == plotPlayer.getAttribute("chat"))) {
return;
}
Plot plot = area.getPlot(location);
@ -1064,12 +1064,12 @@ public class PlayerEvents extends PlotListener implements Listener {
return;
}
} else if (
(location.getY() > area.MAX_BUILD_HEIGHT || location.getY() < area.MIN_BUILD_HEIGHT)
(location.getY() > area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight())
&& !Permissions
.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
event.setCancelled(true);
MainUtil.sendMessage(plotPlayer, Captions.HEIGHT_LIMIT.getTranslated()
.replace("{limit}", String.valueOf(area.MAX_BUILD_HEIGHT)));
.replace("{limit}", String.valueOf(area.getMaxBuildHeight())));
}
if (!plot.hasOwner()) {
if (!Permissions
@ -2094,7 +2094,7 @@ public class PlayerEvents extends PlotListener implements Listener {
case EGG:
case OCELOT_BABY:
case SPAWNER_EGG:
if (!area.SPAWN_EGGS) {
if (!area.isSpawnEggs()) {
event.setCancelled(true);
return;
}
@ -2102,12 +2102,12 @@ public class PlayerEvents extends PlotListener implements Listener {
case REINFORCEMENTS:
case NATURAL:
case CHUNK_GEN:
if (!area.MOB_SPAWNING) {
if (!area.isMobSpawning()) {
event.setCancelled(true);
return;
}
case BREEDING:
if (!area.SPAWN_BREEDING) {
if (!area.isSpawnBreeding()) {
event.setCancelled(true);
return;
}
@ -2116,13 +2116,13 @@ public class PlayerEvents extends PlotListener implements Listener {
case BUILD_SNOWMAN:
case BUILD_WITHER:
case CUSTOM:
if (!area.SPAWN_CUSTOM && entity.getType() != EntityType.ARMOR_STAND) {
if (!area.isSpawnCustom() && entity.getType() != EntityType.ARMOR_STAND) {
event.setCancelled(true);
return;
}
break;
case SPAWNER:
if (!area.MOB_SPAWNER_SPAWNING) {
if (!area.isMobSpawnerSpawning()) {
event.setCancelled(true);
return;
}
@ -2130,7 +2130,7 @@ public class PlayerEvents extends PlotListener implements Listener {
}
Plot plot = area.getOwnedPlotAbs(location);
if (plot == null) {
if (!area.MOB_SPAWNING) {
if (!area.isMobSpawning()) {
event.setCancelled(true);
}
return;
@ -2928,11 +2928,11 @@ public class PlayerEvents extends PlotListener implements Listener {
PlotPlayer pp = BukkitUtil.getPlayer(player);
Plot plot = area.getPlot(location);
if (plot != null) {
if ((location.getY() > area.MAX_BUILD_HEIGHT || location.getY() < area.MIN_BUILD_HEIGHT)
if ((location.getY() > area.getMaxBuildHeight() || location.getY() < area.getMinBuildHeight())
&& !Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_HEIGHT_LIMIT)) {
event.setCancelled(true);
MainUtil.sendMessage(pp, Captions.HEIGHT_LIMIT.getTranslated()
.replace("{limit}", String.valueOf(area.MAX_BUILD_HEIGHT)));
.replace("{limit}", String.valueOf(area.getMaxBuildHeight())));
}
if (!plot.hasOwner()) {
if (!Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_UNOWNED)) {

View File

@ -128,7 +128,7 @@ public class BukkitChunkManager extends ChunkManager {
return existing;
}
PlotArea area = plot.getArea();
World world = BukkitUtil.getWorld(area.worldname);
World world = BukkitUtil.getWorld(area.getWorldName());
Location bot = plot.getBottomAbs();
Location top = plot.getTopAbs();

View File

@ -7,6 +7,7 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.ConfigurationNode;
import com.github.intellectualsites.plotsquared.plot.generator.GeneratorWrapper;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotAreaType;
import com.github.intellectualsites.plotsquared.plot.object.SetupObject;
import com.github.intellectualsites.plotsquared.plot.util.SetupUtils;
import io.papermc.lib.PaperLib;
@ -80,10 +81,10 @@ public class BukkitSetupUtils extends SetupUtils {
SetupUtils.manager.updateGenerators();
ConfigurationNode[] steps = object.step == null ? new ConfigurationNode[0] : object.step;
String world = object.world;
int type = object.type;
PlotAreaType type = object.type;
String worldPath = "worlds." + object.world;
switch (type) {
case 2: {
case PARTIAL: {
if (object.id != null) {
if (!PlotSquared.get().worlds.contains(worldPath)) {
PlotSquared.get().worlds.createSection(worldPath);
@ -101,8 +102,8 @@ public class BukkitSetupUtils extends SetupUtils {
for (ConfigurationNode step : steps) {
options.put(step.getConstant(), step.getValue());
}
options.put("generator.type", object.type);
options.put("generator.terrain", object.terrain);
options.put("generator.type", object.type.toString());
options.put("generator.terrain", object.terrain.toString());
options.put("generator.plugin", object.plotManager);
if (object.setupGenerator != null && !object.setupGenerator
.equals(object.plotManager)) {
@ -127,7 +128,7 @@ public class BukkitSetupUtils extends SetupUtils {
}
break;
}
case 1: {
case AUGMENTED: {
if (!object.plotManager.endsWith(":single")) {
if (!PlotSquared.get().worlds.contains(worldPath)) {
PlotSquared.get().worlds.createSection(worldPath);
@ -140,9 +141,9 @@ public class BukkitSetupUtils extends SetupUtils {
}
}
PlotSquared.get().worlds
.set("worlds." + world + ".generator.type", object.type);
.set("worlds." + world + ".generator.type", object.type.toString());
PlotSquared.get().worlds
.set("worlds." + world + ".generator.terrain", object.terrain);
.set("worlds." + world + ".generator.terrain", object.terrain.toString());
PlotSquared.get().worlds
.set("worlds." + world + ".generator.plugin", object.plotManager);
if (object.setupGenerator != null && !object.setupGenerator
@ -157,7 +158,7 @@ public class BukkitSetupUtils extends SetupUtils {
}
break;
}
case 0: {
case NORMAL: {
if (steps.length != 0) {
if (!PlotSquared.get().worlds.contains(worldPath)) {
PlotSquared.get().worlds.createSection(worldPath);
@ -224,7 +225,7 @@ public class BukkitSetupUtils extends SetupUtils {
if (SetupUtils.generators.isEmpty()) {
updateGenerators();
}
World world = Bukkit.getWorld(plotArea.worldname);
World world = Bukkit.getWorld(plotArea.getWorldName());
if (world == null) {
return null;
}