Skip to content

Commit 9d9f5e9

Browse files
authored
Merge pull request #70 from request-plugins-for-free/dev
v1.2.5 - Spectate Feature & more
2 parents 4f998a0 + 4e54019 commit 9d9f5e9

File tree

16 files changed

+305
-62
lines changed

16 files changed

+305
-62
lines changed

api/src/main/java/me/tofpu/speedbridge/api/game/GameService.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ public interface GameService {
7272
*/
7373
Result join(final Player player, final Mode mode);
7474

75+
/**
76+
* This method will teleport the issuer to the target
77+
* to spectate in their island.
78+
* <p></p>
79+
* The issuer will also get notified when the target scores
80+
* and whether the target have beaten their
81+
* personal score or not.
82+
* <p></p>
83+
* The issuer will also get teleported back to the lobby
84+
* once the target leaves/disconnect from the island.
85+
*
86+
* @param issuer the one whom issued the command
87+
* @param target the one whom the issuer wants to spectate to
88+
*
89+
* @return the result of the action
90+
* <p>
91+
* FULL - If the issuer is playing
92+
* <p>
93+
* FAIL - if the target wasn't playing
94+
* <p>
95+
* SUCCESS - If the action was successful
96+
*/
97+
Result spectate(final Player issuer, final Player target);
98+
7599
/**
76100
* This method is for kicking the player out of the island and
77101
* teleporting them back to the lobby's location.

api/src/main/java/me/tofpu/speedbridge/api/user/User.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.tofpu.speedbridge.api.user;
22

3+
import org.bukkit.entity.Player;
4+
35
import java.util.UUID;
46

57
/**
@@ -15,4 +17,11 @@ public interface User {
1517
* @return the user properties
1618
*/
1719
UserProperties properties();
20+
21+
/**
22+
* Player instance of this user.
23+
*
24+
* @return a player instance of this user, null if they're offline
25+
*/
26+
Player player();
1827
}

spigot/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>me.tofpu</groupId>
1313
<artifactId>speedbridge</artifactId>
14-
<version>1.2.4</version>
14+
<version>1.2.5</version>
1515
<packaging>jar</packaging>
1616
<name>speedbridge</name>
1717

