Spigot: enable command completions for /geyser

This commit is contained in:
Camotoy 2022-03-24 17:39:35 -04:00
parent 877301a500
commit 80b6d14cee
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
4 changed files with 152 additions and 7 deletions

View file

@ -19,6 +19,11 @@
<id>viaversion-repo</id> <id>viaversion-repo</id>
<url>https://repo.viaversion.com</url> <url>https://repo.viaversion.com</url>
</repository> </repository>
<repository>
<!-- For Commodore -->
<id>minecraft-repo</id>
<url>https://libraries.minecraft.net/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -34,6 +39,12 @@
<version>1.18.1-R0.1-SNAPSHOT</version> <version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-mojangapi</artifactId>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>com.viaversion</groupId> <groupId>com.viaversion</groupId>
<artifactId>viaversion</artifactId> <artifactId>viaversion</artifactId>
@ -45,6 +56,12 @@
<artifactId>spigot-all</artifactId> <artifactId>spigot-all</artifactId>
<version>1.4-SNAPSHOT</version> <version>1.4-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>me.lucko</groupId>
<artifactId>commodore</artifactId>
<version>1.13</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>${outputName}-Spigot</finalName> <finalName>${outputName}-Spigot</finalName>
@ -95,6 +112,10 @@
<pattern>org.objectweb.asm</pattern> <pattern>org.objectweb.asm</pattern>
<shadedPattern>org.geysermc.geyser.platform.spigot.shaded.asm</shadedPattern> <shadedPattern>org.geysermc.geyser.platform.spigot.shaded.asm</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>me.lucko.commodore</pattern>
<shadedPattern>org.geysermc.geyser.platform.spigot.shaded.commodore</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</execution> </execution>
@ -118,6 +139,7 @@
<exclude>io.netty:netty-codec-dns:*</exclude> <exclude>io.netty:netty-codec-dns:*</exclude>
<exclude>io.netty:netty-resolver-dns:*</exclude> <exclude>io.netty:netty-resolver-dns:*</exclude>
<exclude>io.netty:netty-resolver-dns-native-macos:*</exclude> <exclude>io.netty:netty-resolver-dns-native-macos:*</exclude>
<exclude>com.mojang:*</exclude> <!-- Commodore includes Brigadier -->
</excludes> </excludes>
</artifactSet> </artifactSet>
</configuration> </configuration>

View file

@ -25,11 +25,15 @@
package org.geysermc.geyser.platform.spigot; package org.geysermc.geyser.platform.spigot;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry; import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import me.lucko.commodore.Commodore;
import me.lucko.commodore.CommodoreProvider;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.geysermc.common.PlatformType; import org.geysermc.common.PlatformType;
import org.geysermc.geyser.Constants; import org.geysermc.geyser.Constants;
@ -43,6 +47,7 @@ import org.geysermc.geyser.level.WorldManager;
import org.geysermc.geyser.network.MinecraftProtocol; import org.geysermc.geyser.network.MinecraftProtocol;
import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough; import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough;
import org.geysermc.geyser.ping.IGeyserPingPassthrough; import org.geysermc.geyser.ping.IGeyserPingPassthrough;
import org.geysermc.geyser.platform.spigot.command.GeyserPaperCommandListener;
import org.geysermc.geyser.platform.spigot.command.GeyserSpigotCommandExecutor; import org.geysermc.geyser.platform.spigot.command.GeyserSpigotCommandExecutor;
import org.geysermc.geyser.platform.spigot.command.GeyserSpigotCommandManager; import org.geysermc.geyser.platform.spigot.command.GeyserSpigotCommandManager;
import org.geysermc.geyser.platform.spigot.command.SpigotCommandSender; import org.geysermc.geyser.platform.spigot.command.SpigotCommandSender;
@ -234,7 +239,29 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
Bukkit.getServer().getPluginManager().registerEvents(new GeyserPistonListener(geyser, this.geyserWorldManager), this); Bukkit.getServer().getPluginManager().registerEvents(new GeyserPistonListener(geyser, this.geyserWorldManager), this);
this.getCommand("geyser").setExecutor(new GeyserSpigotCommandExecutor(geyser)); PluginCommand pluginCommand = this.getCommand("geyser");
pluginCommand.setExecutor(new GeyserSpigotCommandExecutor(geyser));
boolean brigadierSupported = CommodoreProvider.isSupported();
geyserLogger.debug("Brigadier supported? " + brigadierSupported);
if (brigadierSupported) {
// Enable command completions if supported
// This is beneficial because this is sent over the network and Bedrock can see it
Commodore commodore = CommodoreProvider.getCommodore(this);
LiteralArgumentBuilder<?> builder = LiteralArgumentBuilder.literal("geyser");
for (String command : geyserCommandManager.getCommands().keySet()) {
builder.then(LiteralArgumentBuilder.literal(command));
}
commodore.register(pluginCommand, builder);
try {
Class.forName("com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent");
Bukkit.getServer().getPluginManager().registerEvents(new GeyserPaperCommandListener(), this);
geyserLogger.debug("Successfully registered AsyncPlayerSendCommandsEvent listener.");
} catch (ClassNotFoundException e) {
geyserLogger.debug("Not registering AsyncPlayerSendCommandsEvent listener.");
}
}
// Check to ensure the current setup can support the protocol version Geyser uses // Check to ensure the current setup can support the protocol version Geyser uses
GeyserSpigotVersionChecker.checkForSupportedProtocol(geyserLogger, isViaVersion); GeyserSpigotVersionChecker.checkForSupportedProtocol(geyserLogger, isViaVersion);

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.platform.spigot.command;
import com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent;
import com.mojang.brigadier.tree.CommandNode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.GeyserCommand;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.Map;
public final class GeyserPaperCommandListener implements Listener {
@EventHandler
@SuppressWarnings("deprecation") // Used to indicate an unstable event
public void onCommandSend(AsyncPlayerSendCommandsEvent<?> event) {
// Documentation says to check (event.isAsynchronous() || !event.hasFiredAsync()), but as of Paper 1.18.2
// event.hasFiredAsync is never true
if (event.isAsynchronous()) {
CommandNode<?> geyserBrigadier = event.getCommandNode().getChild("geyser");
if (geyserBrigadier != null) {
Player player = event.getPlayer();
boolean isJavaPlayer = isProbablyJavaPlayer(player);
Map<String, GeyserCommand> commands = GeyserImpl.getInstance().getCommandManager().getCommands();
Iterator<? extends CommandNode<?>> it = geyserBrigadier.getChildren().iterator();
while (it.hasNext()) {
CommandNode<?> subnode = it.next();
GeyserCommand command = commands.get(subnode.getName());
if (command != null) {
if ((command.isBedrockOnly() && isJavaPlayer) || !player.hasPermission(command.getPermission())) {
// Remove this from the node as we don't have permission to use it
it.remove();
}
}
}
}
}
}
/**
* This early on, there is a rare chance that Geyser has yet to process the connection. We'll try to minimize that
* chance, though.
*/
private boolean isProbablyJavaPlayer(Player player) {
if (GeyserImpl.getInstance().connectionByUuid(player.getUniqueId()) != null) {
// For sure this is a Bedrock player
return false;
}
if (GeyserImpl.getInstance().getConfig().isUseDirectConnection()) {
InetSocketAddress address = player.getAddress();
if (address != null) {
return address.getPort() != 0;
}
}
return true;
}
}

View file

@ -26,6 +26,7 @@
package org.geysermc.geyser.platform.spigot.command; package org.geysermc.geyser.platform.spigot.command;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
@ -35,17 +36,25 @@ import java.lang.reflect.Field;
public class GeyserSpigotCommandManager extends CommandManager { public class GeyserSpigotCommandManager extends CommandManager {
private static CommandMap COMMAND_MAP; private static final CommandMap COMMAND_MAP;
static { static {
CommandMap commandMap = null;
try {
// Paper-only
Server.class.getMethod("getCommandMap");
commandMap = Bukkit.getServer().getCommandMap();
} catch (NoSuchMethodException e) {
try { try {
Field cmdMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap"); Field cmdMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
cmdMapField.setAccessible(true); cmdMapField.setAccessible(true);
COMMAND_MAP = (CommandMap) cmdMapField.get(Bukkit.getServer()); commandMap = (CommandMap) cmdMapField.get(Bukkit.getServer());
} catch (NoSuchFieldException | IllegalAccessException ex) { } catch (NoSuchFieldException | IllegalAccessException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
COMMAND_MAP = commandMap;
}
public GeyserSpigotCommandManager(GeyserImpl geyser) { public GeyserSpigotCommandManager(GeyserImpl geyser) {
super(geyser); super(geyser);