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