2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-23 04:28:01 +00:00

Reformat source code.

This commit is contained in:
Tux 2014-09-21 13:56:46 -04:00
parent e0ee2ada21
commit aa772ddf1b
8 changed files with 64 additions and 77 deletions

13
pom.xml
View File

@ -7,8 +7,8 @@
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -66,15 +66,18 @@
<relocations> <relocations>
<relocation> <relocation>
<pattern>redis.clients.jedis</pattern> <pattern>redis.clients.jedis</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis</shadedPattern> <shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis
</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>redis.clients.util</pattern> <pattern>redis.clients.util</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil</shadedPattern> <shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil
</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>org.apache.commons.pool</pattern> <pattern>org.apache.commons.pool</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commonspool</shadedPattern> <shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commonspool
</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
</configuration> </configuration>

View File

@ -25,7 +25,7 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.*; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level; import java.util.logging.Level;
@ -234,9 +234,10 @@ public class DataManager implements Listener {
switch (action) { switch (action) {
case JOIN: case JOIN:
final DataManagerMessage<LoginPayload> message1 = RedisBungee.getGson().fromJson(jsonObject, new TypeToken<DataManagerMessage<LoginPayload>>() {}.getType()); final DataManagerMessage<LoginPayload> message1 = RedisBungee.getGson().fromJson(jsonObject, new TypeToken<DataManagerMessage<LoginPayload>>() {
}.getType());
proxyCache.put(message1.getTarget(), message1.getSource()); proxyCache.put(message1.getTarget(), message1.getSource());
lastOnlineCache.put(message1.getTarget(), (long)0); lastOnlineCache.put(message1.getTarget(), (long) 0);
ipCache.put(message1.getTarget(), message1.getPayload().getAddress()); ipCache.put(message1.getTarget(), message1.getPayload().getAddress());
plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() { plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() {
@Override @Override
@ -246,7 +247,8 @@ public class DataManager implements Listener {
}); });
break; break;
case LEAVE: case LEAVE:
final DataManagerMessage<LogoutPayload> message2 = RedisBungee.getGson().fromJson(jsonObject, new TypeToken<DataManagerMessage<LogoutPayload>>() {}.getType()); final DataManagerMessage<LogoutPayload> message2 = RedisBungee.getGson().fromJson(jsonObject, new TypeToken<DataManagerMessage<LogoutPayload>>() {
}.getType());
invalidate(message2.getTarget()); invalidate(message2.getTarget());
lastOnlineCache.put(message2.getTarget(), message2.getPayload().getTimestamp()); lastOnlineCache.put(message2.getTarget(), message2.getPayload().getTimestamp());
plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() { plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() {
@ -257,7 +259,8 @@ public class DataManager implements Listener {
}); });
break; break;
case SERVER_CHANGE: case SERVER_CHANGE:
final DataManagerMessage<ServerChangePayload> message3 = RedisBungee.getGson().fromJson(jsonObject, new TypeToken<DataManagerMessage<ServerChangePayload>>() {}.getType()); final DataManagerMessage<ServerChangePayload> message3 = RedisBungee.getGson().fromJson(jsonObject, new TypeToken<DataManagerMessage<ServerChangePayload>>() {
}.getType());
serverCache.put(message3.getTarget(), message3.getPayload().getServer()); serverCache.put(message3.getTarget(), message3.getPayload().getServer());
plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() { plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() {
@Override @Override
@ -272,16 +275,15 @@ public class DataManager implements Listener {
@Getter @Getter
@RequiredArgsConstructor @RequiredArgsConstructor
static class DataManagerMessage<T> { static class DataManagerMessage<T> {
private final UUID target;
private final String source = RedisBungee.getApi().getServerId();
private final Action action; // for future use!
private final T payload;
enum Action { enum Action {
JOIN, JOIN,
LEAVE, LEAVE,
SERVER_CHANGE SERVER_CHANGE
} }
private final UUID target;
private final String source = RedisBungee.getApi().getServerId();
private final Action action; // for future use!
private final T payload;
} }
@Getter @Getter

View File

@ -12,7 +12,6 @@ import com.google.common.io.ByteStreams;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -20,7 +19,10 @@ import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration; import net.md_5.bungee.config.YamlConfiguration;
import redis.clients.jedis.*; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.exceptions.JedisException;
@ -40,20 +42,19 @@ import static com.google.common.base.Preconditions.checkArgument;
public final class RedisBungee extends Plugin { public final class RedisBungee extends Plugin {
private static Configuration configuration; private static Configuration configuration;
@Getter @Getter
private static Gson gson = new Gson();
private static RedisBungeeAPI api;
private static PubSubListener psl = null;
@Getter
private JedisPool pool; private JedisPool pool;
@Getter @Getter
private UUIDTranslator uuidTranslator; private UUIDTranslator uuidTranslator;
@Getter @Getter
private static Gson gson = new Gson();
@Getter
private String serverId; private String serverId;
@Getter @Getter
private DataManager dataManager; private DataManager dataManager;
@Getter @Getter
private ExecutorService service; private ExecutorService service;
private static RedisBungeeAPI api;
private static PubSubListener psl = null;
private List<String> serverIds; private List<String> serverIds;
private AtomicInteger nagAboutServers = new AtomicInteger(); private AtomicInteger nagAboutServers = new AtomicInteger();
@ -70,6 +71,10 @@ public final class RedisBungee extends Plugin {
return configuration; return configuration;
} }
static PubSubListener getPubSubListener() {
return psl;
}
final List<String> getServerIds() { final List<String> getServerIds() {
return serverIds; return serverIds;
} }
@ -104,10 +109,6 @@ public final class RedisBungee extends Plugin {
} }
} }
static PubSubListener getPubSubListener() {
return psl;
}
final Multimap<String, UUID> serversToPlayers() { final Multimap<String, UUID> serversToPlayers() {
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder();
for (UUID p : getPlayers()) { for (UUID p : getPlayers()) {

View File

@ -81,8 +81,8 @@ public class RedisBungeeAPI {
* are lazily calculated (but cached, see the contract of {@link #getNameFromUuid(java.util.UUID)}).</strong> * are lazily calculated (but cached, see the contract of {@link #getNameFromUuid(java.util.UUID)}).</strong>
* *
* @return a Set with all players found * @return a Set with all players found
* @since 0.3
* @see #getNameFromUuid(java.util.UUID) * @see #getNameFromUuid(java.util.UUID)
* @since 0.3
*/ */
public final Collection<String> getHumanPlayersOnline() { public final Collection<String> getHumanPlayersOnline() {
return Collections2.transform(getPlayersOnline(), new Function<UUID, String>() { return Collections2.transform(getPlayersOnline(), new Function<UUID, String>() {
@ -177,7 +177,7 @@ public class RedisBungeeAPI {
* @since 0.3.3 * @since 0.3.3
*/ */
public final void sendChannelMessage(@NonNull String channel, @NonNull String message) { public final void sendChannelMessage(@NonNull String channel, @NonNull String message) {
plugin.sendChannelMessage(channel, message); plugin.sendChannelMessage(channel, message);
} }
/** /**
@ -248,7 +248,7 @@ public class RedisBungeeAPI {
* <p> * <p>
* If performance is a concern, set {@code expensiveLookups} to false as this will disable lookups via Mojang. * If performance is a concern, set {@code expensiveLookups} to false as this will disable lookups via Mojang.
* *
* @param uuid the UUID to fetch the name for * @param uuid the UUID to fetch the name for
* @param expensiveLookups whether or not to perform potentially expensive lookups * @param expensiveLookups whether or not to perform potentially expensive lookups
* @return the name for the UUID * @return the name for the UUID
* @since 0.3.2 * @since 0.3.2
@ -263,8 +263,8 @@ public class RedisBungeeAPI {
* <p> * <p>
* If performance is a concern, see {@link #getUuidFromName(String, boolean)}, which disables the following functions: * If performance is a concern, see {@link #getUuidFromName(String, boolean)}, which disables the following functions:
* <ul> * <ul>
* <li>Searching local entries case-insensitively</li> * <li>Searching local entries case-insensitively</li>
* <li>Searching Mojang</li> * <li>Searching Mojang</li>
* </ul> * </ul>
* *
* @param name the UUID to fetch the name for * @param name the UUID to fetch the name for
@ -282,7 +282,7 @@ public class RedisBungeeAPI {
* If performance is a concern, set {@code expensiveLookups} to false to disable searching Mojang and searching for usernames * If performance is a concern, set {@code expensiveLookups} to false to disable searching Mojang and searching for usernames
* case-insensitively. * case-insensitively.
* *
* @param name the UUID to fetch the name for * @param name the UUID to fetch the name for
* @param expensiveLookups whether or not to perform potentially expensive lookups * @param expensiveLookups whether or not to perform potentially expensive lookups
* @return the UUID for the name * @return the UUID for the name
* @since 0.3.2 * @since 0.3.2

View File

@ -25,7 +25,7 @@ import java.util.UUID;
/** /**
* This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen. * This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen.
* <p/> * <p>
* All classes use the {@link RedisBungeeAPI}. * All classes use the {@link RedisBungeeAPI}.
* *
* @author tuxed * @author tuxed

View File

@ -29,12 +29,12 @@ import java.util.concurrent.Callable;
@AllArgsConstructor @AllArgsConstructor
public class RedisBungeeListener implements Listener { public class RedisBungeeListener implements Listener {
private final RedisBungee plugin;
private static final BaseComponent[] ALREADY_LOGGED_IN = private static final BaseComponent[] ALREADY_LOGGED_IN =
new ComponentBuilder("You are already logged on to this server.").color(ChatColor.RED) new ComponentBuilder("You are already logged on to this server.").color(ChatColor.RED)
.append("\n\nIf you were disconnected forcefully, please wait up to one minute.\nIf this does not resolve your issue, please contact staff.") .append("\n\nIf you were disconnected forcefully, please wait up to one minute.\nIf this does not resolve your issue, please contact staff.")
.color(ChatColor.GRAY) .color(ChatColor.GRAY)
.create(); .create();
private final RedisBungee plugin;
@EventHandler @EventHandler
public void onPlayerConnect(final PostLoginEvent event) { public void onPlayerConnect(final PostLoginEvent event) {

View File

@ -33,25 +33,6 @@ class UUIDFetcher implements Callable<Map<String, UUID>> {
this(names, true); this(names, true);
} }
public Map<String, UUID> call() throws Exception {
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);
for (Profile profile : array) {
UUID uuid = UUIDFetcher.getUUID(profile.id);
uuidMap.put(profile.name, uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L); // FIXME: This is not how we into concurrency
}
}
return uuidMap;
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception { private static void writeBody(HttpURLConnection connection, String body) throws Exception {
OutputStream stream = connection.getOutputStream(); OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes()); stream.write(body.getBytes());
@ -74,25 +55,23 @@ class UUIDFetcher implements Callable<Map<String, UUID>> {
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32)); return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
} }
public static byte[] toBytes(UUID uuid) { public Map<String, UUID> call() throws Exception {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); Map<String, UUID> uuidMap = new HashMap<>();
byteBuffer.putLong(uuid.getMostSignificantBits()); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
byteBuffer.putLong(uuid.getLeastSignificantBits()); for (int i = 0; i < requests; i++) {
return byteBuffer.array(); HttpURLConnection connection = createConnection();
} String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body);
public static UUID fromBytes(byte[] array) { Profile[] array = RedisBungee.getGson().fromJson(new InputStreamReader(connection.getInputStream()), Profile[].class);
if (array.length != 16) { for (Profile profile : array) {
throw new IllegalArgumentException("Illegal byte array length: " + array.length); UUID uuid = UUIDFetcher.getUUID(profile.id);
uuidMap.put(profile.name, uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L);
}
} }
ByteBuffer byteBuffer = ByteBuffer.wrap(array); return uuidMap;
long mostSignificant = byteBuffer.getLong();
long leastSignificant = byteBuffer.getLong();
return new UUID(mostSignificant, leastSignificant);
}
public static UUID getUUIDOf(String name) throws Exception {
return new UUIDFetcher(Collections.singletonList(name)).call().get(name);
} }
private class Profile { private class Profile {

View File

@ -13,21 +13,23 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.exceptions.JedisException;
import java.util.*; import java.util.Calendar;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@RequiredArgsConstructor @RequiredArgsConstructor
public final class UUIDTranslator { public final class UUIDTranslator {
private static final Pattern UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}");
private static final Pattern MOJANGIAN_UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{32}");
private final RedisBungee plugin; private final RedisBungee plugin;
private final Map<String, CachedUUIDEntry> nameToUuidMap = new ConcurrentHashMap<>(128, 0.5f, 4); private final Map<String, CachedUUIDEntry> nameToUuidMap = new ConcurrentHashMap<>(128, 0.5f, 4);
private final Map<UUID, CachedUUIDEntry> uuidToNameMap = new ConcurrentHashMap<>(128, 0.5f, 4); private final Map<UUID, CachedUUIDEntry> uuidToNameMap = new ConcurrentHashMap<>(128, 0.5f, 4);
private static final Pattern UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}");
private static final Pattern MOJANGIAN_UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{32}");
private void addToMaps(String name, UUID uuid) { private void addToMaps(String name, UUID uuid) {
// This is why I like LocalDate... // This is why I like LocalDate...