Skip to content

Commit

Permalink
Removed code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 8, 2019
1 parent 8e56bc8 commit fdfc080
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/main/java/bentobox/addon/limits/Limits.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public String getGameModeName(World world) {
* @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("");
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/java/bentobox/addon/limits/Settings.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,7 +12,7 @@

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) {
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/bentobox/addon/limits/commands/LimitPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -73,20 +73,16 @@ 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)
.put(Material.CARROTS, Material.CARROT)
.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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/bentobox/addon/limits/commands/LimitsCalc.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(), () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -117,7 +124,7 @@ private void loadAllLimits() {
* @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)) {
Expand Down Expand Up @@ -220,22 +227,13 @@ public void onBlock(BlockFormEvent e) {
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);
}

Expand All @@ -256,10 +254,13 @@ public void onBlock(EntityChangeBlockEvent e) {

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

Expand Down Expand Up @@ -377,7 +378,7 @@ private int checkLimit(World w, Material m, String id) {
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
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;

/**
Expand All @@ -42,22 +44,22 @@ public void onMinecart(VehicleCreateEvent e) {
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
if (island.isSpawn()) {
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())));
}
}
}
Expand Down Expand Up @@ -104,7 +106,7 @@ private boolean checkByPass(Location l) {
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;
}
}
Expand All @@ -120,14 +122,14 @@ private boolean checkByPass(Location l) {
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)));

}
});
Expand All @@ -136,7 +138,7 @@ public void onBlock(HangingPlaceEvent e) {
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
Expand All @@ -145,7 +147,7 @@ private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
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())));
}
}
}
Expand All @@ -158,11 +160,10 @@ private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
/**
* 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {}
Expand Down

0 comments on commit fdfc080

Please sign in to comment.