Remove spigot as a dependency from ServerData

This commit is contained in:
samczsun 2017-01-12 20:06:28 -05:00 committed by cnr
parent cdfc19f566
commit e82d1b5885
5 changed files with 86 additions and 76 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.Gson;
import mineplex.serverdata.Region; import mineplex.serverdata.Region;
import mineplex.serverdata.commands.PlayerJoinCommand; import mineplex.serverdata.commands.PlayerJoinCommand;
@ -42,7 +43,7 @@ public class PlayerTracker implements Listener
_repository = new RedisDataRepository<PlayerStatus>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(), _repository = new RedisDataRepository<PlayerStatus>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
Region.currentRegion(), PlayerStatus.class, "playerStatus"); Region.currentRegion(), PlayerStatus.class, "playerStatus");
ServerCommandManager.getInstance().initializeServer("BUNGEE ENABLE - " + System.currentTimeMillis()); ServerCommandManager.getInstance().initializeServer("BUNGEE ENABLE - " + System.currentTimeMillis(), new Gson());
ServerCommandManager.getInstance().registerCommandType(mineplex.serverdata.commands.PlayerJoinCommand.class, new PlayerJoinHandler(this)); ServerCommandManager.getInstance().registerCommandType(mineplex.serverdata.commands.PlayerJoinCommand.class, new PlayerJoinHandler(this));
System.out.println("Initialized PlayerTracker."); System.out.println("Initialized PlayerTracker.");

View File

@ -1,7 +1,9 @@
package mineplex.core.status; package mineplex.core.status;
import java.io.File; import java.io.File;
import java.lang.reflect.Type;
import java.util.Collection; import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -9,6 +11,19 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.util.UUIDTypeAdapter;
import mineplex.core.MiniPlugin; import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.Callback; import mineplex.core.common.util.Callback;
@ -26,6 +41,12 @@ import mineplex.serverdata.servers.ServerRepository;
public class ServerStatusManager extends MiniPlugin public class ServerStatusManager extends MiniPlugin
{ {
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(GameProfile.class, new GameProfileSerializer())
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.create();
// The default timeout (in seconds) before the ServerStatus expires. // The default timeout (in seconds) before the ServerStatus expires.
public final int DEFAULT_SERVER_TIMEOUT = 30; public final int DEFAULT_SERVER_TIMEOUT = 30;
@ -57,7 +78,7 @@ public class ServerStatusManager extends MiniPlugin
_region = plugin.getConfig().getBoolean("serverstatus.us") ? Region.US : Region.EU; _region = plugin.getConfig().getBoolean("serverstatus.us") ? Region.US : Region.EU;
ServerCommandManager.getInstance().initializeServer(_name); ServerCommandManager.getInstance().initializeServer(_name, GSON);
ServerCommandManager.getInstance().registerCommandType("SuicideCommand", SuicideCommand.class, new SuicideHandler(this, _name, _region)); ServerCommandManager.getInstance().registerCommandType("SuicideCommand", SuicideCommand.class, new SuicideHandler(this, _name, _region));
_repository = ServerManager.getServerRepository(_region); _repository = ServerManager.getServerRepository(_region);
@ -210,4 +231,51 @@ public class ServerStatusManager extends MiniPlugin
_enabled = false; _enabled = false;
saveServerStatus(); saveServerStatus();
} }
private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile>
{
private GameProfileSerializer()
{
}
public GameProfile deserialize(JsonElement var1, Type var2, JsonDeserializationContext var3) throws JsonParseException
{
JsonObject var4 = (JsonObject) var1;
UUID var5 = var4.has("id") ? (UUID) var3.deserialize(var4.get("id"), UUID.class) : null;
String var6 = var4.has("name") ? var4.getAsJsonPrimitive("name").getAsString() : null;
GameProfile gameProfile = new GameProfile(var5, var6);
if (var4.has("properties"))
{
PropertyMap propertyMap = var3.deserialize(var4.get("properties"), PropertyMap.class);
gameProfile.getProperties().putAll(propertyMap);
}
return gameProfile;
}
public JsonElement serialize(GameProfile var1, Type var2, JsonSerializationContext var3)
{
JsonObject var4 = new JsonObject();
if (var1.getId() != null)
{
var4.add("id", var3.serialize(var1.getId()));
}
if (var1.getName() != null)
{
var4.addProperty("name", var1.getName());
}
if (var1.getProperties() != null)
{
var4.add("properties", var3.serialize(var1.getProperties()));
}
return var4;
}
}
} }

View File

