Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…eedBridge into main
  • Loading branch information
Tofpu committed Sep 14, 2021
2 parents 57b0906 + a1d29e9 commit c39dd56
Show file tree
Hide file tree
Showing 26 changed files with 603 additions and 295 deletions.
16 changes: 16 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
<target>6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<Leaderboard> filter, Consumer<Leaderboard> consumer);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 0 additions & 23 deletions api/src/main/java/me/tofpu/speedbridge/api/lobby/Leaderboard.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion spigot/src/main/java/me/tofpu/speedbridge/SpeedBridge.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@
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;
import org.bukkit.entity.Player;

@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
Expand Down Expand Up @@ -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")
Expand Down
47 changes: 33 additions & 14 deletions spigot/src/main/java/me/tofpu/speedbridge/data/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@
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;
import org.bukkit.configuration.file.YamlConfiguration;
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;

Expand All @@ -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<List<BoardUser>>() {}.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);
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class Path {
public static final Value<Boolean> SETTINGS_TELEPORT = new Value<>(SETTINGS + "teleport", true, PathType.SETTINGS);
public static final Value<String> SETTINGS_BLOCK = new Value<>(SETTINGS + "block", "WOOL", PathType.SETTINGS);

private static final String LEADERBOARD = SETTINGS + "leaderboard.";
public static final Value<Integer> LEADERBOARD_SIZE = new Value<>(LEADERBOARD + "size", 10, PathType.SETTINGS);
public static final Value<String> LEADERBOARD_HEADER = new Value<>(LEADERBOARD + "header", "&eLeaderboard", PathType.SETTINGS);
public static final Value<String> LEADERBOARD_STYLE = new Value<>(LEADERBOARD + "style", "&e{position}. {name} &a({score})", PathType.SETTINGS);

public static final Value<String> MESSAGES_JOINED = new Value<>(MESSAGES + "joined", "&aGood Luck!", PathType.MESSAGES);
public static final Value<String> MESSAGES_LEFT = new Value<>(MESSAGES + "left", "", PathType.MESSAGES);
public static final Value<String> MESSAGES_NO_AVAILABLE = new Value<>(MESSAGES + "no-available", "&cThere is no available island right now; try again later!", PathType.MESSAGES);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
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;
import org.bukkit.event.Listener;
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)
Expand All @@ -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);
}
}
Loading

0 comments on commit c39dd56

Please sign in to comment.