Scoreboard for teams functionality and remove all ChatColor deprecated code

This commit is contained in:
Xander 2024-09-15 00:35:36 +02:00
parent 1a672f2990
commit 93739344b7
No known key found for this signature in database
GPG key ID: 79979C7BA303E003
6 changed files with 116 additions and 83 deletions

View file

@ -2,7 +2,6 @@ package gent.zeus.mc13dtl;
import gent.zeus.mc13dtl.group.GroupAddCommand; import gent.zeus.mc13dtl.group.GroupAddCommand;
import gent.zeus.mc13dtl.group.GroupRemoveCommand; import gent.zeus.mc13dtl.group.GroupRemoveCommand;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@ -13,13 +12,10 @@ import gent.zeus.mc13dtl.gamestate.GameStateHandler;
import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager; import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
public class MC13DTL extends JavaPlugin { public class MC13DTL extends JavaPlugin {
public static MC13DTL instance; public static MC13DTL instance;
public static ScoreboardManager sm; public static TeamScoreBoard board;
public static Scoreboard scoreboard;
@Override @Override
public void onEnable() { public void onEnable() {
@ -35,8 +31,7 @@ public class MC13DTL extends JavaPlugin {
BukkitScheduler scheduler = this.getServer().getScheduler(); BukkitScheduler scheduler = this.getServer().getScheduler();
scheduler.runTaskTimer(this, new RandomEventRunner(), 0, 100); scheduler.runTaskTimer(this, new RandomEventRunner(), 0, 100);
scheduler.runTask(this, () -> { scheduler.runTask(this, () -> {
sm = Bukkit.getScoreboardManager(); board = new TeamScoreBoard();
scoreboard = sm.getMainScoreboard();
}); });
} }
} }

View file

@ -0,0 +1,79 @@
package gent.zeus.mc13dtl;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*;
import java.util.ArrayList;
import java.util.List;
public class TeamScoreBoard {
public ScoreboardManager sm;
public Scoreboard scoreboard;
private Objective objective;
public TeamScoreBoard() {
sm = Bukkit.getScoreboardManager();
scoreboard = sm.getMainScoreboard();
objective = scoreboard.getObjective("scores");
if (objective == null) {
objective = scoreboard.registerNewObjective("scores", Criteria.DUMMY, Component.text("Scores"));
}
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
}
public void setScore(Team team, int score) {
Score score_obj = objective.getScore(team.getName());
if (!score_obj.isScoreSet()) {
score_obj.customName(Component.text("Team " + team.getName()).color(team.color()));
}
score_obj.setScore(score);
}
public int getScore(Team team) {
return objective.getScore(team.getName()).getScore();
}
public Team getTeam(Integer team_index) {
Team team = scoreboard.getTeam(team_index.toString());
if (team == null) {
team = MC13DTL.board.scoreboard.registerNewTeam(team_index.toString());
List<NamedTextColor> colors = new ArrayList<>(NamedTextColor.NAMES.values());
team.color(colors.get(team_index % colors.size()));
team.prefix(Component.text("Team " + team_index.toString() + " "));
team.setAllowFriendlyFire(false);
}
return team;
}
public void addToTeam(Player player, Team team) {
team.addPlayer(player);
team.addEntry(player.getUniqueId().toString());
player.sendRichMessage("<rainbow>You have been added to team " + team.getName());
MC13DTL.board.setScore(team, 0);
player.setGlowing(true);
}
public void sendMessageToTeam(Team team, Component message) {
this.getPlayers(team).forEach(p -> p.sendMessage(message));
}
public List<Player> getPlayers(Team team) {
List<Player> players = new ArrayList<>();
for (String entry : team.getEntries()) {
Player player = Bukkit.getPlayer(entry);
if (player != null) {
players.add(player);
}
}
return players;
}
}

View file

@ -1,10 +1,8 @@
package gent.zeus.mc13dtl.events; package gent.zeus.mc13dtl.events;
import java.util.ArrayList; import net.kyori.adventure.text.Component;
import java.util.List; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -13,18 +11,15 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
import net.md_5.bungee.api.ChatColor;
public abstract class Event implements Listener { public abstract class Event implements Listener {
protected Team team; protected Team team;
public Event(Team team) { public Event(Team team) {
this.team = team; this.team = team;
for (Player player : getPlayers()) { MC13DTL.board.sendMessageToTeam(team, Component.text(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC));
player.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + getMessage());
}
MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance); MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance);
if (getPlayers().size() == 0) { if (MC13DTL.board.getPlayers(team).isEmpty()) {
eventFailed(); eventFailed();
} }
} }
@ -34,43 +29,28 @@ public abstract class Event implements Listener {
} }
protected void eventSuccess() { protected void eventSuccess() {
for (Player player : getPlayers()) { MC13DTL.board.sendMessageToTeam(team, Component.text("Congratulations! You survived this event").color(NamedTextColor.GREEN));
player.sendMessage(ChatColor.GREEN + "Congratulations! You survived this event");
}
eventFinished(); eventFinished();
} }
protected void eventFailed() { protected void eventFailed() {
for (Player player : getPlayers()) { MC13DTL.board.sendMessageToTeam(team, Component.text("Try harder next time...").color(NamedTextColor.RED));
player.sendMessage(ChatColor.RED + "Try harder next time...");
}
eventFinished(); eventFinished();
} }
@EventHandler @EventHandler
public void onPlayerDeath(PlayerDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
if (getPlayers().contains(event.getPlayer())) { if (MC13DTL.board.getPlayers(team).contains(event.getPlayer())) {
eventFailed(); eventFailed();
} }
} }
@EventHandler @EventHandler
public void onPlayerDeath(PlayerQuitEvent event) { public void onPlayerDeath(PlayerQuitEvent event) {
if (getPlayers().contains(event.getPlayer())) { if (MC13DTL.board.getPlayers(team).contains(event.getPlayer())) {
eventFailed(); eventFailed();
} }
} }
protected List<Player> getPlayers() {
List<Player> players = new ArrayList<>();
for (String entry : team.getEntries()) {
Player player = Bukkit.getPlayer(entry);
if (player != null) {
players.add(player);
}
}
return players;
}
protected abstract String getMessage(); protected abstract String getMessage();
} }