@ -28,10 +28,6 @@
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.mineplex</groupId>
<artifactId>spigot</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.jooq</groupId> <groupId>org.jooq</groupId>
<artifactId>jooq</artifactId> <artifactId>jooq</artifactId>

View File

@ -1,28 +1,15 @@
package mineplex.serverdata; package mineplex.serverdata;
import java.lang.reflect.Type;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.google.gson.Gson;
import mineplex.serverdata.servers.ConnectionData; import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ServerManager; import mineplex.serverdata.servers.ServerManager;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.util.UUIDTypeAdapter;
/** /**
* Utility offers various necessary utility-based methods for use in Mineplex.ServerData. * Utility offers various necessary utility-based methods for use in Mineplex.ServerData.
@ -35,11 +22,7 @@ public class Utility
private static long _millisTimeDifference; private static long _millisTimeDifference;
// The Gson instance used to serialize/deserialize objects in JSON form. // The Gson instance used to serialize/deserialize objects in JSON form.
private static Gson _gson = new GsonBuilder() private static Gson _gson = new Gson();
.registerTypeAdapter(GameProfile.class, new GameProfileSerializer())
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.create();
public static Gson getGson() { return _gson; } public static Gson getGson() { return _gson; }
// map of all instantiated connection pools, distinguished by their ip:port combination // map of all instantiated connection pools, distinguished by their ip:port combination
@ -187,50 +170,4 @@ public class Utility
_millisTimeDifference = (currentTime * 1000) - System.currentTimeMillis(); _millisTimeDifference = (currentTime * 1000) - System.currentTimeMillis();
} }
private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile>
{
private GameProfileSerializer()
{
}
public GameProfile deserialize(JsonElement var1, Type var2, JsonDeserializationContext var3) throws JsonParseException
{
JsonObject var4 = (JsonObject) var1;
UUID var5 = var4.has("id") ? (UUID) var3.deserialize(var4.get("id"), UUID.class) : null;
String var6 = var4.has("name") ? var4.getAsJsonPrimitive("name").getAsString() : null;
GameProfile gameProfile = new GameProfile(var5, var6);
if (var4.has("properties"))
{
PropertyMap propertyMap = var3.deserialize(var4.get("properties"), PropertyMap.class);
gameProfile.getProperties().putAll(propertyMap);
}
return gameProfile;
}
public JsonElement serialize(GameProfile var1, Type var2, JsonSerializationContext var3)
{
JsonObject var4 = new JsonObject();
if (var1.getId() != null)
{
var4.add("id", var3.serialize(var1.getId()));
}
if (var1.getName() != null)
{
var4.addProperty("name", var1.getName());
}
if (var1.getProperties() != null)
{
var4.add("properties", var3.serialize(var1.getProperties()));
}
return var4;
}
}
} }

View File

@ -3,6 +3,8 @@ package mineplex.serverdata.commands;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.gson.Gson;
import mineplex.serverdata.Utility; import mineplex.serverdata.Utility;
import mineplex.serverdata.servers.ServerManager; import mineplex.serverdata.servers.ServerManager;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@ -23,7 +25,13 @@ public class ServerCommandManager
private Map<String, CommandType> _commandTypes; private Map<String, CommandType> _commandTypes;
private String _localServerName; private String _localServerName;
public void initializeServer(String serverName) { _localServerName = serverName; } private Gson _gson;
public void initializeServer(String serverName, Gson gson)
{
_localServerName = serverName;
_gson = gson;
}
public boolean isServerInitialized() { return _localServerName != null; } public boolean isServerInitialized() { return _localServerName != null; }
public String getServerName() public String getServerName()
{ {
@ -75,7 +83,7 @@ public class ServerCommandManager
public void run() public void run()
{ {
String commandType = serverCommand.getClass().getSimpleName(); String commandType = serverCommand.getClass().getSimpleName();
String serializedCommand = Utility.serialize(serverCommand); String serializedCommand = _gson.toJson(serverCommand);
try(Jedis jedis = _writePool.getResource()) try(Jedis jedis = _writePool.getResource())
{ {
@ -101,7 +109,7 @@ public class ServerCommandManager
if (_commandTypes.containsKey(commandType)) if (_commandTypes.containsKey(commandType))
{ {
Class<? extends ServerCommand> commandClazz = _commandTypes.get(commandType).getCommandType(); Class<? extends ServerCommand> commandClazz = _commandTypes.get(commandType).getCommandType();
final ServerCommand serverCommand = Utility.deserialize(serializedCommand, commandClazz); final ServerCommand serverCommand = _gson.fromJson(serializedCommand, commandClazz);
if (serverCommand.isTargetServer(_localServerName)) if (serverCommand.isTargetServer(_localServerName))
{ {