Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor placeholders #279

Merged
merged 5 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Level
[![Build Status](https://ci.codemc.org/buildStatus/icon?job=BentoBoxWorld/Level)](https://ci.codemc.org/job/BentoBoxWorld/job/Level/)
[![Build Status](https://ci.codemc.org/buildStatus/icon?job=BentoBoxWorld/Level)](https://ci.codemc.org/job/BentoBoxWorld/job/Level/)[
![Bugs](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Level&metric=bugs)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Level)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Level&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Level)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Level&metric=ncloc)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Level)

## Note: Java 16 and Minecraft 17, or higher are now required
## About

Add-on for BentoBox to calculate island levels for BSkyBlock and AcidIsland. This add-on will work
Add-on for BentoBox to calculate island levels for BentoBox game modes like BSkyBlock and AcidIsland. It counts blocks and assigns a value to them.
Players gain levels by accumulating points and can lose levels too if their points go down. This add-on will work
for game modes listed in the config.yml.

Full documentation for Level can be found at [docs.bentobox.world](https://docs.bentobox.world/en/latest/addons/Level/).

Official download releases are at [download.bentobox.world](download.bentobox.world).

## How to use

1. Place the level addon jar in the addons folder of the BentoBox plugin
Expand Down
132 changes: 1 addition & 131 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.World;
Expand All @@ -22,7 +19,6 @@
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.level.calculators.Pipeliner;
import world.bentobox.level.commands.AdminLevelCommand;
Expand All @@ -37,9 +33,7 @@
import world.bentobox.level.listeners.IslandActivitiesListeners;
import world.bentobox.level.listeners.JoinLeaveListener;
import world.bentobox.level.listeners.MigrationListener;
import world.bentobox.level.objects.IslandLevels;
import world.bentobox.level.objects.LevelsData;
import world.bentobox.level.objects.TopTenData;
import world.bentobox.level.requests.LevelRequestHandler;
import world.bentobox.level.requests.TopTenRequestHandler;
import world.bentobox.visit.VisitAddon;
Expand Down Expand Up @@ -121,7 +115,7 @@ public void onEnable() {
.forEach(gm -> {
log("Level hooking into " + gm.getDescription().getName());
registerCommands(gm);
registerPlaceholders(gm);
new PlaceholderManager(this).registerPlaceholders(gm);
registeredGameModes.add(gm);
});
// Register request handlers
Expand Down Expand Up @@ -212,130 +206,6 @@ public static int compareVersions(String version1, String version2) {
return comparisonResult;
}

private void registerPlaceholders(GameModeAddon gm) {
if (getPlugin().getPlaceholdersManager() == null) return;
// Island Level
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_level",
user -> getManager().getIslandLevelString(gm.getOverWorld(), user.getUniqueId()));
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_level_raw",
user -> String.valueOf(getManager().getIslandLevel(gm.getOverWorld(), user.getUniqueId())));
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_total_points",
user -> {
IslandLevels data = getManager().getLevelsData(this.getIslands().getIsland(gm.getOverWorld(), user));
return data.getTotalPoints()+"";
});

getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_points_to_next_level",
user -> getManager().getPointsToNextString(gm.getOverWorld(), user.getUniqueId()));
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_island_level_max",
user -> String.valueOf(getManager().getIslandMaxLevel(gm.getOverWorld(), user.getUniqueId())));

// Visited Island Level
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_visited_island_level", user -> getVisitedIslandLevel(gm, user));

// Register Top Ten Placeholders
for (int i = 1; i < 11; i++) {
final int rank = i;
// Name
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_top_name_" + i, u -> getRankName(gm.getOverWorld(), rank));
// Island Name
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_top_island_name_" + i, u -> getRankIslandName(gm.getOverWorld(), rank));
// Members
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_top_members_" + i, u -> getRankMembers(gm.getOverWorld(), rank));
// Level
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_top_value_" + i, u -> getRankLevel(gm.getOverWorld(), rank));
}

// Personal rank
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_rank_value", u -> getRankValue(gm.getOverWorld(), u));
}

String getRankName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > TEN) rank = TEN;
return getPlayers().getName(getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
}

String getRankIslandName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > TEN) rank = TEN;
UUID owner = getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
if (owner != null) {
Island island = getIslands().getIsland(world, owner);
if (island != null) {
return island.getName() == null ? "" : island.getName();
}
}
return "";
}

String getRankMembers(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > TEN) rank = TEN;
UUID owner = getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
if (owner != null) {
Island island = getIslands().getIsland(world, owner);
if (island != null) {
// Sort members by rank
return island.getMembers().entrySet().stream()
.filter(e -> e.getValue() >= RanksManager.MEMBER_RANK)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.map(Map.Entry::getKey)
.map(getPlayers()::getName)
.collect(Collectors.joining(","));
}
}
return "";
}

String getRankLevel(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > TEN) rank = TEN;
return getManager()
.formatLevel(getManager()
.getTopTen(world, TEN)
.values()
.stream()
.skip(rank - 1L)
.limit(1L)
.findFirst()
.orElse(null));
}

/**
* Return the rank of the player in a world
* @param world world
* @param user player
* @return rank where 1 is the top rank.
*/
String getRankValue(World world, User user) {
if (user == null) {
return "";
}
// Get the island level for this user
long level = getManager().getIslandLevel(world, user.getUniqueId());
return String.valueOf(getManager().getTopTenLists().getOrDefault(world, new TopTenData(world)).getTopTen().values().stream().filter(l -> l > level).count() + 1);
}

