mirror of https://github.com/GeyserMC/Geyser.git
Refactor CommandSourceConverter
This commit is contained in:
parent
e2f71af6e7
commit
411c289fa7
|
@ -177,7 +177,7 @@ 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());
|
||||
var sourceConverter = new CommandSourceConverter<>(CommandSender.class, id -> getProxy().getPlayer(id), () -> getProxy().getConsole());
|
||||
CommandManager<GeyserCommandSource> cloud = new BungeeCommandManager<>(
|
||||
this,
|
||||
CommandExecutionCoordinator.simpleCoordinator(),
|
||||
|
|
|
@ -145,7 +145,7 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
|
|||
|
||||
this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(geyser);
|
||||
|
||||
var sourceConverter = new CommandSourceConverter<>(
|
||||
var sourceConverter = CommandSourceConverter.layered(
|
||||
CommandSourceStack.class,
|
||||
id -> server.getPlayerList().getPlayer(id),
|
||||
Player::createCommandSourceStack,
|
||||
|
|
|
@ -170,7 +170,7 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
|
|||
return;
|
||||
}
|
||||
|
||||
var sourceConverter = CommandSourceConverter.simple(CommandSender.class, Bukkit::getPlayer, Bukkit::getConsoleSender);
|
||||
var sourceConverter = new CommandSourceConverter<>(CommandSender.class, Bukkit::getPlayer, Bukkit::getConsoleSender);
|
||||
PaperCommandManager<GeyserCommandSource> cloud;
|
||||
try {
|
||||
cloud = new PaperCommandManager<>(
|
||||
|
|
|
@ -135,7 +135,7 @@ 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);
|
||||
var sourceConverter = new CommandSourceConverter<>(CommandSource.class, id -> proxyServer.getPlayer(id).orElse(null), proxyServer::getConsoleCommandSource);
|
||||
CommandManager<GeyserCommandSource> cloud = new VelocityCommandManager<>(
|
||||
container,
|
||||
proxyServer,
|
||||
|
|
|
@ -25,18 +25,17 @@
|
|||
|
||||
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,
|
||||
public record CommandSourceConverter<S>(Class<S> senderType,
|
||||
Function<UUID, S> playerLookup,
|
||||
Supplier<S> consoleProvider) {
|
||||
|
||||
public S convert(GeyserCommandSource source) throws IllegalArgumentException {
|
||||
Object handle = source.handle();
|
||||
if (senderType.isAssignableFrom(source.handle().getClass())) {
|
||||
if (senderType.isInstance(handle)) {
|
||||
return (S) handle; // one of the server platform implementations
|
||||
}
|
||||
|
||||
|
@ -44,26 +43,23 @@ public record CommandSourceConverter<S, P>(Class<S> senderType,
|
|||
return consoleProvider.get(); // one of the loggers
|
||||
}
|
||||
|
||||
// GeyserSession
|
||||
Optional<UUID> optionalUUID = source.playerUuid();
|
||||
if (optionalUUID.isPresent()) {
|
||||
UUID uuid = optionalUUID.get();
|
||||
// Handles GeyserSession
|
||||
return source.playerUuid()
|
||||
.map(playerLookup)
|
||||
.orElseThrow(() -> new IllegalArgumentException("failed to find sender for name=%s, uuid=%s".formatted(source.name(), source.playerUuid())));
|
||||
}
|
||||
|
||||
public static <P, S> CommandSourceConverter<S> layered(Class<S> senderType,
|
||||
Function<UUID, P> playerLookup,
|
||||
Function<P, S> senderLookup,
|
||||
Supplier<S> consoleProvider) {
|
||||
Function<UUID, S> lookup = uuid -> {
|
||||
P player = playerLookup.apply(uuid);
|
||||
if (player == null) {
|
||||
throw new IllegalArgumentException("failed to find player for " + uuid);
|
||||
return null;
|
||||
}
|
||||
|
||||
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);
|
||||
};
|
||||
return new CommandSourceConverter<>(senderType, lookup, consoleProvider);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue