Removed code smells

This commit is contained in:
tastybento 2018-08-25 21:15:45 -07:00
parent 4aef325f4d
commit 85c28f8a70
8 changed files with 62 additions and 117 deletions

View File

@ -14,7 +14,6 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import bentobox.addon.level.database.object.LevelsData;
import bentobox.addon.level.database.object.TopTenData; import bentobox.addon.level.database.object.TopTenData;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
@ -33,7 +32,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) {
@ -61,37 +59,13 @@ public class TopTen implements Listener {
// Try and see if the player is online // Try and see if the player is online
Player player = addon.getServer().getPlayer(ownerUUID); Player player = addon.getServer().getPlayer(ownerUUID);
if (player != null) { if (player != null && !player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(world) + ".intopten")) {
// Online
if (!player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(world) + ".intopten")) {
topTenList.get(world).remove(ownerUUID); topTenList.get(world).remove(ownerUUID);
return; return;
} }
}
topTenList.get(world).addLevel(ownerUUID, l); topTenList.get(world).addLevel(ownerUUID, l);
} }
/**
* Creates the top ten list from scratch. Does not get the level of each island. Just
* takes the level from the player's file.
* Runs asynchronously from the main thread.
*/
public void create(String permPrefix) {
// Obtain all the levels for each known player
Database<LevelsData> levelHandler = addon.getHandler();
long index = 0;
for (LevelsData lv : levelHandler.loadObjects()) {
if (index++ % 1000 == 0) {
addon.getLogger().info("Processed " + index + " players for top ten");
}
// Convert to UUID
UUID playerUUID = UUID.fromString(lv.getUniqueId());
// Get the world
lv.getLevels().forEach((k,v) -> addEntry(Bukkit.getWorld(k), playerUUID, v));
}
saveTopTen();
}
/** /**
* Displays the Top Ten list * Displays the Top Ten list
* @param world * @param world
@ -104,8 +78,6 @@ 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());
if (DEBUG)
addon.getLogger().info("DEBUG: GUI display");
PanelBuilder panel = new PanelBuilder() PanelBuilder panel = new PanelBuilder()
.name(user.getTranslation("island.top.gui-title")) .name(user.getTranslation("island.top.gui-title"))
@ -116,22 +88,14 @@ public class TopTen implements Listener {
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<UUID, Long> m = it.next(); Map.Entry<UUID, Long> m = it.next();
UUID topTenUUID = m.getKey(); UUID topTenUUID = m.getKey();
if (DEBUG)
addon.getLogger().info("DEBUG: " + i + ": " + topTenUUID);
// Remove from TopTen if the player is online and has the permission // Remove from TopTen if the player is online and has the permission
Player entry = addon.getServer().getPlayer(topTenUUID); Player entry = addon.getServer().getPlayer(topTenUUID);
boolean show = true; boolean show = true;
if (entry != null) { if (entry != null) {
if (DEBUG)
addon.getLogger().info("DEBUG: removing from topten");
if (!entry.hasPermission(permPrefix + "intopten")) { if (!entry.hasPermission(permPrefix + "intopten")) {
it.remove(); it.remove();
show = false; show = false;
} }
} else {
if (DEBUG)
addon.getLogger().info("DEBUG: player not online, so no per check");
} }
if (show) { if (show) {
panel.item(SLOTS[i-1], getHead(i, m.getValue(), topTenUUID, user, world)); panel.item(SLOTS[i-1], getHead(i, m.getValue(), topTenUUID, user, world));
@ -168,21 +132,6 @@ public class TopTen implements Listener {
.icon(name) .icon(name)
.name(name) .name(name)
.description(description); .description(description);
// If welcome warps is present then add warping
/*
addon.getAddonByName("BSkyBlock-WelcomeWarps").ifPresent(warp -> {
if (((Warp)warp).getWarpSignsManager().hasWarp(world, playerUUID)) {
builder.clickHandler((panel, user, click, slot) -> {
if (click.equals(ClickType.LEFT)) {
user.sendMessage("island.top.warp-to", "[name]", name);
((Warp)warp).getWarpSignsManager().warpPlayer(world, user, playerUUID);
}
return true;
});
}
});*/
return builder.build(); return builder.build();
} }

View File

