Gradle-based plugin targeting the latest Paper API (26.2), with configurable races (laps, checkpoints, lobby/start, min players, countdown), a /boatparty command suite, and lap/checkpoint tracking via player movement. Includes Gitea Actions CI to build with JDK 25. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package us.tss3.boatparty;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import us.tss3.boatparty.command.BoatPartyCommand;
|
||||
import us.tss3.boatparty.game.RaceManager;
|
||||
import us.tss3.boatparty.listener.RaceListener;
|
||||
|
||||
public final class BoatPartyPlugin extends JavaPlugin {
|
||||
|
||||
private RaceManager raceManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
|
||||
this.raceManager = new RaceManager(this);
|
||||
this.raceManager.load();
|
||||
|
||||
BoatPartyCommand commandExecutor = new BoatPartyCommand(this, raceManager);
|
||||
var command = getCommand("boatparty");
|
||||
if (command != null) {
|
||||
command.setExecutor(commandExecutor);
|
||||
command.setTabCompleter(commandExecutor);
|
||||
}
|
||||
|
||||
getServer().getPluginManager().registerEvents(new RaceListener(this, raceManager), this);
|
||||
|
||||
getLogger().info("BoatParty enabled - " + raceManager.getRaces().size() + " race(s) loaded.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if (raceManager != null) {
|
||||
raceManager.stopAllRaces();
|
||||
raceManager.save();
|
||||
}
|
||||
}
|
||||
|
||||
public RaceManager getRaceManager() {
|
||||
return raceManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
package us.tss3.boatparty.command;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.tss3.boatparty.BoatPartyPlugin;
|
||||
import us.tss3.boatparty.game.Race;
|
||||
import us.tss3.boatparty.game.RaceManager;
|
||||
import us.tss3.boatparty.game.RaceState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class BoatPartyCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
private static final List<String> SUBCOMMANDS = List.of(
|
||||
"create", "delete", "setlobby", "setstart", "addcheckpoint", "removecheckpoint",
|
||||
"setlaps", "setminplayers", "setcountdown", "setradius", "join", "leave",
|
||||
"start", "stop", "list", "info");
|
||||
|
||||
private final BoatPartyPlugin plugin;
|
||||
private final RaceManager raceManager;
|
||||
|
||||
public BoatPartyCommand(BoatPartyPlugin plugin, RaceManager raceManager) {
|
||||
this.plugin = plugin;
|
||||
this.raceManager = raceManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
sendHelp(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
String sub = args[0].toLowerCase(Locale.ROOT);
|
||||
switch (sub) {
|
||||
case "create" -> handleCreate(sender, args);
|
||||
case "delete" -> handleDelete(sender, args);
|
||||
case "setlobby" -> handleSetLobby(sender, args);
|
||||
case "setstart" -> handleSetStart(sender, args);
|
||||
case "addcheckpoint" -> handleAddCheckpoint(sender, args);
|
||||
case "removecheckpoint" -> handleRemoveCheckpoint(sender, args);
|
||||
case "setlaps" -> handleSetLaps(sender, args);
|
||||
case "setminplayers" -> handleSetMinPlayers(sender, args);
|
||||
case "setcountdown" -> handleSetCountdown(sender, args);
|
||||
case "setradius" -> handleSetRadius(sender, args);
|
||||
case "join" -> handleJoin(sender, args);
|
||||
case "leave" -> handleLeave(sender);
|
||||
case "start" -> handleStart(sender, args);
|
||||
case "stop" -> handleStop(sender, args);
|
||||
case "list" -> handleList(sender);
|
||||
case "info" -> handleInfo(sender, args);
|
||||
default -> sendHelp(sender);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void handleCreate(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
if (args.length < 2) {
|
||||
msg(sender, "Usage: /boatparty create <name>");
|
||||
return;
|
||||
}
|
||||
String name = args[1];
|
||||
if (raceManager.getRace(name) != null) {
|
||||
msg(sender, "A race named '" + name + "' already exists.");
|
||||
return;
|
||||
}
|
||||
raceManager.createRace(name);
|
||||
raceManager.save();
|
||||
msg(sender, "Created race '" + name + "'. Now set lobby, start, and checkpoints.");
|
||||
}
|
||||
|
||||
private void handleDelete(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
if (args.length < 2) {
|
||||
msg(sender, "Usage: /boatparty delete <name>");
|
||||
return;
|
||||
}
|
||||
if (raceManager.deleteRace(args[1])) {
|
||||
raceManager.save();
|
||||
msg(sender, "Deleted race '" + args[1] + "'.");
|
||||
} else {
|
||||
msg(sender, "No such race.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSetLobby(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Player player = requirePlayer(sender);
|
||||
if (player == null) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
race.setLobby(player.getLocation());
|
||||
raceManager.save();
|
||||
msg(sender, "Lobby set for race '" + race.getName() + "'.");
|
||||
}
|
||||
|
||||
private void handleSetStart(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Player player = requirePlayer(sender);
|
||||
if (player == null) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
race.setStartLocation(player.getLocation());
|
||||
raceManager.save();
|
||||
msg(sender, "Start location set for race '" + race.getName() + "'.");
|
||||
}
|
||||
|
||||
private void handleAddCheckpoint(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Player player = requirePlayer(sender);
|
||||
if (player == null) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
race.addCheckpoint(player.getLocation());
|
||||
raceManager.save();
|
||||
msg(sender, "Checkpoint #" + race.getCheckpoints().size() + " added to '" + race.getName() + "'.");
|
||||
}
|
||||
|
||||
private void handleRemoveCheckpoint(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
if (race.removeLastCheckpoint()) {
|
||||
raceManager.save();
|
||||
msg(sender, "Removed last checkpoint from '" + race.getName() + "'.");
|
||||
} else {
|
||||
msg(sender, "Race '" + race.getName() + "' has no checkpoints.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSetLaps(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
if (args.length < 3) {
|
||||
msg(sender, "Usage: /boatparty setlaps <name> <laps>");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int laps = Integer.parseInt(args[2]);
|
||||
if (laps < 1) throw new NumberFormatException();
|
||||
race.setLaps(laps);
|
||||
raceManager.save();
|
||||
msg(sender, "Laps for '" + race.getName() + "' set to " + laps + ".");
|
||||
} catch (NumberFormatException e) {
|
||||
msg(sender, "Laps must be a positive integer.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSetMinPlayers(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
if (args.length < 3) {
|
||||
msg(sender, "Usage: /boatparty setminplayers <name> <count>");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int count = Integer.parseInt(args[2]);
|
||||
if (count < 1) throw new NumberFormatException();
|
||||
race.setMinPlayers(count);
|
||||
raceManager.save();
|
||||
msg(sender, "Minimum players for '" + race.getName() + "' set to " + count + ".");
|
||||
} catch (NumberFormatException e) {
|
||||
msg(sender, "Value must be a positive integer.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSetCountdown(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
if (args.length < 3) {
|
||||
msg(sender, "Usage: /boatparty setcountdown <name> <seconds>");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int seconds = Integer.parseInt(args[2]);
|
||||
if (seconds < 0) throw new NumberFormatException();
|
||||
race.setCountdownSeconds(seconds);
|
||||
raceManager.save();
|
||||
msg(sender, "Countdown for '" + race.getName() + "' set to " + seconds + "s.");
|
||||
} catch (NumberFormatException e) {
|
||||
msg(sender, "Value must be a non-negative integer.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSetRadius(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
if (args.length < 3) {
|
||||
msg(sender, "Usage: /boatparty setradius <name> <blocks>");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
double radius = Double.parseDouble(args[2]);
|
||||
if (radius <= 0) throw new NumberFormatException();
|
||||
race.setCheckpointRadius(radius);
|
||||
raceManager.save();
|
||||
msg(sender, "Checkpoint radius for '" + race.getName() + "' set to " + radius + ".");
|
||||
} catch (NumberFormatException e) {
|
||||
msg(sender, "Value must be a positive number.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleJoin(CommandSender sender, String[] args) {
|
||||
Player player = requirePlayer(sender);
|
||||
if (player == null) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
|
||||
if (!race.isReady()) {
|
||||
msg(sender, "Race '" + race.getName() + "' is not fully configured yet.");
|
||||
return;
|
||||
}
|
||||
if (race.getState() != RaceState.WAITING) {
|
||||
msg(sender, "Race '" + race.getName() + "' is not accepting new players right now.");
|
||||
return;
|
||||
}
|
||||
Race existing = raceManager.getRaceOf(player);
|
||||
if (existing != null) {
|
||||
msg(sender, "You are already in race '" + existing.getName() + "'. Leave it first.");
|
||||
return;
|
||||
}
|
||||
race.addParticipant(player.getUniqueId());
|
||||
player.teleport(race.getLobby());
|
||||
broadcast(race, player.getName() + " joined the race! (" + race.getParticipants().size() + " players)");
|
||||
|
||||
if (race.getParticipants().size() >= race.getMinPlayers()) {
|
||||
race.startCountdown(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLeave(CommandSender sender) {
|
||||
Player player = requirePlayer(sender);
|
||||
if (player == null) return;
|
||||
Race race = raceManager.getRaceOf(player);
|
||||
if (race == null) {
|
||||
msg(sender, "You are not in a race.");
|
||||
return;
|
||||
}
|
||||
race.removeParticipant(player.getUniqueId());
|
||||
msg(sender, "You left race '" + race.getName() + "'.");
|
||||
if (race.getState() == RaceState.COUNTDOWN && race.getParticipants().size() < race.getMinPlayers()) {
|
||||
race.cancelCountdown();
|
||||
race.setState(RaceState.WAITING);
|
||||
broadcast(race, "Not enough players, countdown cancelled.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStart(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
if (!race.isReady()) {
|
||||
msg(sender, "Race is not fully configured (need lobby, start, and at least one checkpoint).");
|
||||
return;
|
||||
}
|
||||
if (race.getState() != RaceState.WAITING) {
|
||||
msg(sender, "Race is already starting or running.");
|
||||
return;
|
||||
}
|
||||
if (race.getParticipants().isEmpty()) {
|
||||
msg(sender, "No players have joined this race yet.");
|
||||
return;
|
||||
}
|
||||
race.startCountdown(plugin);
|
||||
msg(sender, "Countdown started for '" + race.getName() + "'.");
|
||||
}
|
||||
|
||||
private void handleStop(CommandSender sender, String[] args) {
|
||||
if (!requirePermission(sender, "boatparty.admin")) return;
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
broadcast(race, "The race has been stopped by an admin.");
|
||||
race.reset();
|
||||
msg(sender, "Race '" + race.getName() + "' stopped and reset.");
|
||||
}
|
||||
|
||||
private void handleList(CommandSender sender) {
|
||||
if (raceManager.getRaces().isEmpty()) {
|
||||
msg(sender, "There are no races configured.");
|
||||
return;
|
||||
}
|
||||
msg(sender, "Races: " + raceManager.getRaces().keySet().stream().collect(Collectors.joining(", ")));
|
||||
}
|
||||
|
||||
private void handleInfo(CommandSender sender, String[] args) {
|
||||
Race race = requireRace(sender, args, 1);
|
||||
if (race == null) return;
|
||||
msg(sender, "--- " + race.getName() + " ---");
|
||||
msg(sender, "State: " + race.getState());
|
||||
msg(sender, "Players: " + race.getParticipants().size() + " (min " + race.getMinPlayers() + ")");
|
||||
msg(sender, "Laps: " + race.getLaps() + " | Checkpoints: " + race.getCheckpoints().size());
|
||||
msg(sender, "Ready: " + race.isReady());
|
||||
}
|
||||
|
||||
private void sendHelp(CommandSender sender) {
|
||||
List<String> lines = List.of(
|
||||
"&b&lBoatParty &7- ice boat racing",
|
||||
"&7/boatparty create <name>",
|
||||
"&7/boatparty delete <name>",
|
||||
"&7/boatparty setlobby|setstart <name>",
|
||||
"&7/boatparty addcheckpoint|removecheckpoint <name>",
|
||||
"&7/boatparty setlaps|setminplayers|setcountdown|setradius <name> <value>",
|
||||
"&7/boatparty join|leave <name>",
|
||||
"&7/boatparty start|stop <name>",
|
||||
"&7/boatparty list",
|
||||
"&7/boatparty info <name>");
|
||||
for (String line : lines) {
|
||||
sender.sendMessage(Component.text(line.replace("&", "§")));
|
||||
}
|
||||
}
|
||||
|
||||
private Race requireRace(CommandSender sender, String[] args, int index) {
|
||||
if (args.length <= index) {
|
||||
msg(sender, "You must specify a race name.");
|
||||
return null;
|
||||
}
|
||||
Race race = raceManager.getRace(args[index]);
|
||||
if (race == null) {
|
||||
msg(sender, "No race named '" + args[index] + "' exists.");
|
||||
}
|
||||
return race;
|
||||
}
|
||||
|
||||
private Player requirePlayer(CommandSender sender) {
|
||||
if (sender instanceof Player player) {
|
||||
return player;
|
||||
}
|
||||
msg(sender, "This command can only be used by a player.");
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean requirePermission(CommandSender sender, String permission) {
|
||||
if (sender.hasPermission(permission)) {
|
||||
return true;
|
||||
}
|
||||
msg(sender, "You do not have permission to do that.");
|
||||
return false;
|
||||
}
|
||||
|
||||
private void broadcast(Race race, String message) {
|
||||
for (var uuid : race.getParticipants()) {
|
||||
Player player = plugin.getServer().getPlayer(uuid);
|
||||
if (player != null) {
|
||||
msg(player, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void msg(CommandSender sender, String text) {
|
||||
sender.sendMessage(Component.text("[BoatParty] ", NamedTextColor.AQUA)
|
||||
.append(Component.text(text, NamedTextColor.WHITE)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||
if (args.length == 1) {
|
||||
String prefix = args[0].toLowerCase(Locale.ROOT);
|
||||
return SUBCOMMANDS.stream().filter(s -> s.startsWith(prefix)).collect(Collectors.toList());
|
||||
}
|
||||
if (args.length == 2 && !args[0].equalsIgnoreCase("create")) {
|
||||
String prefix = args[1].toLowerCase(Locale.ROOT);
|
||||
return raceManager.getRaces().keySet().stream()
|
||||
.filter(s -> s.startsWith(prefix))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package us.tss3.boatparty.game;
|
||||
|
||||
public final class PlayerProgress {
|
||||
|
||||
private int lap = 0;
|
||||
private int nextCheckpointIndex = 0;
|
||||
private long startTimeMillis;
|
||||
private long finishTimeMillis = -1;
|
||||
private boolean finished = false;
|
||||
|
||||
public PlayerProgress(long startTimeMillis) {
|
||||
this.startTimeMillis = startTimeMillis;
|
||||
}
|
||||
|
||||
public int getLap() {
|
||||
return lap;
|
||||
}
|
||||
|
||||
public void incrementLap() {
|
||||
this.lap++;
|
||||
}
|
||||
|
||||
public int getNextCheckpointIndex() {
|
||||
return nextCheckpointIndex;
|
||||
}
|
||||
|
||||
public void setNextCheckpointIndex(int nextCheckpointIndex) {
|
||||
this.nextCheckpointIndex = nextCheckpointIndex;
|
||||
}
|
||||
|
||||
public long getStartTimeMillis() {
|
||||
return startTimeMillis;
|
||||
}
|
||||
|
||||
public void setStartTimeMillis(long startTimeMillis) {
|
||||
this.startTimeMillis = startTimeMillis;
|
||||
}
|
||||
|
||||
public long getFinishTimeMillis() {
|
||||
return finishTimeMillis;
|
||||
}
|
||||
|
||||
public void setFinishTimeMillis(long finishTimeMillis) {
|
||||
this.finishTimeMillis = finishTimeMillis;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public void setFinished(boolean finished) {
|
||||
this.finished = finished;
|
||||
}
|
||||
|
||||
public long elapsedMillis() {
|
||||
long end = finished ? finishTimeMillis : System.currentTimeMillis();
|
||||
return Math.max(0, end - startTimeMillis);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package us.tss3.boatparty.game;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.boss.BarColor;
|
||||
import org.bukkit.boss.BarStyle;
|
||||
import org.bukkit.boss.BossBar;
|
||||
import org.bukkit.entity.Boat;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.title.Title;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class Race {
|
||||
|
||||
private final String name;
|
||||
private Location lobby;
|
||||
private Location startLocation;
|
||||
private final List<Location> checkpoints = new ArrayList<>();
|
||||
private double checkpointRadius = 3.0;
|
||||
private int laps = 3;
|
||||
private int minPlayers = 2;
|
||||
private int countdownSeconds = 10;
|
||||
|
||||
private RaceState state = RaceState.WAITING;
|
||||
private final List<UUID> participants = new ArrayList<>();
|
||||
private final Map<UUID, PlayerProgress> progress = new LinkedHashMap<>();
|
||||
private final List<UUID> finishOrder = new ArrayList<>();
|
||||
|
||||
private BukkitTask countdownTask;
|
||||
private BossBar countdownBar;
|
||||
|
||||
public Race(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Location getLobby() {
|
||||
return lobby;
|
||||
}
|
||||
|
||||
public void setLobby(Location lobby) {
|
||||
this.lobby = lobby;
|
||||
}
|
||||
|
||||
public Location getStartLocation() {
|
||||
return startLocation;
|
||||
}
|
||||
|
||||
public void setStartLocation(Location startLocation) {
|
||||
this.startLocation = startLocation;
|
||||
}
|
||||
|
||||
public List<Location> getCheckpoints() {
|
||||
return checkpoints;
|
||||
}
|
||||
|
||||
public void addCheckpoint(Location location) {
|
||||
checkpoints.add(location);
|
||||
}
|
||||
|
||||
public boolean removeLastCheckpoint() {
|
||||
if (checkpoints.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
checkpoints.remove(checkpoints.size() - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
public double getCheckpointRadius() {
|
||||
return checkpointRadius;
|
||||
}
|
||||
|
||||
public void setCheckpointRadius(double checkpointRadius) {
|
||||
this.checkpointRadius = checkpointRadius;
|
||||
}
|
||||
|
||||
public int getLaps() {
|
||||
return laps;
|
||||
}
|
||||
|
||||
public void setLaps(int laps) {
|
||||
this.laps = laps;
|
||||
}
|
||||
|
||||
public int getMinPlayers() {
|
||||
return minPlayers;
|
||||
}
|
||||
|
||||
public void setMinPlayers(int minPlayers) {
|
||||
this.minPlayers = minPlayers;
|
||||
}
|
||||
|
||||
public int getCountdownSeconds() {
|
||||
return countdownSeconds;
|
||||
}
|
||||
|
||||
public void setCountdownSeconds(int countdownSeconds) {
|
||||
this.countdownSeconds = countdownSeconds;
|
||||
}
|
||||
|
||||
public RaceState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(RaceState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public List<UUID> getParticipants() {
|
||||
return participants;
|
||||
}
|
||||
|
||||
public Map<UUID, PlayerProgress> getProgress() {
|
||||
return progress;
|
||||
}
|
||||
|
||||
public List<UUID> getFinishOrder() {
|
||||
return finishOrder;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return lobby != null && startLocation != null && checkpoints.size() >= 1;
|
||||
}
|
||||
|
||||
public boolean addParticipant(UUID uuid) {
|
||||
if (state != RaceState.WAITING) {
|
||||
return false;
|
||||
}
|
||||
if (participants.contains(uuid)) {
|
||||
return false;
|
||||
}
|
||||
participants.add(uuid);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean removeParticipant(UUID uuid) {
|
||||
progress.remove(uuid);
|
||||
finishOrder.remove(uuid);
|
||||
return participants.remove(uuid);
|
||||
}
|
||||
|
||||
public boolean hasParticipant(UUID uuid) {
|
||||
return participants.contains(uuid);
|
||||
}
|
||||
|
||||
public void startCountdown(us.tss3.boatparty.BoatPartyPlugin plugin) {
|
||||
if (state != RaceState.WAITING) {
|
||||
return;
|
||||
}
|
||||
state = RaceState.COUNTDOWN;
|
||||
countdownBar = Bukkit.createBossBar("BoatParty starting...", BarColor.YELLOW, BarStyle.SOLID);
|
||||
for (UUID uuid : participants) {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null) {
|
||||
countdownBar.addPlayer(player);
|
||||
}
|
||||
}
|
||||
|
||||
countdownTask = new BukkitRunnable() {
|
||||
int remaining = countdownSeconds;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (remaining <= 0) {
|
||||
cancel();
|
||||
countdownBar.removeAll();
|
||||
begin(plugin);
|
||||
return;
|
||||
}
|
||||
countdownBar.setProgress(Math.max(0.0, Math.min(1.0, (double) remaining / countdownSeconds)));
|
||||
countdownBar.setTitle("BoatParty starting in " + remaining + "...");
|
||||
for (UUID uuid : participants) {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null) {
|
||||
player.showTitle(Title.title(
|
||||
Component.text(String.valueOf(remaining), NamedTextColor.AQUA),
|
||||
Component.text("Get ready to race!", NamedTextColor.GRAY),
|
||||
Title.Times.times(Duration.ZERO, Duration.ofSeconds(1), Duration.ZERO)));
|
||||
player.playSound(player.getLocation(), org.bukkit.Sound.BLOCK_NOTE_BLOCK_HAT, 1f, 1f);
|
||||
}
|
||||
}
|
||||
remaining--;
|
||||
}
|
||||
}.runTaskTimer(plugin, 0L, 20L);
|
||||
}
|
||||
|
||||
public void cancelCountdown() {
|
||||
if (countdownTask != null) {
|
||||
countdownTask.cancel();
|
||||
countdownTask = null;
|
||||
}
|
||||
if (countdownBar != null) {
|
||||
countdownBar.removeAll();
|
||||
countdownBar = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void begin(us.tss3.boatparty.BoatPartyPlugin plugin) {
|
||||
state = RaceState.RUNNING;
|
||||
long now = System.currentTimeMillis();
|
||||
int index = 0;
|
||||
for (UUID uuid : participants) {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null) {
|
||||
continue;
|
||||
}
|
||||
progress.put(uuid, new PlayerProgress(now));
|
||||
|
||||
Location spawn = startLocation.clone().add((index % 5) * 1.5 - 3, 0, (index / 5) * 2.0);
|
||||
spawn.setYaw(startLocation.getYaw());
|
||||
spawn.setPitch(startLocation.getPitch());
|
||||
player.teleport(spawn);
|
||||
|
||||
Boat boat = spawn.getWorld().spawn(spawn, Boat.class);
|
||||
boat.addPassenger(player);
|
||||
|
||||
player.showTitle(Title.title(
|
||||
Component.text("GO!", NamedTextColor.GREEN),
|
||||
Component.text("Lap 1 / " + laps, NamedTextColor.GRAY)));
|
||||
player.playSound(player.getLocation(), org.bukkit.Sound.ENTITY_FIREWORK_ROCKET_LAUNCH, 1f, 1f);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
cancelCountdown();
|
||||
state = RaceState.WAITING;
|
||||
participants.clear();
|
||||
progress.clear();
|
||||
finishOrder.clear();
|
||||
}
|
||||
|
||||
public List<UUID> getStandings() {
|
||||
return finishOrder.stream().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package us.tss3.boatparty.game;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.tss3.boatparty.BoatPartyPlugin;
|
||||
import us.tss3.boatparty.util.LocationUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class RaceManager {
|
||||
|
||||
private final BoatPartyPlugin plugin;
|
||||
private final File dataFile;
|
||||
private final Map<String, Race> races = new LinkedHashMap<>();
|
||||
|
||||
public RaceManager(BoatPartyPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.dataFile = new File(plugin.getDataFolder(), "races.yml");
|
||||
}
|
||||
|
||||
public Map<String, Race> getRaces() {
|
||||
return races;
|
||||
}
|
||||
|
||||
public Race getRace(String name) {
|
||||
return races.get(name.toLowerCase());
|
||||
}
|
||||
|
||||
public Race createRace(String name) {
|
||||
Race race = new Race(name);
|
||||
races.put(name.toLowerCase(), race);
|
||||
return race;
|
||||
}
|
||||
|
||||
public boolean deleteRace(String name) {
|
||||
Race race = races.remove(name.toLowerCase());
|
||||
if (race != null) {
|
||||
race.reset();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Race getRaceOf(Player player) {
|
||||
for (Race race : races.values()) {
|
||||
if (race.hasParticipant(player.getUniqueId())) {
|
||||
return race;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void stopAllRaces() {
|
||||
for (Race race : races.values()) {
|
||||
race.reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (!dataFile.exists()) {
|
||||
return;
|
||||
}
|
||||
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(dataFile);
|
||||
ConfigurationSection racesSection = yaml.getConfigurationSection("races");
|
||||
if (racesSection == null) {
|
||||
return;
|
||||
}
|
||||
for (String key : racesSection.getKeys(false)) {
|
||||
ConfigurationSection rs = racesSection.getConfigurationSection(key);
|
||||
if (rs == null) {
|
||||
continue;
|
||||
}
|
||||
Race race = new Race(key);
|
||||
race.setLobby(LocationUtil.read(rs, "lobby"));
|
||||
race.setStartLocation(LocationUtil.read(rs, "start"));
|
||||
race.setLaps(rs.getInt("laps", 3));
|
||||
race.setMinPlayers(rs.getInt("min-players", 2));
|
||||
race.setCountdownSeconds(rs.getInt("countdown-seconds", 10));
|
||||
race.setCheckpointRadius(rs.getDouble("checkpoint-radius", 3.0));
|
||||
|
||||
ConfigurationSection cpSection = rs.getConfigurationSection("checkpoints");
|
||||
if (cpSection != null) {
|
||||
int i = 0;
|
||||
while (cpSection.contains(String.valueOf(i))) {
|
||||
Location loc = LocationUtil.read(cpSection, String.valueOf(i));
|
||||
if (loc != null) {
|
||||
race.addCheckpoint(loc);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
races.put(key, race);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
ConfigurationSection racesSection = yaml.createSection("races");
|
||||
for (Race race : races.values()) {
|
||||
ConfigurationSection rs = racesSection.createSection(race.getName());
|
||||
if (race.getLobby() != null) {
|
||||
LocationUtil.write(rs, "lobby", race.getLobby());
|
||||
}
|
||||
if (race.getStartLocation() != null) {
|
||||
LocationUtil.write(rs, "start", race.getStartLocation());
|
||||
}
|
||||
rs.set("laps", race.getLaps());
|
||||
rs.set("min-players", race.getMinPlayers());
|
||||
rs.set("countdown-seconds", race.getCountdownSeconds());
|
||||
rs.set("checkpoint-radius", race.getCheckpointRadius());
|
||||
|
||||
ConfigurationSection cpSection = rs.createSection("checkpoints");
|
||||
for (int i = 0; i < race.getCheckpoints().size(); i++) {
|
||||
LocationUtil.write(cpSection, String.valueOf(i), race.getCheckpoints().get(i));
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!plugin.getDataFolder().exists()) {
|
||||
plugin.getDataFolder().mkdirs();
|
||||
}
|
||||
yaml.save(dataFile);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().warning("Failed to save races.yml: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package us.tss3.boatparty.game;
|
||||
|
||||
public enum RaceState {
|
||||
WAITING,
|
||||
COUNTDOWN,
|
||||
RUNNING,
|
||||
FINISHED
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package us.tss3.boatparty.listener;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.entity.Boat;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.vehicle.VehicleExitEvent;
|
||||
import us.tss3.boatparty.BoatPartyPlugin;
|
||||
import us.tss3.boatparty.game.PlayerProgress;
|
||||
import us.tss3.boatparty.game.Race;
|
||||
import us.tss3.boatparty.game.RaceManager;
|
||||
import us.tss3.boatparty.game.RaceState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class RaceListener implements Listener {
|
||||
|
||||
private final BoatPartyPlugin plugin;
|
||||
private final RaceManager raceManager;
|
||||
|
||||
public RaceListener(BoatPartyPlugin plugin, RaceManager raceManager) {
|
||||
this.plugin = plugin;
|
||||
this.raceManager = raceManager;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onMove(PlayerMoveEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Race race = raceManager.getRaceOf(player);
|
||||
if (race == null || race.getState() != RaceState.RUNNING) {
|
||||
return;
|
||||
}
|
||||
PlayerProgress progress = race.getProgress().get(player.getUniqueId());
|
||||
if (progress == null || progress.isFinished()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<org.bukkit.Location> checkpoints = race.getCheckpoints();
|
||||
int nextIndex = progress.getNextCheckpointIndex();
|
||||
if (nextIndex >= checkpoints.size()) {
|
||||
return;
|
||||
}
|
||||
org.bukkit.Location checkpoint = checkpoints.get(nextIndex);
|
||||
if (checkpoint.getWorld() == null || !checkpoint.getWorld().equals(player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
double radiusSq = race.getCheckpointRadius() * race.getCheckpointRadius();
|
||||
if (player.getLocation().distanceSquared(checkpoint) > radiusSq) {
|
||||
return;
|
||||
}
|
||||
|
||||
int newIndex = nextIndex + 1;
|
||||
if (newIndex >= checkpoints.size()) {
|
||||
progress.incrementLap();
|
||||
progress.setNextCheckpointIndex(0);
|
||||
if (progress.getLap() >= race.getLaps()) {
|
||||
finishPlayer(race, player, progress);
|
||||
} else {
|
||||
player.sendActionBar(Component.text(
|
||||
"Lap " + (progress.getLap() + 1) + " / " + race.getLaps(),
|
||||
NamedTextColor.AQUA));
|
||||
player.playSound(player.getLocation(), org.bukkit.Sound.ENTITY_PLAYER_LEVELUP, 0.6f, 1.6f);
|
||||
}
|
||||
} else {
|
||||
progress.setNextCheckpointIndex(newIndex);
|
||||
player.sendActionBar(Component.text(
|
||||
"Checkpoint " + newIndex + " / " + checkpoints.size() + " | Lap "
|
||||
+ (progress.getLap() + 1) + " / " + race.getLaps(),
|
||||
NamedTextColor.GREEN));
|
||||
player.playSound(player.getLocation(), org.bukkit.Sound.BLOCK_NOTE_BLOCK_CHIME, 0.6f, 1.4f);
|
||||
}
|
||||
}
|
||||
|
||||
private void finishPlayer(Race race, Player player, PlayerProgress progress) {
|
||||
progress.setFinished(true);
|
||||
progress.setFinishTimeMillis(System.currentTimeMillis());
|
||||
race.getFinishOrder().add(player.getUniqueId());
|
||||
|
||||
int place = race.getFinishOrder().size();
|
||||
double seconds = progress.elapsedMillis() / 1000.0;
|
||||
|
||||
for (UUID uuid : race.getParticipants()) {
|
||||
Player p = plugin.getServer().getPlayer(uuid);
|
||||
if (p != null) {
|
||||
p.sendMessage(Component.text("[BoatParty] ", NamedTextColor.AQUA)
|
||||
.append(Component.text(player.getName() + " finished in place #" + place
|
||||
+ " (" + String.format("%.2f", seconds) + "s)", NamedTextColor.GOLD)));
|
||||
}
|
||||
}
|
||||
|
||||
player.showTitle(net.kyori.adventure.title.Title.title(
|
||||
Component.text("Finished! #" + place, NamedTextColor.GOLD),
|
||||
Component.text(String.format("%.2fs", seconds), NamedTextColor.GRAY)));
|
||||
|
||||
for (Boat boat : player.getWorld().getEntitiesByClass(Boat.class)) {
|
||||
if (boat.getPassengers().contains(player)) {
|
||||
boat.eject();
|
||||
boat.remove();
|
||||
}
|
||||
}
|
||||
if (race.getLobby() != null) {
|
||||
player.teleport(race.getLobby());
|
||||
}
|
||||
|
||||
long finishedCount = race.getParticipants().stream()
|
||||
.map(u -> race.getProgress().get(u))
|
||||
.filter(p -> p != null && p.isFinished())
|
||||
.count();
|
||||
if (finishedCount >= race.getParticipants().size()) {
|
||||
endRace(race);
|
||||
}
|
||||
}
|
||||
|
||||
private void endRace(Race race) {
|
||||
List<UUID> standings = new ArrayList<>(race.getFinishOrder());
|
||||
StringBuilder sb = new StringBuilder("Final standings: ");
|
||||
for (int i = 0; i < standings.size(); i++) {
|
||||
Player p = plugin.getServer().getPlayer(standings.get(i));
|
||||
sb.append(i + 1).append(". ").append(p != null ? p.getName() : "?").append(" ");
|
||||
}
|
||||
for (UUID uuid : race.getParticipants()) {
|
||||
Player p = plugin.getServer().getPlayer(uuid);
|
||||
if (p != null) {
|
||||
p.sendMessage(Component.text("[BoatParty] ", NamedTextColor.AQUA)
|
||||
.append(Component.text(sb.toString(), NamedTextColor.GREEN)));
|
||||
}
|
||||
}
|
||||
plugin.getServer().getScheduler().runTaskLater(plugin, race::reset, 100L);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onVehicleExit(VehicleExitEvent event) {
|
||||
if (!(event.getExited() instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
Race race = raceManager.getRaceOf(player);
|
||||
if (race != null && race.getState() == RaceState.RUNNING) {
|
||||
PlayerProgress progress = race.getProgress().get(player.getUniqueId());
|
||||
if (progress != null && !progress.isFinished()) {
|
||||
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
if (player.isOnline() && !player.isInsideVehicle()
|
||||
&& race.getState() == RaceState.RUNNING && !progress.isFinished()) {
|
||||
var boat = player.getWorld().spawn(player.getLocation(), Boat.class);
|
||||
boat.addPassenger(player);
|
||||
}
|
||||
}, 40L);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
Race race = raceManager.getRaceOf(player);
|
||||
if (race != null && (race.getState() == RaceState.RUNNING || race.getState() == RaceState.COUNTDOWN)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Race race = raceManager.getRaceOf(player);
|
||||
if (race != null) {
|
||||
race.removeParticipant(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package us.tss3.boatparty.util;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public final class LocationUtil {
|
||||
|
||||
private LocationUtil() {
|
||||
}
|
||||
|
||||
public static void write(ConfigurationSection section, String path, Location location) {
|
||||
if (location == null) {
|
||||
return;
|
||||
}
|
||||
ConfigurationSection s = section.createSection(path);
|
||||
s.set("world", location.getWorld().getName());
|
||||
s.set("x", location.getX());
|
||||
s.set("y", location.getY());
|
||||
s.set("z", location.getZ());
|
||||
s.set("yaw", location.getYaw());
|
||||
s.set("pitch", location.getPitch());
|
||||
}
|
||||
|
||||
public static Location read(ConfigurationSection section, String path) {
|
||||
ConfigurationSection s = section.getConfigurationSection(path);
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
String worldName = s.getString("world");
|
||||
if (worldName == null || Bukkit.getWorld(worldName) == null) {
|
||||
return null;
|
||||
}
|
||||
return new Location(
|
||||
Bukkit.getWorld(worldName),
|
||||
s.getDouble("x"),
|
||||
s.getDouble("y"),
|
||||
s.getDouble("z"),
|
||||
(float) s.getDouble("yaw"),
|
||||
(float) s.getDouble("pitch"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user