From fdfc0803078ba253df7c922003e6b219191fb80a Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 8 Nov 2019 12:35:47 -0800 Subject: [PATCH] Removed code smells. --- .../java/bentobox/addon/limits/Limits.java | 2 +- .../java/bentobox/addon/limits/Settings.java | 4 +-- .../addon/limits/commands/LimitPanel.java | 12 +++---- .../addon/limits/commands/LimitsCalc.java | 4 +-- .../limits/listeners/BlockLimitsListener.java | 33 ++++++++++--------- .../limits/listeners/EntityLimitListener.java | 23 ++++++------- .../limits/objects/IslandBlockCount.java | 6 ++-- 7 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/main/java/bentobox/addon/limits/Limits.java b/src/main/java/bentobox/addon/limits/Limits.java index 8064fef..f53a5d5 100644 --- a/src/main/java/bentobox/addon/limits/Limits.java +++ b/src/main/java/bentobox/addon/limits/Limits.java @@ -103,7 +103,7 @@ public class Limits extends Addon { * @return game mode name or empty string if none */ public String getGameModePermPrefix(World world) { - return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getPermissionPrefix()).orElse(""); + return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(GameModeAddon::getPermissionPrefix).orElse(""); } diff --git a/src/main/java/bentobox/addon/limits/Settings.java b/src/main/java/bentobox/addon/limits/Settings.java index ce3cdd2..164e0f9 100644 --- a/src/main/java/bentobox/addon/limits/Settings.java +++ b/src/main/java/bentobox/addon/limits/Settings.java @@ -1,7 +1,7 @@ package bentobox.addon.limits; import java.util.Arrays; -import java.util.HashMap; +import java.util.EnumMap; import java.util.List; import java.util.Map; @@ -12,7 +12,7 @@ import bentobox.addon.limits.commands.LimitPanel; public class Settings { - private final Map limits = new HashMap<>(); + private final Map limits = new EnumMap<>(EntityType.class); private final List gameModes; public Settings(Limits addon) { diff --git a/src/main/java/bentobox/addon/limits/commands/LimitPanel.java b/src/main/java/bentobox/addon/limits/commands/LimitPanel.java index 3bdef6e..9728115 100644 --- a/src/main/java/bentobox/addon/limits/commands/LimitPanel.java +++ b/src/main/java/bentobox/addon/limits/commands/LimitPanel.java @@ -30,7 +30,7 @@ public class LimitPanel { private final Limits addon; // This maps the entity types to the icon that should be shown in the panel // If the icon is null, then the entity type is not covered by the addon - public final static Map E2M = ImmutableMap.builder() + public static final Map E2M = ImmutableMap.builder() .put(EntityType.PIG_ZOMBIE, Material.ZOMBIE_PIGMAN_SPAWN_EGG) .put(EntityType.MUSHROOM_COW, Material.MOOSHROOM_SPAWN_EGG) .put(EntityType.SNOWMAN, Material.SNOW_BLOCK) @@ -73,7 +73,7 @@ public class LimitPanel { .put(EntityType.ENDER_DRAGON, null) .build(); // This is a map of blocks to Material - public final static Map B2M; + public static final Map B2M; static { ImmutableMap.Builder builder = ImmutableMap.builder() .put(Material.POTATOES, Material.POTATO) @@ -81,12 +81,8 @@ public class LimitPanel { .put(Material.BEETROOTS, Material.BEETROOT) .put(Material.REDSTONE_WIRE, Material.REDSTONE); // Block to Material icons - Optional.ofNullable(Material.getMaterial("SWEET_BERRY_BUSH")).ifPresent(material -> { - builder.put(material, Material.getMaterial("SWEET_BERRIES")); - }); - Optional.ofNullable(Material.getMaterial("BAMBOO_SAPLING")).ifPresent(material -> { - builder.put(material, Material.getMaterial("BAMBOO")); - }); + Optional.ofNullable(Material.getMaterial("SWEET_BERRY_BUSH")).ifPresent(material -> builder.put(material, Material.getMaterial("SWEET_BERRIES"))); + Optional.ofNullable(Material.getMaterial("BAMBOO_SAPLING")).ifPresent(material -> builder.put(material, Material.getMaterial("BAMBOO"))); B2M = builder.build(); } diff --git a/src/main/java/bentobox/addon/limits/commands/LimitsCalc.java b/src/main/java/bentobox/addon/limits/commands/LimitsCalc.java index de6be8f..1a0d105 100644 --- a/src/main/java/bentobox/addon/limits/commands/LimitsCalc.java +++ b/src/main/java/bentobox/addon/limits/commands/LimitsCalc.java @@ -1,6 +1,6 @@ package bentobox.addon.limits.commands; -import java.util.HashMap; +import java.util.EnumMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; @@ -44,7 +44,7 @@ public class LimitsCalc { this.island = instance.getIslands().getIsland(world, targetPlayer); this.bll = addon.getBlockLimitListener(); this.ibc = bll.getIsland(island.getUniqueId()); - blockCount = new HashMap<>(); + blockCount = new EnumMap<>(Material.class); this.sender = sender; Set> chunksToScan = getChunksToScan(island); this.task = addon.getServer().getScheduler().runTaskTimer(addon.getPlugin(), () -> { diff --git a/src/main/java/bentobox/addon/limits/listeners/BlockLimitsListener.java b/src/main/java/bentobox/addon/limits/listeners/BlockLimitsListener.java index f5185e0..a4af032 100644 --- a/src/main/java/bentobox/addon/limits/listeners/BlockLimitsListener.java +++ b/src/main/java/bentobox/addon/limits/listeners/BlockLimitsListener.java @@ -1,6 +1,13 @@ package bentobox.addon.limits.listeners; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -63,7 +70,7 @@ public class BlockLimitsListener implements Listener { private final Map saveMap = new HashMap<>(); private final Database handler; private final Map> worldLimitMap = new HashMap<>(); - private Map defaultLimitMap = new HashMap<>(); + private Map defaultLimitMap = new EnumMap<>(Material.class); public BlockLimitsListener(Limits addon) { this.addon = addon; @@ -117,7 +124,7 @@ public class BlockLimitsListener implements Listener { * @return limit map */ private Map loadLimits(ConfigurationSection cs) { - Map mats = new HashMap<>(); + Map mats = new EnumMap<>(Material.class); for (String material : cs.getKeys(false)) { Material mat = Material.getMaterial(material); if (mat != null && mat.isBlock() && !DO_NOT_COUNT.contains(mat)) { @@ -220,22 +227,13 @@ public class BlockLimitsListener implements Listener { process(e.getBlock(), true); } - /* - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onBlock(BlockGrowEvent e) { - Bukkit.getLogger().info(e.getEventName()); - process(e.getBlock(), true); - } - */ @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onBlock(BlockSpreadEvent e) { - //Bukkit.getLogger().info(e.getEventName()); process(e.getBlock(), true); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onBlock(EntityBlockFormEvent e) { - //Bukkit.getLogger().info(e.getEventName()); process(e.getBlock(), true); } @@ -256,10 +254,13 @@ public class BlockLimitsListener implements Listener { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onBlock(BlockFromToEvent e) { - if (e.getBlock().isLiquid()) { - if (e.getToBlock().getType() == Material.REDSTONE_WIRE || e.getToBlock().getType() == Material.REPEATER || e.getToBlock().getType() == Material.COMPARATOR || e.getToBlock().getType() == Material.REDSTONE_TORCH || e.getToBlock().getType() == Material.REDSTONE_WALL_TORCH) { + if (e.getBlock().isLiquid() + && (e.getToBlock().getType() == Material.REDSTONE_WIRE + || e.getToBlock().getType() == Material.REPEATER + || e.getToBlock().getType() == Material.COMPARATOR + || e.getToBlock().getType() == Material.REDSTONE_TORCH + || e.getToBlock().getType() == Material.REDSTONE_WALL_TORCH)) { process(e.getToBlock(), false); - } } } @@ -377,7 +378,7 @@ public class BlockLimitsListener implements Listener { */ public Map getMaterialLimits(World w, String id) { // Merge limits - Map result = new HashMap<>(); + Map result = new EnumMap<>(Material.class); // Default defaultLimitMap.forEach(result::put); // World diff --git a/src/main/java/bentobox/addon/limits/listeners/EntityLimitListener.java b/src/main/java/bentobox/addon/limits/listeners/EntityLimitListener.java index f9c0fd3..5d30557 100644 --- a/src/main/java/bentobox/addon/limits/listeners/EntityLimitListener.java +++ b/src/main/java/bentobox/addon/limits/listeners/EntityLimitListener.java @@ -12,11 +12,13 @@ import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.vehicle.VehicleCreateEvent; import bentobox.addon.limits.Limits; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.util.Util; public class EntityLimitListener implements Listener { + private static final String MOD_BYPASS = "mod.bypass"; private final Limits addon; /** @@ -42,7 +44,7 @@ public class EntityLimitListener implements Listener { for (Entity entity : e.getVehicle().getLocation().getWorld().getNearbyEntities(e.getVehicle().getLocation(), 5, 5, 5)) { if (entity instanceof Player) { Player player = (Player)entity; - boolean bypass = (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getVehicle().getWorld()) + "mod.bypass")); + boolean bypass = (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getVehicle().getWorld()) + MOD_BYPASS)); // Check island addon.getIslands().getProtectedIslandAt(e.getVehicle().getLocation()).ifPresent(island -> { // Ignore spawn @@ -50,14 +52,14 @@ public class EntityLimitListener implements Listener { return; } // Check if the player is at the limit - if (atLimit(island, bypass, e.getVehicle())) { + if (!bypass && atLimit(island, e.getVehicle())) { e.setCancelled(true); for (Entity ent : e.getVehicle().getLocation().getWorld().getNearbyEntities(e.getVehicle().getLocation(), 5, 5, 5)) { if (ent instanceof Player) { ((Player) ent).updateInventory(); User.getInstance(ent).sendMessage("entity-limits.hit-limit", "[entity]", Util.prettifyText(e.getVehicle().getType().toString()) - ,"[number]", String.valueOf(addon.getSettings().getLimits().get(e.getVehicle().getType()))); + , TextVariables.NUMBER, String.valueOf(addon.getSettings().getLimits().get(e.getVehicle().getType()))); } } } @@ -104,7 +106,7 @@ public class EntityLimitListener implements Listener { for (Entity entity : l.getWorld().getNearbyEntities(l, 5, 5, 5)) { if (entity instanceof Player) { Player player = (Player)entity; - if (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(l.getWorld()) + "mod.bypass")) { + if (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(l.getWorld()) + MOD_BYPASS)) { return true; } } @@ -120,14 +122,14 @@ public class EntityLimitListener implements Listener { public void onBlock(HangingPlaceEvent e) { Player player = e.getPlayer(); addon.getIslands().getIslandAt(e.getEntity().getLocation()).ifPresent(island -> { - boolean bypass = player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + "mod.bypass"); + boolean bypass = player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + MOD_BYPASS); // Check if entity can be hung - if (!island.isSpawn() && atLimit(island, bypass, e.getEntity())) { + if (!bypass && !island.isSpawn() && atLimit(island, e.getEntity())) { // Not allowed e.setCancelled(true); User.getInstance(player).sendMessage("block-limits.hit-limit", "[material]", Util.prettifyText(e.getEntity().getType().toString()), - "[number]", String.valueOf(addon.getSettings().getLimits().getOrDefault(e.getEntity().getType(), -1))); + TextVariables.NUMBER, String.valueOf(addon.getSettings().getLimits().getOrDefault(e.getEntity().getType(), -1))); } }); @@ -136,7 +138,7 @@ public class EntityLimitListener implements Listener { private void checkLimit(CreatureSpawnEvent e, boolean bypass) { addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> { // Check if creature is allowed to spawn or not - if (!island.isSpawn() && atLimit(island, bypass, e.getEntity())) { + if (!bypass && !island.isSpawn() && atLimit(island, e.getEntity())) { // Not allowed e.setCancelled(true); // If the reason is anything but because of a spawner then tell players within range @@ -145,7 +147,7 @@ public class EntityLimitListener implements Listener { if (ent instanceof Player) { User.getInstance(ent).sendMessage("entity-limits.hit-limit", "[entity]", Util.prettifyText(e.getEntityType().toString()), - "[number]", String.valueOf(addon.getSettings().getLimits().get(e.getEntityType()))); + TextVariables.NUMBER, String.valueOf(addon.getSettings().getLimits().get(e.getEntityType()))); } } } @@ -158,11 +160,10 @@ public class EntityLimitListener implements Listener { /** * Checks if new entities can be added to island * @param island - island - * @param bypass - true if this is being done by a player with authorization to bypass limits * @param ent - the entity * @return true if at the limit, false if not */ - private boolean atLimit(Island island, boolean bypass, Entity ent) { + private boolean atLimit(Island island, Entity ent) { long count = ent.getWorld().getEntities().stream() .filter(e -> e.getType().equals(ent.getType())) .filter(e -> island.inIslandSpace(e.getLocation())).count(); diff --git a/src/main/java/bentobox/addon/limits/objects/IslandBlockCount.java b/src/main/java/bentobox/addon/limits/objects/IslandBlockCount.java index c629de2..fd74db1 100644 --- a/src/main/java/bentobox/addon/limits/objects/IslandBlockCount.java +++ b/src/main/java/bentobox/addon/limits/objects/IslandBlockCount.java @@ -1,6 +1,6 @@ package bentobox.addon.limits.objects; -import java.util.HashMap; +import java.util.EnumMap; import java.util.Map; import org.bukkit.Material; @@ -22,13 +22,13 @@ public class IslandBlockCount implements DataObject { private String gameMode = ""; @Expose - private Map blockCount = new HashMap<>(); + private Map blockCount = new EnumMap<>(Material.class); /** * Permission based limits */ @Expose - private Map blockLimits = new HashMap<>(); + private Map blockLimits = new EnumMap<>(Material.class); // Required for YAML database public IslandBlockCount() {}