Remove essentially all use of Java Optionals from the API

Also fix extended handshake event handling
This commit is contained in:
Andrew Steinborn
2021-05-12 12:33:55 -04:00
parent 6fcef41146
commit 00cc636d6a
76 changed files with 478 additions and 427 deletions

View File

@@ -9,7 +9,6 @@ package com.velocitypowered.api.event;
import com.google.common.base.Preconditions;
import java.util.Objects;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -85,34 +84,30 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
*/
final class ComponentResult implements Result {
private static final ComponentResult ALLOWED = new ComponentResult(true, null);
private static final ComponentResult ALLOWED = new ComponentResult(null);
private final boolean status;
private final @Nullable Component reason;
protected ComponentResult(boolean status, @Nullable Component reason) {
this.status = status;
protected ComponentResult(@Nullable Component reason) {
this.reason = reason;
}
@Override
public boolean isAllowed() {
return status;
return reason == null;
}
public Optional<Component> reason() {
return Optional.ofNullable(reason);
public @Nullable Component reason() {
return reason;
}
@Override
public String toString() {
if (status) {
if (reason == null) {
return "allowed";
}
if (reason != null) {
} else {
return "denied: " + PlainComponentSerializer.plain().serialize(reason);
}
return "denied";
}
public static ComponentResult allowed() {
@@ -121,7 +116,7 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
public static ComponentResult denied(Component reason) {
Preconditions.checkNotNull(reason, "reason");
return new ComponentResult(false, reason);
return new ComponentResult(reason);
}
@Override
@@ -133,12 +128,12 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
return false;
}
ComponentResult that = (ComponentResult) o;
return status == that.status && Objects.equals(reason, that.reason);
return Objects.equals(reason, that.reason);
}
@Override
public int hashCode() {
return Objects.hash(status, reason);
return Objects.hash(reason);
}
}
}

View File

@@ -12,7 +12,6 @@ import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import java.util.Objects;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -45,8 +44,8 @@ public interface CommandExecuteEvent extends ResultedEvent<CommandResult> {
this.command = command;
}
public Optional<String> modifiedCommand() {
return Optional.ofNullable(command);
public @Nullable String modifiedCommand() {
return command;
}
public boolean isForwardToServer() {

View File

@@ -34,7 +34,7 @@ public final class GameProfileRequestEventImpl implements GameProfileRequestEven
boolean onlineMode) {
this.connection = Preconditions.checkNotNull(connection, "connection");
this.originalProfile = Preconditions.checkNotNull(originalProfile, "originalProfile");
this.username = originalProfile.getName();
this.username = originalProfile.name();
this.onlineMode = onlineMode;
}

View File

@@ -12,7 +12,6 @@ import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Objects;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -34,7 +33,7 @@ public interface KickedFromServerEvent extends
*
* @return the server kicked the player from the server
*/
Optional<Component> serverKickReason();
@Nullable Component serverKickReason();
/**
* Returns whether or not the player got kicked while connecting to another server.

View File

@@ -10,7 +10,6 @@ package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -72,8 +71,8 @@ public final class KickedFromServerEventImpl implements KickedFromServerEvent {
* @return the server kicked the player from the server
*/
@Override
public Optional<Component> serverKickReason() {
return Optional.ofNullable(originalReason);
public @Nullable Component serverKickReason() {
return originalReason;
}
/**

View File

@@ -9,7 +9,7 @@ package com.velocitypowered.api.event.player;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Fired when a player has finished connecting to the proxy and we need to choose the first server
@@ -19,7 +19,7 @@ public interface PlayerChooseInitialServerEvent {
Player player();
Optional<RegisteredServer> initialServer();
@Nullable RegisteredServer initialServer();
/**
* Sets the new initial server.

View File

@@ -10,7 +10,6 @@ package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -38,8 +37,8 @@ public class PlayerChooseInitialServerEventImpl implements PlayerChooseInitialSe
}
@Override
public Optional<RegisteredServer> initialServer() {
return Optional.ofNullable(initialServer);
public @Nullable RegisteredServer initialServer() {
return initialServer;
}
/**
@@ -47,7 +46,7 @@ public class PlayerChooseInitialServerEventImpl implements PlayerChooseInitialSe
* @param server the initial server the player should connect to
*/
@Override
public void setInitialServer(RegisteredServer server) {
public void setInitialServer(@Nullable RegisteredServer server) {
this.initialServer = server;
}

View File

@@ -9,7 +9,7 @@ package com.velocitypowered.api.event.player;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* This event is fired once the player has successfully connected to the target server and the
@@ -21,5 +21,5 @@ public interface ServerConnectedEvent {
RegisteredServer target();
Optional<RegisteredServer> previousServer();
@Nullable RegisteredServer previousServer();
}

View File

@@ -10,7 +10,6 @@ package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -47,8 +46,8 @@ public final class ServerConnectedEventImpl implements ServerConnectedEvent {
}
@Override
public Optional<RegisteredServer> previousServer() {
return Optional.ofNullable(previousServer);
public @Nullable RegisteredServer previousServer() {
return previousServer;
}
@Override

View File

@@ -14,7 +14,6 @@ import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder;
import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder.Status;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Objects;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -60,8 +59,8 @@ public interface ServerPreConnectEvent extends ResultedEvent<ServerPreConnectEve
return target != null;
}
public Optional<RegisteredServer> target() {
return Optional.ofNullable(target);
public @Nullable RegisteredServer target() {
return target;
}
@Override

View File

@@ -13,8 +13,8 @@ import com.velocitypowered.api.plugin.meta.PluginDependency;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Represents metadata for a specific version of a plugin.
@@ -39,41 +39,41 @@ public interface PluginDescription {
/**
* Gets the name of the {@link Plugin} within this container.
*
* @return an {@link Optional} with the plugin name, may be empty
* @return a String with the plugin name or the plugin ID
* @see Plugin#name()
*/
default Optional<String> name() {
return Optional.empty();
default String name() {
return id();
}
/**
* Gets the version of the {@link Plugin} within this container.
*
* @return an {@link Optional} with the plugin version, may be empty
* @return a String with the plugin version, may be null
* @see Plugin#version()
*/
default Optional<String> version() {
return Optional.empty();
default @Nullable String version() {
return null;
}
/**
* Gets the description of the {@link Plugin} within this container.
*
* @return an {@link Optional} with the plugin description, may be empty
* @return a String with the plugin description, may be null
* @see Plugin#description()
*/
default Optional<String> description() {
return Optional.empty();
default @Nullable String description() {
return null;
}
/**
* Gets the url or website of the {@link Plugin} within this container.
*
* @return an {@link Optional} with the plugin url, may be empty
* @return an String with the plugin url, may be null
* @see Plugin#url()
*/
default Optional<String> url() {
return Optional.empty();
default @Nullable String url() {
return null;
}
/**
@@ -96,16 +96,16 @@ public interface PluginDescription {
return ImmutableSet.of();
}
default Optional<PluginDependency> getDependency(String id) {
return Optional.empty();
default @Nullable PluginDependency getDependency(String id) {
return null;
}
/**
* Returns the file path the plugin was loaded from.
*
* @return the path the plugin was loaded from or {@link Optional#empty()} if unknown
* @return the path the plugin was loaded from or {@code null} if unknown
*/
default Optional<Path> file() {
return Optional.empty();
default @Nullable Path file() {
return null;
}
}

View File

@@ -7,9 +7,11 @@
package com.velocitypowered.api.plugin;
import static java.util.Objects.requireNonNull;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Manages plugins loaded on the proxy. This manager can retrieve {@link PluginContainer}s from
@@ -24,7 +26,7 @@ public interface PluginManager {
* @param instance the instance
* @return the container
*/
Optional<PluginContainer> fromInstance(Object instance);
@Nullable PluginContainer fromInstance(Object instance);
/**
* Retrieves a {@link PluginContainer} based on its ID.
@@ -32,7 +34,7 @@ public interface PluginManager {
* @param id the plugin ID
* @return the plugin, if available
*/
Optional<PluginContainer> getPlugin(String id);
@Nullable PluginContainer getPlugin(String id);
/**
* Gets a {@link Collection} of all {@link PluginContainer}s.
@@ -57,4 +59,13 @@ public interface PluginManager {
* @throws UnsupportedOperationException if the operation is not applicable to this plugin
*/
void addToClasspath(Object plugin, Path path);
default PluginContainer ensurePluginContainer(Object plugin) {
requireNonNull(plugin, "plugin");
PluginContainer container = fromInstance(plugin);
if (container == null) {
throw new IllegalArgumentException("Specified plugin is not loaded");
}
return container;
}
}

View File

@@ -12,7 +12,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.emptyToNull;
import java.util.Objects;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -43,17 +42,17 @@ public final class PluginDependency {
*
* @return the plugin ID
*/
public String getId() {
public String id() {
return id;
}
/**
* Returns the version this {@link PluginDependency} should match.
*
* @return an {@link Optional} with the plugin version, may be empty
* @return a String with the plugin version, may be {@code null}
*/
public Optional<String> getVersion() {
return Optional.ofNullable(version);
public @Nullable String version() {
return version;
}
/**
@@ -61,7 +60,7 @@ public final class PluginDependency {
*
* @return true if dependency is optional
*/
public boolean isOptional() {
public boolean optional() {
return optional;
}

View File

@@ -19,12 +19,11 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.api.scheduler.Scheduler;
import com.velocitypowered.api.util.ProxyVersion;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Provides an interface to a Minecraft server proxy.
@@ -48,17 +47,17 @@ public interface ProxyServer extends Audience {
* is case-insensitive.
*
* @param username the username to search for
* @return an {@link Optional} with the player, which may be empty
* @return the player instance, if connected, else {@code null}
*/
Optional<Player> getPlayer(String username);
@Nullable Player player(String username);
/**
* Retrieves the player currently connected to this proxy by their Minecraft UUID.
*
* @param uuid the UUID
* @return an {@link Optional} with the player, which may be empty
* @return the player instance, if connected, else {@code null}
*/
Optional<Player> getPlayer(UUID uuid);
@Nullable Player player(UUID uuid);
/**
* Retrieves all players currently connected to this proxy. This call may or may not be a snapshot
@@ -82,7 +81,7 @@ public interface ProxyServer extends Audience {
* @param name the name of the server
* @return the registered server, which may be empty
*/
Optional<RegisteredServer> server(String name);
@Nullable RegisteredServer server(String name);
/**
* Retrieves all {@link RegisteredServer}s registered with this proxy.

View File

@@ -11,7 +11,7 @@ import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.util.Favicon;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Exposes certain proxy configuration information that plugins may use.
@@ -126,7 +126,7 @@ public interface ProxyConfig {
*
* @return optional favicon
*/
Optional<Favicon> getFavicon();
@Nullable Favicon getFavicon();
/**
* Get whether this proxy displays that it supports Forge/FML.

View File

@@ -10,7 +10,7 @@ package com.velocitypowered.api.proxy.connection;
import com.velocitypowered.api.network.ProtocolVersion;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Represents an incoming connection to the proxy.
@@ -29,7 +29,7 @@ public interface InboundConnection {
*
* @return the hostname from the client
*/
Optional<InetSocketAddress> connectedHostname();
@Nullable InetSocketAddress connectedHostname();
/**
* Determine whether or not the player remains online.

View File

@@ -19,10 +19,10 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.GameProfile;
import com.velocitypowered.api.util.ModInfo;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Represents a player who is connected to the proxy.
@@ -47,9 +47,10 @@ public interface Player extends CommandSource, Identified, InboundConnection,
/**
* Returns the server that the player is currently connected to.
*
* @return an {@link Optional} the server that the player is connected to, which may be empty
* @return the server that the player is connected to, which could be {@code null} if no
* connection was made yet (or the player is switching servers)
*/
Optional<ServerConnection> connectedServer();
@Nullable ServerConnection connectedServer();
/**
* Returns the player's client settings.
@@ -61,9 +62,9 @@ public interface Player extends CommandSource, Identified, InboundConnection,
/**
* Returns the player's mod info if they have a modded client.
*
* @return an {@link Optional} the mod info. which may be empty
* @return an the mod info. which may be {@code null} if no info is available
*/
Optional<ModInfo> modInfo();
@Nullable ModInfo modInfo();
/**
* Returns the current player's ping.

View File

@@ -9,9 +9,9 @@ package com.velocitypowered.api.proxy.player;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Provides a fluent interface to send a connection request to another server on the proxy. A
@@ -76,7 +76,7 @@ public interface ConnectionRequestBuilder {
*
* @return the reason why the user could not connect to the server
*/
Optional<Component> failureReason();
@Nullable Component failureReason();
/**
* Returns the server we actually tried to connect to.

View File

@@ -10,7 +10,6 @@ package com.velocitypowered.api.proxy.player;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.util.GameProfile;
import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -45,10 +44,9 @@ public interface TabList {
* the specified {@link UUID}.
*
* @param uuid of the entry
* @return {@link Optional} containing the removed {@link TabListEntry} if present, otherwise
* {@link Optional#empty()}
* @return the removed {@link TabListEntry} if present, otherwise {@code null}
*/
Optional<TabListEntry> removeEntry(UUID uuid);
@Nullable TabListEntry removeEntry(UUID uuid);
/**
* Determines if the specified entry exists in the tab list.

View File

@@ -8,7 +8,6 @@
package com.velocitypowered.api.proxy.player;
import com.velocitypowered.api.util.GameProfile;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -34,18 +33,17 @@ public interface TabListEntry {
GameProfile gameProfile();
/**
* Returns {@link Optional} text {@link Component}, which if present is the text
* Returns an optional text {@link Component}, which if present is the text
* displayed for {@code this} entry in the {@link TabList}, otherwise
* {@link GameProfile#getName()} is shown.
* {@link GameProfile#name()} is shown.
*
* @return {@link Optional} text {@link Component} of name displayed in the tab
* list
* @return text {@link Component} of name displayed in the tab list
*/
Optional<Component> displayName();
@Nullable Component displayName();
/**
* Sets the text {@link Component} to be displayed for {@code this} {@link TabListEntry}. If
* {@code null}, {@link GameProfile#getName()} will be shown.
* {@code null}, {@link GameProfile#name()} will be shown.
*
* @param displayName to show in the {@link TabList} for {@code this} entry
* @return {@code this}, for chaining

View File

@@ -18,7 +18,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -423,8 +422,8 @@ public final class QueryResponse {
return name;
}
public Optional<String> getVersion() {
return Optional.ofNullable(version);
public @Nullable String getVersion() {
return version;
}
public static PluginInformation of(String name, @Nullable String version) {

View File

@@ -16,7 +16,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -60,20 +59,20 @@ public final class ServerPing {
return version;
}
public Optional<Players> players() {
return Optional.ofNullable(players);
public @Nullable Players players() {
return players;
}
public Component description() {
return description;
}
public Optional<Favicon> favicon() {
return Optional.ofNullable(favicon);
public @Nullable Favicon favicon() {
return favicon;
}
public Optional<ModInfo> modInfo() {
return Optional.ofNullable(modinfo);
public @Nullable ModInfo modInfo() {
return modinfo;
}
@Override
@@ -271,12 +270,12 @@ public final class ServerPing {
return samplePlayers;
}
public Optional<Component> getDescription() {
return Optional.ofNullable(description);
public @Nullable Component getDescription() {
return description;
}
public Optional<Favicon> getFavicon() {
return Optional.ofNullable(favicon);
public @Nullable Favicon getFavicon() {
return favicon;
}
public String getModType() {

View File

@@ -11,11 +11,15 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
/**
* Represents a Mojang game profile. This class is immutable.
*/
public final class GameProfile {
public final class GameProfile implements Identified, Identity {
private final UUID id;
private final String undashedId;
@@ -55,7 +59,7 @@ public final class GameProfile {
* Returns the undashed, Mojang-style UUID.
* @return the undashed UUID
*/
public String getUndashedId() {
public String undashedId() {
return undashedId;
}
@@ -63,7 +67,8 @@ public final class GameProfile {
* Returns the UUID associated with this game profile.
* @return the UUID
*/
public UUID getId() {
@Override
public @NotNull UUID uuid() {
return id;
}
@@ -71,7 +76,7 @@ public final class GameProfile {
* Returns the username associated with this profile.
* @return the username
*/
public String getName() {
public String name() {
return name;
}
@@ -79,7 +84,7 @@ public final class GameProfile {
* Returns an immutable list of profile properties associated with this profile.
* @return the properties associated with this profile
*/
public List<Property> getProperties() {
public List<Property> properties() {
return properties;
}
@@ -89,7 +94,7 @@ public final class GameProfile {
* @param id the new unique id
* @return the new {@code GameProfile}
*/
public GameProfile withId(UUID id) {
public GameProfile withUuid(UUID id) {
return new GameProfile(Preconditions.checkNotNull(id, "id"), UuidUtils.toUndashed(id),
this.name, this.properties);
}
@@ -172,6 +177,11 @@ public final class GameProfile {
+ '}';
}
@Override
public @NonNull Identity identity() {
return this;
}
/**
* Represents a Mojang profile property. Just like {@link GameProfile}, this class is immutable.
*/
@@ -193,15 +203,15 @@ public final class GameProfile {
this.signature = Preconditions.checkNotNull(signature, "signature");
}
public String getName() {
public String name() {
return name;
}
public String getValue() {
public String value() {
return value;
}
public String getSignature() {
public String signature() {
return signature;
}