Skip to content

Commit 61ce546

Browse files
committed
Fix compile/test error with HostAndPort
1 parent 10378e8 commit 61ce546

6 files changed

Lines changed: 95 additions & 14 deletions

File tree

common/src/main/java/me/lucko/luckperms/common/messaging/nats/NatsMessenger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.common.io.ByteArrayDataInput;
2929
import com.google.common.io.ByteArrayDataOutput;
3030
import com.google.common.io.ByteStreams;
31-
import com.google.common.net.HostAndPort;
3231
import io.nats.client.Connection;
3332
import io.nats.client.Dispatcher;
3433
import io.nats.client.Message;
@@ -37,6 +36,7 @@
3736
import io.nats.client.Options;
3837
import io.nats.client.Options.Builder;
3938
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
39+
import me.lucko.luckperms.common.util.HostAndPort;
4040
import me.lucko.luckperms.common.util.Throwing;
4141
import net.luckperms.api.messenger.IncomingMessageConsumer;
4242
import net.luckperms.api.messenger.Messenger;
@@ -70,10 +70,10 @@ public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) {
7070
}
7171

7272
public void init(String address, String username, String password, boolean ssl) {
73-
HostAndPort hostAndPort = HostAndPort.fromString(address)
73+
HostAndPort hostAndPort = new HostAndPort(address)
7474
.requireBracketsForIPv6()
7575
.withDefaultPort(Options.DEFAULT_PORT);
76-
String host = hostAndPort.getHostText();
76+
String host = hostAndPort.getHost();
7777
int port = hostAndPort.getPort();
7878

7979
this.connection = createConnection(builder -> {

common/src/main/java/me/lucko/luckperms/common/messaging/rabbitmq/RabbitMQMessenger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.common.io.ByteArrayDataInput;
2929
import com.google.common.io.ByteArrayDataOutput;
3030
import com.google.common.io.ByteStreams;
31-
import com.google.common.net.HostAndPort;
3231
import com.rabbitmq.client.AMQP;
3332
import com.rabbitmq.client.BuiltinExchangeType;
3433
import com.rabbitmq.client.Channel;
@@ -38,6 +37,7 @@
3837
import com.rabbitmq.client.Delivery;
3938
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
4039
import me.lucko.luckperms.common.plugin.scheduler.SchedulerTask;
40+
import me.lucko.luckperms.common.util.HostAndPort;
4141
import net.luckperms.api.messenger.IncomingMessageConsumer;
4242
import net.luckperms.api.messenger.Messenger;
4343
import net.luckperms.api.messenger.message.OutgoingMessage;
@@ -71,11 +71,11 @@ public RabbitMQMessenger(LuckPermsPlugin plugin, IncomingMessageConsumer consume
7171
}
7272

7373
public void init(String address, String virtualHost, String username, String password) {
74-
HostAndPort hostAndPort = HostAndPort.fromString(address)
74+
HostAndPort hostAndPort = new HostAndPort(address)
7575
.requireBracketsForIPv6()
7676
.withDefaultPort(DEFAULT_PORT);
7777

78-
String host = hostAndPort.getHostText();
78+
String host = hostAndPort.getHost();
7979
int port = hostAndPort.getPort();
8080

8181
this.connectionFactory = new ConnectionFactory();

common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ private static JedisClientConfig jedisConfig(String username, String password, b
8686
}
8787

8888
private static HostAndPort parseAddress(String address) {
89-
com.google.common.net.HostAndPort hostAndPort = com.google.common.net.HostAndPort.fromString(address)
89+
me.lucko.luckperms.common.util.HostAndPort hostAndPort = new me.lucko.luckperms.common.util.HostAndPort(address)
9090
.requireBracketsForIPv6()
9191
.withDefaultPort(Protocol.DEFAULT_PORT);
92-
String host = hostAndPort.getHostText();
92+
String host = hostAndPort.getHost();
9393
int port = hostAndPort.getPort();
9494
return new HostAndPort(host, port);
9595
}

common/src/main/java/me/lucko/luckperms/common/storage/implementation/mongodb/MongoStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import com.google.common.annotations.VisibleForTesting;
2929
import com.google.common.base.Strings;
30-
import com.google.common.net.HostAndPort;
3130
import com.mongodb.MongoClient;
3231
import com.mongodb.MongoClientOptions;
3332
import com.mongodb.MongoClientURI;
@@ -60,6 +59,7 @@
6059
import me.lucko.luckperms.common.storage.misc.NodeEntry;
6160
import me.lucko.luckperms.common.storage.misc.PlayerSaveResultImpl;
6261
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
62+
import me.lucko.luckperms.common.util.HostAndPort;
6363
import me.lucko.luckperms.common.util.Iterators;
6464
import net.luckperms.api.actionlog.Action;
6565
import net.luckperms.api.context.Context;
@@ -130,11 +130,11 @@ public void init() {
130130
);
131131
}
132132

133-
HostAndPort hostAndPort = HostAndPort.fromString(this.configuration.getAddress())
133+
HostAndPort hostAndPort = new HostAndPort(this.configuration.getAddress())
134134
.requireBracketsForIPv6()
135135
.withDefaultPort(27017);
136136

137-
String host = hostAndPort.getHostText();
137+
String host = hostAndPort.getHost();
138138
int port = hostAndPort.getPort();
139139
ServerAddress address = new ServerAddress(host, port);
140140

common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
package me.lucko.luckperms.common.storage.implementation.sql.connection.hikari;
2727

2828
import com.google.common.collect.ImmutableList;
29-
import com.google.common.net.HostAndPort;
3029
import com.zaxxer.hikari.HikariConfig;
3130
import com.zaxxer.hikari.HikariDataSource;
3231
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
3332
import me.lucko.luckperms.common.plugin.logging.PluginLogger;
3433
import me.lucko.luckperms.common.storage.StorageMetadata;
3534
import me.lucko.luckperms.common.storage.implementation.sql.connection.ConnectionFactory;
3635
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
36+
import me.lucko.luckperms.common.util.HostAndPort;
3737

3838
import java.sql.Connection;
3939
import java.sql.SQLException;
@@ -118,11 +118,11 @@ public void init(LuckPermsPlugin plugin) {
118118
config.setPoolName("luckperms-hikari");
119119

120120
// get the database info/credentials from the config file
121-
HostAndPort hostAndPort = HostAndPort.fromString(this.configuration.getAddress())
121+
HostAndPort hostAndPort = new HostAndPort(this.configuration.getAddress())
122122
.requireBracketsForIPv6()
123123
.withDefaultPort(defaultPort());
124124

125-
String address = hostAndPort.getHostText();
125+
String address = hostAndPort.getHost();
126126
int port = hostAndPort.getPort();
127127

128128
// allow the implementation to configure the HikariConfig appropriately with these values
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <luck@lucko.me>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package me.lucko.luckperms.common.util;
27+
28+
import java.lang.reflect.Method;
29+
import java.util.Objects;
30+
31+
/**
32+
* A wrapper around Guava's HostAndPort to account for the method name change of getHostText().
33+
*/
34+
@SuppressWarnings("UnstableApiUsage")
35+
public class HostAndPort {
36+
private static final Method GET_HOST_METHOD;
37+
38+
static {
39+
Method getHostMethod = null;
40+
try {
41+
getHostMethod = com.google.common.net.HostAndPort.class.getMethod("getHostText");
42+
} catch (NoSuchMethodException e) {
43+
try {
44+
getHostMethod = com.google.common.net.HostAndPort.class.getMethod("getHost");
45+
} catch (NoSuchMethodException ex) {
46+
// ignore
47+
}
48+
}
49+
Objects.requireNonNull(getHostMethod);
50+
GET_HOST_METHOD = getHostMethod;
51+
}
52+
53+
private com.google.common.net.HostAndPort delegate;
54+
55+
public HostAndPort(String hostAndPort) {
56+
this.delegate = com.google.common.net.HostAndPort.fromString(hostAndPort);
57+
}
58+
59+
public HostAndPort withDefaultPort(int defaultPort) {
60+
this.delegate = this.delegate.withDefaultPort(defaultPort);
61+
return this;
62+
}
63+
64+
public HostAndPort requireBracketsForIPv6() {
65+
this.delegate.requireBracketsForIPv6();
66+
return this;
67+
}
68+
69+
public String getHost() {
70+
try {
71+
return (String) GET_HOST_METHOD.invoke(this.delegate);
72+
} catch (ReflectiveOperationException e) {
73+
throw new RuntimeException(e);
74+
}
75+
}
76+
77+
public int getPort() {
78+
return this.delegate.getPort();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)