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) {
try {
NewIsland.builder(getPlugin())
NewIsland.builder()
.player(user.getPlayer())
.reason(Reason.CREATE)
.build();

View File

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

View File

@ -8,6 +8,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ -29,7 +30,7 @@ public class PlayersManager{
private BSBDatabase database;
private AbstractDatabaseHandler<Players> handler;
private HashMap<UUID, Players> playerCache;
private Map<UUID, Players> playerCache;
private Set<UUID> inTeleport;
/**
@ -240,20 +241,6 @@ public class PlayersManager{
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
* @param playerUUID
@ -313,7 +300,7 @@ public class PlayersManager{
* @param playerUUID
* @return List of home locations
*/
public HashMap<Integer, Location> getHomeLocations(UUID playerUUID) {
public Map<Integer, Location> getHomeLocations(UUID playerUUID) {
addPlayer(playerUUID);
return playerCache.get(playerUUID).getHomeLocations();
}
@ -327,8 +314,7 @@ public class PlayersManager{
public UUID getUUID(String string) {
// See if this is a UUID
try {
UUID uuid = UUID.fromString(string);
return uuid;
return UUID.fromString(string);
} catch (Exception e) {}
// Look in the name cache
return Bukkit.getOfflinePlayer(string).getUniqueId();
@ -336,8 +322,7 @@ public class PlayersManager{
/**
* Sets the player's name and updates the name>UUID database
* @param uniqueId
* @param name
* @param user
*/
public void setPlayerName(User user) {
if (DEBUG) {
@ -380,7 +365,7 @@ public class PlayersManager{
}
// Look in the grid
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
* @return
*/
public User getUser(String string) {
public User getUser(String 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
// 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())) {
int protectionRange = getIslandAt(islandTestLocation).map(x->x.getProtectionRange())
int protectionRange = getIslandAt(islandTestLocation).map(Island::getProtectionRange)
.orElse(plugin.getSettings().getIslandProtectionRange());
if (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
Optional<Island> island = getIslandAt(loc);
if (island.isPresent()) {
//plugin.getLogger().info("DEBUG: island here is " + island.getCenter());
// On an island in the grid
//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");
// Return whether the location is within the protected zone and the player is on the list of acceptable players
return island.get().onIsland(loc) && island.get().getMemberSet().contains(player.getUniqueId());
}
// Not in the grid, so do it the old way
// Make a list of test locations and test them
@ -833,20 +818,13 @@ public class IslandsManager {
islandTestLocations.add(getIslandLocation(player.getUniqueId()));
}
// TODO: Check any coop locations
/*
islandTestLocations.addAll(CoopPlay.getInstance().getCoopIslands(player));
if (islandTestLocations.isEmpty()) {
return false;
}*/
// Run through all the locations
for (Location islandTestLocation : islandTestLocations) {
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.getZ() >= islandTestLocation.getZ() - plugin.getSettings().getIslandProtectionRange()
&& loc.getZ() < islandTestLocation.getZ() + plugin.getSettings().getIslandProtectionRange()) {
return true;
}
&& loc.getZ() < islandTestLocation.getZ() + plugin.getSettings().getIslandProtectionRange();
}
}
return false;
@ -866,7 +844,6 @@ public class IslandsManager {
* @return
*/
private Location netherIsland(Location islandLocation) {
//plugin.getLogger().info("DEBUG: netherworld = " + ASkyBlock.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
* coop island
*
* @param player
* @param user
* @return true if on valid island, false if not
*/
public boolean playerIsOnIsland(User user) {
@ -1025,7 +1002,7 @@ public class IslandsManager {
if (DEBUG) {
plugin.getLogger().info("DEBUG: leaving team");
}
plugin.getPlayers().clearPlayerHomes(playerUUID);
plugin.getPlayers().clearHomeLocations(playerUUID);
removePlayer(playerUUID);
}

View File

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

View File

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

View File

@ -1,9 +1,6 @@
package us.tastybento.bskyblock.database.objects;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -17,13 +14,13 @@ import us.tastybento.bskyblock.BSkyBlock;
* @author tastybento
*/
public class Players implements DataObject {
private HashMap<Integer, Location> homeLocations;
private Map<Integer, Location> homeLocations;
private String uniqueId;
private String playerName;
private int resetsLeft;
private String locale = "";
private int deaths;
private HashMap<Location, Long> kickedList;
private Map<Location, Long> kickedList;
/**
* This is required for database storage
@ -78,28 +75,28 @@ public class Players implements DataObject {
/**
* @return List of home locations
*/
public HashMap<Integer,Location> getHomeLocations() {
public Map<Integer,Location> getHomeLocations() {
return homeLocations;
}
/**
* @return the kickedList
*/
public HashMap<Location, Long> getKickedList() {
public Map<Location, Long> getKickedList() {
return kickedList;
}
/**
* @param kickedList the kickedList to set
*/
public void setKickedList(HashMap<Location, Long> kickedList) {
public void setKickedList(Map<Location, Long> kickedList) {
this.kickedList = kickedList;
}
/**
* @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());
this.homeLocations = homeLocations;
}
@ -248,8 +245,7 @@ public class Players implements DataObject {
// long hours = (coolDownTime.getTimeInMillis() -
// timeNow.getTimeInMillis())/(1000 * 60 * 60);
// Temp minutes
long hours = (coolDownTime.getTimeInMillis() - timeNow.getTimeInMillis()) / (1000 * 60);
return hours;
return (coolDownTime.getTimeInMillis() - timeNow.getTimeInMillis()) / (1000 * 60);
}
}
return 0;
@ -267,14 +263,12 @@ public class Players implements DataObject {
@Override
public String getUniqueId() {
return uniqueId.toString();
return uniqueId;
}
@Override
public void setUniqueId(String uniqueId) {
//Bukkit.getLogger().info("DEBUG: 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;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
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
* {@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}
* @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
public HashMap<Flag, Integer> serialize(Object object) {
HashMap<Flag, Integer> result = new HashMap<>();
public Map<Flag, Integer> serialize(Object object) {
Map<Flag, Integer> result = new HashMap<>();
if (object == null) {
return result;
}
@ -33,7 +34,7 @@ public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>,
result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(key), section.getInt(key));
}
} 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());
}
}
@ -41,18 +42,15 @@ public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>,
}
@Override
public HashMap<String, Integer> deserialize(Object object) {
HashMap<String, Integer> result = new HashMap<>();
public Map<String, Integer> deserialize(Object object) {
Map<String, Integer> result = new HashMap<>();
if (object == null) {
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()) {
result.put(en.getKey().getID(), en.getValue());
}
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
* 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.WHEAT,
Material.CARROT_ITEM,

View File

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