From b8a093dbb4de05405ec3b5016f10a60153b67324 Mon Sep 17 00:00:00 2001 From: Mats Date: Thu, 25 Jan 2024 13:32:29 +0100 Subject: [PATCH] Rework commands --- .tool-versions | 2 +- modules/jutils/dependency-reduced-pom.xml | 2 +- .../LegacySpigotUserAdapter.java | 4 +- .../vistas/client/users/VistasUser.java | 6 +- plugin/src/main/bash/data.bin | 2 +- .../modules/commands/BungeeCommandModule.java | 2 - .../commands/commands/BungeeAudioCommand.java | 13 ++- .../subcommand/BungeePlayCommand.java | 86 ------------------- .../subcommand/BungeeStopCommand.java | 71 --------------- .../player/objects/BungeePlayerSelector.java | 30 ++++++- .../client/objects/ClientConnection.java | 4 +- .../generic/commands/CommandService.java | 4 +- .../commands/interfaces/SubCommand.java | 8 ++ .../selectors/SelectorTranslator.java | 28 ++++++ .../commands/subcommands/PlaySubCommand.java | 50 ++++++----- .../commands/subcommands/StopSubCommand.java | 71 +++++++++++++++ .../platform/FallbackPlayerSelector.java | 71 +++++++++++++++ .../generic/platform/Platform.java | 18 +++- .../openaudiomc/generic/user/User.java | 4 +- .../user/adapters/BungeeUserAdapter.java | 4 +- .../adapters/CommandSenderUserAdapter.java | 4 +- .../user/adapters/SpigotUserAdapter.java | 4 +- .../user/adapters/VelocityUserAdapter.java | 4 +- .../commands/SpigotCommandService.java | 3 +- .../commands/command/SpigotAudioCommand.java | 19 ++-- .../CommandTranslationMiddleware.java | 13 ++- .../commands/subcommands/StopSubCommand.java | 65 -------------- .../players/objects/SpigotPlayerSelector.java | 30 ++++++- .../show/runnables/ActionBarRunnable.java | 18 +++- .../modules/show/runnables/ChatRunnable.java | 12 ++- .../commands/VelocityCommandModule.java | 2 - .../commands/VelocityAudioCommand.java | 15 +++- .../subcommand/VelocityPlayCommand.java | 81 ----------------- .../subcommand/VelocityStopCommand.java | 65 -------------- .../objects/VelocityPlayerSelector.java | 32 ++++++- plugin/src/main/resources/data.bin | 2 +- .../resources/openaudiomc-build.properties | 4 +- 37 files changed, 400 insertions(+), 453 deletions(-) delete mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeePlayCommand.java delete mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeeStopCommand.java create mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/selectors/SelectorTranslator.java rename plugin/src/main/java/com/craftmend/openaudiomc/{spigot/modules => generic}/commands/subcommands/PlaySubCommand.java (52%) create mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/StopSubCommand.java create mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/FallbackPlayerSelector.java delete mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/StopSubCommand.java delete mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityPlayCommand.java delete mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityStopCommand.java diff --git a/.tool-versions b/.tool-versions index b1e51edda..d22bf7efc 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -java adoptopenjdk-8.0.332+9 +java openjdk-18.0.2.1 diff --git a/modules/jutils/dependency-reduced-pom.xml b/modules/jutils/dependency-reduced-pom.xml index e7b3443e2..3415482fa 100644 --- a/modules/jutils/dependency-reduced-pom.xml +++ b/modules/jutils/dependency-reduced-pom.xml @@ -84,7 +84,7 @@ - 8 8 + 8 diff --git a/modules/rinaorc-legacy/src/main/java/com/craftmend/rinaorc/implementation/LegacySpigotUserAdapter.java b/modules/rinaorc-legacy/src/main/java/com/craftmend/rinaorc/implementation/LegacySpigotUserAdapter.java index f85df1683..f075bb681 100644 --- a/modules/rinaorc-legacy/src/main/java/com/craftmend/rinaorc/implementation/LegacySpigotUserAdapter.java +++ b/modules/rinaorc-legacy/src/main/java/com/craftmend/rinaorc/implementation/LegacySpigotUserAdapter.java @@ -21,7 +21,7 @@ @Deprecated @AllArgsConstructor -public class LegacySpigotUserAdapter implements User { +public class LegacySpigotUserAdapter implements User { private CommandSender sender; @@ -126,7 +126,7 @@ public void sendActionbarMessage(String text) { } @Override - public Object getOriginal() { + public CommandSender getOriginal() { return sender; } diff --git a/modules/vistas-client/src/main/java/com/craftmend/openaudiomc/vistas/client/users/VistasUser.java b/modules/vistas-client/src/main/java/com/craftmend/openaudiomc/vistas/client/users/VistasUser.java index 7fb95931e..4fcc74197 100644 --- a/modules/vistas-client/src/main/java/com/craftmend/openaudiomc/vistas/client/users/VistasUser.java +++ b/modules/vistas-client/src/main/java/com/craftmend/openaudiomc/vistas/client/users/VistasUser.java @@ -22,7 +22,7 @@ import java.util.Set; import java.util.UUID; -public class VistasUser implements User { +public class VistasUser implements User { private String name; private UUID uuid; @@ -34,7 +34,7 @@ public class VistasUser implements User { @Getter private Instant offlineSince = Instant.now(); @Getter private Set currentServers = new HashSet<>(); private boolean isSpigot = false; - private User original = null; + private User original = null; public VistasUser(String name, UUID uuid, String ip) { this.name = name; @@ -42,7 +42,7 @@ public VistasUser(String name, UUID uuid, String ip) { this.ip = ip; } - public VistasUser(String name, UUID uuid, User player) { + public VistasUser(String name, UUID uuid, User player) { this.name = name; this.uuid = uuid; this.isSpigot = true; diff --git a/plugin/src/main/bash/data.bin b/plugin/src/main/bash/data.bin index 107567be3..06eea9ce4 100755 --- a/plugin/src/main/bash/data.bin +++ b/plugin/src/main/bash/data.bin @@ -1 +1 @@ -BUILD_NUM="1050" +BUILD_NUM="1056" diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/BungeeCommandModule.java b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/BungeeCommandModule.java index 0fffc4fb6..91fa0ea26 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/BungeeCommandModule.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/BungeeCommandModule.java @@ -31,8 +31,6 @@ public void onEnable() { commandService.registerSubCommands( new HelpSubCommand(), - new BungeePlayCommand(OpenAudioMc.getInstance()), - new BungeeStopCommand(OpenAudioMc.getInstance()), new BungeeRegionCommand(), new BungeeSpeakerCommand(), new BungeeShowCommand(), diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/commands/BungeeAudioCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/commands/BungeeAudioCommand.java index c18aa95a6..8ed6a2507 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/commands/BungeeAudioCommand.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/commands/BungeeAudioCommand.java @@ -1,8 +1,10 @@ package com.craftmend.openaudiomc.bungee.modules.commands.commands; import com.craftmend.openaudiomc.OpenAudioMc; +import com.craftmend.openaudiomc.api.interfaces.Client; import com.craftmend.openaudiomc.bungee.modules.player.objects.BungeePlayerSelector; +import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; import com.craftmend.openaudiomc.generic.commands.helpers.CommandMiddewareExecutor; import com.craftmend.openaudiomc.generic.commands.interfaces.CommandMiddleware; import com.craftmend.openaudiomc.generic.commands.middleware.CatchCrashMiddleware; @@ -17,6 +19,8 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Command; +import java.util.Optional; + public class BungeeAudioCommand extends Command { /** @@ -58,8 +62,13 @@ public void execute(CommandSender sender, String[] args) { return; } - for (ProxiedPlayer player : new BungeePlayerSelector(args[0]).getPlayers(sender)) { - OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()).getAuth().publishSessionUrl(); + BungeePlayerSelector selector = new BungeePlayerSelector(); + selector.setSender(new BungeeUserAdapter(sender)); + selector.setString(args[0]); + + for (User result : selector.getResults()) { + Optional client = result.findClient(); + client.ifPresent(value -> ((ClientConnection) value).getAuth().publishSessionUrl()); } } } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeePlayCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeePlayCommand.java deleted file mode 100644 index 93d582c68..000000000 --- a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeePlayCommand.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.craftmend.openaudiomc.bungee.modules.commands.subcommand; - -import com.craftmend.openaudiomc.OpenAudioMc; -import com.craftmend.openaudiomc.bungee.modules.player.objects.BungeePlayerSelector; - -import com.craftmend.openaudiomc.generic.commands.CommandService; - -import com.craftmend.openaudiomc.generic.logging.OpenAudioLogger; -import com.craftmend.openaudiomc.generic.media.objects.OptionalError; -import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; -import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; -import com.craftmend.openaudiomc.generic.commands.objects.Argument; -import com.craftmend.openaudiomc.generic.media.objects.Media; -import com.craftmend.openaudiomc.generic.media.objects.MediaOptions; - -import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; -import com.craftmend.openaudiomc.generic.user.User; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.CommandSender; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -public class BungeePlayCommand extends SubCommand { - - private final OpenAudioMc openAudioMc; - - public BungeePlayCommand(OpenAudioMc openAudioMc) { - super("play", "p"); - registerArguments( - new Argument(" ", - "Plays a sound for all the players in a selection"), - new Argument(" ", - "Plays a sound with configuration (like fade time, sync etc) for all players in a selection") - ); - this.openAudioMc = openAudioMc; - } - - @Override - public void onExecute(User sender, String[] args) { - if (args.length == 0) { - sendHelp(sender); - return; - } - - if (args.length == 2) { - Media media = new Media(args[1]); - int affected = 0; - for (ProxiedPlayer player : new BungeePlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - if (clientConnection.isConnected()) affected++; - clientConnection.sendMedia(media); - } - message(sender, ChatColor.GREEN + "Media created and requested to be played for " + affected + " clients"); - return; - } - - if (args.length == 3) { - try { - MediaOptions mediaOptions = OpenAudioMc.getGson().fromJson(args[2], MediaOptions.class); - - OptionalError parsingError = mediaOptions.validate(); - if (parsingError.isError()) { - message(sender, ChatColor.RED + "Error! " + parsingError.getMessage()); - return; - } - - Media media = new Media(args[1]).applySettings(mediaOptions); - for (ProxiedPlayer player : new BungeePlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - clientConnection.sendMedia(media); - } - message(sender, ChatColor.GREEN + "Media (with arguments) created and requested to be played."); - } catch (Exception e) { - OpenAudioLogger.handleException(e); - message(sender, ChatColor.RED + "Error. Invalid options. Please refer to the command guide."); - } - return; - } - sendHelp(sender); - } - - private void sendHelp(User genericExecutor) { - OpenAudioMc.getService(CommandService.class).getSubCommand("help").onExecute(genericExecutor, new String[] { - getCommand() - }); - } -} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeeStopCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeeStopCommand.java deleted file mode 100644 index a08f56bd3..000000000 --- a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/commands/subcommand/BungeeStopCommand.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.craftmend.openaudiomc.bungee.modules.commands.subcommand; - -import com.craftmend.openaudiomc.OpenAudioMc; -import com.craftmend.openaudiomc.bungee.modules.player.objects.BungeePlayerSelector; -import com.craftmend.openaudiomc.generic.commands.CommandService; - -import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; -import com.craftmend.openaudiomc.generic.commands.objects.Argument; -import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; -import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; -import com.craftmend.openaudiomc.generic.networking.packets.client.media.PacketClientDestroyMedia; - -import com.craftmend.openaudiomc.generic.user.User; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.CommandSender; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -public class BungeeStopCommand extends SubCommand { - - private final OpenAudioMc openAudioMc; - - public BungeeStopCommand(OpenAudioMc openAudioMc) { - super("stop"); - registerArguments( - new Argument("", - "Stops all manual sounds for all players in a selection"), - new Argument(" ", - "Only stops one specified sound for all players in the selection with a selected ID") - ); - this.openAudioMc = openAudioMc; - } - - @Override - public void onExecute(User sender, String[] args) { - if (args.length == 0) { - sendHelp(sender); - return; - } - - if (args.length == 1) { - int affected = 0; - for (ProxiedPlayer player : new BungeePlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - if (clientConnection.isConnected()) affected++; - clientConnection.getSession().getOngoingMedia().clear(); - OpenAudioMc.getService(NetworkingService.class).send(clientConnection, new PacketClientDestroyMedia(null)); - } - message(sender, ChatColor.GREEN + "Destroyed all normal sounds for " + affected + " clients"); - return; - } - - if (args.length == 2) { - int affected = 0; - for (ProxiedPlayer player : new BungeePlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - if (clientConnection.isConnected()) affected++; - OpenAudioMc.getService(NetworkingService.class).send(clientConnection, new PacketClientDestroyMedia(args[1])); - } - message(sender, ChatColor.GREEN + "Destroyed the sound" + args[1] + " for " + affected + " clients"); - return; - } - - sendHelp(sender); - } - - private void sendHelp(User genericExecutor) { - OpenAudioMc.getService(CommandService.class).getSubCommand("help").onExecute(genericExecutor, new String[] { - getCommand() - }); - } -} \ No newline at end of file diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/player/objects/BungeePlayerSelector.java b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/player/objects/BungeePlayerSelector.java index f6788a8d2..c25f35bf8 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/player/objects/BungeePlayerSelector.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/bungee/modules/player/objects/BungeePlayerSelector.java @@ -1,7 +1,9 @@ package com.craftmend.openaudiomc.bungee.modules.player.objects; +import com.craftmend.openaudiomc.generic.commands.selectors.SelectorTranslator; import com.craftmend.openaudiomc.generic.logging.OpenAudioLogger; -import lombok.AllArgsConstructor; +import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.user.adapters.BungeeUserAdapter; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -9,10 +11,30 @@ import java.util.ArrayList; import java.util.List; -@AllArgsConstructor -public class BungeePlayerSelector { +public class BungeePlayerSelector implements SelectorTranslator { private String selector; + private CommandSender sender; + + @Override + public void setString(String selector) { + this.selector = selector; + } + + @Override + public void setSender(User sender) { + this.sender = sender.getOriginal(); + } + + @Override + public List> getResults() { + List players = getPlayers(sender); + List> users = new ArrayList<>(); + for (ProxiedPlayer player : players) { + users.add(new BungeeUserAdapter(player)); + } + return users; + } /** * this turns selectors like @a[r=5] into a usable list, since @@ -21,7 +43,7 @@ public class BungeePlayerSelector { * @param commandSender the sender * @return players following the selector */ - public List getPlayers(CommandSender commandSender) { + private List getPlayers(CommandSender commandSender) { List players = new ArrayList<>(); if (selector.startsWith("@a")) { diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/client/objects/ClientConnection.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/client/objects/ClientConnection.java index 200509925..788d065a0 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/client/objects/ClientConnection.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/client/objects/ClientConnection.java @@ -49,7 +49,7 @@ public class ClientConnection implements Authenticatable, Client, Serializable { - @Getter private transient final User user; + @Getter private transient final User user; @Getter private final SessionData session; @Setter private ClientAuth auth; @@ -233,7 +233,7 @@ public void onDestroy() { } @Override - public User getOwner() { + public User getOwner() { return this.getUser(); } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/CommandService.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/CommandService.java index 81ff496f0..2d35bc8f5 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/CommandService.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/CommandService.java @@ -33,7 +33,9 @@ public CommandService() { new ReloadSubCommand(), new StateSubCommand(), new ModulesSubCommand(), - new ClientsSubCommand() + new ClientsSubCommand(), + new StopSubCommand(), + new PlaySubCommand() ); } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/SubCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/SubCommand.java index 10c0e5183..a9a548019 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/SubCommand.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/SubCommand.java @@ -2,6 +2,7 @@ import com.craftmend.openaudiomc.OpenAudioMc; import com.craftmend.openaudiomc.generic.commands.objects.CommandError; +import com.craftmend.openaudiomc.generic.commands.selectors.SelectorTranslator; import com.craftmend.openaudiomc.generic.environment.MagicValue; import com.craftmend.openaudiomc.generic.platform.Platform; import com.craftmend.openaudiomc.generic.commands.objects.Argument; @@ -147,6 +148,13 @@ protected T getService(Class service) { return service.cast(OpenAudioMc.getInstance().getServiceManager().loadService(service)); } + protected List> resolveSelector(User sender, String selector) { + SelectorTranslator translator = Platform.getSelectorTranslator(); + translator.setString(selector); + translator.setSenderGeneric(sender); + return translator.getResultsGeneric(); + } + @SneakyThrows public int catchInt(String data) { try { diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/selectors/SelectorTranslator.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/selectors/SelectorTranslator.java new file mode 100644 index 000000000..083b0dbbe --- /dev/null +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/selectors/SelectorTranslator.java @@ -0,0 +1,28 @@ +package com.craftmend.openaudiomc.generic.commands.selectors; + +import com.craftmend.openaudiomc.generic.user.User; + +import java.util.ArrayList; +import java.util.List; + +public interface SelectorTranslator { + + void setString(String selector); + void setSender(User sender); + List> getResults(); + + default void setSenderGeneric(User unknownSender) { + this.setSender((User) unknownSender); + } + + default List> getResultsGeneric() { + // CONVERT TO GENERIC + List> r = getResults(); + List> results = new ArrayList<>(r.size()); + for (User user : r) { + results.add(user); + } + return results; + } + +} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/PlaySubCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/PlaySubCommand.java similarity index 52% rename from plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/PlaySubCommand.java rename to plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/PlaySubCommand.java index 241114aed..e9bc22511 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/PlaySubCommand.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/PlaySubCommand.java @@ -1,26 +1,22 @@ -package com.craftmend.openaudiomc.spigot.modules.commands.subcommands; +package com.craftmend.openaudiomc.generic.commands.subcommands; import com.craftmend.openaudiomc.OpenAudioMc; +import com.craftmend.openaudiomc.api.interfaces.Client; +import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; import com.craftmend.openaudiomc.generic.media.objects.OptionalError; +import com.craftmend.openaudiomc.generic.platform.OaColor; import com.craftmend.openaudiomc.generic.user.User; -import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot; import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; import com.craftmend.openaudiomc.generic.commands.objects.Argument; import com.craftmend.openaudiomc.generic.media.objects.Media; import com.craftmend.openaudiomc.generic.media.objects.MediaOptions; -import com.craftmend.openaudiomc.spigot.modules.players.SpigotPlayerService; -import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotConnection; -import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -public class PlaySubCommand extends SubCommand { +import java.util.Optional; - private final OpenAudioMcSpigot openAudioMcSpigot; +public class PlaySubCommand extends SubCommand { - public PlaySubCommand(OpenAudioMcSpigot openAudioMcSpigot) { + public PlaySubCommand() { super("play", "p"); registerArguments( new Argument(" ", @@ -28,7 +24,6 @@ public PlaySubCommand(OpenAudioMcSpigot openAudioMcSpigot) { new Argument(" ", "Plays a sound with configuration (like fade time, sync etc) for all players in a selection") ); - this.openAudioMcSpigot = openAudioMcSpigot; } @Override @@ -41,12 +36,16 @@ public void onExecute(User sender, String[] args) { if (args.length == 2) { Media media = new Media(args[1]); int affected = 0; - for (Player player : new SpigotPlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - SpigotConnection spigotConnection = OpenAudioMc.getService(SpigotPlayerService.class).getClient(player); - if (spigotConnection.getClientConnection().isConnected()) affected++; - spigotConnection.getClientConnection().sendMedia(media); + + for (User user : resolveSelector(sender, args[0])) { + Optional client = user.findClient(); + if (client.isPresent()) { + if (client.get().isConnected()) affected++; + ClientConnection clientConnection = (ClientConnection) client.get(); + clientConnection.sendMedia(media); + } } - message(sender, ChatColor.GREEN + "Media created and requested to be played for " + affected + " clients"); + message(sender, OaColor.GREEN + "Media created and requested to be played for " + affected + " clients"); return; } @@ -56,16 +55,23 @@ public void onExecute(User sender, String[] args) { OptionalError parsingError = mediaOptions.validate(); if (parsingError.isError()) { - message(sender, ChatColor.RED + "Error! " + parsingError.getMessage()); + message(sender, OaColor.RED + "Error! " + parsingError.getMessage()); return; } Media media = new Media(args[1]).applySettings(mediaOptions); - for (Player player : new SpigotPlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - SpigotConnection spigotConnection = OpenAudioMc.getService(SpigotPlayerService.class).getClient(player); - spigotConnection.getClientConnection().sendMedia(media); + + for (User user : resolveSelector(sender, args[0])) { + Optional client = user.findClient(); + if (client.isPresent()) { + if (client.get().isConnected()) { + ClientConnection clientConnection = (ClientConnection) client.get(); + clientConnection.sendMedia(media); + } + } } - message(sender, ChatColor.GREEN + "Media (with arguments) created and requested to be played."); + + message(sender, OaColor.GREEN + "Media (with arguments) created and requested to be played."); } catch (Exception e) { message(sender, "Error. Invalid options. Please refer to the command guide."); } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/StopSubCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/StopSubCommand.java new file mode 100644 index 000000000..90e0916cc --- /dev/null +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/StopSubCommand.java @@ -0,0 +1,71 @@ +package com.craftmend.openaudiomc.generic.commands.subcommands; + +import com.craftmend.openaudiomc.OpenAudioMc; + +import com.craftmend.openaudiomc.api.interfaces.Client; +import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; +import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; +import com.craftmend.openaudiomc.generic.platform.OaColor; +import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; +import com.craftmend.openaudiomc.generic.commands.objects.Argument; +import com.craftmend.openaudiomc.generic.networking.packets.client.media.PacketClientDestroyMedia; + +import java.util.Optional; + + +public class StopSubCommand extends SubCommand { + + public StopSubCommand() { + super("stop"); + registerArguments( + new Argument("", + "Stops all manual sounds for all players in a selection"), + new Argument(" ", + "Only stops one specified sound for all players in the selection with a selected ID") + ); + } + + @Override + public void onExecute(User sender, String[] args) { + if (args.length == 0) { + sender.makeExecuteCommand("oa help " + getCommand()); + return; + } + + if (args.length == 1) { + int affected = 0; + + for (User user : resolveSelector(sender, args[0])) { + Optional client = user.findClient(); + if (client.isPresent()) { + if (client.get().isConnected()) affected++; + ClientConnection clientConnection = (ClientConnection) client.get(); + clientConnection.getSession().getOngoingMedia().clear(); + OpenAudioMc.getService(NetworkingService.class).send(clientConnection, new PacketClientDestroyMedia(null)); + } + } + + message(sender, OaColor.GREEN + "Destroyed all normal sounds for " + affected + " clients"); + return; + } + + if (args.length == 2) { + int affected = 0; + + for (User user : resolveSelector(sender, args[0])) { + Optional client = user.findClient(); + if (client.isPresent()) { + if (client.get().isConnected()) affected++; + ClientConnection clientConnection = (ClientConnection) client.get(); + OpenAudioMc.getService(NetworkingService.class).send(clientConnection, new PacketClientDestroyMedia(args[1])); + } + } + + message(sender, OaColor.GREEN + "Destroyed the sound" + args[1] + " for " + affected + " clients"); + return; + } + + sender.makeExecuteCommand("oa help " + getCommand()); + } +} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/FallbackPlayerSelector.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/FallbackPlayerSelector.java new file mode 100644 index 000000000..bb2a4742a --- /dev/null +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/FallbackPlayerSelector.java @@ -0,0 +1,71 @@ +package com.craftmend.openaudiomc.generic.platform; + +import com.craftmend.openaudiomc.OpenAudioMc; +import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; +import com.craftmend.openaudiomc.generic.commands.selectors.SelectorTranslator; +import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; +import com.craftmend.openaudiomc.generic.user.User; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class FallbackPlayerSelector implements SelectorTranslator { + + private String selector; + private User sender; + + @Override + public void setString(String selector) { + this.selector = selector; + } + + @Override + public void setSender(User sender) { + this.sender = sender; + } + + @Override + public List> getResults() { + // attempt to find all players + List> clients = OpenAudioMc.getService(NetworkingService.class).getClients() + .stream() + .map(ClientConnection::getUser) + .collect(Collectors.toList()); + + // does the selector start with an @? + if (selector.startsWith("@")) { + // yes, so it's a selector + // check if it's @a + if (selector.equals("@a")) { + // yes, so return all players + return clients; + } else { + // no, so return an empty list and warn + sender.sendMessage("Warning: selectors are not supported on this platform. Please use a player name, uuid or @a instead."); + return new ArrayList<>(); + } + } else { + // is it a uuid? + if (selector.length() == 36) { + // yes, so return the player with that uuid + return clients.stream() + .filter(user -> user.getUniqueId().toString().equals(selector)) + .collect(Collectors.toList()); + } else { + // no, so it's a player name + // check if it's a player name + if (clients.stream().anyMatch(user -> user.getName().equalsIgnoreCase(selector))) { + // yes, so return the player with that name + return clients.stream() + .filter(user -> user.getName().equalsIgnoreCase(selector)) + .collect(Collectors.toList()); + } else { + // no, so return an empty list and warn + sender.sendMessage("Warning: selectors are not supported on this platform. Please use a player name, uuid or @a instead."); + return new ArrayList<>(); + } + } + } + } +} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/Platform.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/Platform.java index ebe616329..a5d6767f8 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/Platform.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/platform/Platform.java @@ -1,6 +1,10 @@ package com.craftmend.openaudiomc.generic.platform; import com.craftmend.openaudiomc.OpenAudioMc; +import com.craftmend.openaudiomc.bungee.modules.player.objects.BungeePlayerSelector; +import com.craftmend.openaudiomc.generic.commands.selectors.SelectorTranslator; +import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; +import com.craftmend.openaudiomc.velocity.modules.player.objects.VelocityPlayerSelector; import com.craftmend.openaudiomc.velocity.utils.VelocityChatColor; public enum Platform { @@ -13,7 +17,7 @@ public enum Platform { ; public static String translateColors(String input) { - switch (OpenAudioMc.getInstance().getPlatform()){ + switch (OpenAudioMc.getInstance().getPlatform()) { case SPIGOT: return org.bukkit.ChatColor.translateAlternateColorCodes('&', input); case BUNGEE: @@ -23,7 +27,19 @@ public static String translateColors(String input) { default: return input; // unknown platform } + } + public static SelectorTranslator getSelectorTranslator() { + switch (OpenAudioMc.getInstance().getPlatform()){ + case SPIGOT: + return new SpigotPlayerSelector(); + case BUNGEE: + return new BungeePlayerSelector(); + case VELOCITY: + return new VelocityPlayerSelector(); + default: + return new FallbackPlayerSelector(); + } } public static String makeColor(String color) { diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/User.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/User.java index 7a784c973..fb54ba1b2 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/User.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/User.java @@ -10,9 +10,9 @@ import java.util.Optional; import java.util.UUID; -public interface User { +public interface User { - @Deprecated Object getOriginal(); + T getOriginal(); String getName(); UUID getUniqueId(); diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/BungeeUserAdapter.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/BungeeUserAdapter.java index 7493ed081..0d47a4b27 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/BungeeUserAdapter.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/BungeeUserAdapter.java @@ -17,7 +17,7 @@ import static com.craftmend.openaudiomc.generic.platform.Platform.translateColors; @AllArgsConstructor -public class BungeeUserAdapter implements User { +public class BungeeUserAdapter implements User { private CommandSender sender; @@ -134,7 +134,7 @@ public String getIpAddress() { } @Override - public Object getOriginal() { + public CommandSender getOriginal() { return sender; } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/CommandSenderUserAdapter.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/CommandSenderUserAdapter.java index 573c86eb4..9f33eed95 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/CommandSenderUserAdapter.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/CommandSenderUserAdapter.java @@ -11,7 +11,7 @@ import java.util.UUID; @AllArgsConstructor -public class CommandSenderUserAdapter implements User { +public class CommandSenderUserAdapter implements User { private CommandSender sender; @@ -68,7 +68,7 @@ public String getIpAddress() { } @Override - public Object getOriginal() { + public CommandSender getOriginal() { return sender; } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/SpigotUserAdapter.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/SpigotUserAdapter.java index 9152b88d1..cad28e1be 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/SpigotUserAdapter.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/SpigotUserAdapter.java @@ -19,7 +19,7 @@ import static com.craftmend.openaudiomc.generic.platform.Platform.translateColors; @AllArgsConstructor -public class SpigotUserAdapter implements User { +public class SpigotUserAdapter implements User { private CommandSender player; @@ -142,7 +142,7 @@ public void sendActionbarMessage(String text) { } @Override - public Object getOriginal() { + public CommandSender getOriginal() { return player; } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/VelocityUserAdapter.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/VelocityUserAdapter.java index a93966e64..9bf96ae7b 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/VelocityUserAdapter.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/user/adapters/VelocityUserAdapter.java @@ -18,7 +18,7 @@ import static com.craftmend.openaudiomc.generic.platform.Platform.translateColors; @AllArgsConstructor -public class VelocityUserAdapter implements User { +public class VelocityUserAdapter implements User { private CommandSource sender; @@ -135,7 +135,7 @@ public String getIpAddress() { } @Override - public Object getOriginal() { + public CommandSource getOriginal() { return sender; } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/SpigotCommandService.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/SpigotCommandService.java index be9b97584..38a8733dc 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/SpigotCommandService.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/SpigotCommandService.java @@ -3,6 +3,7 @@ import com.craftmend.openaudiomc.OpenAudioMc; import com.craftmend.openaudiomc.generic.commands.CommandService; import com.craftmend.openaudiomc.generic.commands.subcommands.HelpSubCommand; +import com.craftmend.openaudiomc.generic.commands.subcommands.PlaySubCommand; import com.craftmend.openaudiomc.generic.service.Inject; import com.craftmend.openaudiomc.generic.service.Service; import com.craftmend.openaudiomc.spigot.modules.commands.command.*; @@ -39,9 +40,7 @@ public void onEnable() { commandService.registerSubCommands( new HelpSubCommand(), new RegionsSubCommand(openAudioMcSpigot), - new PlaySubCommand(openAudioMcSpigot), new SpeakersSubCommand(openAudioMcSpigot), - new StopSubCommand(openAudioMcSpigot), new ShowSubCommand(openAudioMcSpigot), new AliasSubCommand(), new VoiceSubCommand(), diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/command/SpigotAudioCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/command/SpigotAudioCommand.java index 8f19fe4ac..d326f11bb 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/command/SpigotAudioCommand.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/command/SpigotAudioCommand.java @@ -3,6 +3,8 @@ import com.craftmend.openaudiomc.OpenAudioMc; import com.craftmend.openaudiomc.api.impl.event.events.SpigotAudioCommandEvent; import com.craftmend.openaudiomc.api.interfaces.AudioApi; +import com.craftmend.openaudiomc.api.interfaces.Client; +import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; import com.craftmend.openaudiomc.generic.commands.helpers.CommandMiddewareExecutor; import com.craftmend.openaudiomc.generic.commands.interfaces.CommandMiddleware; import com.craftmend.openaudiomc.generic.commands.middleware.CatchCrashMiddleware; @@ -17,7 +19,9 @@ import com.craftmend.openaudiomc.generic.state.interfaces.State; import com.craftmend.openaudiomc.generic.state.states.WorkerState; import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.user.adapters.SpigotUserAdapter; import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; + import lombok.NoArgsConstructor; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -25,6 +29,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.Optional; + @NoArgsConstructor public class SpigotAudioCommand implements CommandExecutor { @@ -42,9 +48,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, return true; } - - - User sua = OpenAudioMc.resolveDependency(UserHooks.class).fromCommandSender(commandSender); + User sua = new SpigotUserAdapter(commandSender); if (CommandMiddewareExecutor.shouldBeCanceled(sua, null, commandMiddleware)) return true; @@ -90,8 +94,13 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, return true; } - for (Player player : new SpigotPlayerSelector(args[0]).getPlayers(commandSender)) { - OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()).getAuth().publishSessionUrl(); + SpigotPlayerSelector selector = new SpigotPlayerSelector(); + selector.setSender(sua); + selector.setString(args[0]); + + for (User result : selector.getResults()) { + Optional client = result.findClient(); + client.ifPresent(value -> ((ClientConnection) value).getAuth().publishSessionUrl()); } } return true; diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/middleware/CommandTranslationMiddleware.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/middleware/CommandTranslationMiddleware.java index 88cfb1b23..f2c5f2573 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/middleware/CommandTranslationMiddleware.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/middleware/CommandTranslationMiddleware.java @@ -1,9 +1,12 @@ package com.craftmend.openaudiomc.spigot.modules.commands.middleware; import com.craftmend.openaudiomc.OpenAudioMc; +import com.craftmend.openaudiomc.api.interfaces.Client; import com.craftmend.openaudiomc.generic.commands.CommandService; import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; import com.craftmend.openaudiomc.generic.environment.MagicValue; +import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.user.adapters.SpigotUserAdapter; import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; import lombok.AllArgsConstructor; import org.bukkit.Bukkit; @@ -48,11 +51,15 @@ public void onCommand(ServerCommandEvent event) { if (commandPreset.charAt(0) == '/') commandPreset = commandPreset.replace("/", ""); commandPreset = commandPreset.replaceAll(Pattern.quote(selector), "%%player%%"); + SpigotPlayerSelector spigotPlayerSelector = new SpigotPlayerSelector(); + spigotPlayerSelector.setSender(new SpigotUserAdapter(event.getSender())); + spigotPlayerSelector.setString(selector); + // process the selector, build a new command and re-run - for (Player player : new SpigotPlayerSelector(selector).getPlayers(event.getSender())) { - String playerCommand = commandPreset.replaceAll("%%player%%", player.getName()); + for (User target : spigotPlayerSelector.getResults()) { + String playerCommand = commandPreset.replaceAll("%%player%%", target.getName()); Bukkit.getServer().dispatchCommand(event.getSender(), playerCommand); - event.getSender().sendMessage(MagicValue.COMMAND_PREFIX.get(String.class) + "Changed selector to execute for " + player.getName()); + event.getSender().sendMessage(MagicValue.COMMAND_PREFIX.get(String.class) + "Changed selector to execute for " + target.getName()); } event.setCancelled(true); diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/StopSubCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/StopSubCommand.java deleted file mode 100644 index 3f473f2da..000000000 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/commands/subcommands/StopSubCommand.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.craftmend.openaudiomc.spigot.modules.commands.subcommands; - -import com.craftmend.openaudiomc.OpenAudioMc; - -import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; -import com.craftmend.openaudiomc.generic.user.User; -import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot; -import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; -import com.craftmend.openaudiomc.generic.commands.objects.Argument; -import com.craftmend.openaudiomc.generic.networking.packets.client.media.PacketClientDestroyMedia; -import com.craftmend.openaudiomc.spigot.modules.players.SpigotPlayerService; -import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotConnection; -import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class StopSubCommand extends SubCommand { - - private final OpenAudioMcSpigot openAudioMcSpigot; - - public StopSubCommand(OpenAudioMcSpigot openAudioMcSpigot) { - super("stop"); - registerArguments( - new Argument("", - "Stops all manual sounds for all players in a selection"), - new Argument(" ", - "Only stops one specified sound for all players in the selection with a selected ID") - ); - this.openAudioMcSpigot = openAudioMcSpigot; - } - - @Override - public void onExecute(User sender, String[] args) { - if (args.length == 0) { - sender.makeExecuteCommand("oa help " + getCommand()); - return; - } - - if (args.length == 1) { - int affected = 0; - for (Player player : new SpigotPlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - SpigotConnection spigotConnection = OpenAudioMc.getService(SpigotPlayerService.class).getClient(player); - if (spigotConnection.getClientConnection().isConnected()) affected++; - spigotConnection.getClientConnection().getSession().getOngoingMedia().clear(); - OpenAudioMc.getService(NetworkingService.class).send(spigotConnection.getClientConnection(), new PacketClientDestroyMedia(null)); - } - message(sender, ChatColor.GREEN + "Destroyed all normal sounds for " + affected + " clients"); - return; - } - - if (args.length == 2) { - int affected = 0; - for (Player player : new SpigotPlayerSelector(args[0]).getPlayers((CommandSender) sender.getOriginal())) { - SpigotConnection spigotConnection = OpenAudioMc.getService(SpigotPlayerService.class).getClient(player); - if (spigotConnection.getClientConnection().isConnected()) affected++; - OpenAudioMc.getService(NetworkingService.class).send(spigotConnection.getClientConnection(), new PacketClientDestroyMedia(args[1])); - } - message(sender, net.md_5.bungee.api.ChatColor.GREEN + "Destroyed the sound" + args[1] + " for " + affected + " clients"); - return; - } - - sender.makeExecuteCommand("oa help " + getCommand()); - } -} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/players/objects/SpigotPlayerSelector.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/players/objects/SpigotPlayerSelector.java index 32d2d5caa..5a03056a5 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/players/objects/SpigotPlayerSelector.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/players/objects/SpigotPlayerSelector.java @@ -1,6 +1,9 @@ package com.craftmend.openaudiomc.spigot.modules.players.objects; +import com.craftmend.openaudiomc.generic.commands.selectors.SelectorTranslator; import com.craftmend.openaudiomc.generic.logging.OpenAudioLogger; +import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.user.adapters.SpigotUserAdapter; import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot; import com.craftmend.openaudiomc.spigot.modules.regions.RegionModule; import com.craftmend.openaudiomc.spigot.modules.show.interfaces.FakeCommandSender; @@ -17,10 +20,30 @@ import java.util.List; import java.util.stream.Collectors; -@AllArgsConstructor -public class SpigotPlayerSelector { +public class SpigotPlayerSelector implements SelectorTranslator { private String selector; + private CommandSender sender; + + @Override + public void setString(String selector) { + this.selector = selector; + } + + @Override + public void setSender(User sender) { + this.sender = sender.getOriginal(); + } + + @Override + public List> getResults() { + List players = getPlayers(sender); + List> users = new ArrayList<>(); + for (Player player : players) { + users.add(new SpigotUserAdapter(player)); + } + return users; + } /** * this turns selectors like @a[r=5] into a usable list, since @@ -29,7 +52,7 @@ public class SpigotPlayerSelector { * @param commandSender the sender * @return players following the selector */ - public List getPlayers(CommandSender commandSender) { + private List getPlayers(CommandSender commandSender) { List players = new ArrayList<>(); if (selector.startsWith("@p")) { @@ -166,5 +189,4 @@ private String getArgument(String key) { return result.toString().replaceAll(".", ""); } - } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ActionBarRunnable.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ActionBarRunnable.java index c02fe50d1..0c4bcbfdf 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ActionBarRunnable.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ActionBarRunnable.java @@ -1,5 +1,7 @@ package com.craftmend.openaudiomc.spigot.modules.show.runnables; +import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.user.adapters.SpigotUserAdapter; import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; import com.craftmend.openaudiomc.spigot.modules.show.interfaces.FakeCommandSender; import com.craftmend.openaudiomc.spigot.modules.show.interfaces.ShowRunnable; @@ -10,12 +12,14 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.List; @AllArgsConstructor @NoArgsConstructor +@Deprecated public class ActionBarRunnable extends ShowRunnable { private String message; @@ -36,12 +40,20 @@ public String serialize() { public void run() { String[] args = message.split(" "); if (args.length < 1) return; - List players = new SpigotPlayerSelector(args[0]).getPlayers(new FakeCommandSender(Bukkit.getWorld(worldName))); + + SpigotPlayerSelector spigotPlayerSelector = new SpigotPlayerSelector(); + spigotPlayerSelector.setSender(new SpigotUserAdapter(new FakeCommandSender(Bukkit.getWorld(worldName)))); + spigotPlayerSelector.setString(args[0]); + + String[] subArgs = new String[args.length - 1]; System.arraycopy(args, 1, subArgs, 0, args.length - 1); String fullMessage = ChatColor.translateAlternateColorCodes('&', String.join(" ", subArgs)); - for (Player player : players) { - player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(fullMessage)); + for (User user : spigotPlayerSelector.getResults()) { + if (user.getOriginal() instanceof Player) { + Player player = (Player) user.getOriginal(); + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(fullMessage)); + } } } } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ChatRunnable.java b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ChatRunnable.java index 4b6f56197..e7a92c36b 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ChatRunnable.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/runnables/ChatRunnable.java @@ -1,5 +1,6 @@ package com.craftmend.openaudiomc.spigot.modules.show.runnables; +import com.craftmend.openaudiomc.generic.user.adapters.SpigotUserAdapter; import com.craftmend.openaudiomc.spigot.modules.players.objects.SpigotPlayerSelector; import com.craftmend.openaudiomc.spigot.modules.show.interfaces.FakeCommandSender; import com.craftmend.openaudiomc.spigot.modules.show.interfaces.ShowRunnable; @@ -39,12 +40,15 @@ public String serialize() { public void run() { String[] args = message.split(" "); if (args.length < 1) return; - List players = new SpigotPlayerSelector(args[0]).getPlayers(new FakeCommandSender(Bukkit.getWorld(worldName))); + + SpigotPlayerSelector spigotPlayerSelector = new SpigotPlayerSelector(); + spigotPlayerSelector.setSender(new SpigotUserAdapter(new FakeCommandSender(Bukkit.getWorld(worldName)))); + spigotPlayerSelector.setString(args[0]); + String[] subArgs = new String[args.length - 1]; System.arraycopy(args, 1, subArgs, 0, args.length - 1); String fullMessage = ChatColor.translateAlternateColorCodes('&', String.join(" ", subArgs)); - for (Player player : players) { - player.sendMessage(fullMessage); - } + + spigotPlayerSelector.getResults().forEach(user -> user.sendMessage(fullMessage)); } } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/VelocityCommandModule.java b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/VelocityCommandModule.java index 40c2169be..6d4d25ec2 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/VelocityCommandModule.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/VelocityCommandModule.java @@ -40,8 +40,6 @@ public VelocityCommandModule(OpenAudioMcVelocity openAudioMcVelocity) { commandService.registerSubCommands( new HelpSubCommand(), - new VelocityPlayCommand(OpenAudioMc.getInstance()), - new VelocityStopCommand(OpenAudioMc.getInstance()), new VelocityRegionCommand(), new VelocitySpeakerCommand(), new VelocityShowCommand(), diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/commands/VelocityAudioCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/commands/VelocityAudioCommand.java index 6a6da88c9..6bb6e970b 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/commands/VelocityAudioCommand.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/commands/VelocityAudioCommand.java @@ -1,6 +1,8 @@ package com.craftmend.openaudiomc.velocity.modules.commands.commands; import com.craftmend.openaudiomc.OpenAudioMc; +import com.craftmend.openaudiomc.api.interfaces.Client; +import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; import com.craftmend.openaudiomc.generic.commands.helpers.CommandMiddewareExecutor; import com.craftmend.openaudiomc.generic.commands.interfaces.CommandMiddleware; @@ -16,6 +18,8 @@ import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.Player; +import java.util.Optional; + public class VelocityAudioCommand implements SimpleCommand { private final CommandMiddleware[] commandMiddleware = new CommandMiddleware[]{ @@ -27,7 +31,7 @@ public class VelocityAudioCommand implements SimpleCommand { @Override public void execute(Invocation invocation) { CommandSource source = invocation.source(); - User user = new VelocityUserAdapter(source); + User user = new VelocityUserAdapter(source); if (CommandMiddewareExecutor.shouldBeCanceled(user, null, commandMiddleware)) return; if (source instanceof Player) { @@ -51,8 +55,13 @@ public void execute(Invocation invocation) { return; } - for (Player player : new VelocityPlayerSelector(args[0]).getPlayers(source)) { - OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()).getAuth().publishSessionUrl(); + VelocityPlayerSelector velocityPlayerSelector = new VelocityPlayerSelector(); + velocityPlayerSelector.setSender(user); + velocityPlayerSelector.setString(args[0]); + + for (User result : velocityPlayerSelector.getResults()) { + Optional client = result.findClient(); + client.ifPresent(value -> ((ClientConnection) value).getAuth().publishSessionUrl()); } } } diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityPlayCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityPlayCommand.java deleted file mode 100644 index c3c6d3e1c..000000000 --- a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityPlayCommand.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.craftmend.openaudiomc.velocity.modules.commands.subcommand; - -import com.craftmend.openaudiomc.OpenAudioMc; -import com.craftmend.openaudiomc.generic.commands.CommandService; - -import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; -import com.craftmend.openaudiomc.generic.commands.objects.Argument; -import com.craftmend.openaudiomc.generic.media.objects.Media; -import com.craftmend.openaudiomc.generic.media.objects.MediaOptions; -import com.craftmend.openaudiomc.generic.media.objects.OptionalError; -import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; -import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; -import com.craftmend.openaudiomc.generic.user.User; -import com.craftmend.openaudiomc.velocity.modules.player.objects.VelocityPlayerSelector; -import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.proxy.Player; - -public class VelocityPlayCommand extends SubCommand { - - private final OpenAudioMc openAudioMc; - - public VelocityPlayCommand(OpenAudioMc openAudioMc) { - super("play", "p"); - registerArguments( - new Argument(" ", - "Plays a sound for all the players in a selection"), - new Argument(" ", - "Plays a sound with configuration (like fade time, sync etc) for all players in a selection") - ); - this.openAudioMc = openAudioMc; - } - - @Override - public void onExecute(User sender, String[] args) { - if (args.length == 0) { - sendHelp(sender); - return; - } - - if (args.length == 2) { - Media media = new Media(args[1]); - int affected = 0; - for (Player player : new VelocityPlayerSelector(args[0]).getPlayers((CommandSource) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - if (clientConnection.isConnected()) affected++; - clientConnection.sendMedia(media); - } - message(sender, "§aMedia created and requested to be played for " + affected + " clients"); - return; - } - - if (args.length == 3) { - try { - MediaOptions mediaOptions = OpenAudioMc.getGson().fromJson(args[2], MediaOptions.class); - - OptionalError parsingError = mediaOptions.validate(); - if (parsingError.isError()) { - message(sender, "§cError! " + parsingError.getMessage()); - return; - } - - Media media = new Media(args[1]).applySettings(mediaOptions); - for (Player player : new VelocityPlayerSelector(args[0]).getPlayers((CommandSource) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - clientConnection.sendMedia(media); - } - message(sender, "§aMedia (with arguments) created and requested to be played."); - } catch (Exception e) { - message(sender, "§cError. Invalid options. Please refer to the command guide."); - } - return; - } - sendHelp(sender); - } - - private void sendHelp(User genericExecutor) { - OpenAudioMc.getService(CommandService.class).getSubCommand("help").onExecute(genericExecutor, new String[]{ - getCommand() - }); - } -} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityStopCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityStopCommand.java deleted file mode 100644 index 444a08466..000000000 --- a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/commands/subcommand/VelocityStopCommand.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.craftmend.openaudiomc.velocity.modules.commands.subcommand; - -import com.craftmend.openaudiomc.OpenAudioMc; -import com.craftmend.openaudiomc.generic.commands.CommandService; - -import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; -import com.craftmend.openaudiomc.generic.commands.objects.Argument; -import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; -import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService; -import com.craftmend.openaudiomc.generic.networking.packets.client.media.PacketClientDestroyMedia; -import com.craftmend.openaudiomc.generic.user.User; -import com.craftmend.openaudiomc.velocity.modules.player.objects.VelocityPlayerSelector; -import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.proxy.Player; - -public class VelocityStopCommand extends SubCommand { - - public VelocityStopCommand(OpenAudioMc openAudioMc) { - super("stop"); - registerArguments( - new Argument("", - "Stops all manual sounds for all players in a selection"), - new Argument(" ", - "Only stops one specified sound for all players in the selection with a selected ID") - ); - } - - @Override - public void onExecute(User sender, String[] args) { - if (args.length == 0) { - sendHelp(sender); - return; - } - - if (args.length == 1) { - int affected = 0; - for (Player player : new VelocityPlayerSelector(args[0]).getPlayers((CommandSource) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - if (clientConnection.isConnected()) affected++; - OpenAudioMc.getService(NetworkingService.class).send(clientConnection, new PacketClientDestroyMedia(null)); - } - message(sender, "§aDestroyed all normal sounds for " + affected + " clients"); - return; - } - - if (args.length == 2) { - int affected = 0; - for (Player player : new VelocityPlayerSelector(args[0]).getPlayers((CommandSource) sender.getOriginal())) { - ClientConnection clientConnection = OpenAudioMc.getService(NetworkingService.class).getClient(player.getUniqueId()); - if (clientConnection.isConnected()) affected++; - OpenAudioMc.getService(NetworkingService.class).send(clientConnection, new PacketClientDestroyMedia(args[1])); - } - message(sender, "§aDestroyed the sound" + args[1] + " for " + affected + " clients"); - return; - } - - sendHelp(sender); - } - - private void sendHelp(User genericExecutor) { - OpenAudioMc.getService(CommandService.class).getSubCommand("help").onExecute(genericExecutor, new String[]{ - getCommand() - }); - } -} \ No newline at end of file diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/player/objects/VelocityPlayerSelector.java b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/player/objects/VelocityPlayerSelector.java index b57b7a959..508a78ac0 100644 --- a/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/player/objects/VelocityPlayerSelector.java +++ b/plugin/src/main/java/com/craftmend/openaudiomc/velocity/modules/player/objects/VelocityPlayerSelector.java @@ -1,11 +1,15 @@ package com.craftmend.openaudiomc.velocity.modules.player.objects; +import com.craftmend.openaudiomc.generic.commands.selectors.SelectorTranslator; import com.craftmend.openaudiomc.generic.logging.OpenAudioLogger; +import com.craftmend.openaudiomc.generic.user.User; +import com.craftmend.openaudiomc.generic.user.adapters.VelocityUserAdapter; import com.craftmend.openaudiomc.velocity.OpenAudioMcVelocity; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.Player; import lombok.AllArgsConstructor; import net.kyori.adventure.text.Component; +import org.bukkit.command.CommandSender; import java.util.ArrayList; import java.util.Collections; @@ -13,10 +17,30 @@ import java.util.Optional; import java.util.stream.Collectors; -@AllArgsConstructor -public class VelocityPlayerSelector { +public class VelocityPlayerSelector implements SelectorTranslator { - private final String selector; + private String selector; + private CommandSource source; + + @Override + public void setString(String selector) { + this.selector = selector; + } + + @Override + public void setSender(User sender) { + this.source = sender.getOriginal(); + } + + @Override + public List> getResults() { + List players = getPlayers(source); + List> users = new ArrayList<>(); + for (Player player : players) { + users.add(new VelocityUserAdapter(player)); + } + return users; + } /** * this turns selectors like @a[r=5] into a usable list, since @@ -25,7 +49,7 @@ public class VelocityPlayerSelector { * @param source the sender * @return players following the selector */ - public List getPlayers(CommandSource source) { + private List getPlayers(CommandSource source) { List players = new ArrayList<>(); if (selector.startsWith("@a")) { diff --git a/plugin/src/main/resources/data.bin b/plugin/src/main/resources/data.bin index 107567be3..06eea9ce4 100755 --- a/plugin/src/main/resources/data.bin +++ b/plugin/src/main/resources/data.bin @@ -1 +1 @@ -BUILD_NUM="1050" +BUILD_NUM="1056" diff --git a/plugin/src/main/resources/openaudiomc-build.properties b/plugin/src/main/resources/openaudiomc-build.properties index 68af46669..58ce0966e 100644 --- a/plugin/src/main/resources/openaudiomc-build.properties +++ b/plugin/src/main/resources/openaudiomc-build.properties @@ -1,3 +1,3 @@ -BUILD_VERSION="1050" -BUILD_COMMIT="825d93914af21843f5d3aaec0901231c141a783f" +BUILD_VERSION="1056" +BUILD_COMMIT="9a0fc10e04fb45767479f9654873332fd3ef9837" BUILD_AUTHOR="Mats"