From 3f94ceab563528e3061f12905040cd1ec187b05f Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 4 Jul 2023 17:28:18 -0700 Subject: [PATCH] Minor refactors. Using final, etc. --- .../bentobox/boxed/AdvancementsManager.java | 2 +- src/main/java/world/bentobox/boxed/Boxed.java | 20 +++----- .../commands/AdminPlaceStructureCommand.java | 12 ++--- .../biomes/AbstractCopyBiomeProvider.java | 3 +- .../biomes/AbstractSeedBiomeProvider.java | 2 +- .../chunks/AbstractBoxedChunkGenerator.java | 10 ++-- .../chunks/BoxedBlockPopulator.java | 6 +-- .../boxed/listeners/AdvancementListener.java | 4 +- .../boxed/listeners/EnderPearlListener.java | 6 +-- .../boxed/listeners/NewAreaListener.java | 49 ++++++++----------- src/main/resources/config.yml | 4 +- 11 files changed, 52 insertions(+), 66 deletions(-) diff --git a/src/main/java/world/bentobox/boxed/AdvancementsManager.java b/src/main/java/world/bentobox/boxed/AdvancementsManager.java index 47308df..d8adc0f 100644 --- a/src/main/java/world/bentobox/boxed/AdvancementsManager.java +++ b/src/main/java/world/bentobox/boxed/AdvancementsManager.java @@ -237,7 +237,7 @@ public class AdvancementsManager { /** * Get the score for this advancement * @param a - advancement - * @return score of advancement, or 0 if cannot be worked out + * @return score of advancement, or 0 if it cannot be worked out */ public int getScore(Advancement a) { String adv = "advancements." + a.getKey().getKey(); diff --git a/src/main/java/world/bentobox/boxed/Boxed.java b/src/main/java/world/bentobox/boxed/Boxed.java index 2c699d6..d5ca749 100644 --- a/src/main/java/world/bentobox/boxed/Boxed.java +++ b/src/main/java/world/bentobox/boxed/Boxed.java @@ -66,11 +66,8 @@ public class Boxed extends GameModeAddon { private final Config configObject = new Config<>(this, Settings.class); private AdvancementsManager advManager; private AbstractBoxedChunkGenerator netherChunkGenerator; - private World baseWorld; - private World baseWorldNether; private World seedWorld; - private World seedWorldNether; - private Map generatorMap = new HashMap<>(); + private final Map generatorMap = new HashMap<>(); //private World seedWorldEnd; private BiomeProvider boxedBiomeProvider; @@ -185,8 +182,8 @@ public class Boxed extends GameModeAddon { log("Creating Boxed Seed Nether world ..."); // This creates a vanilla base world with biomes AbstractBoxedChunkGenerator seedBaseGen = new BoxedSeedChunkGenerator(this, Environment.NETHER); - baseWorldNether = WorldCreator - .name(worldName + "/" + SEED+NETHER+BASE) + World baseWorldNether = WorldCreator + .name(worldName + "/" + SEED + NETHER + BASE) .generator(seedBaseGen) .environment(Environment.NETHER) .seed(getSettings().getSeed()) @@ -200,8 +197,8 @@ public class Boxed extends GameModeAddon { // This copies a base world with custom biomes log("Creating Boxed Biomed Nether world ..."); BoxedSeedChunkGenerator seedWorldNetherGenerator = new BoxedSeedChunkGenerator(this, Environment.NETHER, new NetherSeedBiomeGenerator(this, seedBaseGen)); - seedWorldNether = WorldCreator - .name(worldName + "/" + SEED+NETHER) + World seedWorldNether = WorldCreator + .name(worldName + "/" + SEED + NETHER) .generator(seedWorldNetherGenerator) .environment(Environment.NETHER) .seed(getSettings().getSeed()) @@ -224,8 +221,8 @@ public class Boxed extends GameModeAddon { log("Creating Boxed Seed world ..."); // This creates a vanilla base world with biomes AbstractBoxedChunkGenerator seedBaseGen = new BoxedSeedChunkGenerator(this, Environment.NORMAL); - baseWorld = WorldCreator - .name(worldName + "/" + SEED+BASE) + World baseWorld = WorldCreator + .name(worldName + "/" + SEED + BASE) .generator(seedBaseGen) .environment(Environment.NORMAL) .seed(getSettings().getSeed()) @@ -268,7 +265,6 @@ public class Boxed extends GameModeAddon { * Registers a world with world management plugins * * @param world the World to register - * @param islandWorld true if this is an island world */ private void registerToWorldManagementPlugins(@NonNull World world) { if (getPlugin().getHooks() != null) { @@ -285,7 +281,7 @@ public class Boxed extends GameModeAddon { } /** - * Copies chunks from the seed world so they can be pasted in the game world + * Copies chunks from the seed world, so they can be pasted in the game world * @param world - source world * @param gen - generator to store the chunks */ diff --git a/src/main/java/world/bentobox/boxed/commands/AdminPlaceStructureCommand.java b/src/main/java/world/bentobox/boxed/commands/AdminPlaceStructureCommand.java index c7e1bc3..87e767c 100644 --- a/src/main/java/world/bentobox/boxed/commands/AdminPlaceStructureCommand.java +++ b/src/main/java/world/bentobox/boxed/commands/AdminPlaceStructureCommand.java @@ -91,7 +91,7 @@ public class AdminPlaceStructureCommand extends CompositeCommand { return false; } // First arg must always be the structure name - List options = Bukkit.getStructureManager().getStructures().keySet().stream().map(k -> k.getKey()).toList(); + List options = Bukkit.getStructureManager().getStructures().keySet().stream().map(NamespacedKey::getKey).toList(); if (!options.contains(args.get(0).toLowerCase(Locale.ENGLISH))) { user.sendMessage("boxed.commands.boxadmin.place.unknown-structure"); return false; @@ -144,9 +144,9 @@ public class AdminPlaceStructureCommand extends CompositeCommand { public boolean execute(User user, String label, List args) { NamespacedKey tag = NamespacedKey.fromString(args.get(0).toLowerCase(Locale.ENGLISH)); Structure s = Bukkit.getStructureManager().loadStructure(tag); - int x = args.size() == 1 || args.get(1).equals("~") ? user.getLocation().getBlockX() : Integer.valueOf(args.get(1).trim()); - int y = args.size() == 1 || args.get(2).equals("~") ? user.getLocation().getBlockY() : Integer.valueOf(args.get(2).trim()); - int z = args.size() == 1 || args.get(3).equals("~") ? user.getLocation().getBlockZ() : Integer.valueOf(args.get(3).trim()); + int x = args.size() == 1 || args.get(1).equals("~") ? user.getLocation().getBlockX() : Integer.parseInt(args.get(1).trim()); + int y = args.size() == 1 || args.get(2).equals("~") ? user.getLocation().getBlockY() : Integer.parseInt(args.get(2).trim()); + int z = args.size() == 1 || args.get(3).equals("~") ? user.getLocation().getBlockZ() : Integer.parseInt(args.get(3).trim()); Location spot = new Location(user.getWorld(), x, y, z); s.place(spot, true, sr, mirror, PALETTE, INTEGRITY, new Random()); NewAreaListener.removeJigsaw(new StructureRecord(tag.getKey(), s, spot, sr, mirror, noMobs)); @@ -168,7 +168,7 @@ public class AdminPlaceStructureCommand extends CompositeCommand { try { config.load(structures); StringBuilder v = new StringBuilder(); - v.append(tag.getKey() + "," + sr2.name() + "," + mirror2.name()); + v.append(tag.getKey()).append(",").append(sr2.name()).append(",").append(mirror2.name()); if (noMobs) { v.append(" NO_MOBS"); } @@ -189,7 +189,7 @@ public class AdminPlaceStructureCommand extends CompositeCommand { { String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : ""; if (args.size() == 2) { - return Optional.of(Util.tabLimit(Bukkit.getStructureManager().getStructures().keySet().stream().map(k -> k.getKey()).toList(), lastArg)); + return Optional.of(Util.tabLimit(Bukkit.getStructureManager().getStructures().keySet().stream().map(NamespacedKey::getKey).toList(), lastArg)); } else if (args.size() == 3) { return Optional.of(List.of(String.valueOf(user.getLocation().getBlockX()), "~")); } else if (args.size() == 4) { diff --git a/src/main/java/world/bentobox/boxed/generators/biomes/AbstractCopyBiomeProvider.java b/src/main/java/world/bentobox/boxed/generators/biomes/AbstractCopyBiomeProvider.java index dc8edb9..e7a0167 100644 --- a/src/main/java/world/bentobox/boxed/generators/biomes/AbstractCopyBiomeProvider.java +++ b/src/main/java/world/bentobox/boxed/generators/biomes/AbstractCopyBiomeProvider.java @@ -46,8 +46,7 @@ public abstract class AbstractCopyBiomeProvider extends BiomeProvider { if (c != null) { int xx = Math.floorMod(x, 16); int zz = Math.floorMod(z, 16); - Biome biome = c.chunkBiomes().getOrDefault(new Vector(xx, y, zz), defaultBiome); - return biome; + return c.chunkBiomes().getOrDefault(new Vector(xx, y, zz), defaultBiome); } else { BentoBox.getInstance().logWarning("Snapshot at " + chunkX + " " + chunkZ + " is not stored"); return defaultBiome; diff --git a/src/main/java/world/bentobox/boxed/generators/biomes/AbstractSeedBiomeProvider.java b/src/main/java/world/bentobox/boxed/generators/biomes/AbstractSeedBiomeProvider.java index 15b8bde..8efd5cb 100644 --- a/src/main/java/world/bentobox/boxed/generators/biomes/AbstractSeedBiomeProvider.java +++ b/src/main/java/world/bentobox/boxed/generators/biomes/AbstractSeedBiomeProvider.java @@ -199,7 +199,7 @@ public abstract class AbstractSeedBiomeProvider extends BiomeProvider { * Loads the custom biomes from the config file * @param config - Yaml configuration object * @param sector - the direction section to load - * @return + * @return sorted map of the biomes and their probabilities as keys */ private SortedMap loadQuad(YamlConfiguration config, String sector) { SortedMap result = new TreeMap<>(); diff --git a/src/main/java/world/bentobox/boxed/generators/chunks/AbstractBoxedChunkGenerator.java b/src/main/java/world/bentobox/boxed/generators/chunks/AbstractBoxedChunkGenerator.java index 77d0407..f668666 100644 --- a/src/main/java/world/bentobox/boxed/generators/chunks/AbstractBoxedChunkGenerator.java +++ b/src/main/java/world/bentobox/boxed/generators/chunks/AbstractBoxedChunkGenerator.java @@ -26,10 +26,12 @@ public abstract class AbstractBoxedChunkGenerator extends ChunkGenerator { protected final Boxed addon; protected static int size; - protected Map, ChunkStore> chunks = new HashMap<>(); - public record ChunkStore(ChunkSnapshot snapshot, List bpEnts, List chests, Map chunkBiomes) {}; - public record EntityData(Vector relativeLoc, BlueprintEntity entity) {}; - public record ChestData(Vector relativeLoc, BlueprintBlock chest) {}; + protected final Map, ChunkStore> chunks = new HashMap<>(); + public record ChunkStore(ChunkSnapshot snapshot, List bpEnts, List chests, Map chunkBiomes) {} + + public record EntityData(Vector relativeLoc, BlueprintEntity entity) {} + + public record ChestData(Vector relativeLoc, BlueprintBlock chest) {} //private final WorldRef wordRefNether; diff --git a/src/main/java/world/bentobox/boxed/generators/chunks/BoxedBlockPopulator.java b/src/main/java/world/bentobox/boxed/generators/chunks/BoxedBlockPopulator.java index 811fd79..1f877a9 100644 --- a/src/main/java/world/bentobox/boxed/generators/chunks/BoxedBlockPopulator.java +++ b/src/main/java/world/bentobox/boxed/generators/chunks/BoxedBlockPopulator.java @@ -31,10 +31,10 @@ import world.bentobox.boxed.generators.chunks.AbstractBoxedChunkGenerator.ChunkS */ public class BoxedBlockPopulator extends BlockPopulator { - private Boxed addon; + private final Boxed addon; /** - * @param addon + * @param addon Boxed */ public BoxedBlockPopulator(Boxed addon) { this.addon = addon; @@ -82,7 +82,7 @@ public class BoxedBlockPopulator extends BlockPopulator { /** * Handles signs, chests and mob spawner blocks * - * @param block - block + * @param bs - block state * @param bpBlock - config */ public void setBlockState(BlockState bs, BlueprintBlock bpBlock) { diff --git a/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java b/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java index c5e4a02..45f17a6 100644 --- a/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java +++ b/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java @@ -169,8 +169,8 @@ public class AdvancementListener implements Listener { /** * Special case Advancement awarding - * Awards the nether and end advancements when they portal for the first time. - * @param e + * Awards the nether and end advancements when they use a portal for the first time. + * @param e PlayerPortalEvent */ @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPortal(PlayerPortalEvent e) { diff --git a/src/main/java/world/bentobox/boxed/listeners/EnderPearlListener.java b/src/main/java/world/bentobox/boxed/listeners/EnderPearlListener.java index c90fd99..6574e6c 100644 --- a/src/main/java/world/bentobox/boxed/listeners/EnderPearlListener.java +++ b/src/main/java/world/bentobox/boxed/listeners/EnderPearlListener.java @@ -47,13 +47,12 @@ public class EnderPearlListener implements Listener { } User u = User.getInstance(e.getPlayer()); - // If the to is outside the box, cancel it + // If the to-location is outside the box, cancel it if (e.getTo() != null) { Island i = addon.getIslands().getIsland(e.getFrom().getWorld(), u); if (i == null || !i.onIsland(e.getTo())) { u.sendMessage("boxed.general.errors.no-teleport-outside"); e.setCancelled(true); - return; } } } @@ -92,19 +91,16 @@ public class EnderPearlListener implements Listener { // Moving is allowed moveBox(u, fromIsland, l); Util.teleportAsync(player, l); - return; } } else { // Different box. This is never allowed. Cancel the throw e.setCancelled(true); u.sendMessage("boxed.general.errors.no-teleport-outside"); - return; } }, () -> { // No box. This is never allowed. Cancel the throw e.setCancelled(true); u.sendMessage("boxed.general.errors.no-teleport-outside"); - return; }); }); diff --git a/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java b/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java index 46f9880..303cef1 100644 --- a/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java +++ b/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java @@ -3,13 +3,7 @@ package world.bentobox.boxed.listeners; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Queue; -import java.util.Random; +import java.util.*; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -75,7 +69,7 @@ public class NewAreaListener implements Listener { * @param mirror - mirror setting * @param noMobs - if false, mobs not pasted */ - public record StructureRecord(String name, Structure structure, Location location, StructureRotation rot, Mirror mirror, Boolean noMobs) {}; + public record StructureRecord(String name, Structure structure, Location location, StructureRotation rot, Mirror mirror, Boolean noMobs) {} private static final Map BUTCHER_ANIMALS = Map.of(0, EntityType.COW, 1, EntityType.SHEEP, 2, EntityType.PIG); private static final List CARDINALS = List.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST); @@ -89,16 +83,16 @@ public class NewAreaListener implements Listener { "shipwreck", "stronghold", "swamp_hut", "village_desert", "village_plains", "village_savanna", "village_snowy", "village_taiga"); private final Boxed addon; - private File structureFile; - private Queue itemsToBuild = new LinkedList<>(); - private static Random rand = new Random(); + private final File structureFile; + private final Queue itemsToBuild = new LinkedList<>(); + private static final Random rand = new Random(); private boolean pasting; - private static Gson gson = new Gson(); - Pair min = new Pair(0,0); - Pair max = new Pair(0,0); + private static final Gson gson = new Gson(); + Pair min = new Pair<>(0, 0); + Pair max = new Pair<>(0, 0); // Database handler for structure data private final Database handler; - private Map islandStructureCache = new HashMap<>(); + private final Map islandStructureCache = new HashMap<>(); @@ -118,7 +112,7 @@ public class NewAreaListener implements Listener { } private void runStructurePrinter(Boxed addon2) { - Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), () -> buildStructure(), 20, 20); + Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), this::buildStructure, 20, 20); for (String js : JAR_STRUCTURES) { addon.saveResource("structures/" + js + ".nbt", false); File structureFile = new File(addon.getDataFolder(), "structures/" + js + ".nbt"); @@ -259,13 +253,13 @@ public class NewAreaListener implements Listener { if (e == null) { addon.logError("Error in structures.yml - unknown environment " + env); } else { - place("structure",config.getConfigurationSection(env), center, e); + place(config.getConfigurationSection(env), center, e); } } } - private void place(String string, ConfigurationSection section, Location center, Environment env) { + private void place(ConfigurationSection section, Location center, Environment env) { World world = env.equals(Environment.NORMAL) ? addon.getOverWorld() : addon.getNetherWorld(); // Loop through the structures in the file - there could be more than one for (String vector : section.getKeys(false)) { @@ -296,13 +290,13 @@ public class NewAreaListener implements Listener { // Extract coords String[] value = vector.split(","); if (value.length > 2) { - int x = Integer.valueOf(value[0].strip()) + center.getBlockX(); - int y = Integer.valueOf(value[1].strip()); - int z = Integer.valueOf(value[2].strip()) + center.getBlockZ(); + int x = Integer.parseInt(value[0].strip()) + center.getBlockX(); + int y = Integer.parseInt(value[1].strip()); + int z = Integer.parseInt(value[2].strip()) + center.getBlockZ(); Location l = new Location(world, x, y, z); itemsToBuild.add(new StructureRecord(name, s, l, rot, mirror, noMobs)); } else { - addon.logError("Structure file syntax error: " + vector + ": " + value); + addon.logError("Structure file syntax error: " + vector + ": " + Arrays.toString(value)); } } } @@ -376,13 +370,13 @@ public class NewAreaListener implements Listener { /** * Process a structure block. Sets it to a structure void at a minimum. - * If the structure block has meta data indicating it is a chest, then it will fill + * If the structure block has metadata indicating it is a chest, then it will fill * the chest with a buried treasure loot. If it is waterlogged, then it will change * the void to water. - * @param b structure block block + * @param b structure block */ private static void processStructureBlock(Block b) { - // I would like to read the data from the block an do something with it! + // I would like to read the data from the block and do something with it! String data = nmsData(b); BoxedStructureBlock bsb = gson.fromJson(data, BoxedStructureBlock.class); b.setType(Material.STRUCTURE_VOID); @@ -428,9 +422,8 @@ public class NewAreaListener implements Listener { case "minecraft:village/common/pigs" -> EntityType.PIG; case "minecraft:village/common/cows" -> EntityType.COW; case "minecraft:village/common/iron_golem" -> EntityType.IRON_GOLEM; - case "minecraft:village/common/butcher_animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3)); - case "minecraft:village/common/animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3)); - default -> null; + case "minecraft:village/common/butcher_animals", "minecraft:village/common/animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3)); + default -> null; }; // Boxed if (type == null && bjb.getPool().startsWith("minecraft:boxed/")) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 48fa9e8..22dbaee 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -9,11 +9,11 @@ boxed: admin: boxadmin # The default action for new player command call. # Sub-command of main player command that will be run on first player command call. - # By default it is sub-command 'create'. + # By default, it is sub-command 'create'. new-player-action: create # The default action for player command. # Sub-command of main player command that will be run on each player command call. - # By default it is sub-command 'go'. + # By default, it is sub-command 'go'. default-action: go # Announce advancements. We recommend you set the game rule `/gamerule announceAdvancements false` # but that blocks all new advancement announcements. This setting tells Boxed to broadcast new advancements.