spigot/src/main/java/me/tofpu/speedbridge/command/sub/AdminCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ public void onReload(final CommandSender sender) {
208208
@Description("Lists the PlaceholderAPI extensions")
209209
@CommandPermission("island.info")
210210
public void onExpansion(final CommandSender sender) {
211-
final String format = " &6&l&m*&r &e%bridge_#name#%";
211+
final String format = " &6&l&m*&r &e%bridge_#name#% &6- &e#description#";
212212

213-
for (final String expansion : Util.toString(ExpansionType.values())) {
214-
Util.message(sender, format, new String[]{"#name#"}, false, expansion.toLowerCase(Locale.ROOT));
213+
for (final ExpansionType expansion : ExpansionType.values()) {
214+
Util.message(sender, format, new String[]{"#name#", "#description#"}, false, expansion.name().toLowerCase(Locale.ROOT).replace("_", "-"), expansion.description());
215215
}
216216
}
217217
}

spigot/src/main/java/me/tofpu/speedbridge/command/sub/MainCommand.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import me.tofpu.speedbridge.api.leaderboard.LeaderboardType;
1515
import me.tofpu.speedbridge.island.mode.ModeManager;
1616
import me.tofpu.speedbridge.util.Util;
17+
import org.bukkit.Bukkit;
1718
import org.bukkit.command.CommandSender;
1819
import org.bukkit.entity.Player;
1920

@@ -61,6 +62,39 @@ public void onJoin(final Player player, @Optional String arg) {
6162
onJoin(player, integer, mode);
6263
}
6364

65+
@Subcommand("spectate")
66+
@CommandAlias("spectate")
67+
@Syntax("<target>")
68+
@CommandCompletion("@players")
69+
@Description("Spectates a player")
70+
public void onSpectate(final Player issuer, final String arg) {
71+
final Player target = Bukkit.getPlayer(arg);
72+
if (target == null) {
73+
Util.message(issuer, Path.MESSAGES_SPECTATOR_UNKNOWN);
74+
return;
75+
}
76+
if (issuer.getName().equalsIgnoreCase(target.getName())) {
77+
Util.message(issuer, Path.MESSAGES_SPECTATOR_SELF);
78+
return;
79+
}
80+
final Result result = gameService.spectate(issuer, target);
81+
82+
final Path.Value<?> path;
83+
switch (result) {
84+
case FULL:
85+
path = Path.MESSAGES_SPECTATOR_SELF_PLAYING;
86+
break;
87+
case FAIL:
88+
path = Path.MESSAGES_SPECTATOR_TARGET;
89+
break;
90+
case SUCCESS:
91+
return;
92+
default:
93+
throw new IllegalStateException("Unexpected value: " + result);
94+
}
95+
Util.message(issuer, path);
96+
}
97+
6498
@Subcommand("leave")
6599
@CommandAlias("leave")
66100
@Description("Leaves the practice island")

spigot/src/main/java/me/tofpu/speedbridge/data/file/path/Path.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public class Path {
4949
public static final Value<String> MESSAGES_BEATEN_SCORE = new Value<>(MESSAGES + "beaten-score", "&aYou topped your old personal best score by %calu_score% seconds!", PathType.MESSAGES);
5050
public static final Value<String> MESSAGES_SCORED = new Value<>(MESSAGES + "scored", "&eYou scored &a%scored% &eseconds!", PathType.MESSAGES);
5151

52+
public static final Value<String> MESSAGES_SPECTATING = new Value<>(MESSAGES + "spectating", "&eYou are spectating &6%player%&e!", PathType.MESSAGES);
53+
public static final Value<String> MESSAGES_NOTIFY_SPECTATING = new Value<>(MESSAGES + "notify-spectating", "&6%player% &eis spectating you!", PathType.MESSAGES);
54+
public static final Value<String> MESSAGES_NO_LONGER_SPECTATING = new Value<>(MESSAGES + "no-longer-spectating", "&eYou're no longer spectating &6%player%!", PathType.MESSAGES);
55+
public static final Value<String> MESSAGES_NOTIFY_NOT_SPECTATING = new Value<>(MESSAGES + "notify-not-spectating", "&6%player% &eis no longer spectating you!", PathType.MESSAGES);
56+
57+
public static final Value<String> MESSAGES_SPECTATOR_BEATEN_SCORE = new Value<>(MESSAGES + "spectator-beaten-score", "&6%player% &etopped their personal score by &a%calu_score% &eseconds!", PathType.MESSAGES);
58+
public static final Value<String> MESSAGES_SPECTATOR_SCORED = new Value<>(MESSAGES + "spectator-scored", "&6%player% &escored &a%scored% &eseconds!", PathType.MESSAGES);
59+
60+
public static final Value<String> MESSAGES_SPECTATOR_UNKNOWN = new Value<>(MESSAGES + "spectate-unknown", "&eYou cannot spectate offline players!", PathType.MESSAGES);
61+
public static final Value<String> MESSAGES_SPECTATOR_SELF = new Value<>(MESSAGES + "spectate-self", "&eYou cannot spectate yourself!", PathType.MESSAGES);
62+
public static final Value<String> MESSAGES_SPECTATOR_SELF_PLAYING = new Value<>(MESSAGES + "spectate-self-playing", "&eYou cannot spectate while playing!", PathType.MESSAGES);
63+
public static final Value<String> MESSAGES_SPECTATOR_TARGET = new Value<>(MESSAGES + "spectate-target", "&eYou cannot spectate somebody who isn't playing!", PathType.MESSAGES);
64+
5265
public static List<Value<?>> values() {
5366
return VALUE_LIST;
5467
}

spigot/src/main/java/me/tofpu/speedbridge/expansion/BridgeExpansion.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
44
import me.tofpu.speedbridge.api.game.GameService;
5+
import me.tofpu.speedbridge.api.island.Island;
6+
import me.tofpu.speedbridge.api.island.IslandService;
57
import me.tofpu.speedbridge.api.leaderboard.Leaderboard;
68
import me.tofpu.speedbridge.api.leaderboard.LeaderboardService;
79
import me.tofpu.speedbridge.api.user.User;
@@ -13,18 +15,18 @@
1315
import org.bukkit.plugin.PluginDescriptionFile;
1416
import org.jetbrains.annotations.NotNull;
1517

16-
import java.util.Arrays;
17-
1818
public class BridgeExpansion extends PlaceholderExpansion {
1919
private final PluginDescriptionFile description;
2020

2121
private final UserService userService;
22+
private final IslandService islandService;
2223
private final GameService gameService;
2324
private final LeaderboardService leaderboardService;
2425

25-
public BridgeExpansion(final PluginDescriptionFile description, final UserService userService, final GameService gameService, final LeaderboardService leaderboardService) {
26+
public BridgeExpansion(final PluginDescriptionFile description, final UserService userService, final IslandService islandService, final GameService gameService, final LeaderboardService leaderboardService) {
2627
this.description = description;
2728
this.userService = userService;
29+
this.islandService = islandService;
2830
this.gameService = gameService;
2931
this.leaderboardService = leaderboardService;
3032
}
@@ -58,24 +60,33 @@ public boolean persist() {
5860
public String onPlaceholderRequest(Player player, @NotNull String params) {
5961
if (params.isEmpty()) return "";
6062
final String[] args = params.split("_");
61-
final ExpansionType type = ExpansionType.getMatch(args[0]);
63+
final ExpansionType type = ExpansionType.match(args[0]);
6264
if (type == null) return "";
6365

6466
final User user = userService.get(player.getUniqueId());
6567
boolean isNull = user == null;
6668

6769
final Timer timer;
70+
final Integer slot;
6871
switch (type) {
72+
case SCORE:
73+
if (isNull || (timer = user.properties().timer()) == null) return "N/A";
74+
return timer.result() + "";
75+
case BLOCKS:
76+
if (isNull || ((slot = user.properties().islandSlot()) == null)) return "0";
77+
final Island island = islandService.getIslandBySlot(slot);
78+
79+
if (island == null) {
80+
return "0";
81+
} else {
82+
return island.placedBlocks().size() + "";
83+
}
6984
case ISLAND:
70-
Integer slot;
7185
if (isNull || ((slot = user.properties().islandSlot()) == null)) return "Lobby";
7286
return slot + "";
7387
case LIVE_TIMER:
7488
if (isNull || (timer = gameService.getTimer(user)) == null) return "0";
7589
return Util.toSeconds(timer.start()) + "";
76-
case SCORE:
77-
if (isNull || (timer = user.properties().timer()) == null) return "N/A";
78-
return timer.result() + "";
7990
case LEADERBOARD:
8091
if (args.length <= 1) return null;
8192
final Leaderboard leaderboard = leaderboardService.get(LeaderboardType.GLOBAL);

spigot/src/main/java/me/tofpu/speedbridge/expansion/ExpansionType.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package me.tofpu.speedbridge.expansion;
22

33
public enum ExpansionType {
4-
SCORE, ISLAND, LIVE_TIMER, LEADERBOARD;
4+
SCORE("Displays your personal best score"), BLOCKS("Displays the amount of blocks you placed"), ISLAND("Displays the island slot you are in"), LIVE_TIMER("Displays a live timer of the game"), LEADERBOARD("Displays data from the leaderboard");
55

6-
public static ExpansionType getMatch(final String string) {
6+
private final String description;
7+
8+
ExpansionType(final String description) {
9+
this.description = description;
10+
}
11+
12+
public String description() {
13+
return description;
14+
}
15+
16+
public static ExpansionType match(final String string) {
717
for (final ExpansionType stage : ExpansionType.values()) {
818
if (stage.name().equalsIgnoreCase(string.toUpperCase().replace("-", "_"))) return stage;
919
}

spigot/src/main/java/me/tofpu/speedbridge/game/Game.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void initializePlaceholderApi() {
9898
if (!DependencyAPI.get("PlaceholderAPI").isAvailable()) return;
9999

100100
Util.isPlaceholderHooked = true;
101-
new BridgeExpansion(speedBridge.getDescription(), userService, gameService, leaderboardService).register();
101+
new BridgeExpansion(speedBridge.getDescription(), userService, islandService, gameService, leaderboardService).register();
102102
}
103103

104104
private void registerListeners(){
@@ -121,7 +121,6 @@ public void initialize() {
121121
this.userService.initialize(this.dataManager);
122122
this.islandService.initialize(this.dataManager);
123123

124-
leaderboardService.initialize(this.dataManager.getFiles()[3]);
125124

126125
// update checker async
127126
UpdateChecker.init(speedBridge, 95918).requestUpdateCheck().whenComplete((updateResult, throwable) -> {
@@ -144,6 +143,9 @@ public void initialize() {
144143
// reloading the configurations
145144
dataManager.reload();
146145

146+
// initializing the leaderboards
147+
leaderboardService.initialize(this.dataManager.getFiles()[3]);
148+
147149
// initializing the mode manager
148150
ModeManager.getModeManager().initialize();
149151

spigot/src/main/java/me/tofpu/speedbridge/game/leaderboard/LeaderboardServiceImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.tofpu.speedbridge.api.leaderboard.LeaderboardType;
66
import me.tofpu.speedbridge.api.user.User;
77
import me.tofpu.speedbridge.data.DataManager;
8+
import me.tofpu.speedbridge.data.file.path.Path;
89
import me.tofpu.speedbridge.game.Game;
910
import me.tofpu.speedbridge.game.leaderboard.impl.GlobalLeaderboard;
1011
import me.tofpu.speedbridge.game.leaderboard.impl.SeasonalLeaderboard;
@@ -18,17 +19,19 @@
1819
import java.util.function.Predicate;
1920

2021
public class LeaderboardServiceImpl implements LeaderboardService {
21-
private final List<AbstractLeaderboard> leaderboards = new ArrayList<>();
22+
private final List<AbstractLeaderboard> leaderboards;
2223
private File directory;
2324

2425
public LeaderboardServiceImpl() {
25-
leaderboards.addAll(Arrays.asList(new GlobalLeaderboard(), new SeasonalLeaderboard()));
26+
leaderboards = new ArrayList<>();
2627
}
2728

2829
public void initialize(final File directory) {
2930
this.directory = directory;
3031

31-
// TODO: automatic save here
32+
final int capacity = Path.LEADERBOARD_SIZE.getValue();
33+
leaderboards.addAll(Arrays.asList(new GlobalLeaderboard(capacity), new SeasonalLeaderboard(capacity)));
34+
3235
Game.EXECUTOR.scheduleWithFixedDelay(this::save, 5, 5, TimeUnit.MINUTES);
3336
}
3437

0 commit comments

Comments
 (0)