@ -17,11 +17,9 @@ import org.bukkit.scheduler.BukkitTask;
import com.google.common.collect.HashMultiset; import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset; import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry; import com.google.common.collect.Multiset.Entry;
import bentobox.addon.level.Level;
import com.google.common.collect.Multisets; import com.google.common.collect.Multisets;
import bentobox.addon.level.Level;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Pair; import world.bentobox.bentobox.util.Pair;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@ -31,6 +29,7 @@ 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 static final String LINE_BREAK = "==================================";
private boolean checking = true; private boolean checking = true;
private BukkitTask task; private BukkitTask task;
@ -56,7 +55,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;
@ -226,7 +225,7 @@ public class CalcIslandLevel {
reportLines.add("Level cost = " + addon.getSettings().getLevelCost()); reportLines.add("Level cost = " + addon.getSettings().getLevelCost());
reportLines.add("Deaths handicap = " + result.deathHandicap); reportLines.add("Deaths handicap = " + result.deathHandicap);
reportLines.add("Level calculated = " + result.level); reportLines.add("Level calculated = " + result.level);
reportLines.add("=================================="); reportLines.add(LINE_BREAK);
int total = 0; int total = 0;
if (!result.uwCount.isEmpty()) { if (!result.uwCount.isEmpty()) {
reportLines.add("Underwater block count (Multiplier = x" + addon.getSettings().getUnderWaterMultiplier() + ") value"); reportLines.add("Underwater block count (Multiplier = x" + addon.getSettings().getUnderWaterMultiplier() + ") value");
@ -238,7 +237,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()) {
@ -252,23 +250,22 @@ public class CalcIslandLevel {
} }
reportLines.add(type.getElement().toString() + ": " + String.format("%,d",type.getCount()) + " blocks (max " + limit + explain); reportLines.add(type.getElement().toString() + ": " + String.format("%,d",type.getCount()) + " blocks (max " + limit + explain);
} }
reportLines.add("=================================="); reportLines.add(LINE_BREAK);
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()) {
Entry<Material> type = it.next(); Entry<Material> type = it.next();
reportLines.add(type.getElement().toString() + ": " + String.format("%,d",type.getCount()) + " blocks"); reportLines.add(type.getElement().toString() + ": " + String.format("%,d",type.getCount()) + " blocks");
} }
reportLines.add("================================="); reportLines.add(LINE_BREAK);
return reportLines; return reportLines;
} }
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> r = 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(); Iterator<Entry<Material>> it = entriesSortedByCount.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -280,13 +277,13 @@ public class CalcIslandLevel {
// Specific // Specific
value = addon.getSettings().getBlockValues().get(type); value = addon.getSettings().getBlockValues().get(type);
} }
result.add(type.toString() + ":" 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()); total += (value * en.getCount());
} }
result.add("Subtotal = " + total); r.add("Subtotal = " + total);
result.add("=================================="); r.add(LINE_BREAK);
return result; return r;
} }
/** /**

View File

@ -44,20 +44,26 @@ public class PlayerLevel {
addon.getServer().getPluginManager().callEvent(e); addon.getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) { if (!e.isCancelled()) {
// Calculate if not cancelled // Calculate if not cancelled
calc = new CalcIslandLevel(addon, island, ()-> informPlayers()); calc = new CalcIslandLevel(addon, island, ()-> fireIslandLevelCalcEvent());
} }
} }
private void informPlayers() { private void fireIslandLevelCalcEvent() {
// Fire post calculation event // Fire post calculation event
IslandLevelCalculatedEvent ilce = new IslandLevelCalculatedEvent(targetPlayer, island, calc.getResult()); IslandLevelCalculatedEvent ilce = new IslandLevelCalculatedEvent(targetPlayer, island, calc.getResult());
addon.getServer().getPluginManager().callEvent(ilce); addon.getServer().getPluginManager().callEvent(ilce);
Results results = ilce.getResults(); Results results = ilce.getResults();
// Save the results // Save the results
island.getMemberSet().forEach(m -> addon.setIslandLevel(world, m, results.getLevel())); island.getMemberSet().forEach(m -> addon.setIslandLevel(world, m, results.getLevel()));
// Display result // Display result if event is not cancelled
if (!ilce.isCancelled()) { if (!ilce.isCancelled()) {
informPlayers(results);
}
}
private void informPlayers(Results results) {
// Tell the asker // Tell the asker
asker.sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer))); asker.sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer)));
// Console // Console
@ -75,13 +81,11 @@ public class PlayerLevel {
} }
// Tell other team members // Tell other team members
if (addon.getIslandLevel(world, targetPlayer) != oldLevel) { if (addon.getIslandLevel(world, targetPlayer) != oldLevel) {
for (UUID member : island.getMemberSet()) { island.getMemberSet().stream()
if (!member.equals(asker.getUniqueId())) { .filter(u -> !u.equals(asker.getUniqueId()))
User.getInstance(member).sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer))); .forEach(m -> User.getInstance(m).sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer))));
}
}
}
} }
} }

View File

@ -22,7 +22,6 @@ public class AdminLevel extends CompositeCommand {
// Asking for another player's level? // Asking for another player's level?
// Convert name to a UUID // Convert name to a UUID
final UUID playerUUID = getPlugin().getPlayers().getUUID(args.get(0)); final UUID playerUUID = getPlugin().getPlayers().getUUID(args.get(0));
//getLogger().info("DEBUG: console player info UUID = " + playerUUID);
if (playerUUID == null) { if (playerUUID == null) {
user.sendMessage("general.errors.unknown-player"); user.sendMessage("general.errors.unknown-player");
return true; return true;

View File

@ -22,7 +22,6 @@ public class IslandLevel extends CompositeCommand {
// Asking for another player's level? // Asking for another player's level?
// Convert name to a UUID // Convert name to a UUID
final UUID playerUUID = getPlugin().getPlayers().getUUID(args.get(0)); final UUID playerUUID = getPlugin().getPlayers().getUUID(args.get(0));
//getLogger().info("DEBUG: console player info UUID = " + playerUUID);
if (playerUUID == null) { if (playerUUID == null) {
user.sendMessage("general.errors.unknown-player"); user.sendMessage("general.errors.unknown-player");
return true; return true;

View File

@ -47,30 +47,29 @@ public class Settings {
} }
if (level.getConfig().isSet("limits")) { if (level.getConfig().isSet("limits")) {
HashMap<Material, Integer> blockLimits = new HashMap<>(); HashMap<Material, Integer> bl = new HashMap<>();
for (String material : level.getConfig().getConfigurationSection("limits").getKeys(false)) { for (String material : level.getConfig().getConfigurationSection("limits").getKeys(false)) {
try { try {
Material mat = Material.valueOf(material); Material mat = Material.valueOf(material);
blockLimits.put(mat, level.getConfig().getInt("limits." + material, 0)); bl.put(mat, level.getConfig().getInt("limits." + material, 0));
} catch (Exception e) { } catch (Exception e) {
level.getLogger().warning("Unknown material (" + material + ") in blockvalues.yml Limits section. Skipping..."); level.getLogger().warning(() -> "Unknown material (" + material + ") in blockvalues.yml Limits section. Skipping...");
} }
} }
setBlockLimits(blockLimits); setBlockLimits(bl);
} }
if (level.getConfig().isSet("blocks")) { if (level.getConfig().isSet("blocks")) {
Map<Material, Integer> blockValues = new HashMap<>(); Map<Material, Integer> bv = new HashMap<>();
for (String material : level.getConfig().getConfigurationSection("blocks").getKeys(false)) { for (String material : level.getConfig().getConfigurationSection("blocks").getKeys(false)) {
try { try {
Material mat = Material.valueOf(material); Material mat = Material.valueOf(material);
blockValues.put(mat, level.getConfig().getInt("blocks." + material, 0)); bv.put(mat, level.getConfig().getInt("blocks." + material, 0));
} catch (Exception e) { } catch (Exception e) {
// e.printStackTrace(); level.getLogger().warning(()-> "Unknown material (" + material + ") in config.yml blocks section. Skipping...");
level.getLogger().warning("Unknown material (" + material + ") in config.yml blocks section. Skipping...");
} }
} }
setBlockValues(blockValues); setBlockValues(bv);
} else { } else {
level.getLogger().severe("No block values in config.yml! All island levels will be zero!"); level.getLogger().severe("No block values in config.yml! All island levels will be zero!");
} }
@ -88,7 +87,7 @@ public class Settings {
worldBlockValues.put(bWorld, values); worldBlockValues.put(bWorld, values);
} }
} else { } else {
level.getLogger().severe("Level Addon: No such world : " + world); level.getLogger().severe(() -> "Level Addon: No such world : " + world);
} }
} }
} }

View File

@ -23,8 +23,6 @@ public class TopTenData implements DataObject {
@Expose @Expose
private Map<UUID, Long> topTen = new LinkedHashMap<>(); private Map<UUID, Long> topTen = new LinkedHashMap<>();
public TopTenData() {}
public Map<UUID, Long> getTopTen() { public Map<UUID, Long> getTopTen() {
return topTen.entrySet().stream() return topTen.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(10) .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(10)

View File

@ -40,7 +40,7 @@ public class TopTenClick extends Event implements Cancellable {
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return getHandlerList();
} }
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {