Fixed bugs and removed code smells.

This commit is contained in:
tastybento 2019-11-16 19:19:03 -08:00
parent 05437ca14a
commit 255711afa4
13 changed files with 55 additions and 77 deletions

View File

@ -16,8 +16,8 @@ for game modes listed in the config.yml.
1. Read the release notes carefully, but you may have to delete the old config.yml to use a new one. 1. Read the release notes carefully, but you may have to delete the old config.yml to use a new one.
## Permissons ## Permissions
Permissions are given automatically to players as listed below for BSkyBlock, AcidIsland and CaveBlock. If your permissions plugin strips permissions then you may have to allocate these manually. Note that if a player doesn't have the intopten permission, they will not be listed in the top ten. Permissions are given automatically to players as listed below for BSkyBlock, AcidIsland and CaveBlock. If your permissions plugin strips permissions then you may have to allocate these manually. Note that if a player doesn't have the `intopten` permission, they will not be listed in the top ten.
``` ```
permissions: permissions:

View File

@ -29,11 +29,11 @@ import world.bentobox.level.objects.TopTenData;
* *
*/ */
public class TopTen implements Listener { public class TopTen implements Listener {
private Level addon; private final Level addon;
// 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 Database<TopTenData> handler; private final Database<TopTenData> handler;
public TopTen(Level addon) { public TopTen(Level addon) {
this.addon = addon; this.addon = addon;

View File

@ -46,7 +46,6 @@ public class CalcIslandLevel {
// Copy the limits hash map // Copy the limits hash map
private final HashMap<Material, Integer> limitCount; private final HashMap<Material, Integer> limitCount;
private final World world; private final World world;
private final List<World> worlds;
private int count; private int count;
@ -62,16 +61,6 @@ public class CalcIslandLevel {
this.addon = addon; this.addon = addon;
this.island = island; this.island = island;
this.world = island.getWorld(); this.world = island.getWorld();
this.worlds = new ArrayList<>();
this.worlds.add(world);
if (addon.getSettings().isNether()) {
World netherWorld = addon.getPlugin().getIWM().getNetherWorld(world);
if (netherWorld != null) this.worlds.add(netherWorld);
}
if (addon.getSettings().isEnd()) {
World endWorld = addon.getPlugin().getIWM().getEndWorld(world);
if (endWorld != null) this.worlds.add(endWorld);
}
this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits()); this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits());
this.onExit = onExit; this.onExit = onExit;
@ -84,8 +73,7 @@ public class CalcIslandLevel {
// Get chunks to scan // Get chunks to scan
chunksToScan = getChunksToScan(island); chunksToScan = getChunksToScan(island);
count = 0; count = 0;
chunksToScan.forEach(c -> { chunksToScan.forEach(c -> Util.getChunkAtAsync(world, c.x, c.z).thenAccept(ch -> {
Util.getChunkAtAsync(world, c.x, c.z).thenAccept(ch -> {
ChunkSnapshot snapShot = ch.getChunkSnapshot(); ChunkSnapshot snapShot = ch.getChunkSnapshot();
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> { Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
this.scanChunk(snapShot); this.scanChunk(snapShot);
@ -94,8 +82,7 @@ public class CalcIslandLevel {
this.tidyUp(); this.tidyUp();
} }
}); });
}); }));
});
} }
@ -459,11 +446,22 @@ public class CalcIslandLevel {
while (ch >= 'a' && ch <= 'z') nextChar(); while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos); String func = str.substring(startPos, this.pos);
x = parseFactor(); x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x); switch (func) {
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x)); case "sqrt":
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x)); x = Math.sqrt(x);
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x)); break;
else throw new RuntimeException("Unknown function: " + func); case "sin":
x = Math.sin(Math.toRadians(x));
break;
case "cos":
x = Math.cos(Math.toRadians(x));
break;
case "tan":
x = Math.tan(Math.toRadians(x));
break;
default:
throw new RuntimeException("Unknown function: " + func);
}
} else { } else {
throw new RuntimeException("Unexpected: " + (char)ch); throw new RuntimeException("Unexpected: " + (char)ch);
} }

View File

@ -5,6 +5,6 @@ public interface IslandLevelCalculator {
/** /**
* @return the results of the island calculation * @return the results of the island calculation
*/ */
public Results getResult(); Results getResult();
} }

View File

