Fixed some code smells

This commit is contained in:
Florian CUNY 2018-02-13 21:38:19 +01:00
parent 0b6da56e82
commit 4aaf559704
10 changed files with 68 additions and 137 deletions

View File

@ -55,7 +55,7 @@ public class IslandCreateCommand extends CompositeCommand {
*/ */
protected void createIsland(User user) { protected void createIsland(User user) {
try { try {
NewIsland.builder(getPlugin()) NewIsland.builder()
.player(user.getPlayer()) .player(user.getPlayer())
.reason(Reason.CREATE) .reason(Reason.CREATE)
.build(); .build();

View File

@ -59,7 +59,7 @@ public class IslandResetCommand extends CompositeCommand {
getPlugin().getLogger().info("DEBUG: making new island "); getPlugin().getLogger().info("DEBUG: making new island ");
} }
try { try {
NewIsland.builder(getPlugin()) NewIsland.builder()
.player(player) .player(player)
.reason(Reason.RESET) .reason(Reason.RESET)
.oldIsland(oldIsland) .oldIsland(oldIsland)

View File

@ -8,6 +8,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -29,7 +30,7 @@ public class PlayersManager{
private BSBDatabase database; private BSBDatabase database;
private AbstractDatabaseHandler<Players> handler; private AbstractDatabaseHandler<Players> handler;
private HashMap<UUID, Players> playerCache; private Map<UUID, Players> playerCache;
private Set<UUID> inTeleport; private Set<UUID> inTeleport;
/** /**
@ -240,20 +241,6 @@ public class PlayersManager{
return plugin.getIslands().getMembers(playerUUID).size() > 1; return plugin.getIslands().getMembers(playerUUID).size() > 1;
} }
/**
* Clears player home locations
*
* @param playerUUID
*/
public void clearPlayerHomes(UUID playerUUID) {
Players player = addPlayer(playerUUID);
player.clearHomeLocations();
/*
* TODO
playerCache.get(playerUUID).save(); // Needed?
TopTen.topTenRemoveEntry(playerUUID);*/
}
/** /**
* Sets the home location for the player * Sets the home location for the player
* @param playerUUID * @param playerUUID
@ -313,7 +300,7 @@ public class PlayersManager{
* @param playerUUID * @param playerUUID
* @return List of home locations * @return List of home locations
*/ */
public HashMap<Integer, Location> getHomeLocations(UUID playerUUID) { public Map<Integer, Location> getHomeLocations(UUID playerUUID) {
addPlayer(playerUUID); addPlayer(playerUUID);
return playerCache.get(playerUUID).getHomeLocations(); return playerCache.get(playerUUID).getHomeLocations();
} }
@ -327,8 +314,7 @@ public class PlayersManager{
public UUID getUUID(String string) { public UUID getUUID(String string) {
// See if this is a UUID // See if this is a UUID
try { try {
UUID uuid = UUID.fromString(string); return UUID.fromString(string);
return uuid;
} catch (Exception e) {} } catch (Exception e) {}
// Look in the name cache // Look in the name cache
return Bukkit.getOfflinePlayer(string).getUniqueId(); return Bukkit.getOfflinePlayer(string).getUniqueId();
@ -336,8 +322,7 @@ public class PlayersManager{
/** /**
* Sets the player's name and updates the name>UUID database * Sets the player's name and updates the name>UUID database
* @param uniqueId * @param user
* @param name
*/ */
public void setPlayerName(User user) { public void setPlayerName(User user) {
if (DEBUG) { if (DEBUG) {
@ -380,7 +365,7 @@ public class PlayersManager{
} }
// Look in the grid // Look in the grid
Optional<Island> island = plugin.getIslands().getIslandAt(loc); Optional<Island> island = plugin.getIslands().getIslandAt(loc);
return island.map(x->x.getOwner()).orElse(null); return island.map(Island::getOwner).orElse(null);
} }
/** /**
@ -617,7 +602,7 @@ public class PlayersManager{
* @param string * @param string
* @return * @return
*/ */
public User getUser(String string) { public User getUser(String string) {
return User.getInstance(getUUID(string)); return User.getInstance(getUUID(string));
} }

View File

@ -780,7 +780,7 @@ public class IslandsManager {
// Must be in the same world as the locations being checked // Must be in the same world as the locations being checked
// Note that getWorld can return null if a world has been deleted on the server // Note that getWorld can return null if a world has been deleted on the server
if (islandTestLocation != null && islandTestLocation.getWorld() != null && islandTestLocation.getWorld().equals(loc.getWorld())) { if (islandTestLocation != null && islandTestLocation.getWorld() != null && islandTestLocation.getWorld().equals(loc.getWorld())) {
int protectionRange = getIslandAt(islandTestLocation).map(x->x.getProtectionRange()) int protectionRange = getIslandAt(islandTestLocation).map(Island::getProtectionRange)
.orElse(plugin.getSettings().getIslandProtectionRange()); .orElse(plugin.getSettings().getIslandProtectionRange());
if (loc.getX() > islandTestLocation.getX() - protectionRange if (loc.getX() > islandTestLocation.getX() - protectionRange
&& loc.getX() < islandTestLocation.getX() + protectionRange && loc.getX() < islandTestLocation.getX() + protectionRange
@ -808,23 +808,8 @@ public class IslandsManager {
// Get the player's island from the grid if it exists // Get the player's island from the grid if it exists
Optional<Island> island = getIslandAt(loc); Optional<Island> island = getIslandAt(loc);
if (island.isPresent()) { if (island.isPresent()) {
//plugin.getLogger().info("DEBUG: island here is " + island.getCenter()); // Return whether the location is within the protected zone and the player is on the list of acceptable players
// On an island in the grid return island.get().onIsland(loc) && island.get().getMemberSet().contains(player.getUniqueId());
//plugin.getLogger().info("DEBUG: onIsland = " + island.onIsland(loc));
//plugin.getLogger().info("DEBUG: members = " + island.getMembers());
//plugin.getLogger().info("DEBUG: player UUID = " + player.getUniqueId());
if (island.get().onIsland(loc) && island.get().getMemberSet().contains(player.getUniqueId())) {
//plugin.getLogger().info("DEBUG: allowed");
// In a protected zone but is on the list of acceptable players
return true;
} else {
// Not allowed
//plugin.getLogger().info("DEBUG: not allowed");
return false;
}
} else {
//plugin.getLogger().info("DEBUG: no island at this location");
} }
// Not in the grid, so do it the old way // Not in the grid, so do it the old way
// Make a list of test locations and test them // Make a list of test locations and test them
@ -833,20 +818,13 @@ public class IslandsManager {
islandTestLocations.add(getIslandLocation(player.getUniqueId())); islandTestLocations.add(getIslandLocation(player.getUniqueId()));
} }
// TODO: Check any coop locations // TODO: Check any coop locations
/*
islandTestLocations.addAll(CoopPlay.getInstance().getCoopIslands(player));
if (islandTestLocations.isEmpty()) {
return false;
}*/
// Run through all the locations // Run through all the locations
for (Location islandTestLocation : islandTestLocations) { for (Location islandTestLocation : islandTestLocations) {
if (loc.getWorld().equals(islandTestLocation.getWorld())) { if (loc.getWorld().equals(islandTestLocation.getWorld())) {
if (loc.getX() >= islandTestLocation.getX() - plugin.getSettings().getIslandProtectionRange() return loc.getX() >= islandTestLocation.getX() - plugin.getSettings().getIslandProtectionRange()
&& loc.getX() < islandTestLocation.getX() + plugin.getSettings().getIslandProtectionRange() && loc.getX() < islandTestLocation.getX() + plugin.getSettings().getIslandProtectionRange()
&& loc.getZ() >= islandTestLocation.getZ() - plugin.getSettings().getIslandProtectionRange() && loc.getZ() >= islandTestLocation.getZ() - plugin.getSettings().getIslandProtectionRange()
&& loc.getZ() < islandTestLocation.getZ() + plugin.getSettings().getIslandProtectionRange()) { && loc.getZ() < islandTestLocation.getZ() + plugin.getSettings().getIslandProtectionRange();
return true;
}
} }
} }
return false; return false;
@ -866,7 +844,6 @@ public class IslandsManager {
* @return * @return
*/ */
private Location netherIsland(Location islandLocation) { private Location netherIsland(Location islandLocation) {
//plugin.getLogger().info("DEBUG: netherworld = " + ASkyBlock.getNetherWorld());
return islandLocation.toVector().toLocation(plugin.getIslandWorldManager().getNetherWorld()); return islandLocation.toVector().toLocation(plugin.getIslandWorldManager().getNetherWorld());
} }
@ -874,7 +851,7 @@ public class IslandsManager {
* Checks if an online player is in the protected area of their island, a team island or a * Checks if an online player is in the protected area of their island, a team island or a
* coop island * coop island
* *
* @param player * @param user
* @return true if on valid island, false if not * @return true if on valid island, false if not
*/ */
public boolean playerIsOnIsland(User user) { public boolean playerIsOnIsland(User user) {
@ -1025,7 +1002,7 @@ public class IslandsManager {
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: leaving team"); plugin.getLogger().info("DEBUG: leaving team");
} }
plugin.getPlayers().clearPlayerHomes(playerUUID); plugin.getPlayers().clearHomeLocations(playerUUID);
removePlayer(playerUUID); removePlayer(playerUUID);
} }

View File

@ -47,10 +47,9 @@ public class NewIsland {
/** /**
* Start building a new island * Start building a new island
* @param plugin
* @return New island builder object * @return New island builder object
*/ */
public static Builder builder(BSkyBlock plugin) { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -106,7 +105,7 @@ public class NewIsland {
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: finding island location"); plugin.getLogger().info("DEBUG: finding island location");
} }
Location next = getNextIsland(player.getUniqueId()); Location next = getNextIsland();
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: found " + next); plugin.getLogger().info("DEBUG: found " + next);
} }
@ -173,7 +172,7 @@ public class NewIsland {
default: default:
break; break;
} }
event = IslandEvent.builder() IslandEvent.builder()
.involvedPlayer(player.getUniqueId()) .involvedPlayer(player.getUniqueId())
.reason(reasonDone) .reason(reasonDone)
.island(island) .island(island)
@ -184,14 +183,12 @@ public class NewIsland {
/** /**
* Get the location of next free island spot * Get the location of next free island spot
* @param playerUUID
* @return Location of island spot * @return Location of island spot
*/ */
private Location getNextIsland(UUID playerUUID) { private Location getNextIsland() {
Location last = plugin.getIslands().getLast(); Location last = plugin.getIslands().getLast();
if (DEBUG) if (DEBUG) {
{
plugin.getLogger().info("DEBUG: last = " + last); plugin.getLogger().info("DEBUG: last = " + last);
// Find the next free spot // Find the next free spot
} }
@ -209,7 +206,7 @@ public class NewIsland {
plugin.getLogger().info("DEBUG: getting next loc"); plugin.getLogger().info("DEBUG: getting next loc");
} }
next = nextGridLocation(next); next = nextGridLocation(next);
}; }
// Make the last next, last // Make the last next, last
last = next.clone(); last = next.clone();
if (DEBUG) { if (DEBUG) {

View File

@ -1,10 +1,7 @@
package us.tastybento.bskyblock.database.objects; package us.tastybento.bskyblock.database.objects;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -77,7 +74,7 @@ public class Island implements DataObject {
//// Team //// //// Team ////
private UUID owner; private UUID owner;
private HashMap<UUID, Integer> members = new HashMap<>(); private Map<UUID, Integer> members = new HashMap<>();
//// State //// //// State ////
private boolean locked = false; private boolean locked = false;
@ -88,7 +85,7 @@ public class Island implements DataObject {
//// Protection flags //// //// Protection flags ////
@Adapter(FlagSerializer.class) @Adapter(FlagSerializer.class)
private HashMap<Flag, Integer> flags = new HashMap<>(); private Map<Flag, Integer> flags = new HashMap<>();
private int levelHandicap; private int levelHandicap;
@ -175,7 +172,7 @@ public class Island implements DataObject {
/** /**
* @return the flags * @return the flags
*/ */
public HashMap<Flag, Integer> getFlags() { public Map<Flag, Integer> getFlags() {
return flags; return flags;
} }
@ -196,7 +193,7 @@ public class Island implements DataObject {
/** /**
* @return the members * @return the members
*/ */
public HashMap<UUID, Integer> getMembers() { public Map<UUID, Integer> getMembers() {
return members; return members;
} }
@ -292,14 +289,7 @@ public class Island implements DataObject {
*/ */
public int getRank(User user) { public int getRank(User user) {
//Bukkit.getLogger().info("DEBUG: user UUID = " + user.getUniqueId()); //Bukkit.getLogger().info("DEBUG: user UUID = " + user.getUniqueId());
return members.containsKey(user.getUniqueId()) ? members.get(user.getUniqueId()) : RanksManager.VISITOR_RANK; return members.getOrDefault(user.getUniqueId(), RanksManager.VISITOR_RANK);
}
/**
* @return the ranks
*/
public HashMap<UUID, Integer> getRanks() {
return members;
} }
/** /**
@ -480,10 +470,8 @@ public class Island implements DataObject {
*/ */
public boolean onIsland(Location target) { public boolean onIsland(Location target) {
if (center != null && center.getWorld() != null) { if (center != null && center.getWorld() != null) {
if (target.getBlockX() >= minProtectedX && target.getBlockX() < (minProtectedX + protectionRange * 2) return target.getBlockX() >= minProtectedX && target.getBlockX() < (minProtectedX + protectionRange * 2)
&& target.getBlockZ() >= minProtectedZ && target.getBlockZ() < (minProtectedZ + protectionRange * 2)) { && target.getBlockZ() >= minProtectedZ && target.getBlockZ() < (minProtectedZ + protectionRange * 2);
return true;
}
} }
return false; return false;
@ -530,7 +518,7 @@ public class Island implements DataObject {
/** /**
* @param flags the flags to set * @param flags the flags to set
*/ */
public void setFlags(HashMap<Flag, Integer> flags) { public void setFlags(Map<Flag, Integer> flags) {
this.flags = flags; this.flags = flags;
} }
@ -574,7 +562,7 @@ public class Island implements DataObject {
/** /**
* @param members the members to set * @param members the members to set
*/ */
public void setMembers(HashMap<UUID, Integer> members) { public void setMembers(Map<UUID, Integer> members) {
this.members = members; this.members = members;
} }
@ -667,7 +655,7 @@ public class Island implements DataObject {
/** /**
* @param ranks the ranks to set * @param ranks the ranks to set
*/ */
public void setRanks(HashMap<UUID, Integer> ranks) { public void setRanks(Map<UUID, Integer> ranks) {
members = ranks; members = ranks;
} }

View File

@ -1,9 +1,6 @@
package us.tastybento.bskyblock.database.objects; package us.tastybento.bskyblock.database.objects;
import java.util.Calendar; import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -17,13 +14,13 @@ import us.tastybento.bskyblock.BSkyBlock;
* @author tastybento * @author tastybento
*/ */
public class Players implements DataObject { public class Players implements DataObject {
private HashMap<Integer, Location> homeLocations; private Map<Integer, Location> homeLocations;
private String uniqueId; private String uniqueId;
private String playerName; private String playerName;
private int resetsLeft; private int resetsLeft;
private String locale = ""; private String locale = "";
private int deaths; private int deaths;
private HashMap<Location, Long> kickedList; private Map<Location, Long> kickedList;
/** /**
* This is required for database storage * This is required for database storage
@ -78,28 +75,28 @@ public class Players implements DataObject {
/** /**
* @return List of home locations * @return List of home locations
*/ */
public HashMap<Integer,Location> getHomeLocations() { public Map<Integer,Location> getHomeLocations() {
return homeLocations; return homeLocations;
} }
/** /**
* @return the kickedList * @return the kickedList
*/ */
public HashMap<Location, Long> getKickedList() { public Map<Location, Long> getKickedList() {
return kickedList; return kickedList;
} }
/** /**
* @param kickedList the kickedList to set * @param kickedList the kickedList to set
*/ */
public void setKickedList(HashMap<Location, Long> kickedList) { public void setKickedList(Map<Location, Long> kickedList) {
this.kickedList = kickedList; this.kickedList = kickedList;
} }
/** /**
* @param homeLocations the homeLocations to set * @param homeLocations the homeLocations to set
*/ */
public void setHomeLocations(HashMap<Integer, Location> homeLocations) { public void setHomeLocations(Map<Integer, Location> homeLocations) {
//Bukkit.getLogger().info("DEBUG: " + homeLocations.toString()); //Bukkit.getLogger().info("DEBUG: " + homeLocations.toString());
this.homeLocations = homeLocations; this.homeLocations = homeLocations;
} }
@ -248,8 +245,7 @@ public class Players implements DataObject {
// long hours = (coolDownTime.getTimeInMillis() - // long hours = (coolDownTime.getTimeInMillis() -
// timeNow.getTimeInMillis())/(1000 * 60 * 60); // timeNow.getTimeInMillis())/(1000 * 60 * 60);
// Temp minutes // Temp minutes
long hours = (coolDownTime.getTimeInMillis() - timeNow.getTimeInMillis()) / (1000 * 60); return (coolDownTime.getTimeInMillis() - timeNow.getTimeInMillis()) / (1000 * 60);
return hours;
} }
} }
return 0; return 0;
@ -267,14 +263,12 @@ public class Players implements DataObject {
@Override @Override
public String getUniqueId() { public String getUniqueId() {
return uniqueId.toString(); return uniqueId;
} }
@Override @Override
public void setUniqueId(String uniqueId) { public void setUniqueId(String uniqueId) {
//Bukkit.getLogger().info("DEBUG: uniqueId = " + uniqueId);
this.uniqueId = uniqueId; this.uniqueId = uniqueId;
//Bukkit.getLogger().info("DEBUG: UUID = " + this.uniqueId);
} }
} }

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.database.objects.adapters; package us.tastybento.bskyblock.database.objects.adapters;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -11,16 +12,16 @@ import us.tastybento.bskyblock.api.flags.Flag;
/** /**
* Serializes the {@link us.tastybento.bskyblock.database.objects.Island#getFlags() getFlags()} and * Serializes the {@link us.tastybento.bskyblock.database.objects.Island#getFlags() getFlags()} and
* {@link us.tastybento.bskyblock.database.objects.Island#setFlags() setFlags()} * {@link us.tastybento.bskyblock.database.objects.Island#setFlags(Map)} () setFlags()}
* in {@link us.tastybento.bskyblock.database.objects.Island} * in {@link us.tastybento.bskyblock.database.objects.Island}
* @author tastybento * @author tastybento
* *
*/ */
public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>, HashMap<String, Integer>> { public class FlagSerializer implements AdapterInterface<Map<Flag, Integer>, Map<String, Integer>> {
@Override @Override
public HashMap<Flag, Integer> serialize(Object object) { public Map<Flag, Integer> serialize(Object object) {
HashMap<Flag, Integer> result = new HashMap<>(); Map<Flag, Integer> result = new HashMap<>();
if (object == null) { if (object == null) {
return result; return result;
} }
@ -33,7 +34,7 @@ public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>,
result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(key), section.getInt(key)); result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(key), section.getInt(key));
} }
} else { } else {
for (Entry<String, Integer> en : ((HashMap<String, Integer>)object).entrySet()) { for (Entry<String, Integer> en : ((Map<String, Integer>)object).entrySet()) {
result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(en.getKey()), en.getValue()); result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(en.getKey()), en.getValue());
} }
} }
@ -41,18 +42,15 @@ public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>,
} }
@Override @Override
public HashMap<String, Integer> deserialize(Object object) { public Map<String, Integer> deserialize(Object object) {
HashMap<String, Integer> result = new HashMap<>(); Map<String, Integer> result = new HashMap<>();
if (object == null) { if (object == null) {
return result; return result;
} }
HashMap<Flag, Integer> flags = (HashMap<Flag, Integer>)object; Map<Flag, Integer> flags = (Map<Flag, Integer>)object;
for (Entry<Flag, Integer> en: flags.entrySet()) { for (Entry<Flag, Integer> en: flags.entrySet()) {
result.put(en.getKey().getID(), en.getValue()); result.put(en.getKey().getID(), en.getValue());
} }
return result; return result;
} }
} }

View File

@ -28,7 +28,7 @@ public class BreedingListener extends AbstractFlagListener {
* A list of items that cause breeding if a player has them in their hand and they click an animal * A list of items that cause breeding if a player has them in their hand and they click an animal
* This list may need to be extended with future versions of Minecraft. * This list may need to be extended with future versions of Minecraft.
*/ */
private final static List<Material> BREEDING_ITEMS = Arrays.asList( private static final List<Material> BREEDING_ITEMS = Arrays.asList(
Material.EGG, Material.EGG,
Material.WHEAT, Material.WHEAT,
Material.CARROT_ITEM, Material.CARROT_ITEM,

View File

@ -43,33 +43,25 @@ public class MobSpawnListener extends AbstractFlagListener {
|| e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) { || e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) {
Optional<Island> island = getIslands().getIslandAt(e.getLocation()); Optional<Island> island = getIslands().getIslandAt(e.getLocation());
if (island.isPresent()) { if (island.isPresent()) {
if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) { if ((e.getEntity() instanceof Monster || e.getEntity() instanceof Slime)
if (!island.get().isAllowed(Flags.MOB_SPAWN)) { && !island.get().isAllowed(Flags.MOB_SPAWN)) {
// Mobs not allowed to spawn // Mobs not allowed to spawn
e.setCancelled(true); e.setCancelled(true);
return; } else if (e.getEntity() instanceof Animals
} && !island.get().isAllowed(Flags.MONSTER_SPAWN)) {
} else if (e.getEntity() instanceof Animals) { // Mobs not allowed to spawn
if (!island.get().isAllowed(Flags.MONSTER_SPAWN)) { e.setCancelled(true);
// Mobs not allowed to spawn
e.setCancelled(true);
return;
}
} }
} else { } else {
// Outside of the island // Outside of the island
if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) { if ((e.getEntity() instanceof Monster || e.getEntity() instanceof Slime)
if (!Flags.MOB_SPAWN.isDefaultSetting()) { && !Flags.MOB_SPAWN.isDefaultSetting()) {
// Mobs not allowed to spawn // Mobs not allowed to spawn
e.setCancelled(true); e.setCancelled(true);
return; } else if (e.getEntity() instanceof Animals
} && !Flags.MONSTER_SPAWN.isDefaultSetting()) {
} else if (e.getEntity() instanceof Animals) { // Mobs not allowed to spawn
if (!Flags.MONSTER_SPAWN.isDefaultSetting()) { e.setCancelled(true);
// Mobs not allowed to spawn
e.setCancelled(true);
return;
}
} }
} }
} }