Fix compile/test error with HostAndPort

This commit is contained in:
Luck committed 2025-11-09 13:13:50 +00:00
1 parent 10378e882b
commit 61ce546a04
6 files changed
+95 -14

No files matched your search

@@ -28,7 +28,6 @@ package me.lucko.luckperms.common.messaging.nats;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.common.net.HostAndPort;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import io.nats.client.Message;
@@ -37,6 +36,7 @@ import io.nats.client.Nats;
import io.nats.client.Options;
import io.nats.client.Options.Builder;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.util.HostAndPort;
import me.lucko.luckperms.common.util.Throwing;
import net.luckperms.api.messenger.IncomingMessageConsumer;
import net.luckperms.api.messenger.Messenger;
@@ -70,10 +70,10 @@ public class NatsMessenger implements Messenger {
}
public void init(String address, String username, String password, boolean ssl) {
HostAndPort hostAndPort = HostAndPort.fromString(address)
HostAndPort hostAndPort = new HostAndPort(address)
.requireBracketsForIPv6()
.withDefaultPort(Options.DEFAULT_PORT);
String host = hostAndPort.getHostText();
String host = hostAndPort.getHost();
int port = hostAndPort.getPort();
this.connection = createConnection(builder -> {
@@ -28,7 +28,6 @@ package me.lucko.luckperms.common.messaging.rabbitmq;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.common.net.HostAndPort;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
@@ -38,6 +37,7 @@ import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.plugin.scheduler.SchedulerTask;
import me.lucko.luckperms.common.util.HostAndPort;
import net.luckperms.api.messenger.IncomingMessageConsumer;
import net.luckperms.api.messenger.Messenger;
import net.luckperms.api.messenger.message.OutgoingMessage;
@@ -71,11 +71,11 @@ public class RabbitMQMessenger implements Messenger {
}
public void init(String address, String virtualHost, String username, String password) {
HostAndPort hostAndPort = HostAndPort.fromString(address)
HostAndPort hostAndPort = new HostAndPort(address)
.requireBracketsForIPv6()
.withDefaultPort(DEFAULT_PORT);
String host = hostAndPort.getHostText();
String host = hostAndPort.getHost();
int port = hostAndPort.getPort();
this.connectionFactory = new ConnectionFactory();
@@ -86,10 +86,10 @@ public class RedisMessenger implements Messenger {
}
private static HostAndPort parseAddress(String address) {
com.google.common.net.HostAndPort hostAndPort = com.google.common.net.HostAndPort.fromString(address)
me.lucko.luckperms.common.util.HostAndPort hostAndPort = new me.lucko.luckperms.common.util.HostAndPort(address)
.requireBracketsForIPv6()
.withDefaultPort(Protocol.DEFAULT_PORT);
String host = hostAndPort.getHostText();
String host = hostAndPort.getHost();
int port = hostAndPort.getPort();
return new HostAndPort(host, port);
}
@@ -27,7 +27,6 @@ package me.lucko.luckperms.common.storage.implementation.mongodb;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.net.HostAndPort;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
@@ -60,6 +59,7 @@ import me.lucko.luckperms.common.storage.implementation.StorageImplementation;
import me.lucko.luckperms.common.storage.misc.NodeEntry;
import me.lucko.luckperms.common.storage.misc.PlayerSaveResultImpl;
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
import me.lucko.luckperms.common.util.HostAndPort;
import me.lucko.luckperms.common.util.Iterators;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.context.Context;
@@ -130,11 +130,11 @@ public class MongoStorage implements StorageImplementation {
);
}
HostAndPort hostAndPort = HostAndPort.fromString(this.configuration.getAddress())
HostAndPort hostAndPort = new HostAndPort(this.configuration.getAddress())
.requireBracketsForIPv6()
.withDefaultPort(27017);
String host = hostAndPort.getHostText();
String host = hostAndPort.getHost();
int port = hostAndPort.getPort();
ServerAddress address = new ServerAddress(host, port);
@@ -26,7 +26,6 @@
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
import com.google.common.collect.ImmutableList;
import com.google.common.net.HostAndPort;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
@@ -34,6 +33,7 @@ import me.lucko.luckperms.common.plugin.logging.PluginLogger;
import me.lucko.luckperms.common.storage.StorageMetadata;
import me.lucko.luckperms.common.storage.implementation.sql.connection.ConnectionFactory;
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
import me.lucko.luckperms.common.util.HostAndPort;
import java.sql.Connection;
import java.sql.SQLException;
@@ -118,11 +118,11 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
config.setPoolName("luckperms-hikari");
// get the database info/credentials from the config file
HostAndPort hostAndPort = HostAndPort.fromString(this.configuration.getAddress())
HostAndPort hostAndPort = new HostAndPort(this.configuration.getAddress())
.requireBracketsForIPv6()
.withDefaultPort(defaultPort());
String address = hostAndPort.getHostText();
String address = hostAndPort.getHost();
int port = hostAndPort.getPort();
// allow the implementation to configure the HikariConfig appropriately with these values
@@ -0,0 +1,81 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.util;
import java.lang.reflect.Method;
import java.util.Objects;
/**
* A wrapper around Guava's HostAndPort to account for the method name change of getHostText().
*/
@SuppressWarnings("UnstableApiUsage")
public class HostAndPort {
private static final Method GET_HOST_METHOD;
static {
Method getHostMethod = null;
try {
getHostMethod = com.google.common.net.HostAndPort.class.getMethod("getHostText");
} catch (NoSuchMethodException e) {
try {
getHostMethod = com.google.common.net.HostAndPort.class.getMethod("getHost");
} catch (NoSuchMethodException ex) {
// ignore
}
}
Objects.requireNonNull(getHostMethod);
GET_HOST_METHOD = getHostMethod;
}
private com.google.common.net.HostAndPort delegate;
public HostAndPort(String hostAndPort) {
this.delegate = com.google.common.net.HostAndPort.fromString(hostAndPort);
}
public HostAndPort withDefaultPort(int defaultPort) {
this.delegate = this.delegate.withDefaultPort(defaultPort);
return this;
}
public HostAndPort requireBracketsForIPv6() {
this.delegate.requireBracketsForIPv6();
return this;
}
public String getHost() {
try {
return (String) GET_HOST_METHOD.invoke(this.delegate);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public int getPort() {
return this.delegate.getPort();
}
}