Removed code smells.

This commit is contained in:
tastybento 2019-11-08 12:35:47 -08:00
parent 8e56bc8c78
commit fdfc080307
7 changed files with 41 additions and 43 deletions

View File

@ -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("");
}

View File

@ -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<EntityType, Integer> limits = new HashMap<>();
private final Map<EntityType, Integer> limits = new EnumMap<>(EntityType.class);
private final List<String> gameModes;
public Settings(Limits addon) {

View File

@ -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<EntityType, Material> E2M = ImmutableMap.<EntityType, Material>builder()
public static final Map<EntityType, Material> E2M = ImmutableMap.<EntityType, Material>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<Material, Material> B2M;
public static final Map<Material, Material> B2M;
static {
ImmutableMap.Builder<Material, Material> builder = ImmutableMap.<Material, Material>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();
}

View File

@ -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<Pair<Integer, Integer>> chunksToScan = getChunksToScan(island);
this.task = addon.getServer().getScheduler().runTaskTimer(addon.getPlugin(), () -> {

View File

@ -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<String, Integer> saveMap = new HashMap<>();
private final Database<IslandBlockCount> handler;
private final Map<World, Map<Material, Integer>> worldLimitMap = new HashMap<>();
private Map<Material, Integer> defaultLimitMap = new HashMap<>();
private Map<Material, Integer> 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<Material, Integer> loadLimits(ConfigurationSection cs) {
Map<Material, Integer> mats = new HashMap<>();
Map<Material, Integer> 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<Material, Integer> getMaterialLimits(World w, String id) {
// Merge limits
Map<Material, Integer> result = new HashMap<>();
Map<Material, Integer> result = new EnumMap<>(Material.class);
// Default
defaultLimitMap.forEach(result::put);
// World

View File

@ -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();

View File

@ -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<Material, Integer> blockCount = new HashMap<>();
private Map<Material, Integer> blockCount = new EnumMap<>(Material.class);
/**
* Permission based limits
*/
@Expose
private Map<Material, Integer> blockLimits = new HashMap<>();
private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
// Required for YAML database
public IslandBlockCount() {}