View file

@ -16,7 +16,7 @@ public class RandomEventRunner implements Runnable {
long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG); long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG);
long day = Bukkit.getWorld("world").getFullTime() / 24000; long day = Bukkit.getWorld("world").getFullTime() / 24000;
if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) { if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) {
for (Team i : MC13DTL.scoreboard.getTeams()) { for (Team i : MC13DTL.board.scoreboard.getTeams()) {
RandomEventExecutor.executeRandomEvent(i); RandomEventExecutor.executeRandomEvent(i);
} }
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day); world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day);

View file

@ -3,11 +3,6 @@ package gent.zeus.mc13dtl.group;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.command.brigadier.BasicCommand; import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -23,24 +18,8 @@ public class GroupAddCommand implements BasicCommand {
Player player = Bukkit.getPlayerExact(args[0]); Player player = Bukkit.getPlayerExact(args[0]);
if (player != null) { if (player != null) {
Integer index = Integer.parseInt(args[1]); Integer index = Integer.parseInt(args[1]);
Team team = MC13DTL.board.getTeam(index);
Team team = null; MC13DTL.board.addToTeam(player, team);
try {
team = MC13DTL.scoreboard.registerNewTeam(index.toString());
List<NamedTextColor> colors = new ArrayList<>(NamedTextColor.NAMES.values());
team.color(colors.get(index % colors.size()));
team.prefix(Component.text("Team " + index.toString() + " "));
team.setAllowFriendlyFire(false);
} catch (IllegalArgumentException e) {
team = MC13DTL.scoreboard.getTeam(index.toString());
}
if (team != null) {
team.addPlayer(player);
team.addEntry(player.getUniqueId().toString());
player.sendRichMessage("<rainbow>You have been added to team " + index.toString());
player.setGlowing(true);
}
} else { } else {
commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist."); commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist.");
} }

View file

@ -15,7 +15,7 @@ public class GroupRemoveCommand implements BasicCommand {
if (args.length > 0) { if (args.length > 0) {
Player player = Bukkit.getPlayerExact(args[0]); Player player = Bukkit.getPlayerExact(args[0]);
if (player != null) { if (player != null) {
Team team = MC13DTL.scoreboard.getEntryTeam(player.getUniqueId().toString()); Team team = MC13DTL.board.scoreboard.getEntryTeam(player.getUniqueId().toString());
if (team != null) { if (team != null) {
team.removeEntry(player.getName()); team.removeEntry(player.getName());
player.sendMessage("You have been removed from your team"); player.sendMessage("You have been removed from your team");