Merged some modules to fix future dependency issues

This commit is contained in:
2022-12-24 11:33:54 +01:00
parent 6564beb3a0
commit 0756781b26
80 changed files with 50 additions and 224 deletions

View File

@@ -31,6 +31,11 @@
<artifactId>pandalib-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-chat</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-players</artifactId>
@@ -41,6 +46,11 @@
<artifactId>pandalib-reflect</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fr.pandacube.lib</groupId>
<artifactId>pandalib-commands</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>

View File

@@ -0,0 +1,136 @@
package fr.pandacube.lib.bungee.commands;
import com.mojang.brigadier.context.StringRange;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.tree.LiteralCommandNode;
import fr.pandacube.lib.commands.BrigadierCommand;
import fr.pandacube.lib.commands.SuggestionsSupplier;
import fr.pandacube.lib.util.Log;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Abstract class that holds the logic of a specific command to be registered in {@link BungeeBrigadierDispatcher} and
* BungeeCord command API.
*/
public abstract class BungeeBrigadierCommand extends BrigadierCommand<CommandSender> {
/**
* The command dispatcher.
*/
protected BungeeBrigadierDispatcher dispatcher;
/**
* Instanciate this command instance.
* @param d the dispatcher in which to register this command.
*/
public BungeeBrigadierCommand(BungeeBrigadierDispatcher d) {
if (d == null) {
throw new IllegalStateException("BungeeBrigadierDispatcher not provided.");
}
dispatcher = d;
LiteralCommandNode<CommandSender> commandNode;
String[] aliases;
try {
commandNode = buildCommand().build();
postBuildCommand(commandNode);
aliases = getAliases();
} catch (Exception e) {
Log.severe("Exception encountered when building Brigadier command " + getClass().getName(), e);
return;
}
if (aliases == null)
aliases = new String[0];
dispatcher.register(commandNode);
// still have to be registered for console
ProxyServer.getInstance().getPluginManager().registerCommand(dispatcher.plugin, new CommandRelay(commandNode.getLiteral()));
for (String alias : aliases) {
dispatcher.register(literal(alias)
.requires(commandNode.getRequirement())
.executes(commandNode.getCommand())
.redirect(commandNode)
.build()
);
ProxyServer.getInstance().getPluginManager().registerCommand(dispatcher.plugin, new CommandRelay(alias));
}
}
private class CommandRelay extends Command implements TabExecutor {
private final String alias;
public CommandRelay(String alias) {
super(alias);
this.alias = alias;
}
@Override
public void execute(CommandSender sender, String[] args) {
dispatcher.execute(sender, alias + (args.length == 0 ? "" : (" " + String.join(" ", args))));
}
@Override
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
String cursor = "/" + alias + " " + String.join(" ", args);
StringRange supportedRange = StringRange.between(cursor.lastIndexOf(' ') + 1, cursor.length());
Suggestions suggestions = dispatcher.getSuggestions(sender, cursor.substring(1));
if (!suggestions.getRange().equals(supportedRange))
return Collections.emptyList();
return suggestions.getList()
.stream()
.filter(s -> s.getRange().equals(supportedRange))
.map(Suggestion::getText)
.collect(Collectors.toList());
}
}
public boolean isConsole(CommandSender sender) {
return ProxyServer.getInstance().getConsole().equals(sender);
}
public boolean isPlayer(CommandSender sender) {
return sender instanceof ProxiedPlayer;
}
public Predicate<CommandSender> hasPermission(String permission) {
return sender -> sender.hasPermission(permission);
}
/**
* Wraps the provided {@link SuggestionsSupplier} into a Brigadiers {@link SuggestionProvider}.
* @param suggestions the suggestions to wrap.
* @return a {@link SuggestionProvider} generating the suggestions from the provided {@link SuggestionsSupplier}.
*/
protected SuggestionProvider<CommandSender> wrapSuggestions(SuggestionsSupplier<CommandSender> suggestions) {
return wrapSuggestions(suggestions, Function.identity());
}
}

View File

@@ -0,0 +1,62 @@
package fr.pandacube.lib.bungee.commands;
import fr.pandacube.lib.chat.Chat;
import fr.pandacube.lib.commands.BrigadierDispatcher;
import net.kyori.adventure.text.ComponentLike;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
/**
* Implementation of {@link BrigadierDispatcher} that integrates the commands into BungeeCord API, so the players and
* the console can actually execute them.
*/
public class BungeeBrigadierDispatcher extends BrigadierDispatcher<CommandSender> implements Listener {
/* package */ final Plugin plugin;
/**
* Create a new instance of {@link BungeeBrigadierDispatcher}.
* @param pl the plugin that creates this dispatcher.
*/
public BungeeBrigadierDispatcher(Plugin pl) {
plugin = pl;
ProxyServer.getInstance().getPluginManager().registerListener(plugin, this);
}
/**
* Called when a player sends a chat message. Used to gets the typed command and execute it.
* @param event the event.
*/
@EventHandler
public void onChat(ChatEvent event) {
if (!event.getMessage().startsWith("/"))
return;
String commandLine = event.getMessage().substring(1);
String commandName = commandLine.split(" ", -1)[0];
if (getDispatcher().getRoot().getChild(commandName) == null)
return;
event.setCancelled(true);
ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> execute((ProxiedPlayer) event.getSender(), commandLine));
}
@Override
protected void sendSenderMessage(CommandSender sender, ComponentLike message) {
sender.sendMessage(Chat.toBungee(message.asComponent()));
}
}