diff --git a/api/pom.xml b/api/pom.xml
index 770dbf5..8aa7049 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -22,6 +22,22 @@
6
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 8
+ 8
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 8
+ 8
+
+
jar
diff --git a/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/Leaderboard.java b/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/Leaderboard.java
new file mode 100644
index 0000000..e2bd3cc
--- /dev/null
+++ b/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/Leaderboard.java
@@ -0,0 +1,24 @@
+package me.tofpu.speedbridge.api.leaderboard;
+
+public interface Leaderboard {
+ /**
+ * @return the leaderboard identifier
+ */
+ String identifier();
+
+ /**
+ * Fetches the position from this leaderboard to a string.
+ *
+ * @param position the position that you would like to fetch from the leaderboard
+ *
+ * @return nicely formatted string position, will return "N/A" if the postiion was filled
+ */
+ String parse(final int position);
+
+ /**
+ * A nicely formatted string from this leaderboard.
+ *
+ * @return nicely formatted leaderboard
+ */
+ String print();
+}
diff --git a/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/LeaderboardService.java b/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/LeaderboardService.java
new file mode 100644
index 0000000..f1c141e
--- /dev/null
+++ b/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/LeaderboardService.java
@@ -0,0 +1,24 @@
+package me.tofpu.speedbridge.api.leaderboard;
+
+
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+public interface LeaderboardService {
+ /**
+ * Retrieves a leaderboard that associates with the type.
+ *
+ * @param type the leaderboard type
+ *
+ * @return the leaderboard instance that associates with the type, otherwise null.
+ */
+ Leaderboard get(final LeaderboardType type);
+
+ /**
+ * This method allows you to run a method across all the leaderboard within a single method, so long it wasn't filtered.
+ *
+ * @param filter filter
+ * @param consumer the consumer value
+ */
+ void compute(final Predicate filter, Consumer consumer);
+}
diff --git a/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/LeaderboardType.java b/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/LeaderboardType.java
new file mode 100644
index 0000000..9ba12ab
--- /dev/null
+++ b/api/src/main/java/me/tofpu/speedbridge/api/leaderboard/LeaderboardType.java
@@ -0,0 +1,13 @@
+package me.tofpu.speedbridge.api.leaderboard;
+
+public enum LeaderboardType {
+ GLOBAL, SEASONAL;
+
+ public static LeaderboardType match(final String identifier) {
+ for (LeaderboardType type : values()){
+ if (type.name().equalsIgnoreCase(identifier))
+ return type;
+ }
+ return null;
+ }
+}
diff --git a/api/src/main/java/me/tofpu/speedbridge/api/lobby/Leaderboard.java b/api/src/main/java/me/tofpu/speedbridge/api/lobby/Leaderboard.java
deleted file mode 100644
index d53a5dd..0000000
--- a/api/src/main/java/me/tofpu/speedbridge/api/lobby/Leaderboard.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package me.tofpu.speedbridge.api.lobby;
-
-import me.tofpu.speedbridge.api.user.User;
-import org.bukkit.plugin.Plugin;
-
-import java.util.List;
-
-// TODO: DUMMY INTERFACE FOR NOW
-public interface Leaderboard {
- void start(final Plugin plugin);
-
- void check(final User user);
-
- String parse(final int position);
-
- List positions();
-
- void cancel();
-
- void addAll(final List list);
-
- String print();
-}
diff --git a/api/src/main/java/me/tofpu/speedbridge/api/lobby/LobbyService.java b/api/src/main/java/me/tofpu/speedbridge/api/lobby/LobbyService.java
index 40cc58e..082e086 100644
--- a/api/src/main/java/me/tofpu/speedbridge/api/lobby/LobbyService.java
+++ b/api/src/main/java/me/tofpu/speedbridge/api/lobby/LobbyService.java
@@ -12,9 +12,7 @@ public interface LobbyService {
boolean hasLobbyLocation();
- void save(final Gson gson, final File lobbyFile, final File leaderboardFile);
+ void save(final Gson gson, final File lobbyFile);
- void load(final Gson gson, final File lobbyFile, final File leaderboardFile);
-
- Leaderboard getLeaderboard();
+ void load(final Gson gson, final File lobbyFile);
}
\ No newline at end of file
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/SpeedBridge.java b/spigot/src/main/java/me/tofpu/speedbridge/SpeedBridge.java
index 50d2a60..c3ffb06 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/SpeedBridge.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/SpeedBridge.java
@@ -1,6 +1,7 @@
package me.tofpu.speedbridge;
import me.tofpu.speedbridge.game.Game;
+import me.tofpu.speedbridge.game.leaderboard.AbstractLeaderboard;
import org.bukkit.plugin.java.JavaPlugin;
public final class SpeedBridge extends JavaPlugin {
@@ -28,7 +29,7 @@ public void onDisable() {
game().dataManager().shutdown();
// cancelling the leaderboard task
- game().lobbyService().getLeaderboard().cancel();
+ game.leaderboardManager().compute(null, leaderboard -> ((AbstractLeaderboard) leaderboard).cancel());
}
public Game game() {
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/command/CommandHandler.java b/spigot/src/main/java/me/tofpu/speedbridge/command/CommandHandler.java
index 6e0a610..f48b521 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/command/CommandHandler.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/command/CommandHandler.java
@@ -30,7 +30,7 @@ public CommandHandler(Game game, final SpeedBridge plugin) {
this.commandManager.getCommandCompletions().registerCompletion("setupStage", context -> Util.toString(SetupStage.values()));
// registrations
- registerCommand(new MainCommand(game.userService(), game.gameService(), game.lobbyService()));
+ registerCommand(new MainCommand(game.userService(), game.gameService(), game.lobbyService(), game.leaderboardManager()));
registerCommand(new AdminCommand(plugin, game.lobbyService(), game.gameService(), game.gameController(), game.dataManager()));
}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/command/sub/MainCommand.java b/spigot/src/main/java/me/tofpu/speedbridge/command/sub/MainCommand.java
index ed906ec..7c98d21 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/command/sub/MainCommand.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/command/sub/MainCommand.java
@@ -4,12 +4,14 @@
import me.tofpu.speedbridge.api.game.GameService;
import me.tofpu.speedbridge.api.game.Result;
import me.tofpu.speedbridge.api.island.mode.Mode;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardService;
import me.tofpu.speedbridge.api.lobby.LobbyService;
import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.api.user.UserProperties;
import me.tofpu.speedbridge.api.user.UserService;
import me.tofpu.speedbridge.command.BridgeBaseCommand;
import me.tofpu.speedbridge.data.file.path.Path;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardType;
import me.tofpu.speedbridge.island.mode.ModeManager;
import me.tofpu.speedbridge.util.Util;
import org.bukkit.command.CommandSender;
@@ -17,16 +19,17 @@
@CommandAlias("speedbridge|game")
public class MainCommand extends BridgeBaseCommand {
-
private final UserService userService;
private final GameService gameService;
private final LobbyService lobbyService;
+ private final LeaderboardService leaderboardService;
- public MainCommand(final UserService userService, final GameService gameService, final LobbyService lobbyService) {
+ public MainCommand(final UserService userService, final GameService gameService, final LobbyService lobbyService, final LeaderboardService leaderboardService) {
super("game");
this.userService = userService;
this.gameService = gameService;
this.lobbyService = lobbyService;
+ this.leaderboardService = leaderboardService;
}
@Override
@@ -73,7 +76,7 @@ public void onLeave(final Player player) {
@CommandAlias("leaderboard")
@Description("Lists the top 10 best performers")
public void onLeaderboard(final CommandSender player) {
- player.sendMessage(lobbyService.getLeaderboard().print());
+ player.sendMessage(leaderboardService.get(LeaderboardType.GLOBAL).print());
}
@Subcommand("score")
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/data/DataManager.java b/spigot/src/main/java/me/tofpu/speedbridge/data/DataManager.java
index 46a87ff..6431e5c 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/data/DataManager.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/data/DataManager.java
@@ -4,21 +4,23 @@
import com.github.requestpluginsforfree.fileutil.file.PluginFile;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
-import com.google.gson.reflect.TypeToken;
import me.tofpu.speedbridge.api.island.Island;
import me.tofpu.speedbridge.api.island.IslandService;
-import me.tofpu.speedbridge.api.lobby.BoardUser;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardService;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardType;
import me.tofpu.speedbridge.api.lobby.LobbyService;
import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.api.user.UserService;
import me.tofpu.speedbridge.data.adapter.IslandAdapter;
-import me.tofpu.speedbridge.data.adapter.LeaderboardTypeAdapter;
import me.tofpu.speedbridge.data.adapter.LocationAdapter;
import me.tofpu.speedbridge.data.adapter.UserAdapter;
import me.tofpu.speedbridge.data.file.path.Path;
import me.tofpu.speedbridge.data.file.path.PathType;
import me.tofpu.speedbridge.data.file.type.MessageFile;
import me.tofpu.speedbridge.data.file.type.SettingsFile;
+import me.tofpu.speedbridge.game.leaderboard.AbstractLeaderboard;
+import me.tofpu.speedbridge.game.leaderboard.LeaderboardAdapter;
+import me.tofpu.speedbridge.game.leaderboard.LeaderboardServiceImpl;
import me.tofpu.speedbridge.island.service.IslandServiceImpl;
import me.tofpu.speedbridge.user.service.UserServiceImpl;
import org.bukkit.Location;
@@ -26,8 +28,9 @@
import org.bukkit.plugin.Plugin;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.io.IOException;
-import java.util.List;
import java.util.Locale;
import java.util.UUID;
@@ -36,35 +39,38 @@ public class DataManager {
.registerTypeAdapter(Location.class, new LocationAdapter())
.registerTypeAdapter(Island.class, new IslandAdapter())
.registerTypeAdapter(User.class, new UserAdapter())
- .registerTypeAdapter(
- new TypeToken>() {}.getType(),
- new LeaderboardTypeAdapter())
+ .registerTypeAdapter(AbstractLeaderboard.class, new LeaderboardAdapter())
.setPrettyPrinting()
.serializeNulls()
.create();
+ private final Plugin plugin;
private final File[] files;
private final PluginFile[] pluginFiles;
private IslandServiceImpl islandService;
private UserServiceImpl userService;
private LobbyService lobbyService;
+ private LeaderboardServiceImpl leaderboardService;
- public DataManager() {
+ public DataManager(final Plugin plugin) {
+ this.plugin = plugin;
this.files = new File[5];
this.pluginFiles = new PluginFile[2];
}
- public void initialize(final IslandService islandService, final UserService userService, final LobbyService lobbyService, final Plugin plugin, final File parentDirectory) {
+ public void initialize(final IslandService islandService, final UserService userService, final LobbyService lobbyService, final LeaderboardServiceImpl leaderboardService, final Plugin plugin, final File parentDirectory) {
this.islandService = (IslandServiceImpl) islandService;
this.userService = (UserServiceImpl) userService;
this.lobbyService = lobbyService;
+ this.leaderboardService = leaderboardService;
this.files[0] = parentDirectory;
this.files[1] = new File(parentDirectory, "islands");
this.files[2] = new File(parentDirectory, "users");
- this.files[3] = new File(parentDirectory, "lobby.json");
- this.files[4] = new File(parentDirectory, "leaderboard.json");
+ this.files[3] = new File(parentDirectory, "leaderboards");
+ this.files[4] = new File(parentDirectory, "lobby.json");
+// this.files[4] = new File(parentDirectory, "leaderboard.json");
this.pluginFiles[0] = new SettingsFile(plugin, parentDirectory);
this.pluginFiles[1] = new MessageFile(plugin, parentDirectory);
@@ -124,15 +130,28 @@ public void unloadUser(final UUID uuid) {
userService.removeUser(user);
}
- public void load() {
- lobbyService.load(GSON, files[3], files[4]);
+ public void load() throws IOException {
+ lobbyService.load(GSON, files[4]);
islandService.loadAll();
+
+ final File file = new File(getFiles()[0], "leaderboard.json");
+ if (file.exists()) {
+ try (final FileReader reader = new FileReader(file)) {
+ plugin.getLogger().info("Migrating your outdated leaderboard to the new leaderboard system now...");
+ leaderboardService.get(LeaderboardType.GLOBAL).addAll(GSON.fromJson(reader, AbstractLeaderboard.class).positions());
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ }
+ file.delete();
+ }
+ leaderboardService.load();
}
public void shutdown() {
islandService.saveAll(true);
userService.saveAll(true);
- lobbyService.save(GSON, files[3], files[4]);
+ lobbyService.save(GSON, files[4]);
+ leaderboardService.save();
}
public File[] getFiles() {
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/data/adapter/LeaderboardTypeAdapter.java b/spigot/src/main/java/me/tofpu/speedbridge/data/adapter/LeaderboardTypeAdapter.java
index ce9e80b..58bfd1b 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/data/adapter/LeaderboardTypeAdapter.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/data/adapter/LeaderboardTypeAdapter.java
@@ -5,7 +5,7 @@
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import me.tofpu.speedbridge.api.lobby.BoardUser;
-import me.tofpu.speedbridge.lobby.leaderboard.BoardUserImpl;
+import me.tofpu.speedbridge.game.leaderboard.BoardUserImpl;
import java.io.IOException;
import java.util.ArrayList;
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/data/file/path/Path.java b/spigot/src/main/java/me/tofpu/speedbridge/data/file/path/Path.java
index d4993b1..5b8320c 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/data/file/path/Path.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/data/file/path/Path.java
@@ -15,6 +15,11 @@ public class Path {
public static final Value SETTINGS_TELEPORT = new Value<>(SETTINGS + "teleport", true, PathType.SETTINGS);
public static final Value SETTINGS_BLOCK = new Value<>(SETTINGS + "block", "WOOL", PathType.SETTINGS);
+ private static final String LEADERBOARD = SETTINGS + "leaderboard.";
+ public static final Value LEADERBOARD_SIZE = new Value<>(LEADERBOARD + "size", 10, PathType.SETTINGS);
+ public static final Value LEADERBOARD_HEADER = new Value<>(LEADERBOARD + "header", "&eLeaderboard", PathType.SETTINGS);
+ public static final Value LEADERBOARD_STYLE = new Value<>(LEADERBOARD + "style", "&e{position}. {name} &a({score})", PathType.SETTINGS);
+
public static final Value MESSAGES_JOINED = new Value<>(MESSAGES + "joined", "&aGood Luck!", PathType.MESSAGES);
public static final Value MESSAGES_LEFT = new Value<>(MESSAGES + "left", "", PathType.MESSAGES);
public static final Value MESSAGES_NO_AVAILABLE = new Value<>(MESSAGES + "no-available", "&cThere is no available island right now; try again later!", PathType.MESSAGES);
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/data/listener/PlayerJoinListener.java b/spigot/src/main/java/me/tofpu/speedbridge/data/listener/PlayerJoinListener.java
index 86bbb23..68077a8 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/data/listener/PlayerJoinListener.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/data/listener/PlayerJoinListener.java
@@ -1,8 +1,9 @@
package me.tofpu.speedbridge.data.listener;
import me.tofpu.speedbridge.api.lobby.LobbyService;
-import me.tofpu.speedbridge.data.DataManager;
+import me.tofpu.speedbridge.api.user.UserService;
import me.tofpu.speedbridge.data.file.path.Path;
+import me.tofpu.speedbridge.game.leaderboard.LeaderboardServiceImpl;
import me.tofpu.speedbridge.util.Util;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -10,12 +11,14 @@
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoinListener implements Listener {
+ private final UserService userService;
private final LobbyService lobbyService;
- private final DataManager dataManager;
+ private final LeaderboardServiceImpl leaderboardService;
- public PlayerJoinListener(final LobbyService lobbyService, final DataManager dataManager) {
+ public PlayerJoinListener(final UserService userService, final LobbyService lobbyService, final LeaderboardServiceImpl leaderboardService) {
+ this.userService = userService;
this.lobbyService = lobbyService;
- this.dataManager = dataManager;
+ this.leaderboardService = leaderboardService;
}
@EventHandler(ignoreCancelled = true)
@@ -30,6 +33,6 @@ private void onPlayerJoin(final PlayerJoinEvent event) {
Util.message(player, Path.MESSAGES_NO_LOBBY);
}
- lobbyService.getLeaderboard().check(dataManager.loadUser(event.getPlayer().getUniqueId()));
+ leaderboardService.check(userService.getOrDefault(player.getUniqueId(), true), null);
}
}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/expansion/BridgeExpansion.java b/spigot/src/main/java/me/tofpu/speedbridge/expansion/BridgeExpansion.java
index fbc4ed6..bb35605 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/expansion/BridgeExpansion.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/expansion/BridgeExpansion.java
@@ -2,11 +2,12 @@
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.tofpu.speedbridge.api.game.GameService;
-import me.tofpu.speedbridge.api.lobby.Leaderboard;
-import me.tofpu.speedbridge.api.lobby.LobbyService;
+import me.tofpu.speedbridge.api.leaderboard.Leaderboard;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardService;
import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.api.user.UserService;
import me.tofpu.speedbridge.api.user.timer.Timer;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardType;
import me.tofpu.speedbridge.util.Util;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
@@ -19,13 +20,13 @@ public class BridgeExpansion extends PlaceholderExpansion {
private final UserService userService;
private final GameService gameService;
- private final LobbyService lobbyService;
+ private final LeaderboardService leaderboardService;
- public BridgeExpansion(final PluginDescriptionFile description, final UserService userService, final GameService gameService, final LobbyService lobbyService) {
+ public BridgeExpansion(final PluginDescriptionFile description, final UserService userService, final GameService gameService, final LeaderboardService leaderboardService) {
this.description = description;
this.userService = userService;
this.gameService = gameService;
- this.lobbyService = lobbyService;
+ this.leaderboardService = leaderboardService;
}
@Override
@@ -66,7 +67,7 @@ public String onPlaceholderRequest(Player player, @NotNull String params) {
final Timer timer;
switch (type) {
case ISLAND:
- Integer slot = null;
+ Integer slot;
if (isNull || ((slot = user.properties().islandSlot()) == null)) return "Lobby";
return slot + "";
case LIVE_TIMER:
@@ -77,8 +78,8 @@ public String onPlaceholderRequest(Player player, @NotNull String params) {
return timer.result() + "";
case LEADERBOARD:
if (args.length <= 1) return null;
- final Leaderboard leaderboard = lobbyService.getLeaderboard();
- System.out.println(Arrays.toString(args));
+ final Leaderboard leaderboard = leaderboardService.get(LeaderboardType.GLOBAL);
+
final Integer integer = Util.parseInt(args[1]);
return integer == null ? "Provide a number!" : integer == 0 ? "Only number 1 or above is allowed!" : leaderboard.parse(integer - 1);
default:
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/Game.java b/spigot/src/main/java/me/tofpu/speedbridge/game/Game.java
index d5bb660..39477eb 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/game/Game.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/Game.java
@@ -8,6 +8,7 @@
import me.tofpu.speedbridge.api.SpeedBridgeAPI;
import me.tofpu.speedbridge.api.game.GameService;
import me.tofpu.speedbridge.api.island.IslandService;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardService;
import me.tofpu.speedbridge.api.lobby.LobbyService;
import me.tofpu.speedbridge.api.user.UserService;
import me.tofpu.speedbridge.command.CommandHandler;
@@ -17,6 +18,8 @@
import me.tofpu.speedbridge.data.listener.PlayerQuitListener;
import me.tofpu.speedbridge.expansion.BridgeExpansion;
import me.tofpu.speedbridge.game.controller.GameController;
+import me.tofpu.speedbridge.game.leaderboard.AbstractLeaderboard;
+import me.tofpu.speedbridge.game.leaderboard.LeaderboardServiceImpl;
import me.tofpu.speedbridge.game.listener.functionality.BlockBreakListener;
import me.tofpu.speedbridge.game.listener.functionality.EntityDamageListener;
import me.tofpu.speedbridge.game.listener.functionality.FoodLevelChangeListener;
@@ -25,7 +28,7 @@
import me.tofpu.speedbridge.game.service.GameServiceImpl;
import me.tofpu.speedbridge.island.mode.ModeManager;
import me.tofpu.speedbridge.island.service.IslandServiceImpl;
-import me.tofpu.speedbridge.lobby.service.LobbyServiceImpl;
+import me.tofpu.speedbridge.lobby.LobbyServiceImpl;
import me.tofpu.speedbridge.user.service.UserServiceImpl;
import me.tofpu.speedbridge.util.UpdateChecker;
import me.tofpu.speedbridge.util.Util;
@@ -35,6 +38,7 @@
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -50,7 +54,9 @@ public class Game {
private final IslandServiceImpl islandService;
private final UserServiceImpl userService;
- private final LobbyService lobbyService;
+ private final LobbyServiceImpl lobbyService;
+
+ private final LeaderboardServiceImpl leaderboardService;
private final GameController gameController;
private final GameService gameService;
@@ -59,17 +65,19 @@ public class Game {
public Game(final SpeedBridge speedBridge) {
this.speedBridge = speedBridge;
- this.dataManager = new DataManager();
+ this.dataManager = new DataManager(speedBridge);
this.islandService = new IslandServiceImpl();
this.userService = new UserServiceImpl();
this.lobbyService = new LobbyServiceImpl();
+ this.leaderboardService = new LeaderboardServiceImpl();
+
this.gameController = new GameController(islandService);
- this.gameService = new GameServiceImpl(speedBridge, islandService, userService, lobbyService);
+ this.gameService = new GameServiceImpl(speedBridge, islandService, userService, lobbyService, leaderboardService);
this.listeners = new ArrayList<>(Arrays.asList(
- new PlayerJoinListener(lobbyService, dataManager),
+ new PlayerJoinListener(userService, lobbyService, leaderboardService),
new PlayerQuitListener(userService, gameService, dataManager),
new PlayerInteractListener(userService, islandService, gameService),
new BlockPlaceListener(userService, islandService, gameService),
@@ -90,7 +98,7 @@ private void initializePlaceholderApi() {
if (!DependencyAPI.get("PlaceholderAPI").isAvailable()) return;
Util.isPlaceholderHooked = true;
- new BridgeExpansion(speedBridge.getDescription(), userService, gameService, lobbyService).register();
+ new BridgeExpansion(speedBridge.getDescription(), userService, gameService, leaderboardService).register();
}
private void registerListeners(){
@@ -108,11 +116,13 @@ public void initialize() {
SpeedBridgeAPI.setInstance(this.speedBridge.getLogger(), this.userService, this.islandService, this.gameService, this.lobbyService);
// initializing the files
- this.dataManager.initialize(this.islandService, this.userService, this.lobbyService, this.speedBridge, this.speedBridge.getDataFolder());
+ this.dataManager.initialize(this.islandService, this.userService, this.lobbyService, this.leaderboardService, this.speedBridge, this.speedBridge.getDataFolder());
this.userService.initialize(this.dataManager);
this.islandService.initialize(this.dataManager);
+ leaderboardService.initialize(this.dataManager.getFiles()[3]);
+
// update checker async
UpdateChecker.init(speedBridge, 95918).requestUpdateCheck().whenComplete((updateResult, throwable) -> {
final Logger logger = this.speedBridge.getLogger();
@@ -151,7 +161,11 @@ public void load() {
registerListeners();
- this.dataManager.load();
+ try {
+ this.dataManager.load();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
// adds in options that are missing from the configurations
boolean update = false;
@@ -177,7 +191,7 @@ public void load() {
Bukkit.getOnlinePlayers().forEach(player -> dataManager.loadUser(player.getUniqueId()));
// starting up the leaderboard
- lobbyService.getLeaderboard().start(speedBridge);
+ leaderboardService.compute(null, leaderboard -> ((AbstractLeaderboard) leaderboard).start(speedBridge));
}
public IslandService islandService() {
@@ -196,6 +210,10 @@ public GameService gameService() {
return this.gameService;
}
+ public LeaderboardService leaderboardManager() {
+ return leaderboardService;
+ }
+
public LobbyService lobbyService() {
return this.lobbyService;
}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/AbstractLeaderboard.java b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/AbstractLeaderboard.java
new file mode 100644
index 0000000..7485218
--- /dev/null
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/AbstractLeaderboard.java
@@ -0,0 +1,145 @@
+package me.tofpu.speedbridge.game.leaderboard;
+
+import me.tofpu.speedbridge.api.lobby.BoardUser;
+import me.tofpu.speedbridge.api.leaderboard.Leaderboard;
+import me.tofpu.speedbridge.api.user.User;
+import me.tofpu.speedbridge.api.user.timer.Timer;
+import me.tofpu.speedbridge.util.Util;
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.scheduler.BukkitTask;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+public abstract class AbstractLeaderboard implements Leaderboard {
+ protected final List leaderboard;
+
+ private final String identifier;
+ private final int capacity;
+ private BukkitTask bukkitTask;
+
+ public AbstractLeaderboard(final String identifier, final int capacity) {
+ this.identifier = identifier;
+ this.capacity = capacity;
+ this.leaderboard = new ArrayList<>(capacity);
+ }
+
+ @Override
+ public String identifier() {
+ return identifier;
+ }
+
+ public void start(Plugin plugin) {
+ bukkitTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
+ final List users = sortGet();
+ leaderboard.clear();
+ leaderboard.addAll(users.subList(0, Math.min(users.size(), capacity)));
+ }, 0, 20 * 10);
+ }
+
+ public void cancel() {
+ bukkitTask.cancel();
+ }
+
+ public BoardUser get(final UUID uniqueId) {
+ for (final BoardUser user : leaderboard) {
+ if (user.uniqueId().equals(uniqueId))
+ return user;
+ }
+ return null;
+ }
+
+ public void add(final BoardUser boardUser){
+ if (boardUser == null || boardUser.score() == null) return;
+ for (final BoardUser user : leaderboard) {
+ if (user.equals(boardUser)) {
+ if (user.score() > boardUser.score()) {
+ user.score(boardUser.score());
+ }
+ return;
+ }
+ }
+ leaderboard.add(boardUser);
+ }
+
+ public void addAll(final List users) {
+ for (final BoardUser user : users) {
+ add(user);
+ }
+ }
+
+ public void check(final User user) {
+ final Player player = Bukkit.getPlayer(user.uniqueId());
+ final Timer timer = user.properties().timer();
+ if (player == null || timer == null) return;
+
+ BoardUser boardUser = get(user.uniqueId());
+ if (boardUser == null) {
+ boardUser = new BoardUserImpl.Builder().name(player.getName()).uniqueId(user.uniqueId()).result(timer == null ? null : timer.result()).build();
+ } else {
+ if (boardUser.score() == null || boardUser.score() > timer.result()) {
+ boardUser.score(timer.result());
+ }
+ }
+ add(boardUser);
+ }
+
+ public List sortGet() {
+ final List leaderboard = new ArrayList<>(this.leaderboard);
+ if (leaderboard.isEmpty()) return leaderboard;
+
+ int max;
+ for (int i = 0; i < leaderboard.size(); i++) {
+ max = i;
+ BoardUser playerMax = leaderboard.get(max);
+ BoardUser playerJ;
+
+ for (int j = i; j < leaderboard.size(); j++) {
+ playerJ = leaderboard.get(j);
+ if (playerJ.score() < playerMax.score()) {
+ max = j;
+ playerMax = leaderboard.get(max);
+ }
+ }
+ //Swap biggest and current locations
+ final BoardUser placeholder = leaderboard.get(i);
+ leaderboard.set(i, playerMax);
+ if (!placeholder.equals(playerMax)) leaderboard.set(max, placeholder);
+ }
+
+ return leaderboard;
+ }
+
+ @Override
+ public String print(){
+ final StringBuilder builder = new StringBuilder();
+ builder.append("&e").append(identifier).append(" ").append("Leaderboard");
+
+ for (int i = 0; i < positions().size(); i++) {
+ if (i >= capacity) break;
+
+ if (builder.capacity() != 1) builder.append("\n");
+ builder.append("&e").append(i + 1).append(". ").append(parse(i));
+ }
+
+ return Util.colorize(builder.toString());
+ }
+
+ @Override
+ public String parse(final int slot){
+ if (leaderboard.size() <= slot) return "N/A";
+ final BoardUser user = leaderboard.get(slot);
+ return user == null ? "N/A" : Util.colorize(user.name() + " &a(" + user.score() + ")");
+ }
+
+ public List positions() {
+ return leaderboard;
+ }
+
+ public int capacity() {
+ return capacity;
+ }
+}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/lobby/leaderboard/BoardUserImpl.java b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/BoardUserImpl.java
similarity index 97%
rename from spigot/src/main/java/me/tofpu/speedbridge/lobby/leaderboard/BoardUserImpl.java
rename to spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/BoardUserImpl.java
index 49d1d2a..98a5ff6 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/lobby/leaderboard/BoardUserImpl.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/BoardUserImpl.java
@@ -1,4 +1,4 @@
-package me.tofpu.speedbridge.lobby.leaderboard;
+package me.tofpu.speedbridge.game.leaderboard;
import com.google.common.base.Objects;
import me.tofpu.speedbridge.api.lobby.BoardUser;
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardAdapter.java b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardAdapter.java
new file mode 100644
index 0000000..fdbf946
--- /dev/null
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardAdapter.java
@@ -0,0 +1,88 @@
+package me.tofpu.speedbridge.game.leaderboard;
+
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonToken;
+import com.google.gson.stream.JsonWriter;
+import me.tofpu.speedbridge.api.lobby.BoardUser;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+public class LeaderboardAdapter extends TypeAdapter {
+ @Override
+ public void write(final JsonWriter out, final AbstractLeaderboard value) throws IOException {
+ out.beginObject();
+
+ out.name("identifier").value(value.identifier());
+ out.name("capacity").value(value.capacity());
+
+ out.name("positions").beginArray();
+ for (final BoardUser user : value.positions()) {
+ if (user.uniqueId() == null) continue;
+ out.beginObject();
+ out.name("name").value(user.name());
+ out.name("uniqueId").value(user.uniqueId().toString());
+ out.name("score").value(user.score());
+ out.endObject();
+ }
+ out.endArray();
+ out.endObject();
+ }
+
+ @Override
+ public AbstractLeaderboard read(final JsonReader in) throws IOException {
+ in.beginObject();
+
+ String identifier = "";
+ int capacity = 0;
+
+ final List list = new ArrayList<>();
+ while (in.hasNext()) {
+ switch (in.nextName()) {
+ case "identifier":
+ identifier = in.nextString();
+ break;
+ case "capacity":
+ capacity = in.nextInt();
+ break;
+ case "users":
+ case "positions":
+ in.beginArray();
+ while (in.hasNext()) {
+ in.beginObject();
+ final BoardUserImpl.Builder builder = new BoardUserImpl.Builder();
+
+ while (in.hasNext()) {
+ switch (in.nextName()) {
+ case "name":
+ builder.name(in.nextString());
+ break;
+ case "uuid":
+ case "uniqueId":
+ builder.uniqueId(UUID.fromString(in.nextString()));
+ break;
+ case "result":
+ case "score":
+ if (in.peek() != JsonToken.NULL)
+ builder.result(in.nextDouble());
+ break;
+ }
+ }
+ in.endObject();
+ list.add(builder.build());
+ }
+ in.endArray();
+ break;
+ }
+ }
+ in.endObject();
+
+ final AbstractLeaderboard leaderboard = new AbstractLeaderboard(identifier, capacity) {};
+ leaderboard.addAll(list);
+
+ return leaderboard;
+ }
+}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardServiceImpl.java b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardServiceImpl.java
new file mode 100644
index 0000000..e088ad6
--- /dev/null
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardServiceImpl.java
@@ -0,0 +1,90 @@
+package me.tofpu.speedbridge.game.leaderboard;
+
+import me.tofpu.speedbridge.api.leaderboard.Leaderboard;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardService;
+import me.tofpu.speedbridge.api.leaderboard.LeaderboardType;
+import me.tofpu.speedbridge.api.user.User;
+import me.tofpu.speedbridge.data.DataManager;
+import me.tofpu.speedbridge.game.Game;
+import me.tofpu.speedbridge.game.leaderboard.impl.GlobalLeaderboard;
+import me.tofpu.speedbridge.game.leaderboard.impl.SeasonalLeaderboard;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+public class LeaderboardServiceImpl implements LeaderboardService {
+ private final List leaderboards = new ArrayList<>();
+ private File directory;
+
+ public LeaderboardServiceImpl() {
+ leaderboards.addAll(Arrays.asList(new GlobalLeaderboard(), new SeasonalLeaderboard()));
+ }
+
+ public void initialize(final File directory) {
+ this.directory = directory;
+
+ // TODO: automatic save here
+ Game.EXECUTOR.scheduleWithFixedDelay(this::save, 5, 5, TimeUnit.MINUTES);
+ }
+
+ @Override
+ public AbstractLeaderboard get(final LeaderboardType type) {
+ for (final AbstractLeaderboard leaderboard : leaderboards){
+ if (!leaderboard.identifier().equalsIgnoreCase(type.name())) continue;
+ return leaderboard;
+ }
+ return null;
+ }
+
+ public void check(final User user, final Predicate filter) {
+ for (final AbstractLeaderboard leaderboard : leaderboards){
+ if (filter != null && filter.test(leaderboard)) continue;
+ leaderboard.check(user);
+ }
+ }
+
+ @Override
+ public void compute(final Predicate filter, Consumer consumer){
+ for (final AbstractLeaderboard leaderboard : leaderboards){
+ if (filter != null && filter.test(leaderboard)) continue;
+ consumer.accept(leaderboard);
+ }
+ }
+
+ public void load() {
+ for (final File file : directory.listFiles()) {
+ final String name = file.getName();
+
+ if (!name.endsWith(".json")) continue;
+ try (final FileReader reader = new FileReader(file)) {
+ final AbstractLeaderboard leaderboard = DataManager.GSON.fromJson(reader, AbstractLeaderboard.class);
+
+ final LeaderboardType type = LeaderboardType.match(leaderboard.identifier());
+ if (type == null) continue;
+
+ get(type).addAll(leaderboard.positions());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void save() {
+ for (final AbstractLeaderboard leaderboard : leaderboards){
+ try (final FileWriter writer = new FileWriter(new File(directory, leaderboard.identifier() + ".json"))) {
+ writer.write(DataManager.GSON.toJson(leaderboard, AbstractLeaderboard.class));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public File directory() {
+ return directory;
+ }
+}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/impl/GlobalLeaderboard.java b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/impl/GlobalLeaderboard.java
new file mode 100644
index 0000000..2888df0
--- /dev/null
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/impl/GlobalLeaderboard.java
@@ -0,0 +1,34 @@
+package me.tofpu.speedbridge.game.leaderboard.impl;
+
+import me.tofpu.speedbridge.api.lobby.BoardUser;
+import me.tofpu.speedbridge.data.file.path.Path;
+import me.tofpu.speedbridge.game.leaderboard.AbstractLeaderboard;
+import me.tofpu.speedbridge.util.Util;
+
+public class GlobalLeaderboard extends AbstractLeaderboard {
+ public GlobalLeaderboard() {
+ super("Global", 10);
+ }
+
+ @Override
+ public String print() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append(Path.LEADERBOARD_HEADER.getValue());
+
+ for (int i = 0; i < positions().size(); i++) {
+ if (builder.capacity() != 1) builder.append("\n");
+ builder.append(parse(i));
+ }
+
+ return Util.colorize(builder.toString());
+ }
+
+ @Override
+ public String parse(final int slot) {
+ if (positions().size() <= slot) return "N/A";
+ final int position = slot + 1;
+ final BoardUser user = positions().get(slot);
+
+ return user == null ? "N/A" : Util.colorize(Util.WordReplacer.replace(Path.LEADERBOARD_STYLE.getValue(), new String[]{"{position}", "{name}", "{score}"}, position + "", user.name(), user.score() + ""));
+ }
+}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/impl/SeasonalLeaderboard.java b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/impl/SeasonalLeaderboard.java
new file mode 100644
index 0000000..38fd581
--- /dev/null
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/impl/SeasonalLeaderboard.java
@@ -0,0 +1,9 @@
+package me.tofpu.speedbridge.game.leaderboard.impl;
+
+import me.tofpu.speedbridge.game.leaderboard.AbstractLeaderboard;
+
+public class SeasonalLeaderboard extends AbstractLeaderboard {
+ public SeasonalLeaderboard() {
+ super("Seasonal", 10);
+ }
+}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/game/service/GameServiceImpl.java b/spigot/src/main/java/me/tofpu/speedbridge/game/service/GameServiceImpl.java
index 6628dc5..b48c553 100644
--- a/spigot/src/main/java/me/tofpu/speedbridge/game/service/GameServiceImpl.java
+++ b/spigot/src/main/java/me/tofpu/speedbridge/game/service/GameServiceImpl.java
@@ -11,6 +11,7 @@
import me.tofpu.speedbridge.api.user.UserService;
import me.tofpu.speedbridge.api.user.timer.Timer;
import me.tofpu.speedbridge.data.file.path.Path;
+import me.tofpu.speedbridge.game.leaderboard.LeaderboardServiceImpl;
import me.tofpu.speedbridge.game.runnable.GameRunnable;
import me.tofpu.speedbridge.island.mode.ModeManager;
import me.tofpu.speedbridge.user.properties.timer.TimerFactory;
@@ -29,16 +30,18 @@ public class GameServiceImpl implements GameService {
private final IslandService islandService;
private final UserService userService;
private final LobbyService lobbyService;
+ private final LeaderboardServiceImpl leaderboardService;
private final Map gameTimer;
private final Map gameChecker;
private final GameRunnable runnable;
- public GameServiceImpl(final Plugin plugin, final IslandService islandService, final UserService userService, final LobbyService lobbyService) {
+ public GameServiceImpl(final Plugin plugin, final IslandService islandService, final UserService userService, final LobbyService lobbyService, final LeaderboardServiceImpl leaderboardService) {
this.islandService = islandService;
this.userService = userService;
this.lobbyService = lobbyService;
+ this.leaderboardService = leaderboardService;
this.gameTimer = new HashMap<>();
this.gameChecker = new HashMap<>();
@@ -334,8 +337,9 @@ public void updateTimer(final User user) {
// replacing the old record with the player's current record
properties.timer(gameTimer);
+
// manually triggering the leaderboard
- lobbyService.getLeaderboard().check(user);
+ leaderboardService.check(user, null);
}
}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/lobby/LobbyServiceImpl.java b/spigot/src/main/java/me/tofpu/speedbridge/lobby/LobbyServiceImpl.java
new file mode 100644
index 0000000..d45bf00
--- /dev/null
+++ b/spigot/src/main/java/me/tofpu/speedbridge/lobby/LobbyServiceImpl.java
@@ -0,0 +1,47 @@
+package me.tofpu.speedbridge.lobby;
+
+import com.google.gson.Gson;
+import me.tofpu.speedbridge.api.lobby.LobbyService;
+import org.bukkit.Location;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class LobbyServiceImpl implements LobbyService {
+ private Location location;
+
+ @Override
+ public Location getLobbyLocation() {
+ return location;
+ }
+
+ @Override
+ public void setLobbyLocation(final Location location) {
+ this.location = location;
+ }
+
+ @Override
+ public boolean hasLobbyLocation() {
+ return location != null;
+ }
+
+ @Override
+ public void save(final Gson gson, final File lobbyFile) {
+ try (final FileWriter writer = new FileWriter(lobbyFile)) {
+ if (hasLobbyLocation()) writer.write(gson.toJson(getLobbyLocation(), Location.class));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void load(final Gson gson, final File lobbyFile) {
+ try (final FileReader reader = new FileReader(lobbyFile)) {
+ setLobbyLocation(gson.fromJson(reader, Location.class));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/lobby/leaderboard/LeaderboardImpl.java b/spigot/src/main/java/me/tofpu/speedbridge/lobby/leaderboard/LeaderboardImpl.java
deleted file mode 100644
index 6cfaf6a..0000000
--- a/spigot/src/main/java/me/tofpu/speedbridge/lobby/leaderboard/LeaderboardImpl.java
+++ /dev/null
@@ -1,144 +0,0 @@
-package me.tofpu.speedbridge.lobby.leaderboard;
-
-import com.google.common.collect.Lists;
-import me.tofpu.speedbridge.api.lobby.BoardUser;
-import me.tofpu.speedbridge.api.lobby.Leaderboard;
-import me.tofpu.speedbridge.api.user.User;
-import me.tofpu.speedbridge.api.user.timer.Timer;
-import me.tofpu.speedbridge.util.Util;
-import org.bukkit.Bukkit;
-import org.bukkit.entity.Player;
-import org.bukkit.plugin.Plugin;
-import org.bukkit.scheduler.BukkitTask;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
-public final class LeaderboardImpl implements Leaderboard {
- private final List mainLeaderboard;
- private final List cacheLeaderboard;
-
- private final int limitSize;
- private BukkitTask update;
-
- public LeaderboardImpl(final int limitSize) {
- this.limitSize = limitSize;
-
- mainLeaderboard = new ArrayList<>(limitSize);
- cacheLeaderboard = new ArrayList<>(limitSize);
- }
-
- @Override
- public void start(final Plugin plugin) {
- update = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
- mainLeaderboard.clear();
- final List users = sortGet();
- mainLeaderboard.addAll(users.subList(0, Math.min(users.size(), this.limitSize)));
- }, 0, 20 * 10);
- }
-
- @Override
- public void cancel() {
- this.update.cancel();
- }
-
- @Override
- public void check(final User user) {
- final Player player = Bukkit.getPlayer(user.uniqueId());
- if (player == null) return;
- final Timer timer = user.properties().timer();
-
- BoardUser boardUser = get(user.uniqueId());
- if (boardUser == null) {
- boardUser = new BoardUserImpl.Builder().name(player.getName()).uniqueId(user.uniqueId()).result(timer == null ? null : timer.result()).build();
- } else {
- if (boardUser.score() == null || boardUser.score() > timer.result()) {
- boardUser.score(timer.result());
- }
- }
- add(boardUser);
- }
-
- private List sortGet() {
- final List players = new ArrayList<>(getCacheLeaderboard());
- if (players.isEmpty()) return players;
-
- int max;
- for (int i = 0; i < players.size(); i++) {
- max = i;
- BoardUser playerMax = players.get(max);
- BoardUser playerJ;
-
- for (int j = i; j < players.size(); j++) {
- playerJ = players.get(j);
- if (playerJ.score() < playerMax.score()) {
- max = j;
- playerMax = players.get(max);
- }
- }
- //Swap biggest and current locations
- final BoardUser placeholder = players.get(i);
- players.set(i, playerMax);
- if (!placeholder.equals(playerMax)) players.set(max, placeholder);
- }
-
- return players;
- }
-
- public BoardUser get(final UUID uuid) {
- for (final BoardUser user : getCacheLeaderboard()) {
- if (user.uniqueId().equals(uuid)) return user;
- }
- return null;
- }
-
- public void add(final BoardUser boardUser) {
- if (boardUser == null || boardUser.score() == null) return;
- for (final BoardUser user : getCacheLeaderboard()) {
- if (user.equals(boardUser)) {
- if (user.score() > boardUser.score()) {
- user.score(boardUser.score());
- }
- return;
- }
- }
- cacheLeaderboard.add(boardUser);
- }
-
- @Override
- public void addAll(final List users) {
- for (final BoardUser user : users) {
- add(user);
- }
- }
-
- @Override
- public String print() {
- final StringBuilder builder = new StringBuilder();
- builder.append("&eLeaderboard");
-
- int length = 1;
- for (final BoardUser user : positions()) {
- builder.append("\n").append("&e").append(length).append(". ").append(user.name()).append(" &a(").append(user.score()).append(")");
- length++;
- }
-
- return Util.colorize(builder.toString());
- }
-
- @Override
- public String parse(final int slot){
- if (positions().size() <= slot) return "N/A";
- final BoardUser user = positions().get(slot);
- return user == null ? "N/A" : Util.colorize(user.name() + " &a(" + user.score() + ")");
- }
-
- public List positions() {
- return Lists.newArrayList(mainLeaderboard);
- }
-
- public List getCacheLeaderboard() {
- return Lists.newArrayList(cacheLeaderboard);
- }
-}
diff --git a/spigot/src/main/java/me/tofpu/speedbridge/lobby/service/LobbyServiceImpl.java b/spigot/src/main/java/me/tofpu/speedbridge/lobby/service/LobbyServiceImpl.java
deleted file mode 100644
index 560dce9..0000000
--- a/spigot/src/main/java/me/tofpu/speedbridge/lobby/service/LobbyServiceImpl.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package me.tofpu.speedbridge.lobby.service;
-
-import com.google.gson.Gson;
-import com.google.gson.reflect.TypeToken;
-import me.tofpu.speedbridge.api.lobby.BoardUser;
-import me.tofpu.speedbridge.api.lobby.Leaderboard;
-import me.tofpu.speedbridge.api.lobby.LobbyService;
-import me.tofpu.speedbridge.lobby.leaderboard.LeaderboardImpl;
-import org.bukkit.Location;
-
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.List;
-
-public class LobbyServiceImpl implements LobbyService {
- private final LeaderboardImpl leaderboard;
-
- private Location location;
-
- public LobbyServiceImpl() {
- this.leaderboard = new LeaderboardImpl(10);
- }
-
- @Override
- public Location getLobbyLocation() {
- return location;
- }
-
- @Override
- public void setLobbyLocation(final Location location) {
- this.location = location;
- }
-
- @Override
- public boolean hasLobbyLocation() {
- return location != null;
- }
-
- @Override
- public void save(final Gson gson, final File lobbyFile, final File leaderboardFile) {
- try (final FileWriter writer = new FileWriter(lobbyFile)) {
- if (hasLobbyLocation()) writer.write(gson.toJson(getLobbyLocation(), Location.class));
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- try (final FileWriter writer = new FileWriter(leaderboardFile)) {
- writer.write(gson.toJson(getLeaderboard().positions(), new TypeToken>() {
- }.getType()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- public void load(final Gson gson, final File lobbyFile, final File leaderboardFile) {
- try (final FileReader reader = new FileReader(lobbyFile)) {
- setLobbyLocation(gson.fromJson(reader, Location.class));
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- try (final FileReader reader = new FileReader(leaderboardFile)) {
- final List users = gson.fromJson(reader, new TypeToken>() {
- }.getType());
- if (users == null || users.isEmpty()) return;
- getLeaderboard().addAll(users);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- public Leaderboard getLeaderboard() {
- return leaderboard;
- }
-}
diff --git a/spigot/src/main/resources/settings.yml b/spigot/src/main/resources/settings.yml
index 360048f..08552f0 100644
--- a/spigot/src/main/resources/settings.yml
+++ b/spigot/src/main/resources/settings.yml
@@ -8,6 +8,18 @@ settings:
# block: WOOL:14 = RED WOOL
block: WOOL
+ leaderboard:
+ # the leaderboard size
+ size: 10
+
+ format:
+ # the message header
+ header: "&eLeaderboard"
+
+ # the message style
+ style: "&e{position}. {name} &a({score})"
+
+
mode:
default: hard
list: