Be more lenient with backwardsCommandSenderMappers

any GeyserCommandSource should be valid to use in any CommandManager as long as one of the following is satisfied
1. it is a platform implementation
2. isConsole() returns true
2. playerUuid() returns a valid uuid and the player lookup succeeds
This commit is contained in:
Konicai 2023-06-02 18:52:37 -04:00
parent 9da3345982
commit 37797d7ae3
No known key found for this signature in database
GPG Key ID: 710D09287708C823
14 changed files with 111 additions and 33 deletions

View File

@ -181,10 +181,9 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap {
this,
CommandExecutionCoordinator.simpleCoordinator(),
BungeeCommandSource::new,
origin -> (CommandSender) origin.handle()
this::convertCommandSource
);
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
this.geyserCommandManager.init();
if (geyserConfig.isLegacyPingPassthrough()) {
this.geyserBungeePingPassthrough = GeyserLegacyPingPassthrough.init(geyser);
@ -203,6 +202,28 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap {
}
}
private CommandSender convertCommandSource(GeyserCommandSource source) {
if (source.handle() instanceof CommandSender sender) {
return sender;
}
if (source.isConsole()) {
return getProxy().getConsole();
}
Optional<UUID> optionalUuid = source.playerUuid();
if (optionalUuid.isPresent()) {
UUID uuid = optionalUuid.get();
CommandSender sender = getProxy().getPlayer(uuid);
if (sender == null) {
throw new IllegalArgumentException("failed to find player for " + uuid);
}
return sender;
}
throw new IllegalArgumentException("failed to convert source for " + source);
}
@Override
public GeyserBungeeConfiguration getGeyserConfig() {
return geyserConfig;

View File

@ -27,8 +27,10 @@ package org.geysermc.geyser.platform.bungeecord.command;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.GeyserLocale;
@ -38,9 +40,9 @@ import java.util.UUID;
public class BungeeCommandSource implements GeyserCommandSource {
private final net.md_5.bungee.api.CommandSender handle;
private final CommandSender handle;
public BungeeCommandSource(net.md_5.bungee.api.CommandSender handle) {
public BungeeCommandSource(CommandSender handle) {
this.handle = handle;
// Ensure even Java players' languages are loaded
GeyserLocale.loadGeyserLocale(this.locale());
@ -52,7 +54,7 @@ public class BungeeCommandSource implements GeyserCommandSource {
}
@Override
public void sendMessage(String message) {
public void sendMessage(@NonNull String message) {
handle.sendMessage(TextComponent.fromLegacyText(message));
}

View File

@ -27,8 +27,6 @@ package org.geysermc.geyser.platform.fabric;
import cloud.commandframework.CommandManager;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.fabric.FabricClientCommandManager;
import cloud.commandframework.fabric.FabricCommandManager;
import cloud.commandframework.fabric.FabricServerCommandManager;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
@ -37,8 +35,8 @@ import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.player.Player;
import org.apache.logging.log4j.LogManager;
import org.geysermc.common.PlatformType;
import org.geysermc.geyser.GeyserBootstrap;
@ -150,10 +148,9 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
CommandManager<GeyserCommandSource> cloud = new FabricServerCommandManager<>(
CommandExecutionCoordinator.simpleCoordinator(),
FabricCommandSource::new,
source -> (CommandSourceStack) source.handle()
this::convertCommandSource
);
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
this.geyserCommandManager.init();
this.geyserWorldManager = new GeyserFabricWorldManager(server);
}
@ -169,6 +166,28 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
}
}
private CommandSourceStack convertCommandSource(GeyserCommandSource source) {
if (source.handle() instanceof CommandSourceStack stack) {
return stack;
}
if (source.isConsole()) {
return server.createCommandSourceStack(); // todo: commands something better?
}
Optional<UUID> optionalUUID = source.playerUuid();
if (optionalUUID.isPresent()) {
UUID uuid = optionalUUID.get();
Player player = server.getPlayerList().getPlayer(uuid);
if (player == null) {
throw new IllegalArgumentException("failed to find player for " + uuid);
}
return player.createCommandSourceStack();
}
throw new IllegalArgumentException("failed to convert source for " + source);
}
@Override
public GeyserConfiguration getGeyserConfig() {
return geyserConfig;

View File

@ -30,11 +30,11 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.ChatColor;
import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.UUID;
@ -52,7 +52,7 @@ public class FabricCommandSource implements GeyserCommandSource {
}
@Override
public void sendMessage(@Nonnull String message) {
public void sendMessage(@NonNull String message) {
if (source.getEntity() instanceof ServerPlayer) {
((ServerPlayer) source.getEntity()).displayClientMessage(Component.literal(message), false);
} else {

View File

@ -73,6 +73,7 @@ import java.net.SocketAddress;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
@ -175,7 +176,7 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
this,
CommandExecutionCoordinator.simpleCoordinator(),
SpigotCommandSource::new,
source -> (CommandSender) source.handle()
this::convertCommandSource
);
} catch (Exception e) {
throw new RuntimeException(e);
@ -185,15 +186,14 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
// Should always be available on 1.13 and up
cloud.registerBrigadier();
} catch (BukkitCommandManager.BrigadierFailureException e) {
geyserLogger.error("Failed to initialize Brigadier support: " + e.getMessage());
geyserLogger.debug("Failed to initialize Brigadier support: " + e.getMessage());
if (e.getReason() == BukkitCommandManager.BrigadierFailureReason.VERSION_TOO_HIGH) {
// Commodore brig only supports Spigot 1.13 - 1.18.2
geyserLogger.error("Using Paper instead of Spigot will likely fix this.");
geyserLogger.debug("Using Paper instead of Spigot will likely fix this.");
}
}
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
this.geyserCommandManager.init();
if (!INITIALIZED) {
// Needs to be an anonymous inner class otherwise Bukkit complains about missing classes
@ -342,6 +342,28 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
}
}
private CommandSender convertCommandSource(GeyserCommandSource source) {
if (source.handle() instanceof CommandSender sender) {
return sender;
}
if (source.isConsole()) {
return getServer().getConsoleSender();
}
Optional<UUID> optionalUuid = source.playerUuid();
if (optionalUuid.isPresent()) {
UUID uuid = optionalUuid.get();
CommandSender sender = getServer().getPlayer(uuid);
if (sender == null) {
throw new IllegalArgumentException("failed to find player for " + uuid);
}
return sender;
}
throw new IllegalArgumentException("failed to convert source for " + source);
}
@Override
public GeyserSpigotConfiguration getGeyserConfig() {
return geyserConfig;

View File

@ -27,8 +27,10 @@ package org.geysermc.geyser.platform.spigot.command;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.platform.spigot.PaperAdventure;
import org.geysermc.geyser.text.GeyserLocale;
@ -37,9 +39,9 @@ import java.util.Optional;
import java.util.UUID;
public class SpigotCommandSource implements GeyserCommandSource {
private final org.bukkit.command.CommandSender handle;
private final CommandSender handle;
public SpigotCommandSource(org.bukkit.command.CommandSender handle) {
public SpigotCommandSource(CommandSender handle) {
this.handle = handle;
// Ensure even Java players' languages are loaded
GeyserLocale.loadGeyserLocale(locale());
@ -51,7 +53,7 @@ public class SpigotCommandSource implements GeyserCommandSource {
}
@Override
public void sendMessage(String message) {
public void sendMessage(@NonNull String message) {
handle.sendMessage(message);
}

View File

@ -46,7 +46,6 @@ import org.spongepowered.api.event.lifecycle.ConstructPluginEvent;
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;
import javax.annotation.Nonnull;
@ -67,9 +66,6 @@ public class GeyserSpongePlugin implements GeyserBootstrap {
*/
private boolean enabled = true;
@Inject
private PluginContainer pluginContainer;
@Inject
private Logger logger;
@ -141,7 +137,6 @@ public class GeyserSpongePlugin implements GeyserBootstrap {
this.geyser = GeyserImpl.load(PlatformType.SPONGE, this);
this.geyserCommandManager = new GeyserCommandManager(geyser, Objects.requireNonNull(null)); // todo: commands
this.geyserCommandManager.init();
}
/**

View File

@ -221,7 +221,6 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
GeyserImpl.start();
geyserCommandManager = new GeyserCommandManager(geyser, new GeyserStandaloneCommandManager());
geyserCommandManager.init();
if (gui != null) {
gui.setupInterface(geyserLogger, geyserCommandManager);

View File

@ -61,6 +61,7 @@ import java.io.IOException;
import java.net.SocketAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.UUID;
@Plugin(id = "geyser", name = GeyserImpl.NAME + "-Velocity", version = GeyserImpl.VERSION, url = "https://geysermc.org", authors = "GeyserMC")
@ -139,10 +140,9 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
proxyServer,
CommandExecutionCoordinator.simpleCoordinator(),
VelocityCommandSource::new,
origin -> (CommandSource) origin.handle()
this::convertCommandSource
);
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
this.geyserCommandManager.init();
if (geyserConfig.isLegacyPingPassthrough()) {
this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(geyser);
@ -163,6 +163,24 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
}
}
private CommandSource convertCommandSource(GeyserCommandSource source) {
if (source.handle() instanceof CommandSource velocitySource) {
return velocitySource;
}
if (source.isConsole()) {
return proxyServer.getConsoleCommandSource();
}
Optional<UUID> optionalUUID = source.playerUuid();
if (optionalUUID.isPresent()) {
UUID uuid = optionalUUID.get();
return proxyServer.getPlayer(uuid).orElseThrow(() -> new IllegalArgumentException("failed to find player for " + uuid));
}
throw new IllegalArgumentException("failed to convert source for " + source);
}
@Override
public GeyserVelocityConfiguration getGeyserConfig() {
return geyserConfig;

View File

@ -30,6 +30,7 @@ import com.velocitypowered.api.proxy.ConsoleCommandSource;
import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.GeyserLocale;
@ -58,7 +59,7 @@ public class VelocityCommandSource implements GeyserCommandSource {
}
@Override
public void sendMessage(String message) {
public void sendMessage(@NonNull String message) {
handle.sendMessage(LegacyComponentSerializer.legacy('§').deserialize(message));
}

View File

@ -26,6 +26,7 @@
package org.geysermc.geyser;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.command.GeyserCommandSource;
import javax.annotation.Nullable;
@ -121,7 +122,7 @@ public interface GeyserLogger extends GeyserCommandSource {
}
@Override
default void sendMessage(String message) {
default void sendMessage(@NonNull String message) {
info(message);
}

View File

@ -72,9 +72,7 @@ public class GeyserCommandManager {
public GeyserCommandManager(GeyserImpl geyser, CommandManager<GeyserCommandSource> cloud) {
this.geyser = geyser;
this.cloud = cloud;
}
public void init() {
registerBuiltInCommand(new HelpCommand(geyser, "help", "geyser.commands.help.desc", "geyser.command.help", "geyser", this.commands));
registerBuiltInCommand(new ListCommand(geyser, "list", "geyser.commands.list.desc", "geyser.command.list"));
registerBuiltInCommand(new ReloadCommand(geyser, "reload", "geyser.commands.reload.desc", "geyser.command.reload"));

View File

@ -1390,7 +1390,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
}
@Override
public void sendMessage(String message) {
public void sendMessage(@NonNull String message) {
TextPacket textPacket = new TextPacket();
textPacket.setPlatformChatId("");
textPacket.setSourceName("");

@ -1 +1 @@
Subproject commit f6685c4ccc6e77b07402d45cb41213559004b7d6
Subproject commit 24be9ef7f850f7d180650a65792c266c709cadf5