Removed code smells

This commit is contained in:
tastybento 2018-08-26 08:21:41 -07:00
parent 85c28f8a70
commit f686060e54
15 changed files with 87 additions and 129 deletions

View File

@ -55,7 +55,7 @@ public class Level extends Addon {
/**
* Get level from cache for a player
* @param targetPlayer
* @param targetPlayer - target player
* @return Level of player
*/
public long getIslandLevel(World world, UUID targetPlayer) {
@ -92,7 +92,7 @@ public class Level extends Addon {
public void onDisable(){
// Save the cache
if (levelsCache != null) {
save(false);
save();
}
if (topTen != null) {
topTen.saveTopTen();
@ -147,18 +147,17 @@ public class Level extends Addon {
/**
* Save the levels to the database
* @param async - if true, saving will be done async
*/
public void save(boolean async){
private void save(){
// No async for now
levelsCache.values().forEach(handler::saveObject);
}
/**
* Sets the player's level to a value
* @param world
* @param targetPlayer
* @param level
* @param world - world
* @param targetPlayer - target player
* @param level - level
*/
public void setIslandLevel(World world, UUID targetPlayer, long level) {
LevelsData ld = getLevelsData(targetPlayer);

View File

@ -10,14 +10,14 @@ import bentobox.addon.level.calculators.PlayerLevel;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
public class LevelPresenter {
class LevelPresenter {
private int levelWait;
private final Level addon;
private final BentoBox plugin;
// Level calc cool down
private HashMap<UUID, Long> levelWaitTime = new HashMap<UUID, Long>();
private final HashMap<UUID, Long> levelWaitTime = new HashMap<>();
public LevelPresenter(Level addon, BentoBox plugin) {
this.addon = addon;
@ -28,10 +28,9 @@ public class LevelPresenter {
* Calculates the island level
* @param world - world to check
* @param sender - asker of the level info
* @param targetPlayer
* @return - false if this is cannot be done
* @param targetPlayer - target player
*/
public boolean calculateIslandLevel(World world, final User sender, UUID targetPlayer) {
public void calculateIslandLevel(World world, final User sender, UUID targetPlayer) {
// Get permission prefix for this world
String permPrefix = plugin.getIWM().getPermissionPrefix(world);
// Check if target has island
@ -43,7 +42,7 @@ public class LevelPresenter {
inTeam = true;
} else {
sender.sendMessage("general.errors.player-has-no-island");
return false;
return;
}
}
// Player asking for their own island calc
@ -62,25 +61,21 @@ public class LevelPresenter {
// Asking for the level of another player
sender.sendMessage("island.level.island-level-is","[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer)));
}
return true;
}
/**
* Sets cool down for the level command
*
* @param player
* @param user - user
*/
private void setLevelWaitTime(final User player) {
levelWaitTime.put(player.getUniqueId(), Long.valueOf(Calendar.getInstance().getTimeInMillis() + levelWait * 1000));
private void setLevelWaitTime(final User user) {
levelWaitTime.put(user.getUniqueId(), Calendar.getInstance().getTimeInMillis() + levelWait * 1000);
}
private boolean onLevelWaitTime(final User sender) {
if (levelWaitTime.containsKey(sender.getUniqueId())) {
if (levelWaitTime.get(sender.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
return true;
}
return levelWaitTime.get(sender.getUniqueId()) > Calendar.getInstance().getTimeInMillis();
return false;
}
return false;
@ -88,8 +83,8 @@ public class LevelPresenter {
private long getLevelWaitTime(final User sender) {
if (levelWaitTime.containsKey(sender.getUniqueId())) {
if (levelWaitTime.get(sender.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
return (levelWaitTime.get(sender.getUniqueId()).longValue() - Calendar.getInstance().getTimeInMillis()) / 1000;
if (levelWaitTime.get(sender.getUniqueId()) > Calendar.getInstance().getTimeInMillis()) {
return (levelWaitTime.get(sender.getUniqueId()) - Calendar.getInstance().getTimeInMillis()) / 1000;
}
return 0L;

View File

@ -28,11 +28,11 @@ import world.bentobox.bentobox.database.Database;
*
*/
public class TopTen implements Listener {
private Level addon;
private final Level addon;
// Top ten list of players
private Map<World,TopTenData> topTenList;
private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
private Database<TopTenData> handler;
private final Database<TopTenData> handler;
public TopTen(Level addon) {
this.addon = addon;
@ -45,8 +45,8 @@ public class TopTen implements Listener {
/**
* Adds a player to the top ten, if the level is good enough
*
* @param ownerUUID
* @param l
* @param ownerUUID - owner UUID
* @param l - level
*/
public void addEntry(World world, UUID ownerUUID, long l) {
// Check if player is an island owner or not
@ -68,13 +68,10 @@ public class TopTen implements Listener {
/**
* Displays the Top Ten list
* @param world
*
* @param user
* - the requesting player
* @return - true if successful, false if no Top Ten list exists
* @param world - world
* @param user - the requesting player
*/
public boolean getGUI(World world, final User user, String permPrefix) {
public void getGUI(World world, final User user, String permPrefix) {
// Check world
topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).setUniqueId(world.getName());
@ -103,7 +100,6 @@ public class TopTen implements Listener {
}
}
panel.build();
return true;
}
/**
@ -142,7 +138,7 @@ public class TopTen implements Listener {
/**
* Loads all the top tens from the database
*/
public void loadTopTen() {
private void loadTopTen() {
topTenList = new HashMap<>();
handler.loadObjects().forEach(tt -> topTenList.put(Bukkit.getWorld(tt.getUniqueId()), tt));
}
@ -150,7 +146,7 @@ public class TopTen implements Listener {
/**
* Removes ownerUUID from the top ten list
*
* @param ownerUUID
* @param ownerUUID - uuid to remove
*/
public void removeEntry(World world, UUID ownerUUID) {
topTenList.putIfAbsent(world, new TopTenData());

View File

@ -30,19 +30,19 @@ public class CalcIslandLevel {
private static final int MAX_CHUNKS = 200;
private static final long SPEED = 1;
private static final String LINE_BREAK = "==================================";
private boolean checking = true;
private BukkitTask task;
private boolean checking;
private final BukkitTask task;
private Level addon;
private final Level addon;
private Set<Pair<Integer, Integer>> chunksToScan;
private Island island;
private World world;
private Results result;
private Runnable onExit;
private final Set<Pair<Integer, Integer>> chunksToScan;
private final Island island;
private final World world;
private final Results result;
private final Runnable onExit;
// Copy the limits hashmap
HashMap<Material, Integer> limitCount;
private final HashMap<Material, Integer> limitCount;
/**
@ -123,7 +123,7 @@ public class CalcIslandLevel {
for (int y = 0; y < island.getCenter().getWorld().getMaxHeight(); y++) {
Material blockData = chunk.getBlockType(x, y, z);
boolean belowSeaLevel = (addon.getSettings().getSeaHeight() > 0 && y<=addon.getSettings().getSeaHeight()) ? true : false;
boolean belowSeaLevel = addon.getSettings().getSeaHeight() > 0 && y <= addon.getSettings().getSeaHeight();
// Air is free
if (!blockData.equals(Material.AIR)) {
checkBlock(blockData, belowSeaLevel);
@ -146,7 +146,7 @@ public class CalcIslandLevel {
/**
* Checks if a block has been limited or not and whether a block has any value or not
* @param md
* @param md - Material to check
* @return value of the block if can be counted
*/
private int limitCount(Material md) {
@ -170,7 +170,7 @@ public class CalcIslandLevel {
/**
* Get value of a material
* World blocks trump regular block values
* @param md
* @param md - Material to check
* @return value of a material
*/
private int getValue(Material md) {
@ -182,8 +182,8 @@ public class CalcIslandLevel {
/**
* Get a set of all the chunks in island
* @param island
* @return
* @param island - island
* @return - set of pairs of x,z coordinates to check
*/
private Set<Pair<Integer, Integer>> getChunksToScan(Island island) {
Set<Pair<Integer, Integer>> chunkSnapshot = new HashSet<>();
@ -267,9 +267,7 @@ public class CalcIslandLevel {
private Collection<String> sortedReport(int total, Multiset<Material> MaterialCount) {
Collection<String> r = new ArrayList<>();
Iterable<Multiset.Entry<Material>> entriesSortedByCount = Multisets.copyHighestCountFirst(MaterialCount).entrySet();
Iterator<Entry<Material>> it = entriesSortedByCount.iterator();
while (it.hasNext()) {
Entry<Material> en = it.next();
for (Entry<Material> en : entriesSortedByCount) {
Material type = en.getElement();
int value = 0;
@ -278,7 +276,7 @@ public class CalcIslandLevel {
value = addon.getSettings().getBlockValues().get(type);
}
r.add(type.toString() + ":"
+ String.format("%,d",en.getCount()) + " blocks x " + value + " = " + (value * en.getCount()));
+ String.format("%,d", en.getCount()) + " blocks x " + value + " = " + (value * en.getCount()));
total += (value * en.getCount());
}
r.add("Subtotal = " + total);
@ -299,10 +297,10 @@ public class CalcIslandLevel {
*/
public class Results {
private List<String> report;
private Multiset<Material> mdCount = HashMultiset.create();
private Multiset<Material> uwCount = HashMultiset.create();
private Multiset<Material> ncCount = HashMultiset.create();
private Multiset<Material> ofCount = HashMultiset.create();
private final Multiset<Material> mdCount = HashMultiset.create();
private final Multiset<Material> uwCount = HashMultiset.create();
private final Multiset<Material> ncCount = HashMultiset.create();
private final Multiset<Material> ofCount = HashMultiset.create();
private long rawBlockCount = 0;
private long underWaterBlockCount = 0;
private long level = 0;
@ -314,12 +312,7 @@ public class CalcIslandLevel {
public int getDeathHandicap() {
return deathHandicap;
}
/**
* @param deathHandicap the deathHandicap to set
*/
public void setDeathHandicap(int deathHandicap) {
this.deathHandicap = deathHandicap;
}
/**
* @return the report
*/
@ -338,24 +331,6 @@ public class CalcIslandLevel {
public long getPointsToNextLevel() {
return pointsToNextLevel;
}
/**
* @param report the report to set
*/
public void setReport(List<String> report) {
this.report = report;
}
/**
* @param level the level to set
*/
public void setLevel(long level) {
this.level = level;
}
/**
* @param pointsToNextLevel the pointsToNextLevel to set
*/
public void setPointsToNextLevel(long pointsToNextLevel) {
this.pointsToNextLevel = pointsToNextLevel;
}
}
}

View File

@ -19,14 +19,14 @@ import world.bentobox.bentobox.database.objects.Island;
*/
public class PlayerLevel {
private Level addon;
private final Level addon;
private Island island;
private World world;
private User asker;
private UUID targetPlayer;
private final Island island;
private final World world;
private final User asker;
private final UUID targetPlayer;
private long oldLevel;
private final long oldLevel;
private CalcIslandLevel calc;
@ -34,7 +34,7 @@ public class PlayerLevel {
public PlayerLevel(final Level addon, final Island island, final UUID targetPlayer, final User asker) {
this.addon = addon;
this.island = island;
this.world = island != null ? island.getCenter().getWorld() : null;
this.world = island.getCenter().getWorld();
this.asker = asker;
this.targetPlayer = targetPlayer;
this.oldLevel = addon.getIslandLevel(world, targetPlayer);
@ -44,7 +44,7 @@ public class PlayerLevel {
addon.getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
// Calculate if not cancelled
calc = new CalcIslandLevel(addon, island, ()-> fireIslandLevelCalcEvent());
calc = new CalcIslandLevel(addon, island, this::fireIslandLevelCalcEvent);
}
}

View File

@ -23,7 +23,7 @@ public class AdminTop extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
// Get world
World world = null;
World world;
if (args.isEmpty()) {
if (getPlugin().getIWM().getOverWorlds().size() == 1) {
world = getPlugin().getIWM().getOverWorlds().get(0);

View File

@ -16,7 +16,7 @@ public class Settings {
private int seaHeight;
private Map<Material, Integer> blockLimits = new HashMap<>();
private Map<Material, Integer> blockValues = new HashMap<>();
private Map<World, Map<Material, Integer>> worldBlockValues = new HashMap<>();
private final Map<World, Map<Material, Integer>> worldBlockValues = new HashMap<>();
private double underWaterMultiplier;
private int deathpenalty;
private long levelCost;
@ -103,7 +103,7 @@ public class Settings {
/**
* @param sumTeamDeaths the sumTeamDeaths to set
*/
public final void setSumTeamDeaths(boolean sumTeamDeaths) {
private void setSumTeamDeaths(boolean sumTeamDeaths) {
this.sumTeamDeaths = sumTeamDeaths;
}
/**
@ -127,7 +127,7 @@ public class Settings {
/**
* @param blockLimits2 the blockLimits to set
*/
public final void setBlockLimits(HashMap<Material, Integer> blockLimits2) {
private void setBlockLimits(HashMap<Material, Integer> blockLimits2) {
this.blockLimits = blockLimits2;
}
/**
@ -139,7 +139,7 @@ public class Settings {
/**
* @param blockValues2 the blockValues to set
*/
public final void setBlockValues(Map<Material, Integer> blockValues2) {
private void setBlockValues(Map<Material, Integer> blockValues2) {
this.blockValues = blockValues2;
}
/**
@ -151,7 +151,7 @@ public class Settings {
/**
* @param underWaterMultiplier the underWaterMultiplier to set
*/
public final void setUnderWaterMultiplier(double underWaterMultiplier) {
private void setUnderWaterMultiplier(double underWaterMultiplier) {
this.underWaterMultiplier = underWaterMultiplier;
}
/**
@ -163,7 +163,7 @@ public class Settings {
/**
* @param deathpenalty the deathpenalty to set
*/
public final void setDeathpenalty(int deathpenalty) {
private void setDeathpenalty(int deathpenalty) {
this.deathpenalty = deathpenalty;
}
/**
@ -175,7 +175,7 @@ public class Settings {
/**
* @param levelCost the levelCost to set
*/
public final void setLevelCost(long levelCost) {
private void setLevelCost(long levelCost) {
this.levelCost = levelCost;
}
/**
@ -193,16 +193,17 @@ public class Settings {
/**
* @return the levelWait
*/
public final int getLevelWait() {
private int getLevelWait() {
return levelWait;
}
/**
* @param levelWait the levelWait to set
*/
public final void setLevelWait(int levelWait) {
private void setLevelWait(int levelWait) {
this.levelWait = levelWait;
}
/**
* TODO: Use max deaths
* @return the maxDeaths
*/
public final int getMaxDeaths() {
@ -211,7 +212,7 @@ public class Settings {
/**
* @param maxDeaths the maxDeaths to set
*/
public final void setMaxDeaths(int maxDeaths) {
private void setMaxDeaths(int maxDeaths) {
this.maxDeaths = maxDeaths;
}
/**
@ -223,7 +224,7 @@ public class Settings {
/**
* @param islandResetDeathReset the islandResetDeathReset to set
*/
public final void setIslandResetDeathReset(boolean islandResetDeathReset) {
private void setIslandResetDeathReset(boolean islandResetDeathReset) {
this.islandResetDeathReset = islandResetDeathReset;
}
/**
@ -235,7 +236,7 @@ public class Settings {
/**
* @param teamJoinDeathReset the teamJoinDeathReset to set
*/
public final void setTeamJoinDeathReset(boolean teamJoinDeathReset) {
private void setTeamJoinDeathReset(boolean teamJoinDeathReset) {
this.teamJoinDeathReset = teamJoinDeathReset;
}

View File

@ -26,9 +26,9 @@ public class LevelsData implements DataObject {
/**
* Create a level entry for target player
* @param targetPlayer
* @param level
* @param world
* @param targetPlayer - target player
* @param level - level
* @param world - world
*/
public LevelsData(UUID targetPlayer, long level, World world) {
uniqueId = targetPlayer.toString();

View File

@ -55,7 +55,7 @@ public class TopTenData implements DataObject {
/**
* Get the level for this UUID, or zero if the UUID is not found
* @param uuid
* @param uuid - UUID to check
* @return island level
*/
public long getLevel(UUID uuid) {
@ -66,7 +66,7 @@ public class TopTenData implements DataObject {
/**
* Removes ownerUUID from the top ten
* @param ownerUUID
* @param ownerUUID - UUID to remove
*/
public void remove(UUID ownerUUID) {
this.topTen.remove(ownerUUID);

View File

@ -18,9 +18,9 @@ public class IslandLevelCalculatedEvent extends IslandBaseEvent {
private UUID targetPlayer;
/**
* @param targetPlayer
* @param island
* @param results
* @param targetPlayer - target player
* @param island - island
* @param results - results object to set
*/
public IslandLevelCalculatedEvent(UUID targetPlayer, Island island, Results results) {
super(island);

View File

@ -12,7 +12,7 @@ import world.bentobox.bentobox.database.objects.Island;
*/
public class IslandPreLevelEvent extends IslandBaseEvent {
private UUID targetPlayer;
private final UUID targetPlayer;
/**

View File

@ -9,7 +9,7 @@ import org.bukkit.event.HandlerList;
*
* @author tastybento
*/
public class TopTenClick extends Event implements Cancellable {
class TopTenClick extends Event implements Cancellable {
private boolean cancelled;
private static final HandlerList handlers = new HandlerList();
@ -43,7 +43,7 @@ public class TopTenClick extends Event implements Cancellable {
return getHandlerList();
}
public static HandlerList getHandlerList() {
private static HandlerList getHandlerList() {
return handlers;
}

View File

@ -15,10 +15,10 @@ import bentobox.addon.level.Level;
*/
public class JoinLeaveListener implements Listener {
private Level addon;
private final Level addon;
/**
* @param addon
* @param addon - addon
*/
public JoinLeaveListener(Level addon) {
this.addon = addon;

View File

@ -20,11 +20,11 @@ import world.bentobox.bentobox.database.objects.Island;
*/
public class NewIslandListener implements Listener {
private Level addon;
private Map<Island, CalcIslandLevel> cil;
private final Level addon;
private final Map<Island, CalcIslandLevel> cil;
/**
* @param addon
* @param addon - addon
*/
public NewIslandListener(Level addon) {
this.addon = addon;

View File

@ -1,9 +1,5 @@
/**
*
*/
package bentobox.addon.level;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -35,12 +31,8 @@ public class LevelPresenterTest {
private BentoBox plugin;
private Level addon;
private IslandsManager im;
private PlayerLevel pl;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
plugin = mock(BentoBox.class);
@ -48,7 +40,7 @@ public class LevelPresenterTest {
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("world");
im = mock(IslandsManager.class);
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
// Has island
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
@ -67,7 +59,7 @@ public class LevelPresenterTest {
*/
@Test
public void testLevelPresenter() {
assertNotNull(new LevelPresenter(addon, plugin));
new LevelPresenter(addon, plugin);
}
/**