From 31455a033d713f7d3a6ee657b813eee5f628a63c Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 11 Jul 2018 22:49:20 -0700 Subject: [PATCH] Fixed code smells and bugs, added flower pot protection --- .../api/configuration/WorldSettings.java | 2 +- .../bskyblock/database/BSBDatabase.java | 8 ++-- .../bskyblock/database/objects/Island.java | 3 +- .../objects/adapters/FlagSerializer2.java | 2 +- .../bskyblock/island/builders/Clipboard.java | 45 ++++++++++--------- .../listeners/BannedVisitorCommands.java | 5 +-- .../bskyblock/listeners/NetherPortals.java | 8 ++-- .../flags/BlockInteractionListener.java | 37 ++++----------- .../listeners/flags/BreakBlocksListener.java | 5 ++- .../bskyblock/managers/IslandsManager.java | 1 - .../bskyblock/managers/SchemsManager.java | 1 - .../bskyblock/managers/island/NewIsland.java | 4 +- 12 files changed, 47 insertions(+), 74 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/api/configuration/WorldSettings.java b/src/main/java/us/tastybento/bskyblock/api/configuration/WorldSettings.java index 2fe30ee25..8324ef3c9 100644 --- a/src/main/java/us/tastybento/bskyblock/api/configuration/WorldSettings.java +++ b/src/main/java/us/tastybento/bskyblock/api/configuration/WorldSettings.java @@ -211,6 +211,6 @@ public interface WorldSettings { /** * @return the visitorBannedCommands */ - public List getVisitorBannedCommands(); + List getVisitorBannedCommands(); } diff --git a/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java b/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java index 703311040..158f99bc4 100644 --- a/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java +++ b/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java @@ -30,7 +30,7 @@ public class BSBDatabase { public BSBDatabase(Addon addon, Class type) { this.logger = addon.getLogger(); handler = (AbstractDatabaseHandler) BSBDbSetup.getDatabase().getHandler(type); - + } /** @@ -44,7 +44,6 @@ public class BSBDatabase { } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException | IntrospectionException e) { logger.severe(() -> "Could not load objects from database! Error: " + e.getMessage()); - e.printStackTrace(); } return result; } @@ -61,7 +60,6 @@ public class BSBDatabase { } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException | IntrospectionException e) { logger.severe(() -> "Could not load object from database! " + e.getMessage()); - e.printStackTrace(); } return result; } @@ -100,14 +98,14 @@ public class BSBDatabase { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IntrospectionException e) { logger.severe(() -> "Could not delete config! Error: " + e.getMessage()); - } + } } /** * Close the database */ public void close() { - handler.close(); + handler.close(); } diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java index c6cdb32e7..8c59edd63 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java @@ -1,6 +1,7 @@ package us.tastybento.bskyblock.database.objects; import java.util.Date; +import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -103,7 +104,7 @@ public class Island implements DataObject { @Expose private int levelHandicap; @Expose - private Map spawnPoint = new HashMap<>(); + private Map spawnPoint = new EnumMap<>(Environment.class); public Island() {} public Island(Location location, UUID owner, int protectionRange) { diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/adapters/FlagSerializer2.java b/src/main/java/us/tastybento/bskyblock/database/objects/adapters/FlagSerializer2.java index d078788e5..13be63f03 100644 --- a/src/main/java/us/tastybento/bskyblock/database/objects/adapters/FlagSerializer2.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/adapters/FlagSerializer2.java @@ -45,7 +45,7 @@ public class FlagSerializer2 implements AdapterInterface, Map } Map flags = (Map)object; for (Entry en: flags.entrySet()) { - result.put(en.getKey().getID(), en.getValue() < 0 ? false : true); + result.put(en.getKey().getID(), en.getValue() >= 0); } return result; } diff --git a/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java b/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java index d201a4b60..ab5428168 100644 --- a/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java +++ b/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java @@ -21,7 +21,6 @@ import org.bukkit.Bukkit; import org.bukkit.DyeColor; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.Nameable; import org.bukkit.World; import org.bukkit.block.Banner; import org.bukkit.block.Block; @@ -83,6 +82,14 @@ public class Clipboard { private static final String LOAD_ERROR = "Could not load schems file - does not exist : "; + private static final String BEDROCK = "bedrock"; + + private static final String INVENTORY = "inventory"; + + private static final String ENTITY = "entity"; + + private static final String COLOR = "color"; + private YamlConfiguration blockConfig = new YamlConfiguration(); private Location pos1; private Location pos2; @@ -199,8 +206,8 @@ public class Clipboard { public void paste(World world, Island island, Runnable task) { // Offset due to bedrock Vector off = new Vector(0,0,0); - if (blockConfig.contains("bedrock")) { - String[] offset = blockConfig.getString("bedrock").split(","); + if (blockConfig.contains(BEDROCK)) { + String[] offset = blockConfig.getString(BEDROCK).split(","); off = new Vector(Integer.valueOf(offset[0]), Integer.valueOf(offset[1]), Integer.valueOf(offset[2])); } // Calculate location for pasting @@ -302,7 +309,6 @@ public class Clipboard { if (md instanceof Directional) { Directional facing = (Directional)md; if (md instanceof Stairs) { - //facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING)).getOppositeFace()); Stairs stairs = (Stairs)md; stairs.setInverted(config.getBoolean("inverted")); stairs.setFacingDirection(BlockFace.valueOf(config.getString(FACING, "NORTH"))); @@ -359,26 +365,24 @@ public class Clipboard { if (bs instanceof InventoryHolder) { bs.update(true, false); Inventory ih = ((InventoryHolder)bs).getInventory(); - if (config.isConfigurationSection("inventory")) { - ConfigurationSection inv = config.getConfigurationSection("inventory"); + if (config.isConfigurationSection(INVENTORY)) { + ConfigurationSection inv = config.getConfigurationSection(INVENTORY); inv.getKeys(false).forEach(i -> ih.setItem(Integer.valueOf(i), (ItemStack)inv.get(i))); } } // Entities - if (config.isConfigurationSection("entity")) { - ConfigurationSection en = config.getConfigurationSection("entity"); + if (config.isConfigurationSection(ENTITY)) { + ConfigurationSection en = config.getConfigurationSection(ENTITY); en.getKeys(false).forEach(k -> { ConfigurationSection ent = en.getConfigurationSection(k); Location center = block.getLocation().add(new Vector(0.5, 0.0, 0.5)); LivingEntity e = (LivingEntity)block.getWorld().spawnEntity(center, EntityType.valueOf(ent.getString("type", "PIG"))); - if (e instanceof Nameable) { + if (e != null) { e.setCustomName(ent.getString("name")); } - if (e instanceof Colorable) { - if (ent.contains("color")) { - ((Colorable) e).setColor(DyeColor.valueOf(ent.getString("color"))); - } + if (e instanceof Colorable && ent.contains(COLOR)) { + ((Colorable) e).setColor(DyeColor.valueOf(ent.getString(COLOR))); } if (e instanceof Tameable) { ((Tameable)e).setTamed(ent.getBoolean("tamed")); @@ -396,7 +400,7 @@ public class Clipboard { if (e instanceof AbstractHorse) { AbstractHorse horse = (AbstractHorse)e; horse.setDomestication(ent.getInt("domestication")); - ConfigurationSection inv = ent.getConfigurationSection("inventory"); + ConfigurationSection inv = ent.getConfigurationSection(INVENTORY); inv.getKeys(false).forEach(i -> horse.getInventory().setItem(Integer.valueOf(i), (ItemStack)inv.get(i))); } @@ -427,12 +431,10 @@ public class Clipboard { for (LivingEntity e: entities) { ConfigurationSection en = s.createSection("entity." + e.getUniqueId()); en.set("type", e.getType().name()); - if (e instanceof Nameable) { - en.set("name", e.getCustomName()); - } + en.set("name", e.getCustomName()); if (e instanceof Colorable) { Colorable c = (Colorable)e; - en.set("color", c.getColor().name()); + en.set(COLOR, c.getColor().name()); } if (e instanceof Tameable && ((Tameable)e).isTamed()) { en.set("tamed", true); @@ -471,7 +473,7 @@ public class Clipboard { s.set("data", block.getData()); } if (block.getType().equals(Material.BEDROCK)) { - blockConfig.set("bedrock", x + "," + y + "," + z); + blockConfig.set(BEDROCK, x + "," + y + "," + z); } // Block state @@ -485,7 +487,6 @@ public class Clipboard { } if (md instanceof Directional) { if (md instanceof Stairs) { - //facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING)).getOppositeFace()); Stairs stairs = (Stairs)md; s.set("inverted", stairs.isInverted()); s.set(FACING, stairs.getAscendingDirection().name()); @@ -502,11 +503,11 @@ public class Clipboard { } if (md instanceof Colorable) { Colorable c = (Colorable)md; - s.set("color", c.getColor().name()); + s.set(COLOR, c.getColor().name()); } if (block.getType().equals(Material.CARPET)) { DyeColor c = DyeColor.getByWoolData(block.getData()); - s.set("color", c.name()); + s.set(COLOR, c.name()); } if (md instanceof Redstone) { Redstone r = (Redstone)md; diff --git a/src/main/java/us/tastybento/bskyblock/listeners/BannedVisitorCommands.java b/src/main/java/us/tastybento/bskyblock/listeners/BannedVisitorCommands.java index 12b5e2ec0..4a77bc915 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/BannedVisitorCommands.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/BannedVisitorCommands.java @@ -1,6 +1,3 @@ -/** - * - */ package us.tastybento.bskyblock.listeners; import org.bukkit.event.EventHandler; @@ -22,7 +19,7 @@ public class BannedVisitorCommands implements Listener { private BSkyBlock plugin; /** - * @param plugin + * @param plugin - plugin */ public BannedVisitorCommands(BSkyBlock plugin) { this.plugin = plugin; diff --git a/src/main/java/us/tastybento/bskyblock/listeners/NetherPortals.java b/src/main/java/us/tastybento/bskyblock/listeners/NetherPortals.java index 3c8956151..2c35b7719 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/NetherPortals.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/NetherPortals.java @@ -1,7 +1,5 @@ package us.tastybento.bskyblock.listeners; -import java.util.Objects; - import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -119,7 +117,7 @@ public class NetherPortals implements Listener { if (plugin.getIWM().isEndGenerate(overWorld) && plugin.getIWM().isEndIslands(overWorld)) { World endWorld = plugin.getIWM().getEndWorld(overWorld); // End exists and end islands are being used - Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.THE_END)).filter(Objects::nonNull).orElse(e.getFrom().toVector().toLocation(endWorld)); + Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.THE_END)).orElse(e.getFrom().toVector().toLocation(endWorld)); e.setCancelled(true); new SafeTeleportBuilder(plugin) .entity(e.getPlayer()) @@ -179,7 +177,7 @@ public class NetherPortals implements Listener { if (e.getFrom().getWorld().getEnvironment().equals(Environment.NETHER)) { // If this is from the island nether, then go to the same vector, otherwise try island home location Location to = plugin.getIWM().isNetherIslands(overWorld) - ? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).filter(Objects::nonNull).orElse(e.getFrom().toVector().toLocation(overWorld)) + ? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NORMAL)).orElse(e.getFrom().toVector().toLocation(overWorld)) : plugin.getIslands().getIslandLocation(overWorld, e.getPlayer().getUniqueId()); e.setCancelled(true); @@ -194,7 +192,7 @@ public class NetherPortals implements Listener { World nether = plugin.getIWM().getNetherWorld(overWorld); // If this is to island nether, then go to the same vector, otherwise try spawn Location to = (plugin.getIWM().isNetherIslands(overWorld) && plugin.getIWM().isNetherGenerate(overWorld)) - ? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NETHER)).filter(Objects::nonNull).orElse(e.getFrom().toVector().toLocation(nether)) + ? plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.NETHER)).orElse(e.getFrom().toVector().toLocation(nether)) : nether.getSpawnLocation(); e.setCancelled(true); // Else other worlds teleport to the nether diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java index 38364d078..6d87c7e6f 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java @@ -1,5 +1,7 @@ package us.tastybento.bskyblock.listeners.flags; +import org.bukkit.Material; +import org.bukkit.block.FlowerPot; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.Action; @@ -124,40 +126,19 @@ public class BlockInteractionListener extends AbstractFlagListener { case REDSTONE_COMPARATOR: checkIsland(e, e.getClickedBlock().getLocation(), Flags.REDSTONE); break; - case ARMOR_STAND: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; - case BANNER: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; - case BED: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; - case BOAT: - case BOAT_ACACIA: - case BOAT_BIRCH: - case BOAT_DARK_OAK: - case BOAT_JUNGLE: - case BOAT_SPRUCE: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; case DRAGON_EGG: checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS); break; case ENDER_PORTAL_FRAME: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); + checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS); break; case FLOWER_POT: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; - case MOB_SPAWNER: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; - case OBSERVER: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); - break; - case POWERED_MINECART: - this.getPlugin().log("DEBUG: " + e.getClickedBlock().getType()); + FlowerPot pot = (FlowerPot) e.getClickedBlock().getState(); + if (pot.getContents() == null || pot.getContents().getItemType().equals(Material.AIR)) { + checkIsland(e, e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS); + } else { + checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS); + } break; default: break; diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java index 683e706d9..2090421d2 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java @@ -18,6 +18,7 @@ import org.bukkit.event.vehicle.VehicleDamageEvent; import org.bukkit.util.BlockIterator; import us.tastybento.bskyblock.api.flags.AbstractFlagListener; +import us.tastybento.bskyblock.api.localization.TextVariables; import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.lists.Flags; @@ -100,14 +101,14 @@ public class BreakBlocksListener extends AbstractFlagListener { getIslands().getIslandAt(e.getVehicle().getLocation()).ifPresent(x -> { if (!x.isAllowed(user, Flags.BREAK_BLOCKS)) { e.setCancelled(true); - user.sendMessage("protection.protected"); + user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation(Flags.BREAK_BLOCKS.getHintReference())); } }); // The player is in the world, but not on an island, so general world settings apply if (!Flags.BREAK_BLOCKS.isSetForWorld(e.getVehicle().getWorld())) { e.setCancelled(true); - user.sendMessage("protection.protected"); + user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation(Flags.BREAK_BLOCKS.getHintReference())); } } } diff --git a/src/main/java/us/tastybento/bskyblock/managers/IslandsManager.java b/src/main/java/us/tastybento/bskyblock/managers/IslandsManager.java index 1dc1f284b..c197053d0 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/IslandsManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/IslandsManager.java @@ -704,7 +704,6 @@ public class IslandsManager { handler.saveObject(island); } catch (Exception e) { plugin.logError("Could not save island to database when running async! " + e.getMessage()); - e.printStackTrace(); } } midSave = false; diff --git a/src/main/java/us/tastybento/bskyblock/managers/SchemsManager.java b/src/main/java/us/tastybento/bskyblock/managers/SchemsManager.java index 1439b36a4..d176d2242 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/SchemsManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/SchemsManager.java @@ -82,7 +82,6 @@ public class SchemsManager { islandSchems.put(world, cb); } catch (IOException | InvalidConfigurationException e) { plugin.logError("Could not load " + name + " schem"); - e.printStackTrace(); return false; } return true; diff --git a/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java b/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java index afd26017c..0c409b360 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java +++ b/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java @@ -190,9 +190,7 @@ public class NewIsland { // Find a free spot Map result = new EnumMap<>(Result.class); Result r = isIsland(last); - while (!r.equals(Result.FREE) - && (result.getOrDefault(Result.BLOCK_AT_CENTER, 0) < MAX_UNOWNED_ISLANDS - && result.getOrDefault(Result.BLOCK_AT_CENTER, 0) < MAX_UNOWNED_ISLANDS)) { + while (!r.equals(Result.FREE) && result.getOrDefault(Result.BLOCK_AT_CENTER, 0) < MAX_UNOWNED_ISLANDS) { last = nextGridLocation(last); result.merge(r, 1, (k,v) -> v++); r = isIsland(last);