Refactoring code

This commit is contained in:
tastybento 2018-08-31 10:59:10 +09:00
parent 4aef325f4d
commit c2320aee3a
5 changed files with 29 additions and 60 deletions

View File

@ -76,11 +76,8 @@ public class LevelPresenter {
private boolean onLevelWaitTime(final User sender) { private boolean onLevelWaitTime(final User sender) {
if (levelWaitTime.containsKey(sender.getUniqueId())) { if (levelWaitTime.containsKey(sender.getUniqueId())) {
if (levelWaitTime.get(sender.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) { return levelWaitTime.get(sender.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis();
return true;
}
return false;
} }
return false; return false;

View File

@ -33,7 +33,6 @@ public class TopTen implements Listener {
// Top ten list of players // Top ten list of players
private Map<World,TopTenData> topTenList; private Map<World,TopTenData> topTenList;
private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25}; private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
private final boolean DEBUG = false;
private Database<TopTenData> handler; private Database<TopTenData> handler;
public TopTen(Level addon) { public TopTen(Level addon) {
@ -104,6 +103,7 @@ public class TopTen implements Listener {
// Check world // Check world
topTenList.putIfAbsent(world, new TopTenData()); topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).setUniqueId(world.getName()); topTenList.get(world).setUniqueId(world.getName());
boolean DEBUG = false;
if (DEBUG) if (DEBUG)
addon.getLogger().info("DEBUG: GUI display"); addon.getLogger().info("DEBUG: GUI display");

View File

@ -31,19 +31,19 @@ public class CalcIslandLevel {
private static final int MAX_CHUNKS = 200; private static final int MAX_CHUNKS = 200;
private static final long SPEED = 1; private static final long SPEED = 1;
private boolean checking = true; private boolean checking;
private BukkitTask task; private final BukkitTask task;
private Level addon; private final Level addon;
private Set<Pair<Integer, Integer>> chunksToScan; private final Set<Pair<Integer, Integer>> chunksToScan;
private Island island; private final Island island;
private World world; private final World world;
private Results result; private final Results result;
private Runnable onExit; private final Runnable onExit;
// Copy the limits hashmap // Copy the limits hash map
HashMap<Material, Integer> limitCount; private final HashMap<Material, Integer> limitCount;
/** /**
@ -56,7 +56,7 @@ public class CalcIslandLevel {
public CalcIslandLevel(final Level addon, final Island island, final Runnable onExit) { public CalcIslandLevel(final Level addon, final Island island, final Runnable onExit) {
this.addon = addon; this.addon = addon;
this.island = island; this.island = island;
this.world = island != null ? island.getCenter().getWorld() : null; this.world = island.getCenter().getWorld();
this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits()); this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits());
this.onExit = onExit; this.onExit = onExit;
@ -112,19 +112,19 @@ public class CalcIslandLevel {
private void scanChunk(ChunkSnapshot chunk) { private void scanChunk(ChunkSnapshot chunk) {
for (int x = 0; x< 16; x++) { for (int x = 0; x< 16; x++) {
// Check if the block coord is inside the protection zone and if not, don't count it // Check if the block coordinate is inside the protection zone and if not, don't count it
if (chunk.getX() * 16 + x < island.getMinProtectedX() || chunk.getX() * 16 + x >= island.getMinProtectedX() + island.getProtectionRange() * 2) { if (chunk.getX() * 16 + x < island.getMinProtectedX() || chunk.getX() * 16 + x >= island.getMinProtectedX() + island.getProtectionRange() * 2) {
continue; continue;
} }
for (int z = 0; z < 16; z++) { for (int z = 0; z < 16; z++) {
// Check if the block coord is inside the protection zone and if not, don't count it // Check if the block coordinate is inside the protection zone and if not, don't count it
if (chunk.getZ() * 16 + z < island.getMinProtectedZ() || chunk.getZ() * 16 + z >= island.getMinProtectedZ() + island.getProtectionRange() * 2) { if (chunk.getZ() * 16 + z < island.getMinProtectedZ() || chunk.getZ() * 16 + z >= island.getMinProtectedZ() + island.getProtectionRange() * 2) {
continue; continue;
} }
for (int y = 0; y < island.getCenter().getWorld().getMaxHeight(); y++) { for (int y = 0; y < island.getCenter().getWorld().getMaxHeight(); y++) {
Material blockData = chunk.getBlockType(x, y, z); 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 // Air is free
if (!blockData.equals(Material.AIR)) { if (!blockData.equals(Material.AIR)) {
checkBlock(blockData, belowSeaLevel); checkBlock(blockData, belowSeaLevel);
@ -147,7 +147,7 @@ public class CalcIslandLevel {
/** /**
* Checks if a block has been limited or not and whether a block has any value or not * Checks if a block has been limited or not and whether a block has any value or not
* @param md * @param md Material
* @return value of the block if can be counted * @return value of the block if can be counted
*/ */
private int limitCount(Material md) { private int limitCount(Material md) {
@ -171,7 +171,7 @@ public class CalcIslandLevel {
/** /**
* Get value of a material * Get value of a material
* World blocks trump regular block values * World blocks trump regular block values
* @param md * @param md Material
* @return value of a material * @return value of a material
*/ */
private int getValue(Material md) { private int getValue(Material md) {
@ -183,8 +183,8 @@ public class CalcIslandLevel {
/** /**
* Get a set of all the chunks in island * Get a set of all the chunks in island
* @param island * @param island - island
* @return * @return - set of all the chunks in the island to scan
*/ */
private Set<Pair<Integer, Integer>> getChunksToScan(Island island) { private Set<Pair<Integer, Integer>> getChunksToScan(Island island) {
Set<Pair<Integer, Integer>> chunkSnapshot = new HashSet<>(); Set<Pair<Integer, Integer>> chunkSnapshot = new HashSet<>();
@ -238,7 +238,6 @@ public class CalcIslandLevel {
reportLines.addAll(sortedReport(total, result.mdCount)); reportLines.addAll(sortedReport(total, result.mdCount));
reportLines.add("Blocks not counted because they exceeded limits: " + String.format("%,d",result.ofCount.size())); reportLines.add("Blocks not counted because they exceeded limits: " + String.format("%,d",result.ofCount.size()));
//entriesSortedByCount = Multisets.copyHighestCountFirst(ofCount).entrySet();
Iterable<Multiset.Entry<Material>> entriesSortedByCount = result.ofCount.entrySet(); Iterable<Multiset.Entry<Material>> entriesSortedByCount = result.ofCount.entrySet();
Iterator<Entry<Material>> it = entriesSortedByCount.iterator(); Iterator<Entry<Material>> it = entriesSortedByCount.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -255,7 +254,6 @@ public class CalcIslandLevel {
reportLines.add("=================================="); reportLines.add("==================================");
reportLines.add("Blocks on island that are not in config.yml"); reportLines.add("Blocks on island that are not in config.yml");
reportLines.add("Total number = " + String.format("%,d",result.ncCount.size())); reportLines.add("Total number = " + String.format("%,d",result.ncCount.size()));
//entriesSortedByCount = Multisets.copyHighestCountFirst(ncCount).entrySet();
entriesSortedByCount = result.ncCount.entrySet(); entriesSortedByCount = result.ncCount.entrySet();
it = entriesSortedByCount.iterator(); it = entriesSortedByCount.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -270,9 +268,7 @@ public class CalcIslandLevel {
private Collection<String> sortedReport(int total, Multiset<Material> MaterialCount) { private Collection<String> sortedReport(int total, Multiset<Material> MaterialCount) {
Collection<String> result = new ArrayList<>(); Collection<String> result = new ArrayList<>();
Iterable<Multiset.Entry<Material>> entriesSortedByCount = Multisets.copyHighestCountFirst(MaterialCount).entrySet(); Iterable<Multiset.Entry<Material>> entriesSortedByCount = Multisets.copyHighestCountFirst(MaterialCount).entrySet();
Iterator<Entry<Material>> it = entriesSortedByCount.iterator(); for (Entry<Material> en : entriesSortedByCount) {
while (it.hasNext()) {
Entry<Material> en = it.next();
Material type = en.getElement(); Material type = en.getElement();
int value = 0; int value = 0;
@ -281,7 +277,7 @@ public class CalcIslandLevel {
value = addon.getSettings().getBlockValues().get(type); value = addon.getSettings().getBlockValues().get(type);
} }
result.add(type.toString() + ":" result.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()); total += (value * en.getCount());
} }
result.add("Subtotal = " + total); result.add("Subtotal = " + total);
@ -302,10 +298,10 @@ public class CalcIslandLevel {
*/ */
public class Results { public class Results {
private List<String> report; private List<String> report;
private Multiset<Material> mdCount = HashMultiset.create(); private final Multiset<Material> mdCount = HashMultiset.create();
private Multiset<Material> uwCount = HashMultiset.create(); private final Multiset<Material> uwCount = HashMultiset.create();
private Multiset<Material> ncCount = HashMultiset.create(); private final Multiset<Material> ncCount = HashMultiset.create();
private Multiset<Material> ofCount = HashMultiset.create(); private final Multiset<Material> ofCount = HashMultiset.create();
private long rawBlockCount = 0; private long rawBlockCount = 0;
private long underWaterBlockCount = 0; private long underWaterBlockCount = 0;
private long level = 0; private long level = 0;
@ -317,12 +313,7 @@ public class CalcIslandLevel {
public int getDeathHandicap() { public int getDeathHandicap() {
return deathHandicap; return deathHandicap;
} }
/**
* @param deathHandicap the deathHandicap to set
*/
public void setDeathHandicap(int deathHandicap) {
this.deathHandicap = deathHandicap;
}
/** /**
* @return the report * @return the report
*/ */
@ -341,24 +332,6 @@ public class CalcIslandLevel {
public long getPointsToNextLevel() { public long getPointsToNextLevel() {
return pointsToNextLevel; 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

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

View File

@ -35,7 +35,6 @@ public class LevelPresenterTest {
private BentoBox plugin; private BentoBox plugin;
private Level addon; private Level addon;
private IslandsManager im;
private PlayerLevel pl; private PlayerLevel pl;
/** /**
@ -48,7 +47,7 @@ public class LevelPresenterTest {
IslandWorldManager iwm = mock(IslandWorldManager.class); IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("world"); when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("world");
im = mock(IslandsManager.class); IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has island // Has island
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);