Skip to content

Commit

Permalink
Adds shorthand level presentation for large level values.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Aug 9, 2019
1 parent d49859b commit 39de2b9
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 31 deletions.
15 changes: 11 additions & 4 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Level extends Addon {
private TopTen topTen;

// Level calculator
private LevelPresenter levelCalc;
private LevelPresenter levelPresenter;

/**
* Calculates a user's island
Expand All @@ -56,7 +56,7 @@ public class Level extends Addon {
* @param playerUUID - the target island member's UUID
*/
public void calculateIslandLevel(World world, @Nullable User user, UUID playerUUID) {
levelCalc.calculateIslandLevel(world, user, playerUUID);
levelPresenter.calculateIslandLevel(world, user, playerUUID);
}

/**
Expand Down Expand Up @@ -86,14 +86,21 @@ public LevelsData getLevelsData(UUID targetPlayer) {
/**
* @return the settings
*/
public final Settings getSettings() {
public Settings getSettings() {
return settings;
}

public TopTen getTopTen() {
return topTen;
}

/**
* @return the levelPresenter
*/
public LevelPresenter getLevelPresenter() {
return levelPresenter;
}

@Override
public void onDisable(){
// Save the cache
Expand All @@ -116,7 +123,7 @@ public void onEnable() {
// Initialize the cache
levelsCache = new HashMap<>();
// Load the calculator
levelCalc = new LevelPresenter(this, this.getPlugin());
levelPresenter = new LevelPresenter(this, this.getPlugin());
// Start the top ten and register it for clicks
topTen = new TopTen(this);
registerListener(topTen);
Expand Down
46 changes: 43 additions & 3 deletions src/main/java/world/bentobox/level/LevelPresenter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package world.bentobox.level;

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import org.bukkit.World;
Expand All @@ -10,7 +14,18 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.level.calculators.PlayerLevel;

class LevelPresenter {
public class LevelPresenter {

private static final BigInteger THOUSAND = BigInteger.valueOf(1000);
private static final TreeMap<BigInteger, String> LEVELS;
static {
LEVELS = new TreeMap<>();

LEVELS.put(THOUSAND, "k");
LEVELS.put(THOUSAND.pow(2), "M");
LEVELS.put(THOUSAND.pow(3), "G");
LEVELS.put(THOUSAND.pow(4), "T");
}

private int levelWait;
private final Level addon;
Expand Down Expand Up @@ -60,9 +75,34 @@ public void calculateIslandLevel(World world, final User sender, UUID targetPlay
}

} else {
// Asking for the level of another player
sender.sendMessage("island.level.island-level-is","[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer)));
long lvl = addon.getIslandLevel(world, targetPlayer);

sender.sendMessage("island.level.island-level-is","[level]", getLevelString(lvl));
}
}

/**
* Get the string representation of the level. May be converted to shorthand notation, e.g., 104556 -> 10.5k
* @param lvl - long value to represent
* @return string of the level.
*/
public String getLevelString(long lvl) {
String level = String.valueOf(lvl);
// Asking for the level of another player
if(addon.getSettings().isShortHand()) {
BigInteger levelValue = BigInteger.valueOf(lvl);

Map.Entry<BigInteger, String> stage = LEVELS.floorEntry(levelValue);

if (stage != null) { // level > 1000
// 1 052 -> 1.0k
// 1 527 314 -> 1.5M
// 3 874 130 021 -> 3.8G
// 4 002 317 889 -> 4.0T
level = new DecimalFormat("#.#").format(levelValue.divide(stage.getKey().divide(THOUSAND)).doubleValue()/1000.0) + stage.getValue();
}
}
return level;
}

/**
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 @@ -125,12 +125,12 @@ public void getGUI(World world, final User user, String permPrefix) {
* @param asker - the asker of the top ten
* @return PanelItem
*/
private PanelItem getHead(int rank, Long level, UUID playerUUID, User asker, World world) {
private PanelItem getHead(int rank, long level, UUID playerUUID, User asker, World world) {
final String name = addon.getPlayers().getName(playerUUID);
List<String> description = new ArrayList<>();
if (name != null) {
description.add(asker.getTranslation("island.top.gui-heading", "[name]", name, "[rank]", String.valueOf(rank)));
description.add(asker.getTranslation("island.top.island-level","[level]", String.valueOf(level)));
description.add(asker.getTranslation("island.top.island-level","[level]", addon.getLevelPresenter().getLevelString(level)));
if (addon.getIslands().inTeam(world, playerUUID)) {
List<String> memberList = new ArrayList<>();
for (UUID members : addon.getIslands().getMembers(world, playerUUID)) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/world/bentobox/level/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,10 @@ public boolean isLogin() {
return level.getConfig().getBoolean("login");
}

/**
* @return true if levels should be shown in shorthand notation, e.g., 10,234 -> 10k
*/
public boolean isShortHand() {
return level.getConfig().getBoolean("shorthand");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.level.placeholders;

import world.bentobox.bentobox.api.addons.GameModeAddon;
Expand Down Expand Up @@ -32,7 +29,7 @@ public LevelPlaceholder(Level addon, GameModeAddon gm) {
*/
@Override
public String onReplace(User user) {
return String.valueOf(addon.getIslandLevel(gm.getOverWorld(), user.getUniqueId()));
return addon.getLevelPresenter().getLevelString(addon.getIslandLevel(gm.getOverWorld(), user.getUniqueId()));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.level.placeholders;

import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.level.placeholders;

import java.util.Collection;
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ deathpenalty: 100
sumteamdeaths: false
# For other death related settings, see the GameModeAddon's config.yml settings.

# Shorthand island level
# Shows large level values rounded down, e.g., 10,345 -> 10k
shorthand: false

# This section lists the limits for any particular block. Blocks over this amount
# are not counted.
# Format:
Expand Down
38 changes: 35 additions & 3 deletions src/test/java/world/bentobox/level/LevelPresenterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.level;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -10,6 +11,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand All @@ -20,6 +22,7 @@
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.level.calculators.PlayerLevel;
import world.bentobox.level.config.Settings;

/**
* @author tastybento
Expand All @@ -29,14 +32,17 @@
@PrepareForTest({Bukkit.class, LevelPresenter.class})
public class LevelPresenterTest {

@Mock
private BentoBox plugin;
@Mock
private Level addon;
@Mock
private PlayerLevel pl;
@Mock
private Settings settings;

@Before
public void setUp() throws Exception {
plugin = mock(BentoBox.class);
addon = mock(Level.class);
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("world");
Expand All @@ -49,9 +55,11 @@ public void setUp() throws Exception {
// team leader
when(im.getOwner(Mockito.any(), Mockito.any())).thenReturn(UUID.randomUUID());

pl = mock(PlayerLevel.class);
// Player level
PowerMockito.whenNew(PlayerLevel.class).withAnyArguments().thenReturn(pl);

// Settings
when(addon.getSettings()).thenReturn(settings);
}

/**
Expand All @@ -76,4 +84,28 @@ public void testCalculateIslandLevel() {
Mockito.verify(sender).sendMessage("island.level.calculating");
}

/**
* Test method for {@link LevelPresenter#getLevelString(long)}.
*/
@Test
public void testGetLevelStringLong() {
LevelPresenter lp = new LevelPresenter(addon, plugin);
assertEquals("123456789", lp.getLevelString(123456789L));

}

/**
* Test method for {@link LevelPresenter#getLevelString(long)}.
*/
@Test
public void testGetLevelStringLongShorthand() {
when(settings.isShortHand()).thenReturn(true);
LevelPresenter lp = new LevelPresenter(addon, plugin);
assertEquals("123.5M", lp.getLevelString(123456789L));
assertEquals("1.2k", lp.getLevelString(1234L));
assertEquals("123.5G", lp.getLevelString(123456789352L));
assertEquals("1.2T", lp.getLevelString(1234567893524L));
assertEquals("12345.7T", lp.getLevelString(12345678345345349L));

}
}
23 changes: 14 additions & 9 deletions src/test/java/world/bentobox/level/TopTenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -23,7 +24,6 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -44,6 +44,7 @@
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.level.config.Settings;
import world.bentobox.level.objects.TopTenData;

@RunWith(PowerMockRunner.class)
Expand Down Expand Up @@ -71,6 +72,11 @@ public class TopTenTest {
private PlayersManager pm;
@Mock
private Inventory inv;
@Mock
private LevelPresenter lp;
@Mock
private Settings settings;


@SuppressWarnings("unchecked")
@BeforeClass
Expand All @@ -87,7 +93,6 @@ public static void beforeClass() {

@Before
public void setUp() throws Exception {
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(addon.getPlugin()).thenReturn(plugin);

Expand Down Expand Up @@ -142,10 +147,10 @@ public void setUp() throws Exception {

// Inventory GUI
when(Bukkit.createInventory(any(), anyInt(), anyString())).thenReturn(inv);
}

@After
public void tearDown() throws Exception {
// Level presenter
when(addon.getLevelPresenter()).thenReturn(lp);
when(lp.getLevelString(anyLong())).thenAnswer((Answer<String>) invocation -> String.valueOf(invocation.getArgument(0, Long.class)));
}

@Test
Expand Down

0 comments on commit 39de2b9

Please sign in to comment.