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) {
if (levelWaitTime.containsKey(sender.getUniqueId())) {
if (levelWaitTime.get(sender.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis()) {
return true;
}
return levelWaitTime.get(sender.getUniqueId()).longValue() > Calendar.getInstance().getTimeInMillis();
return false;
}
return false;

View File

@ -33,7 +33,6 @@ public class TopTen implements Listener {
// 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 final boolean DEBUG = false;
private Database<TopTenData> handler;
public TopTen(Level addon) {
@ -104,6 +103,7 @@ public class TopTen implements Listener {
// Check world
topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).setUniqueId(world.getName());
boolean DEBUG = false;
if (DEBUG)
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 long SPEED = 1;
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;
// Copy the limits hash map
private final HashMap<Material, Integer> limitCount;
/**
@ -56,7 +56,7 @@ public class CalcIslandLevel {
public CalcIslandLevel(final Level addon, final Island island, final Runnable onExit) {
this.addon = addon;
this.island = island;
this.world = island != null ? island.getCenter().getWorld() : null;
this.world = island.getCenter().getWorld();
this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits());
this.onExit = onExit;
@ -112,19 +112,19 @@ public class CalcIslandLevel {
private void scanChunk(ChunkSnapshot chunk) {
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) {
continue;
}
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) {
continue;
}
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);
@ -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
* @param md
* @param md Material
* @return value of the block if can be counted
*/
private int limitCount(Material md) {
@ -171,7 +171,7 @@ public class CalcIslandLevel {
/**
* Get value of a material
* World blocks trump regular block values
* @param md
* @param md Material
* @return value of a material
*/
private int getValue(Material md) {
@ -183,8 +183,8 @@ public class CalcIslandLevel {
/**
* Get a set of all the chunks in island
* @param island
* @return
* @param island - island
* @return - set of all the chunks in the island to scan
*/
private Set<Pair<Integer, Integer>> getChunksToScan(Island island) {
Set<Pair<Integer, Integer>> chunkSnapshot = new HashSet<>();
@ -238,7 +238,6 @@ public class CalcIslandLevel {
reportLines.addAll(sortedReport(total, result.mdCount));
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();
Iterator<Entry<Material>> it = entriesSortedByCount.iterator();
while (it.hasNext()) {
@ -255,7 +254,6 @@ public class CalcIslandLevel {
reportLines.add("==================================");
reportLines.add("Blocks on island that are not in config.yml");
reportLines.add("Total number = " + String.format("%,d",result.ncCount.size()));
//entriesSortedByCount = Multisets.copyHighestCountFirst(ncCount).entrySet();
entriesSortedByCount = result.ncCount.entrySet();
it = entriesSortedByCount.iterator();
while (it.hasNext()) {
@ -270,9 +268,7 @@ public class CalcIslandLevel {
private Collection<String> sortedReport(int total, Multiset<Material> MaterialCount) {
Collection<String> result = 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;
@ -281,7 +277,7 @@ public class CalcIslandLevel {
value = addon.getSettings().getBlockValues().get(type);
}
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());
}
result.add("Subtotal = " + total);
@ -302,10 +298,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;
@ -317,12 +313,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
*/
@ -341,24 +332,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

@ -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

@ -35,7 +35,6 @@ public class LevelPresenterTest {
private BentoBox plugin;
private Level addon;
private IslandsManager im;
private PlayerLevel pl;
/**
@ -48,7 +47,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);