Let's be legal
This commit is contained in:
parent
e82d1b5885
commit
251eb2e53e
@ -1,7 +1,41 @@
|
||||
package mineplex.core.common;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.PropertyMap;
|
||||
import com.mojang.util.UUIDTypeAdapter;
|
||||
|
||||
public class Constants
|
||||
{
|
||||
public static final String WEB_ADDRESS = "http://accounts.mineplex.com/";
|
||||
public static final String WEB_CONFIG_KEY = "webServer";
|
||||
|
||||
public static Gson GSON;
|
||||
|
||||
static
|
||||
{
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
|
||||
try
|
||||
{
|
||||
Class<?> clazz = Class.forName("com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService$GameProfileSerializer");
|
||||
Constructor<?> ctor = clazz.getDeclaredConstructor();
|
||||
ctor.setAccessible(true);
|
||||
builder.registerTypeAdapter(GameProfile.class, ctor.newInstance());
|
||||
}
|
||||
catch (ReflectiveOperationException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
builder
|
||||
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
|
||||
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter());
|
||||
|
||||
GSON = builder.create();
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,9 @@ import com.google.gson.JsonSerializer;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.PropertyMap;
|
||||
import com.mojang.util.UUIDTypeAdapter;
|
||||
|
||||
import mineplex.core.common.Constants;
|
||||
import mineplex.core.status.ServerStatusManager;
|
||||
import mineplex.serverdata.data.Data;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
@ -19,17 +22,6 @@ import java.util.UUID;
|
||||
|
||||
public class DisguisePlayerBean implements Data
|
||||
{
|
||||
private static final Gson GSON;
|
||||
|
||||
static
|
||||
{
|
||||
GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(GameProfile.class, new GameProfileSerializer())
|
||||
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
|
||||
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
|
||||
.create();
|
||||
}
|
||||
|
||||
private int _accountID;
|
||||
private String _playerName;
|
||||
|
||||
@ -82,56 +74,11 @@ public class DisguisePlayerBean implements Data
|
||||
|
||||
private String serialize(GameProfile gameProfile)
|
||||
{
|
||||
return GSON.toJson(gameProfile);
|
||||
return Constants.GSON.toJson(gameProfile);
|
||||
}
|
||||
|
||||
private GameProfile deserialize(String in)
|
||||
{
|
||||
return GSON.fromJson(in, GameProfile.class);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return Constants.GSON.fromJson(in, GameProfile.class);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package mineplex.core.status;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -13,19 +13,13 @@ 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.account.CoreClientManager;
|
||||
import mineplex.core.common.Constants;
|
||||
import mineplex.core.common.util.Callback;
|
||||
import mineplex.core.monitor.LagMeter;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
@ -41,85 +35,79 @@ import mineplex.serverdata.servers.ServerRepository;
|
||||
|
||||
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.
|
||||
public final int DEFAULT_SERVER_TIMEOUT = 30;
|
||||
|
||||
|
||||
private ServerRepository _repository;
|
||||
private CoreClientManager _clientManager;
|
||||
private LagMeter _lagMeter;
|
||||
|
||||
|
||||
private String _name;
|
||||
private Region _region;
|
||||
|
||||
|
||||
private boolean _enabled = true;
|
||||
|
||||
|
||||
private long _startUpDate;
|
||||
|
||||
public ServerStatusManager(JavaPlugin plugin, CoreClientManager clientManager, LagMeter lagMeter)
|
||||
{
|
||||
super("Server Status Manager", plugin);
|
||||
|
||||
|
||||
_startUpDate = Utility.currentTimeSeconds();
|
||||
_clientManager = clientManager;
|
||||
_lagMeter = lagMeter;
|
||||
|
||||
|
||||
if (new File("IgnoreUpdates.dat").exists() && !(new File("EnableStatus.dat").exists()))
|
||||
_enabled = false;
|
||||
|
||||
|
||||
setupConfigValues();
|
||||
|
||||
|
||||
_name = plugin.getConfig().getString("serverstatus.name");
|
||||
|
||||
|
||||
_region = plugin.getConfig().getBoolean("serverstatus.us") ? Region.US : Region.EU;
|
||||
|
||||
ServerCommandManager.getInstance().initializeServer(_name, GSON);
|
||||
|
||||
ServerCommandManager.getInstance().initializeServer(_name, Constants.GSON);
|
||||
ServerCommandManager.getInstance().registerCommandType("SuicideCommand", SuicideCommand.class, new SuicideHandler(this, _name, _region));
|
||||
|
||||
|
||||
_repository = ServerManager.getServerRepository(_region);
|
||||
saveServerStatus();
|
||||
}
|
||||
|
||||
|
||||
private void setupConfigValues()
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
getPlugin().getConfig().addDefault("serverstatus.connectionurl", "db.mineplex.com:3306");
|
||||
getPlugin().getConfig().set("serverstatus.connectionurl", getPlugin().getConfig().getString("serverstatus.connectionurl"));
|
||||
|
||||
|
||||
getPlugin().getConfig().addDefault("serverstatus.username", "MilitaryPolice");
|
||||
getPlugin().getConfig().set("serverstatus.username", getPlugin().getConfig().getString("serverstatus.username"));
|
||||
|
||||
|
||||
getPlugin().getConfig().addDefault("serverstatus.password", "CUPr6Wuw2Rus$qap");
|
||||
getPlugin().getConfig().set("serverstatus.password", getPlugin().getConfig().getString("serverstatus.password"));
|
||||
|
||||
|
||||
getPlugin().getConfig().addDefault("serverstatus.us", true);
|
||||
getPlugin().getConfig().set("serverstatus.us", getPlugin().getConfig().getBoolean("serverstatus.us"));
|
||||
|
||||
|
||||
getPlugin().getConfig().addDefault("serverstatus.name", "TEST-1");
|
||||
getPlugin().getConfig().set("serverstatus.name", getPlugin().getConfig().getString("serverstatus.name"));
|
||||
|
||||
|
||||
getPlugin().getConfig().addDefault("serverstatus.group", "Testing");
|
||||
getPlugin().getConfig().set("serverstatus.group", getPlugin().getConfig().getString("serverstatus.group"));
|
||||
|
||||
|
||||
getPlugin().saveConfig();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void retrieveServerStatuses(final Callback<Collection<MinecraftServer>> callback)
|
||||
{
|
||||
if (!_enabled)
|
||||
return;
|
||||
|
||||
|
||||
getPlugin().getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
||||
{
|
||||
public void run()
|
||||
@ -131,17 +119,17 @@ public class ServerStatusManager extends MiniPlugin
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@EventHandler
|
||||
public void saveServerStatus(UpdateEvent event)
|
||||
{
|
||||
if (event.getType() != UpdateType.FASTER)
|
||||
return;
|
||||
|
||||
|
||||
if (_enabled)
|
||||
saveServerStatus();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save the current {@link MinecraftServer} snapshot of this server to
|
||||
* the {@link ServerRepository}.
|
||||
@ -155,26 +143,26 @@ public class ServerStatusManager extends MiniPlugin
|
||||
{
|
||||
MinecraftServer server = _repository.getServerStatus(serverSnapshot.getName());
|
||||
int timeout = DEFAULT_SERVER_TIMEOUT;
|
||||
|
||||
|
||||
if (server != null && !server.getPublicAddress().equalsIgnoreCase(serverSnapshot.getPublicAddress()))
|
||||
{
|
||||
timeout = -DEFAULT_SERVER_TIMEOUT;
|
||||
}
|
||||
|
||||
|
||||
_repository.updataServerStatus(serverSnapshot, timeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return a newly instanced {@link MinecraftServer} snapshot that represents the
|
||||
* @return a newly instanced {@link MinecraftServer} snapshot that represents the
|
||||
* current internal state of this minecraft server.
|
||||
*/
|
||||
private MinecraftServer generateServerSnapshot()
|
||||
{
|
||||
ServerListPingEvent event = new ServerListPingEvent(null, getPlugin().getServer().getMotd(), getPlugin().getServer().getOnlinePlayers().size(), getPlugin().getServer().getMaxPlayers());
|
||||
getPluginManager().callEvent(event);
|
||||
|
||||
|
||||
String motd = _enabled ? event.getMotd() : "Restarting";
|
||||
int playerCount = _clientManager.getPlayerCountIncludingConnecting();
|
||||
int maxPlayerCount = event.getMaxPlayers();
|
||||
@ -184,9 +172,9 @@ public class ServerStatusManager extends MiniPlugin
|
||||
String group = _plugin.getConfig().getString("serverstatus.group") + "";
|
||||
int ram = (int) ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory()) / 1048576);
|
||||
int maxRam = (int) (Runtime.getRuntime().maxMemory() / 1048576);
|
||||
|
||||
|
||||
int donorsOnline = 0;
|
||||
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers())
|
||||
{
|
||||
if (_clientManager.Get(player).GetRank().isDonor())
|
||||
@ -194,9 +182,9 @@ public class ServerStatusManager extends MiniPlugin
|
||||
donorsOnline++;
|
||||
}
|
||||
}
|
||||
|
||||
return new MinecraftServer(_name, group, motd, address, port, playerCount,
|
||||
maxPlayerCount, tps, ram, maxRam, _startUpDate, donorsOnline);
|
||||
|
||||
return new MinecraftServer(_name, group, motd, address, port, playerCount,
|
||||
maxPlayerCount, tps, ram, maxRam, _startUpDate, donorsOnline);
|
||||
}
|
||||
|
||||
public String getCurrentServerName()
|
||||
@ -208,7 +196,7 @@ public class ServerStatusManager extends MiniPlugin
|
||||
{
|
||||
if (!_enabled)
|
||||
return;
|
||||
|
||||
|
||||
getPlugin().getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
|
||||
{
|
||||
public void run()
|
||||
@ -231,51 +219,4 @@ public class ServerStatusManager extends MiniPlugin
|
||||
_enabled = false;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user