@ -34,7 +34,7 @@ public class IslandValueCommand extends CompositeCommand {
int value = plugin.getConfig().getInt("blocks." + material.toString()); int value = plugin.getConfig().getInt("blocks." + material.toString());
user.sendMessage("island.value.success", "[value]", value + ""); user.sendMessage("island.value.success", "[value]", value + "");
if (plugin.getConfig().get("underwater") != null) { if (plugin.getConfig().get("underwater") != null) {
Double underWater = plugin.getConfig().getDouble("underwater"); double underWater = plugin.getConfig().getDouble("underwater");
if (underWater > 1.0) { if (underWater > 1.0) {
user.sendMessage("island.value.success-underwater", "[value]", (underWater * value) + ""); user.sendMessage("island.value.success-underwater", "[value]", (underWater * value) + "");
} }

View File

@ -3,6 +3,7 @@ package world.bentobox.level.config;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -13,7 +14,7 @@ import world.bentobox.level.Level;
public class Settings { public class Settings {
private Level level; private final Level level;
private boolean sumTeamDeaths; private boolean sumTeamDeaths;
private Map<Material, Integer> blockLimits = new HashMap<>(); private Map<Material, Integer> blockLimits = new HashMap<>();
private Map<Material, Integer> blockValues = new HashMap<>(); private Map<Material, Integer> blockValues = new HashMap<>();
@ -48,7 +49,7 @@ public class Settings {
if (level.getConfig().isSet("limits")) { if (level.getConfig().isSet("limits")) {
HashMap<Material, Integer> bl = new HashMap<>(); HashMap<Material, Integer> bl = new HashMap<>();
for (String material : level.getConfig().getConfigurationSection("limits").getKeys(false)) { for (String material : Objects.requireNonNull(level.getConfig().getConfigurationSection("limits")).getKeys(false)) {
try { try {
Material mat = Material.valueOf(material); Material mat = Material.valueOf(material);
bl.put(mat, level.getConfig().getInt("limits." + material, 0)); bl.put(mat, level.getConfig().getInt("limits." + material, 0));
@ -60,7 +61,7 @@ public class Settings {
} }
if (level.getConfig().isSet("blocks")) { if (level.getConfig().isSet("blocks")) {
Map<Material, Integer> bv = new HashMap<>(); Map<Material, Integer> bv = new HashMap<>();
for (String material : level.getConfig().getConfigurationSection("blocks").getKeys(false)) { for (String material : Objects.requireNonNull(level.getConfig().getConfigurationSection("blocks")).getKeys(false)) {
try { try {
Material mat = Material.valueOf(material); Material mat = Material.valueOf(material);
@ -76,11 +77,11 @@ public class Settings {
// Worlds // Worlds
if (level.getConfig().isSet("worlds")) { if (level.getConfig().isSet("worlds")) {
ConfigurationSection worlds = level.getConfig().getConfigurationSection("worlds"); ConfigurationSection worlds = level.getConfig().getConfigurationSection("worlds");
for (String world : worlds.getKeys(false)) { for (String world : Objects.requireNonNull(worlds).getKeys(false)) {
World bWorld = Bukkit.getWorld(world); World bWorld = Bukkit.getWorld(world);
if (bWorld != null) { if (bWorld != null) {
ConfigurationSection worldValues = worlds.getConfigurationSection(world); ConfigurationSection worldValues = worlds.getConfigurationSection(world);
for (String material : worldValues.getKeys(false)) { for (String material : Objects.requireNonNull(worldValues).getKeys(false)) {
Material mat = Material.valueOf(material); Material mat = Material.valueOf(material);
Map<Material, Integer> values = worldBlockValues.getOrDefault(bWorld, new HashMap<>()); Map<Material, Integer> values = worldBlockValues.getOrDefault(bWorld, new HashMap<>());
values.put(mat, worldValues.getInt(material)); values.put(mat, worldValues.getInt(material));

View File

@ -11,8 +11,8 @@ import world.bentobox.level.Level;
*/ */
public class LevelPlaceholder implements PlaceholderReplacer { public class LevelPlaceholder implements PlaceholderReplacer {
private Level addon; private final Level addon;
private GameModeAddon gm; private final GameModeAddon gm;
/** /**
* Provides placeholder support * Provides placeholder support

View File

@ -10,7 +10,7 @@ import world.bentobox.level.Level;
public class LevelRequestHandler extends AddonRequestHandler { public class LevelRequestHandler extends AddonRequestHandler {
private Level addon; private final Level addon;
public LevelRequestHandler(Level addon) { public LevelRequestHandler(Level addon) {
super("island-level"); super("island-level");

View File

@ -20,7 +20,7 @@ public class TopTenRequestHandler extends AddonRequestHandler {
/** /**
* The level addon field. * The level addon field.
*/ */
private Level addon; private final Level addon;
/** /**
* This constructor creates a new TopTenRequestHandler instance. * This constructor creates a new TopTenRequestHandler instance.
@ -33,7 +33,7 @@ public class TopTenRequestHandler extends AddonRequestHandler {
} }
/** /**
* @see {@link AddonRequestHandler#handle(Map)} * See {@link AddonRequestHandler#handle(Map)}
*/ */
@Override @Override
public Object handle(Map<String, Object> map) { public Object handle(Map<String, Object> map) {

View File

@ -11,6 +11,9 @@ admin:
description: "show the top ten list" description: "show the top ten list"
unknown-world: "&cUnknown world!" unknown-world: "&cUnknown world!"
display: "&f[rank]. &a[name] &7- &b[level]" display: "&f[rank]. &a[name] &7- &b[level]"
remove:
description: "remove player from Top Ten"
parameters: "<player>"
island: island:
level: level:
@ -28,9 +31,6 @@ island:
gui-heading: "&6[name]: &B[rank]" gui-heading: "&6[name]: &B[rank]"
island-level: "&BLevel [level]" island-level: "&BLevel [level]"
warp-to: "&AWarping to [name]'s island" warp-to: "&AWarping to [name]'s island"
remove:
description: "remove player from Top Ten"
parameters: "<player>"
value: value:
description: "shows the value of any block" description: "shows the value of any block"

View File

@ -2,7 +2,6 @@ package world.bentobox.level;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
@ -59,7 +58,6 @@ public class TopTenTest {
private BentoBox plugin; private BentoBox plugin;
@Mock @Mock
private static AbstractDatabaseHandler<Object> handler; private static AbstractDatabaseHandler<Object> handler;
private List<Object> topTen;
@Mock @Mock
private IslandsManager im; private IslandsManager im;
@Mock @Mock
@ -106,7 +104,7 @@ public class TopTenTest {
// Fill the top ten // Fill the top ten
TopTenData ttd = new TopTenData(); TopTenData ttd = new TopTenData();
ttd.setUniqueId("world"); ttd.setUniqueId("world");
topTen = new ArrayList<>(); List<Object> topTen = new ArrayList<>();
for (long i = -100; i < 100; i ++) { for (long i = -100; i < 100; i ++) {
ttd.addLevel(UUID.randomUUID(), i); ttd.addLevel(UUID.randomUUID(), i);
topTen.add(ttd); topTen.add(ttd);
@ -184,7 +182,7 @@ public class TopTenTest {
TopTen tt = new TopTen(addon); TopTen tt = new TopTen(addon);
UUID ownerUUID = UUID.randomUUID(); UUID ownerUUID = UUID.randomUUID();
tt.addEntry(world, ownerUUID, 200L); tt.addEntry(world, ownerUUID, 200L);
assertTrue(tt.getTopTenList(world).getTopTen().get(ownerUUID) == 200L); assertEquals(200L, (long) tt.getTopTenList(world).getTopTen().get(ownerUUID));
} }
@Test @Test
@ -264,7 +262,7 @@ public class TopTenTest {
TopTen tt = new TopTen(addon); TopTen tt = new TopTen(addon);
UUID ownerUUID = UUID.randomUUID(); UUID ownerUUID = UUID.randomUUID();
tt.addEntry(world, ownerUUID, 200L); tt.addEntry(world, ownerUUID, 200L);
assertTrue(tt.getTopTenList(world).getTopTen().get(ownerUUID) == 200L); assertEquals(200L, (long) tt.getTopTenList(world).getTopTen().get(ownerUUID));
// Remove it // Remove it
tt.removeEntry(world, ownerUUID); tt.removeEntry(world, ownerUUID);
assertNull(tt.getTopTenList(world).getTopTen().get(ownerUUID)); assertNull(tt.getTopTenList(world).getTopTen().get(ownerUUID));

View File

@ -77,11 +77,8 @@ public class AdminTopRemoveCommandTest {
@Mock @Mock
private TopTenData ttd; private TopTenData ttd;
/**
* @throws java.lang.Exception
*/
@Before @Before
public void setUp() throws Exception { public void setUp() {
// Set up plugin // Set up plugin
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@ -118,11 +115,8 @@ public class AdminTopRemoveCommandTest {
atrc = new AdminTopRemoveCommand(addon, ic); atrc = new AdminTopRemoveCommand(addon, ic);
} }
/**
* @throws java.lang.Exception
*/
@After @After
public void tearDown() throws Exception { public void tearDown() {
User.clearUsers(); User.clearUsers();
} }

View File

@ -7,7 +7,6 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -17,15 +16,12 @@ import org.junit.Test;
*/ */
public class TopTenDataTest { public class TopTenDataTest {
private Map<UUID, Long> topTen = new LinkedHashMap<>(); private final Map<UUID, Long> topTen = new LinkedHashMap<>();
private TopTenData ttd; private TopTenData ttd;
private UUID uuid = UUID.randomUUID(); private final UUID uuid = UUID.randomUUID();
/**
* @throws java.lang.Exception
*/
@Before @Before
public void setUp() throws Exception { public void setUp() {
// Create a top ten map // Create a top ten map
for (long i = 0; i < 100; i++) { for (long i = 0; i < 100; i++) {
topTen.put(UUID.randomUUID(), i); topTen.put(UUID.randomUUID(), i);
@ -39,13 +35,6 @@ public class TopTenDataTest {
ttd = new TopTenData(); ttd = new TopTenData();
} }
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/** /**
* Test method for {@link world.bentobox.level.objects.TopTenData#getTopTen()}. * Test method for {@link world.bentobox.level.objects.TopTenData#getTopTen()}.
*/ */
@ -93,9 +82,7 @@ public class TopTenDataTest {
@Test @Test
public void testAddAndGetLevel() { public void testAddAndGetLevel() {
topTen.forEach(ttd::addLevel); topTen.forEach(ttd::addLevel);
topTen.keySet().forEach(k -> { topTen.keySet().forEach(k -> assertEquals((long) topTen.get(k), ttd.getLevel(k)));
assertTrue(topTen.get(k) == ttd.getLevel(k));
});
} }
/** /**