mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-22 20:28:00 +00:00
Convert RedisBungee to okhttp.
This commit is contained in:
parent
b375b5402d
commit
ba825c1880
16
pom.xml
16
pom.xml
@ -79,6 +79,16 @@
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commonspool
|
||||
</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>com.squareup.okhttp</pattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.okhttp
|
||||
</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>okio</pattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.okio
|
||||
</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
</execution>
|
||||
@ -147,5 +157,11 @@
|
||||
<version>1.12.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>2.2.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -12,6 +12,7 @@ import com.google.common.io.ByteStreams;
|
||||
import com.google.gson.Gson;
|
||||
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
|
||||
import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
@ -56,6 +57,8 @@ public final class RedisBungee extends Plugin {
|
||||
private DataManager dataManager;
|
||||
@Getter
|
||||
private ExecutorService service;
|
||||
@Getter
|
||||
private static OkHttpClient httpClient;
|
||||
private List<String> serverIds;
|
||||
private AtomicInteger nagAboutServers = new AtomicInteger();
|
||||
private ScheduledTask integrityCheck;
|
||||
@ -428,6 +431,7 @@ public final class RedisBungee extends Plugin {
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
service = Executors.newFixedThreadPool(16);
|
||||
httpClient = new OkHttpClient();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
@ -47,7 +47,7 @@ public class RedisBungeeListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
plugin.getService().submit((Callable<Void>) new RedisCallable<Void>(plugin) {
|
||||
plugin.getService().submit(new RedisCallable<Void>(plugin) {
|
||||
@Override
|
||||
protected Void call(Jedis jedis) {
|
||||
jedis.sadd("proxy:" + RedisBungee.getApi().getServerId() + ":usersOnline", event.getPlayer().getUniqueId().toString());
|
||||
@ -68,7 +68,7 @@ public class RedisBungeeListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDisconnect(final PlayerDisconnectEvent event) {
|
||||
plugin.getService().submit((Callable<Void>) new RedisCallable<Void>(plugin) {
|
||||
plugin.getService().submit(new RedisCallable<Void>(plugin) {
|
||||
@Override
|
||||
protected Void call(Jedis jedis) {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
@ -84,7 +84,7 @@ public class RedisBungeeListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onServerChange(final ServerConnectedEvent event) {
|
||||
plugin.getService().submit((Callable<Void>) new RedisCallable<Void>(plugin) {
|
||||
plugin.getService().submit(new RedisCallable<Void>(plugin) {
|
||||
@Override
|
||||
protected Void call(Jedis jedis) {
|
||||
jedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName());
|
||||
|
@ -6,17 +6,14 @@
|
||||
*/
|
||||
package com.imaginarycode.minecraft.redisbungee.util;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
||||
import com.squareup.okhttp.Request;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@ -24,16 +21,12 @@ import java.util.UUID;
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class NameFetcher {
|
||||
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
|
||||
URLConnection connection = new URL("https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "").toLowerCase() + "/names").openConnection();
|
||||
|
||||
String text;
|
||||
|
||||
try (InputStream is = connection.getInputStream()) {
|
||||
text = new String(ByteStreams.toByteArray(is));
|
||||
}
|
||||
String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names";
|
||||
Request request = new Request.Builder().url(url).get().build();
|
||||
String response = RedisBungee.getHttpClient().newCall(request).execute().body().string();
|
||||
|
||||
Type listType = new TypeToken<List<Name>>() {}.getType();
|
||||
List<Name> names = RedisBungee.getGson().fromJson(text, listType);
|
||||
List<Name> names = RedisBungee.getGson().fromJson(response, listType);
|
||||
|
||||
List<String> humanNames = new ArrayList<>();
|
||||
for (Name name : names) {
|
||||
|
@ -15,31 +15,20 @@ import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@AllArgsConstructor
|
||||
public abstract class RedisCallable<T> implements Callable<T>, Runnable {
|
||||
public abstract class RedisCallable<T> implements Callable<T> {
|
||||
private final RedisBungee plugin;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
run(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T call() {
|
||||
return run(false);
|
||||
}
|
||||
|
||||
private T run(boolean retry) {
|
||||
Jedis jedis = null;
|
||||
|
||||
try {
|
||||
jedis = plugin.getPool().getResource();
|
||||
try (Jedis jedis = plugin.getPool().getResource()) {
|
||||
return call(jedis);
|
||||
} catch (JedisConnectionException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to get connection", e);
|
||||
|
||||
if (jedis != null)
|
||||
plugin.getPool().returnBrokenResource(jedis);
|
||||
|
||||
if (!retry) {
|
||||
// Wait one second before retrying the task
|
||||
try {
|
||||
@ -49,10 +38,6 @@ public abstract class RedisCallable<T> implements Callable<T>, Runnable {
|
||||
}
|
||||
run(true);
|
||||
}
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
plugin.getPool().returnResource(jedis);
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException("task failed to run");
|
||||
|
@ -8,12 +8,10 @@ package com.imaginarycode.minecraft.redisbungee.util;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
||||
import com.squareup.okhttp.MediaType;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.RequestBody;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@ -21,6 +19,7 @@ import java.util.concurrent.Callable;
|
||||
class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||
private static final double PROFILES_PER_REQUEST = 100;
|
||||
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
|
||||
private static final MediaType JSON = MediaType.parse("application/json");
|
||||
private final List<String> names;
|
||||
private final boolean rateLimiting;
|
||||
|
||||
@ -33,24 +32,6 @@ class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||
this(names, true);
|
||||
}
|
||||
|
||||
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
|
||||
OutputStream stream = connection.getOutputStream();
|
||||
stream.write(body.getBytes());
|
||||
stream.flush();
|
||||
stream.close();
|
||||
}
|
||||
|
||||
private static HttpURLConnection createConnection() throws Exception {
|
||||
URL url = new URL(PROFILE_URL);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setUseCaches(false);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public static UUID getUUID(String id) {
|
||||
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
|
||||
}
|
||||
@ -59,10 +40,10 @@ class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||
Map<String, UUID> uuidMap = new HashMap<>();
|
||||
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
|
||||
for (int i = 0; i < requests; i++) {
|
||||
HttpURLConnection connection = createConnection();
|
||||
String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
|
||||
writeBody(connection, body);
|
||||
Profile[] array = RedisBungee.getGson().fromJson(new InputStreamReader(connection.getInputStream()), Profile[].class);
|
||||
Request request = new Request.Builder().url(PROFILE_URL).post(RequestBody.create(JSON, body)).build();
|
||||
String response = RedisBungee.getHttpClient().newCall(request).execute().body().string();
|
||||
Profile[] array = RedisBungee.getGson().fromJson(response, Profile[].class);
|
||||
for (Profile profile : array) {
|
||||
UUID uuid = UUIDFetcher.getUUID(profile.id);
|
||||
uuidMap.put(profile.name, uuid);
|
||||
|
Loading…
Reference in New Issue
Block a user