String getVisitedIslandLevel(GameModeAddon gm, User user) {
if (user == null || !gm.inWorld(user.getLocation())) return "";
return getIslands().getIslandAt(user.getLocation())
.map(island -> getManager().getIslandLevelString(gm.getOverWorld(), island.getOwner()))
.orElse("0");
}


private void registerCommands(GameModeAddon gm) {
gm.getAdminCommand().ifPresent(adminCommand -> {
new AdminLevelCommand(this, adminCommand);
Expand Down
176 changes: 176 additions & 0 deletions src/main/java/world/bentobox/level/PlaceholderManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package world.bentobox.level;

import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import org.bukkit.World;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.level.objects.IslandLevels;
import world.bentobox.level.objects.TopTenData;

/**
* Handles Level placeholders
* @author tastybento
*
*/
public class PlaceholderManager {

private final Level addon;
private final BentoBox plugin;

public PlaceholderManager(Level addon) {
this.addon = addon;
this.plugin = addon.getPlugin();
}

protected void registerPlaceholders(GameModeAddon gm) {
if (plugin.getPlaceholdersManager() == null) return;
PlaceholdersManager bpm = plugin.getPlaceholdersManager();
// Island Level
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_level",
user -> addon.getManager().getIslandLevelString(gm.getOverWorld(), user.getUniqueId()));
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_level_raw",
user -> String.valueOf(addon.getManager().getIslandLevel(gm.getOverWorld(), user.getUniqueId())));
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_total_points",
user -> {
IslandLevels data = addon.getManager().getLevelsData(addon.getIslands().getIsland(gm.getOverWorld(), user));
return data.getTotalPoints()+"";
});

bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_points_to_next_level",
user -> addon.getManager().getPointsToNextString(gm.getOverWorld(), user.getUniqueId()));
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_level_max",
user -> String.valueOf(addon.getManager().getIslandMaxLevel(gm.getOverWorld(), user.getUniqueId())));

// Visited Island Level
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_visited_island_level", user -> getVisitedIslandLevel(gm, user));

// Register Top Ten Placeholders
for (int i = 1; i < 11; i++) {
final int rank = i;
// Name
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_name_" + i, u -> getRankName(gm.getOverWorld(), rank));
// Island Name
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_island_name_" + i, u -> getRankIslandName(gm.getOverWorld(), rank));
// Members
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_members_" + i, u -> getRankMembers(gm.getOverWorld(), rank));
// Level
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_value_" + i, u -> getRankLevel(gm.getOverWorld(), rank));
}

// Personal rank
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_rank_value", u -> getRankValue(gm.getOverWorld(), u));
}

/**
* Get the name of the player who holds the rank in this world
* @param world world
* @param rank rank 1 to 10
* @return rank name
*/
String getRankName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
return addon.getPlayers().getName(addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
}

/**
* Get the island name for this rank
* @param world world
* @param rank rank 1 to 10
* @return name of island or nothing if there isn't one
*/
String getRankIslandName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
if (owner != null) {
Island island = addon.getIslands().getIsland(world, owner);
if (island != null) {
return island.getName() == null ? "" : island.getName();
}
}
return "";
}

/**
* Gets a comma separated string of island member names
* @param world world
* @param rank rank to request
* @return comma separated string of island member names
*/
String getRankMembers(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
if (owner != null) {
Island island = addon.getIslands().getIsland(world, owner);
if (island != null) {
// Sort members by rank
return island.getMembers().entrySet().stream()
.filter(e -> e.getValue() >= RanksManager.MEMBER_RANK)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.map(Map.Entry::getKey)
.map(addon.getPlayers()::getName)
.collect(Collectors.joining(","));
}
}
return "";
}

String getRankLevel(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
return addon.getManager()
.formatLevel(addon.getManager()
.getTopTen(world, Level.TEN)
.values()
.stream()
.skip(rank - 1L)
.limit(1L)
.findFirst()
.orElse(null));
}

/**
* Return the rank of the player in a world
* @param world world
* @param user player
* @return rank where 1 is the top rank.
*/
private String getRankValue(World world, User user) {
if (user == null) {
return "";
}
// Get the island level for this user
long level = addon.getManager().getIslandLevel(world, user.getUniqueId());
return String.valueOf(addon.getManager().getTopTenLists().getOrDefault(world, new TopTenData(world)).getTopTen().values().stream().filter(l -> l > level).count() + 1);
}

String getVisitedIslandLevel(GameModeAddon gm, User user) {
if (user == null || !gm.inWorld(user.getWorld())) return "";
return addon.getIslands().getIslandAt(user.getLocation())
.map(island -> addon.getManager().getIslandLevelString(gm.getOverWorld(), island.getOwner()))
.orElse("0");
}

}
8 changes: 0 additions & 8 deletions src/test/java/world/bentobox/level/LevelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,4 @@ public void testGetSettings() {
assertEquals(100, s.getLevelCost());
}

/**
* Test method for {@link world.bentobox.level.Level#getRankLevel(World, int)}.
*/
@Test
public void testRankLevel() {
addon.onEnable();
assertEquals("",addon.getRankLevel(world, 1));
}
}
Loading