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.
## Permissons
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 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:
@ -61,7 +61,7 @@ This section defines a number of overall settings for the add-on.
* Underwater block multiplier - default value = 1. If this value is > 1 then blocks below sea level will have a greater value.
* Level cost - Value of one island level. Default 100. Minimum value is 1.
* Level wait - Cooldown between level requests in seconds
* Level wait - Cool down between level requests in seconds
* Death penalty - How many block values a player will lose per death. Default value of 100 means that for every death, the player will lose 1 level (if levelcost is 100)
* Sum Team Deaths - if true, all the team member deaths are summed. If false, only the leader's deaths counts.
* Max deaths - If player dies more than this, it doesn't count anymore.

View File

@ -29,11 +29,11 @@ import world.bentobox.level.objects.TopTenData;
*
*/
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;

View File

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

View File

@ -5,6 +5,6 @@ public interface IslandLevelCalculator {
/**
* @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());
user.sendMessage("island.value.success", "[value]", value + "");
if (plugin.getConfig().get("underwater") != null) {
Double underWater = plugin.getConfig().getDouble("underwater");
double underWater = plugin.getConfig().getDouble("underwater");
if (underWater > 1.0) {
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.List;
import java.util.Map;
import java.util.Objects;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -13,7 +14,7 @@ import world.bentobox.level.Level;
public class Settings {
private Level level;
private final Level level;
private boolean sumTeamDeaths;
private Map<Material, Integer> blockLimits = new HashMap<>();
private Map<Material, Integer> blockValues = new HashMap<>();
@ -48,7 +49,7 @@ public class Settings {
if (level.getConfig().isSet("limits")) {
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 {
Material mat = Material.valueOf(material);
bl.put(mat, level.getConfig().getInt("limits." + material, 0));
@ -60,7 +61,7 @@ public class Settings {
}
if (level.getConfig().isSet("blocks")) {
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 {
Material mat = Material.valueOf(material);
@ -76,11 +77,11 @@ public class Settings {
// Worlds
if (level.getConfig().isSet("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);
if (bWorld != null) {
ConfigurationSection worldValues = worlds.getConfigurationSection(world);
for (String material : worldValues.getKeys(false)) {
for (String material : Objects.requireNonNull(worldValues).getKeys(false)) {
Material mat = Material.valueOf(material);
Map<Material, Integer> values = worldBlockValues.getOrDefault(bWorld, new HashMap<>());
values.put(mat, worldValues.getInt(material));

View File

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

View File

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

View File

@ -20,7 +20,7 @@ public class TopTenRequestHandler extends AddonRequestHandler {
/**
* The level addon field.
*/
private Level addon;
private final Level addon;
/**
* 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
public Object handle(Map<String, Object> map) {

View File

@ -11,6 +11,9 @@ admin:
description: "show the top ten list"
unknown-world: "&cUnknown world!"
display: "&f[rank]. &a[name] &7- &b[level]"
remove:
description: "remove player from Top Ten"
parameters: "<player>"
island:
level:
@ -28,9 +31,6 @@ island:
gui-heading: "&6[name]: &B[rank]"
island-level: "&BLevel [level]"
warp-to: "&AWarping to [name]'s island"
remove:
description: "remove player from Top Ten"
parameters: "<player>"
value:
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.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
@ -59,7 +58,6 @@ public class TopTenTest {
private BentoBox plugin;
@Mock
private static AbstractDatabaseHandler<Object> handler;
private List<Object> topTen;
@Mock
private IslandsManager im;
@Mock
@ -106,7 +104,7 @@ public class TopTenTest {
// Fill the top ten
TopTenData ttd = new TopTenData();
ttd.setUniqueId("world");
topTen = new ArrayList<>();
List<Object> topTen = new ArrayList<>();
for (long i = -100; i < 100; i ++) {
ttd.addLevel(UUID.randomUUID(), i);
topTen.add(ttd);
@ -184,7 +182,7 @@ public class TopTenTest {
TopTen tt = new TopTen(addon);
UUID ownerUUID = UUID.randomUUID();
tt.addEntry(world, ownerUUID, 200L);
assertTrue(tt.getTopTenList(world).getTopTen().get(ownerUUID) == 200L);
assertEquals(200L, (long) tt.getTopTenList(world).getTopTen().get(ownerUUID));
}
@Test
@ -264,7 +262,7 @@ public class TopTenTest {
TopTen tt = new TopTen(addon);
UUID ownerUUID = UUID.randomUUID();
tt.addEntry(world, ownerUUID, 200L);
assertTrue(tt.getTopTenList(world).getTopTen().get(ownerUUID) == 200L);
assertEquals(200L, (long) tt.getTopTenList(world).getTopTen().get(ownerUUID));
// Remove it
tt.removeEntry(world, ownerUUID);
assertNull(tt.getTopTenList(world).getTopTen().get(ownerUUID));

View File

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

View File

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