Don't use Optional for CommandSource#playerUuid/connection

This commit is contained in:
Konicai 2023-10-02 18:04:50 -04:00
parent 0cfee52e48
commit 956d60721c
9 changed files with 44 additions and 35 deletions

View File

@ -26,9 +26,9 @@
package org.geysermc.geyser.api.command;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.api.connection.Connection;
import java.util.Optional;
import java.util.UUID;
/**
@ -69,15 +69,15 @@ public interface CommandSource {
boolean isConsole();
/**
* @return an Optional containing a Java UUID if this source represents any player, otherwise empty
* @return a Java UUID if this source represents a player, otherwise null
*/
Optional<UUID> playerUuid();
@Nullable UUID playerUuid();
/**
* @return an Optional with a present Connection if this source represents a Bedrock player that is connected
* to this Geyser instance. Otherwise, returns an empty optional.
* @return a Connection if this source represents a Bedrock player that is connected
* to this Geyser instance, otherwise null
*/
Optional<? extends Connection> connection();
@Nullable Connection connection();
/**
* Returns the locale of the command source.

View File

@ -31,11 +31,11 @@ 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.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.GeyserLocale;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;
public class BungeeCommandSource implements GeyserCommandSource {
@ -76,11 +76,11 @@ public class BungeeCommandSource implements GeyserCommandSource {
}
@Override
public Optional<UUID> playerUuid() {
public @Nullable UUID playerUuid() {
if (handle instanceof ProxiedPlayer player) {
return Optional.of(player.getUniqueId());
return player.getUniqueId();
}
return Optional.empty();
return null;
}
@Override

View File

@ -31,11 +31,11 @@ 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.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.ChatColor;
import java.util.Optional;
import java.util.UUID;
public class FabricCommandSource implements GeyserCommandSource {
@ -76,11 +76,11 @@ public class FabricCommandSource implements GeyserCommandSource {
}
@Override
public Optional<UUID> playerUuid() {
public @Nullable UUID playerUuid() {
if (source.getEntity() instanceof ServerPlayer player) {
return Optional.of(player.getUUID());
return player.getUUID();
}
return Optional.empty();
return null;
}
@Override

View File

@ -31,6 +31,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.platform.spigot.PaperAdventure;
import org.geysermc.geyser.text.GeyserLocale;
@ -79,11 +80,11 @@ public class SpigotCommandSource implements GeyserCommandSource {
}
@Override
public Optional<UUID> playerUuid() {
public @Nullable UUID playerUuid() {
if (handle instanceof Player player) {
return Optional.of(player.getUniqueId());
return player.getUniqueId();
}
return Optional.empty();
return null;
}
@Override

View File

@ -31,6 +31,7 @@ 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.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.GeyserLocale;
@ -75,11 +76,11 @@ public class VelocityCommandSource implements GeyserCommandSource {
}
@Override
public Optional<UUID> playerUuid() {
public @Nullable UUID playerUuid() {
if (handle instanceof Player player) {
return Optional.of(player.getUniqueId());
return player.getUniqueId();
}
return Optional.empty();
return null;
}
@Override

View File

@ -27,10 +27,9 @@ package org.geysermc.geyser;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.command.GeyserCommandSource;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.UUID;
public interface GeyserLogger extends GeyserCommandSource {
@ -132,8 +131,8 @@ public interface GeyserLogger extends GeyserCommandSource {
}
@Override
default Optional<UUID> playerUuid() {
return Optional.empty();
default @Nullable UUID playerUuid() {
return null;
}
@Override

View File

@ -64,10 +64,13 @@ public record CommandSourceConverter<S>(Class<S> senderType,
}
}
// Ideally this should only handle GeyserSession
return source.playerUuid()
.map(playerLookup)
.orElseThrow(() -> new IllegalArgumentException("failed to find sender for name=%s, uuid=%s".formatted(source.name(), source.playerUuid())));
// Ideally lookup should only be necessary for GeyserSession
UUID uuid = source.playerUuid();
if (uuid != null) {
return playerLookup.apply(uuid);
}
throw new IllegalArgumentException("failed to find sender for name=%s, uuid=%s".formatted(source.name(), source.playerUuid()));
}
/**

View File

@ -25,6 +25,7 @@
package org.geysermc.geyser.command;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.command.CommandSource;
import org.geysermc.geyser.session.GeyserSession;
@ -32,7 +33,7 @@ import org.geysermc.geyser.text.GeyserLocale;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.Optional;
import java.util.UUID;
/**
* Implemented on top of any class that can send a command.
@ -60,8 +61,12 @@ public interface GeyserCommandSource extends CommandSource {
}
@Override
default Optional<GeyserSession> connection() {
return playerUuid().map(id -> GeyserImpl.getInstance().connectionByUuid(id));
default @Nullable GeyserSession connection() {
UUID uuid = playerUuid();
if (uuid == null) {
return null;
}
return GeyserImpl.getInstance().connectionByUuid(uuid);
}
/**

View File

@ -1451,13 +1451,13 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
}
@Override
public Optional<UUID> playerUuid() {
return Optional.of(playerEntity.getUuid());
public @NonNull UUID playerUuid() {
return playerEntity.getUuid();
}
@Override
public Optional<GeyserSession> connection() {
return Optional.of(this);
public @NonNull GeyserSession connection() {
return this;
}
@Override