From 0c2de4136da6c513c189343e4a036d3646a8d4a3 Mon Sep 17 00:00:00 2001 From: kuroppoi <68156848+kuroppoi@users.noreply.github.com> Date: Mon, 19 Apr 2021 02:17:16 +0200 Subject: [PATCH] Added a broadcast command --- .../gameserver/command/CommandManager.java | 9 +++++ .../command/commands/BroadcastCommand.java | 33 +++++++++++++++++++ .../entity/player/PlayerManager.java | 5 +++ 3 files changed, 47 insertions(+) create mode 100644 gameserver/src/main/java/brainwine/gameserver/command/commands/BroadcastCommand.java diff --git a/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java b/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java index da06175..2dd4a86 100644 --- a/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java +++ b/gameserver/src/main/java/brainwine/gameserver/command/CommandManager.java @@ -6,6 +6,13 @@ import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import brainwine.gameserver.command.commands.BroadcastCommand; +import brainwine.gameserver.command.commands.KickCommand; +import brainwine.gameserver.command.commands.RegisterCommand; +import brainwine.gameserver.command.commands.SayCommand; +import brainwine.gameserver.command.commands.StopCommand; +import brainwine.gameserver.command.commands.TeleportCommand; +import brainwine.gameserver.command.commands.ThinkCommand; import brainwine.gameserver.entity.player.Player; public class CommandManager { @@ -25,12 +32,14 @@ public class CommandManager { } private static void registerCommands() { + logger.info("Registering commands ..."); registerCommand("stop", new StopCommand()); registerCommand("register", new RegisterCommand()); registerCommand("tp", new TeleportCommand()); registerCommand("kick", new KickCommand()); registerCommand("say", new SayCommand()); registerCommand("think", new ThinkCommand()); + registerCommand("bc", new BroadcastCommand()); } public static void executeCommand(CommandExecutor executor, String commandLine) { diff --git a/gameserver/src/main/java/brainwine/gameserver/command/commands/BroadcastCommand.java b/gameserver/src/main/java/brainwine/gameserver/command/commands/BroadcastCommand.java new file mode 100644 index 0000000..9e743d2 --- /dev/null +++ b/gameserver/src/main/java/brainwine/gameserver/command/commands/BroadcastCommand.java @@ -0,0 +1,33 @@ +package brainwine.gameserver.command.commands; + +import brainwine.gameserver.GameServer; +import brainwine.gameserver.command.Command; +import brainwine.gameserver.command.CommandExecutor; +import brainwine.gameserver.entity.player.Player; + +public class BroadcastCommand extends Command { + + @Override + public void execute(CommandExecutor executor, String[] args) { + if(args.length == 0) { + executor.alert("Usage: /broadcast "); + return; + } + + String text = String.join(" ", args); + + // TODO hook the console up to some kind of chat feed? + if(executor instanceof GameServer) { + executor.alert(text); + } + + for(Player player : GameServer.getInstance().getPlayerManager().getPlayers()) { + player.notify(text, 9); + } + } + + @Override + public boolean requiresAdmin() { + return true; + } +} diff --git a/gameserver/src/main/java/brainwine/gameserver/entity/player/PlayerManager.java b/gameserver/src/main/java/brainwine/gameserver/entity/player/PlayerManager.java index 084be92..5cdd1c3 100644 --- a/gameserver/src/main/java/brainwine/gameserver/entity/player/PlayerManager.java +++ b/gameserver/src/main/java/brainwine/gameserver/entity/player/PlayerManager.java @@ -1,6 +1,7 @@ package brainwine.gameserver.entity.player; import java.io.File; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -153,4 +154,8 @@ public class PlayerManager { public Player getPlayer(Connection connection) { return playersByConnection.get(connection); } + + public Collection getPlayers() { + return playersById.values(); + } }