Skip to content

Commit

Permalink
Fixed bugs and removed code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 17, 2019
1 parent 05437ca commit 255711a
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 77 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/level/TopTen.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
*
*/
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;
Expand Down
52 changes: 25 additions & 27 deletions src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -62,16 +61,6 @@ public CalcIslandLevel(final Level addon, final Island island, final Runnable on
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;

Expand All @@ -84,18 +73,16 @@ public CalcIslandLevel(final Level addon, final Island island, final Runnable on
// 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();
}
});
});
}));

}

Expand Down Expand Up @@ -459,11 +446,22 @@ boolean eat(int charToEat) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public interface IslandLevelCalculator {
/**
* @return the results of the island calculation
*/
public Results getResult();
Results getResult();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public boolean execute(User user, String label, List<String> args) {
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) + "");
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/world/bentobox/level/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand All @@ -13,7 +14,7 @@

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<>();
Expand Down Expand Up @@ -48,7 +49,7 @@ public Settings(Level level) {

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));
Expand All @@ -60,7 +61,7 @@ public Settings(Level level) {
}
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);
Expand All @@ -76,11 +77,11 @@ public Settings(Level level) {
// 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*/
public class LevelPlaceholder implements PlaceholderReplacer {

private Level addon;
private GameModeAddon gm;
private final Level addon;
private final GameModeAddon gm;

/**
* Provides placeholder support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class LevelRequestHandler extends AddonRequestHandler {

private Level addon;
private final Level addon;

public LevelRequestHandler(Level addon) {
super("island-level");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +33,7 @@ public TopTenRequestHandler(Level addon) {
}

/**
* @see {@link AddonRequestHandler#handle(Map)}
* See {@link AddonRequestHandler#handle(Map)}
*/
@Override
public Object handle(Map<String, Object> map) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/world/bentobox/level/TopTenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -106,7 +104,7 @@ public void setUp() throws Exception {
// 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);
Expand Down Expand Up @@ -184,7 +182,7 @@ public void testAddEntryIsOwner() throws Exception {
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
Expand Down Expand Up @@ -264,7 +262,7 @@ public void testRemoveEntry() throws Exception {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -118,11 +115,8 @@ public void setUp() throws Exception {
atrc = new AdminTopRemoveCommand(addon, ic);
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
public void tearDown() {
User.clearUsers();
}

Expand Down
21 changes: 4 additions & 17 deletions src/test/java/world/bentobox/level/objects/TopTenDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Map;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -17,15 +16,12 @@
*/
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);
Expand All @@ -39,13 +35,6 @@ public void setUp() throws Exception {
ttd = new TopTenData();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* Test method for {@link world.bentobox.level.objects.TopTenData#getTopTen()}.
*/
Expand Down Expand Up @@ -93,9 +82,7 @@ public void testSetUniqueId() {
@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)));
}

/**
Expand Down

0 comments on commit 255711a

Please sign in to comment.