Skip to content

Commit

Permalink
Refactor placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 28, 2024
1 parent 0594933 commit f353c02
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 50 deletions.
37 changes: 3 additions & 34 deletions src/main/java/world/bentobox/aoneblock/AOneBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class AOneBlock extends GameModeAddon {
private final Config<Settings> configObject = new Config<>(this, Settings.class);
private BlockListener blockListener;
private OneBlocksManager oneBlockManager;
private PlaceholdersManager phManager;
private AOneBlockPlaceholders phManager;
private HoloListener holoListener;

@Override
Expand Down Expand Up @@ -105,7 +105,7 @@ public void onEnable() {
registerListener(new JoinLeaveListener(this));
registerListener(new InfoListener(this));
// Register placeholders
registerPlaceholders();
phManager = new AOneBlockPlaceholders(this, getPlugin().getPlaceholdersManager());

// Register request handlers
registerRequestHandler(new IslandStatsHandler(this));
Expand All @@ -130,37 +130,6 @@ public boolean loadData() {
return false;
}

private void registerPlaceholders() {
phManager = new PlaceholdersManager(this);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_phase",
phManager::getPhaseByLocation);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_count",
phManager::getCountByLocation);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_phase", phManager::getPhase);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_count", phManager::getCount);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_next_phase",
phManager::getNextPhaseByLocation);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_next_phase", phManager::getNextPhase);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_blocks_for_phase",
phManager::getPhaseBlocks);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_blocks_to_next_phase",
phManager::getNextPhaseBlocks);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_blocks_to_next_phase",
phManager::getNextPhaseBlocksByLocation);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_percent_done",
phManager::getPercentDone);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_percent_done",
phManager::getPercentDoneByLocation);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_done_scale", phManager::getDoneScale);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_done_scale",
phManager::getDoneScaleByLocation);
// Since 1.10
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "visited_island_lifetime_count",
phManager::getLifetimeByLocation);
getPlugin().getPlaceholdersManager().registerPlaceholder(this, "my_island_lifetime_count",
phManager::getLifetime);
}

@Override
public void onDisable() {
// save cache
Expand Down Expand Up @@ -319,7 +288,7 @@ public BlockListener getBlockListener() {
*
* @return the phManager
*/
public PlaceholdersManager getPlaceholdersManager() {
public AOneBlockPlaceholders getPlaceholdersManager() {
return phManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;

public class PlaceholdersManager {
public class AOneBlockPlaceholders {

private static final TreeMap<Double, String> SCALE;
private static final String INFINITE = "aoneblock.placeholders.infinite";
Expand All @@ -26,8 +26,26 @@ public class PlaceholdersManager {

private final AOneBlock addon;

public PlaceholdersManager(AOneBlock addon) {
public AOneBlockPlaceholders(AOneBlock addon,
world.bentobox.bentobox.managers.PlaceholdersManager placeholdersManager) {
this.addon = addon;
placeholdersManager.registerPlaceholder(addon, "visited_island_phase", this::getPhaseByLocation);
placeholdersManager.registerPlaceholder(addon, "visited_island_count", this::getCountByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_phase", this::getPhase);
placeholdersManager.registerPlaceholder(addon, "my_island_count", this::getCount);
placeholdersManager.registerPlaceholder(addon, "visited_island_next_phase", this::getNextPhaseByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_next_phase", this::getNextPhase);
placeholdersManager.registerPlaceholder(addon, "my_island_blocks_for_phase", this::getPhaseBlocks);
placeholdersManager.registerPlaceholder(addon, "my_island_blocks_to_next_phase", this::getNextPhaseBlocks);
placeholdersManager.registerPlaceholder(addon, "visited_island_blocks_to_next_phase",
this::getNextPhaseBlocksByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_percent_done", this::getPercentDone);
placeholdersManager.registerPlaceholder(addon, "visited_island_percent_done", this::getPercentDoneByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_done_scale", this::getDoneScale);
placeholdersManager.registerPlaceholder(addon, "visited_island_done_scale", this::getDoneScaleByLocation);
// Since 1.10
placeholdersManager.registerPlaceholder(addon, "visited_island_lifetime_count", this::getLifetimeByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_lifetime_count", this::getLifetime);
}

/**
Expand Down
31 changes: 17 additions & 14 deletions src/test/java/world/bentobox/aoneblock/PlaceholdersManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;

/**
* @author tastybento
Expand All @@ -36,7 +37,7 @@ public class PlaceholdersManagerTest {

private UUID uuid = UUID.randomUUID();

private PlaceholdersManager pm;
private AOneBlockPlaceholders pm;
@Mock
private IslandsManager im;
@Mock
Expand All @@ -49,6 +50,8 @@ public class PlaceholdersManagerTest {
@Mock
private OneBlocksManager obm;
private Settings settings;
@Mock
private PlaceholdersManager phm;

/**
* @throws java.lang.Exception
Expand Down Expand Up @@ -76,11 +79,11 @@ public void setUp() throws Exception {
settings = new Settings();
when(addon.getSettings()).thenReturn(settings);

pm = new PlaceholdersManager(addon);
pm = new AOneBlockPlaceholders(addon, phm);
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getPhaseByLocation(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getPhaseByLocation(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetPhaseByLocation() {
Expand All @@ -93,7 +96,7 @@ public void testGetPhaseByLocation() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getCountByLocation(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getCountByLocation(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetCountByLocation() {
Expand All @@ -106,7 +109,7 @@ public void testGetCountByLocation() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getPhase(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getPhase(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetPhase() {
Expand All @@ -119,7 +122,7 @@ public void testGetPhase() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getCount(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getCount(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetCount() {
Expand All @@ -132,7 +135,7 @@ public void testGetCount() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getNextPhaseByLocation(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getNextPhaseByLocation(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetNextPhaseByLocation() {
Expand All @@ -145,7 +148,7 @@ public void testGetNextPhaseByLocation() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getNextPhase(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getNextPhase(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetNextPhase() {
Expand All @@ -158,7 +161,7 @@ public void testGetNextPhase() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getNextPhaseBlocksByLocation(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getNextPhaseBlocksByLocation(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetNextPhaseBlocksByLocation() {
Expand All @@ -173,7 +176,7 @@ public void testGetNextPhaseBlocksByLocation() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getNextPhaseBlocks(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getNextPhaseBlocks(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetNextPhaseBlocks() {
Expand All @@ -188,7 +191,7 @@ public void testGetNextPhaseBlocks() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getPercentDoneByLocation(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getPercentDoneByLocation(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetPercentDoneByLocation() {
Expand All @@ -201,7 +204,7 @@ public void testGetPercentDoneByLocation() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getPercentDone(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getPercentDone(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetPercentDone() {
Expand All @@ -214,7 +217,7 @@ public void testGetPercentDone() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getDoneScaleByLocation(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getDoneScaleByLocation(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetDoneScaleByLocation() {
Expand All @@ -227,7 +230,7 @@ public void testGetDoneScaleByLocation() {
}

/**
* Test method for {@link world.bentobox.aoneblock.PlaceholdersManager#getDoneScale(world.bentobox.bentobox.api.user.User)}.
* Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getDoneScale(world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetDoneScale() {
Expand Down

0 comments on commit f353c02

Please sign in to comment.