Cleanup the last commit

This commit is contained in:
Konicai 2023-06-02 20:27:29 -04:00
parent 37797d7ae3
commit 8e6d7d3cc0
No known key found for this signature in database
GPG Key ID: 710D09287708C823
5 changed files with 87 additions and 92 deletions

View File

@ -38,6 +38,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.common.PlatformType;
import org.geysermc.geyser.GeyserBootstrap;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.CommandSourceConverter;
import org.geysermc.geyser.command.GeyserCommandManager;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.configuration.GeyserConfiguration;
@ -176,12 +177,12 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap {
this.geyserInjector = new GeyserBungeeInjector(this);
this.geyserInjector.initializeLocalChannel(this);
var sourceConverter = CommandSourceConverter.simple(CommandSender.class, id -> getProxy().getPlayer(id), () -> getProxy().getConsole());
CommandManager<GeyserCommandSource> cloud = new BungeeCommandManager<>(
this,
CommandExecutionCoordinator.simpleCoordinator(),
BungeeCommandSource::new,
this::convertCommandSource
sourceConverter::convert
);
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
@ -202,28 +203,6 @@ 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

@ -42,6 +42,7 @@ import org.geysermc.common.PlatformType;
import org.geysermc.geyser.GeyserBootstrap;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.command.CommandSourceConverter;
import org.geysermc.geyser.command.GeyserCommandManager;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.configuration.GeyserConfiguration;
@ -144,11 +145,16 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(geyser);
var sourceConverter = new CommandSourceConverter<>(
CommandSourceStack.class,
id -> server.getPlayerList().getPlayer(id),
Player::createCommandSourceStack,
server::createCommandSourceStack
);
CommandManager<GeyserCommandSource> cloud = new FabricServerCommandManager<>(
CommandExecutionCoordinator.simpleCoordinator(),
FabricCommandSource::new,
this::convertCommandSource
sourceConverter::convert
);
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
@ -166,28 +172,6 @@ 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

@ -49,6 +49,7 @@ import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.adapters.spigot.SpigotAdapters;
import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.api.extension.Extension;
import org.geysermc.geyser.command.CommandSourceConverter;
import org.geysermc.geyser.command.GeyserCommandManager;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.configuration.GeyserConfiguration;
@ -73,7 +74,6 @@ 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;
@ -170,13 +170,14 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
return;
}
var sourceConverter = CommandSourceConverter.simple(CommandSender.class, Bukkit::getPlayer, Bukkit::getConsoleSender);
PaperCommandManager<GeyserCommandSource> cloud;
try {
cloud = new PaperCommandManager<>(
this,
CommandExecutionCoordinator.simpleCoordinator(),
SpigotCommandSource::new,
this::convertCommandSource
sourceConverter::convert
);
} catch (Exception e) {
throw new RuntimeException(e);
@ -342,28 +343,6 @@ 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

@ -43,6 +43,7 @@ import net.kyori.adventure.util.Codec;
import org.geysermc.common.PlatformType;
import org.geysermc.geyser.GeyserBootstrap;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.CommandSourceConverter;
import org.geysermc.geyser.command.GeyserCommandManager;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.configuration.GeyserConfiguration;
@ -61,7 +62,6 @@ 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")
@ -135,12 +135,13 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
this.geyserInjector = new GeyserVelocityInjector(proxyServer);
// Will be initialized after the proxy has been bound
var sourceConverter = CommandSourceConverter.simple(CommandSource.class, id -> proxyServer.getPlayer(id).get(), proxyServer::getConsoleCommandSource);
CommandManager<GeyserCommandSource> cloud = new VelocityCommandManager<>(
container,
proxyServer,
CommandExecutionCoordinator.simpleCoordinator(),
VelocityCommandSource::new,
this::convertCommandSource
sourceConverter::convert
);
this.geyserCommandManager = new GeyserCommandManager(geyser, cloud);
@ -163,24 +164,6 @@ 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

@ -0,0 +1,70 @@
/*
* Copyright (c) 2019-2023 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.command;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
public record CommandSourceConverter<S, P>(Class<S> senderType,
Function<UUID, P> playerLookup, Function<P, S> senderLookup,
Supplier<S> consoleProvider) {
public S convert(GeyserCommandSource source) throws IllegalArgumentException {
Object handle = source.handle();
if (senderType.isAssignableFrom(source.handle().getClass())) {
return (S) handle; // one of the server platform implementations
}
if (source.isConsole()) {
return consoleProvider.get(); // one of the loggers
}
// GeyserSession
Optional<UUID> optionalUUID = source.playerUuid();
if (optionalUUID.isPresent()) {
UUID uuid = optionalUUID.get();
P player = playerLookup.apply(uuid);
if (player == null) {
throw new IllegalArgumentException("failed to find player for " + uuid);
}
return senderLookup.apply(player);
}
throw new IllegalArgumentException("failed to convert source for " + source);
}
public static <S, P extends S> CommandSourceConverter<S, P> simple(Class<S> senderType,
Function<UUID, P> playerLookup,
Supplier<S> consoleProvider) {
return new CommandSourceConverter<>(senderType, playerLookup, s -> s, consoleProvider);
}
}