Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-16 21:10:30 +01:00
Banish all use of Optionals from Velocity entirely
Dieser Commit ist enthalten in:
Ursprung
6cd3b00428
Commit
14be98c88c
@ -7,7 +7,7 @@
|
||||
|
||||
package com.velocitypowered.api.plugin;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
* A wrapper around a plugin loaded by the proxy.
|
||||
@ -26,7 +26,7 @@ public interface PluginContainer {
|
||||
*
|
||||
* @return the instance if available
|
||||
*/
|
||||
default Optional<?> instance() {
|
||||
return Optional.empty();
|
||||
default @Nullable Object instance() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -39,13 +39,9 @@
|
||||
|
||||
<module name="TreeWalker">
|
||||
<module name="OuterTypeFilename"/>
|
||||
<!-- <module name="IllegalTokenText">
|
||||
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
|
||||
<property name="format"
|
||||
value="\\u00(09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\\(0(10|11|12|14|15|42|47)|134)"/>
|
||||
<property name="message"
|
||||
value="Consider using special escape sequence instead of octal value or Unicode escaped value."/>
|
||||
</module> -->
|
||||
<module name="IllegalImport">
|
||||
<property name="illegalClasses" value="java.util.Optional, java.util.OptionalDouble, java.util.OptionalInt, java.util.OptionalLong"/>
|
||||
</module>
|
||||
<module name="AvoidEscapedUnicodeCharacters">
|
||||
<property name="allowEscapesForControlCharacters" value="true"/>
|
||||
<property name="allowByTailComment" value="true"/>
|
||||
|
@ -78,7 +78,6 @@ import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@ -327,10 +326,10 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
|
||||
|
||||
// Register the plugin main classes so that we can fire the proxy initialize event
|
||||
for (PluginContainer plugin : pluginManager.plugins()) {
|
||||
Optional<?> instance = plugin.instance();
|
||||
if (instance.isPresent()) {
|
||||
Object instance = plugin.instance();
|
||||
if (instance != null) {
|
||||
try {
|
||||
eventManager.registerInternally(plugin, instance.get());
|
||||
eventManager.registerInternally(plugin, instance);
|
||||
} catch (Exception e) {
|
||||
logger.error("Unable to register plugin listener for {}",
|
||||
MoreObjects.firstNonNull(plugin.description().name(), plugin.description().id()), e);
|
||||
|
@ -29,7 +29,6 @@ import com.velocitypowered.api.proxy.connection.ServerConnection;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import com.velocitypowered.api.proxy.server.ServerInfo;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
@ -76,13 +75,16 @@ public class ServerCommand implements SimpleCommand {
|
||||
}
|
||||
|
||||
private void outputServerInformation(Player executor) {
|
||||
String currentServer = Optional.ofNullable(executor.connectedServer())
|
||||
.map(ServerConnection::serverInfo)
|
||||
.map(ServerInfo::name).orElse("<unknown>");
|
||||
String currentServerName = "<unknown>";
|
||||
ServerConnection connectedTo = executor.connectedServer();
|
||||
if (connectedTo != null) {
|
||||
currentServerName = connectedTo.serverInfo().name();
|
||||
}
|
||||
|
||||
executor.sendMessage(Identity.nil(), Component.translatable(
|
||||
"velocity.command.server-current-server",
|
||||
NamedTextColor.YELLOW,
|
||||
Component.text(currentServer)));
|
||||
Component.text(currentServerName)));
|
||||
|
||||
List<RegisteredServer> servers = BuiltinCommandUtil.sortedServerList(server);
|
||||
if (servers.size() > MAX_SERVERS_TO_LIST) {
|
||||
@ -98,7 +100,7 @@ public class ServerCommand implements SimpleCommand {
|
||||
.append(Component.space());
|
||||
for (int i = 0; i < servers.size(); i++) {
|
||||
RegisteredServer rs = servers.get(i);
|
||||
serverListBuilder.append(formatServerComponent(currentServer, rs));
|
||||
serverListBuilder.append(formatServerComponent(currentServerName, rs));
|
||||
if (i != servers.size() - 1) {
|
||||
serverListBuilder.append(Component.text(", ", NamedTextColor.GRAY));
|
||||
}
|
||||
|
@ -49,7 +49,6 @@ import java.util.IdentityHashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -69,8 +68,10 @@ public class VelocityPluginManager implements PluginManager {
|
||||
|
||||
private void registerPlugin(PluginContainer plugin) {
|
||||
plugins.put(plugin.description().id(), plugin);
|
||||
Optional<?> instance = plugin.instance();
|
||||
instance.ifPresent(o -> pluginInstances.put(o, plugin));
|
||||
Object instance = plugin.instance();
|
||||
if (instance != null) {
|
||||
pluginInstances.put(instance, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,10 +199,7 @@ public class VelocityPluginManager implements PluginManager {
|
||||
throw new IllegalArgumentException("plugin is not loaded");
|
||||
}
|
||||
|
||||
Optional<?> optInstance = optContainer.instance();
|
||||
checkArgument(optInstance.isPresent(), "plugin has no instance");
|
||||
|
||||
ClassLoader pluginClassloader = optInstance.get().getClass().getClassLoader();
|
||||
ClassLoader pluginClassloader = plugin.getClass().getClassLoader();
|
||||
if (pluginClassloader instanceof PluginClassLoader) {
|
||||
((PluginClassLoader) pluginClassloader).addPath(path);
|
||||
} else {
|
||||
|
@ -19,8 +19,8 @@ package com.velocitypowered.proxy.plugin.loader;
|
||||
|
||||
import com.velocitypowered.api.plugin.PluginContainer;
|
||||
import com.velocitypowered.api.plugin.PluginDescription;
|
||||
import java.util.Optional;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public class VelocityPluginContainer implements PluginContainer {
|
||||
|
||||
@ -37,8 +37,8 @@ public class VelocityPluginContainer implements PluginContainer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<?> instance() {
|
||||
return Optional.ofNullable(instance);
|
||||
public @Nullable Object instance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void setInstance(Object instance) {
|
||||
|
@ -41,10 +41,10 @@ import java.nio.file.Path;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarInputStream;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public class JavaPluginLoader implements PluginLoader {
|
||||
|
||||
@ -56,18 +56,16 @@ public class JavaPluginLoader implements PluginLoader {
|
||||
|
||||
@Override
|
||||
public PluginDescription loadPluginDescription(Path source) throws Exception {
|
||||
Optional<SerializedPluginDescription> serialized = getSerializedPluginInfo(source);
|
||||
|
||||
if (serialized.isEmpty()) {
|
||||
SerializedPluginDescription serialized = getSerializedPluginInfo(source);
|
||||
if (serialized == null) {
|
||||
throw new InvalidPluginException("Did not find a valid velocity-plugin.json.");
|
||||
}
|
||||
|
||||
SerializedPluginDescription pd = serialized.get();
|
||||
if (!SerializedPluginDescription.ID_PATTERN.matcher(pd.getId()).matches()) {
|
||||
throw new InvalidPluginException("Plugin ID '" + pd.getId() + "' is invalid.");
|
||||
if (!SerializedPluginDescription.ID_PATTERN.matcher(serialized.getId()).matches()) {
|
||||
throw new InvalidPluginException("Plugin ID '" + serialized.getId() + "' is invalid.");
|
||||
}
|
||||
|
||||
return createCandidateDescription(pd, source);
|
||||
return createCandidateDescription(serialized, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -131,7 +129,7 @@ public class JavaPluginLoader implements PluginLoader {
|
||||
((VelocityPluginContainer) container).setInstance(instance);
|
||||
}
|
||||
|
||||
private Optional<SerializedPluginDescription> getSerializedPluginInfo(Path source)
|
||||
private @Nullable SerializedPluginDescription getSerializedPluginInfo(Path source)
|
||||
throws Exception {
|
||||
boolean foundBungeeBukkitPluginFile = false;
|
||||
try (JarInputStream in = new JarInputStream(
|
||||
@ -140,8 +138,8 @@ public class JavaPluginLoader implements PluginLoader {
|
||||
while ((entry = in.getNextJarEntry()) != null) {
|
||||
if (entry.getName().equals("velocity-plugin.json")) {
|
||||
try (Reader pluginInfoReader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
|
||||
return Optional.of(VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader,
|
||||
SerializedPluginDescription.class));
|
||||
return VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader,
|
||||
SerializedPluginDescription.class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,7 +154,7 @@ public class JavaPluginLoader implements PluginLoader {
|
||||
+ "plugins.");
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,6 @@ import com.velocitypowered.api.plugin.PluginDescription;
|
||||
import com.velocitypowered.api.plugin.PluginManager;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
@ -89,8 +88,8 @@ public class FakePluginManager implements PluginManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<?> instance() {
|
||||
return Optional.of(instance);
|
||||
public Object instance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren