From 61ce546a041dff4c8e6c1ff6b9d33f4c2820bc85 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 9 Nov 2025 13:13:50 +0000 Subject: [PATCH] Fix compile/test error with HostAndPort --- .../common/messaging/nats/NatsMessenger.java | 6 +- .../messaging/rabbitmq/RabbitMQMessenger.java | 6 +- .../messaging/redis/RedisMessenger.java | 4 +- .../implementation/mongodb/MongoStorage.java | 6 +- .../hikari/HikariConnectionFactory.java | 6 +- .../luckperms/common/util/HostAndPort.java | 81 +++++++++++++++++++ 6 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/util/HostAndPort.java diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/nats/NatsMessenger.java b/common/src/main/java/me/lucko/luckperms/common/messaging/nats/NatsMessenger.java index ab2c34768..a5b7667f3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/nats/NatsMessenger.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/nats/NatsMessenger.java @@ -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 -> { diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/rabbitmq/RabbitMQMessenger.java b/common/src/main/java/me/lucko/luckperms/common/messaging/rabbitmq/RabbitMQMessenger.java index 1cdee4e5d..22b62f805 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/rabbitmq/RabbitMQMessenger.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/rabbitmq/RabbitMQMessenger.java @@ -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(); diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java b/common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java index a90306c49..199cb2843 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java @@ -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); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java index f43359c48..8b666ba3c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java @@ -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); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java index 3eb0f7d05..8e9d28cce 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java @@ -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 diff --git a/common/src/main/java/me/lucko/luckperms/common/util/HostAndPort.java b/common/src/main/java/me/lucko/luckperms/common/util/HostAndPort.java new file mode 100644 index 000000000..b861ce9ab --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/util/HostAndPort.java @@ -0,0 +1,81 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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(); + } + +}