Add callback feature set for cross-server command system to reduce dependencies, clean up old constant references and add in ability to initialize server name on ServerData. Clean up old 'this' references to '_' prefixed variables and standardize field naming.
This commit is contained in:
parent
5c9ee3e79e
commit
fc98cf4f6c
@ -27,6 +27,8 @@ import mineplex.core.updater.event.UpdateEvent;
|
||||
import mineplex.serverdata.Region;
|
||||
import mineplex.serverdata.ServerCommandManager;
|
||||
import mineplex.serverdata.ServerManager;
|
||||
import mineplex.serverdata.transfers.ServerTransfer;
|
||||
import mineplex.serverdata.transfers.TransferCommand;
|
||||
|
||||
public class Portal extends MiniPlugin
|
||||
{
|
||||
@ -53,7 +55,7 @@ public class Portal extends MiniPlugin
|
||||
Bukkit.getMessenger().registerOutgoingPluginChannel(GetPlugin(), "BungeeCord");
|
||||
|
||||
// Register the server command type for future use
|
||||
ServerCommandManager.getInstance().registerCommandType(TransferCommand.class);
|
||||
ServerCommandManager.getInstance().registerCommandType(TransferCommand.class, new TransferHandler());
|
||||
}
|
||||
|
||||
public void SendAllPlayers(String serverName)
|
||||
|
@ -0,0 +1,29 @@
|
||||
package mineplex.core.portal;
|
||||
|
||||
import mineplex.serverdata.CommandCallback;
|
||||
import mineplex.serverdata.ServerCommand;
|
||||
import mineplex.serverdata.transfers.ServerTransfer;
|
||||
import mineplex.serverdata.transfers.TransferCommand;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class TransferHandler implements CommandCallback
|
||||
{
|
||||
|
||||
public void run(ServerCommand command)
|
||||
{
|
||||
if (command instanceof TransferCommand)
|
||||
{
|
||||
TransferCommand transferCommand = (TransferCommand) command;
|
||||
ServerTransfer transfer = transferCommand.getTransfer();
|
||||
|
||||
Player player = Bukkit.getPlayer(transfer.getPlayerName());
|
||||
|
||||
if (player != null && player.isOnline())
|
||||
{
|
||||
Portal.getInstance().SendPlayerToServer(player, transfer.getServerName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
import mineplex.serverdata.MinecraftServer;
|
||||
import mineplex.serverdata.Region;
|
||||
import mineplex.serverdata.ServerCommandManager;
|
||||
import mineplex.serverdata.ServerManager;
|
||||
import mineplex.serverdata.ServerRepository;
|
||||
import mineplex.serverdata.Utility;
|
||||
@ -47,6 +48,9 @@ public class ServerStatusManager extends MiniPlugin
|
||||
setupConfigValues();
|
||||
|
||||
_name = plugin.getConfig().getString("serverstatus.name");
|
||||
|
||||
ServerCommandManager.getInstance().initializeServer(_name);
|
||||
|
||||
_us = plugin.getConfig().getBoolean("serverstatus.us");
|
||||
|
||||
Region region = _us ? Region.US : Region.EU;
|
||||
|
@ -8,6 +8,5 @@
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/httpcore-4.2.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/Mineplex.ServerData"/>
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/jedis-2.4.2.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/Mineplex.Core"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
@ -9,14 +9,14 @@ import java.util.Set;
|
||||
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import mineplex.core.portal.Portal;
|
||||
import mineplex.core.portal.ServerTransfer;
|
||||
import mineplex.serverdata.DataRepository;
|
||||
import mineplex.serverdata.MinecraftServer;
|
||||
import mineplex.serverdata.RedisDataRepository;
|
||||
import mineplex.serverdata.Region;
|
||||
import mineplex.serverdata.ServerManager;
|
||||
import mineplex.serverdata.ServerRepository;
|
||||
import mineplex.serverdata.transfers.ServerTransfer;
|
||||
import mineplex.serverdata.transfers.TransferCommand;
|
||||
|
||||
public class QueueRepository
|
||||
{
|
||||
@ -130,7 +130,10 @@ public class QueueRepository
|
||||
{
|
||||
for (String playerName : queueParty.getPlayers())
|
||||
{
|
||||
Portal.transferPlayer(playerName, emptyServer.getName());
|
||||
// Execute a transfer command
|
||||
ServerTransfer serverTransfer = new ServerTransfer(playerName, emptyServer.getName());
|
||||
TransferCommand transferCommand = new TransferCommand(serverTransfer);
|
||||
transferCommand.publish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package mineplex.serverdata;
|
||||
|
||||
public interface CommandCallback
|
||||
{
|
||||
|
||||
public void run(ServerCommand command);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package mineplex.serverdata;
|
||||
|
||||
public class CommandType
|
||||
{
|
||||
|
||||
private Class<? extends ServerCommand> _commandClazz;
|
||||
public Class<? extends ServerCommand> getCommandType() { return _commandClazz; }
|
||||
|
||||
private CommandCallback _commandCallback;
|
||||
public CommandCallback getCallback() { return _commandCallback; }
|
||||
|
||||
public CommandType(Class<? extends ServerCommand> commandClazz, CommandCallback commandCallback)
|
||||
{
|
||||
_commandClazz = commandClazz;
|
||||
_commandCallback = commandCallback;
|
||||
}
|
||||
}
|
@ -29,9 +29,9 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
|
||||
// The geographical region of the servers stored by this ServerRepository
|
||||
private Region _region;
|
||||
|
||||
private Class<T> elementType;
|
||||
private Class<T> _elementType;
|
||||
|
||||
private String elementLabel;
|
||||
private String _elementLabel;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
@ -42,10 +42,10 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
|
||||
public RedisDataRepository(String host, int port, Region region,
|
||||
Class<T> elementType, String elementLabel)
|
||||
{
|
||||
this._jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
|
||||
this._region = region;
|
||||
this.elementType = elementType;
|
||||
this.elementLabel = elementLabel;
|
||||
_jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
|
||||
_region = region;
|
||||
_elementType = elementType;
|
||||
_elementLabel = elementLabel;
|
||||
}
|
||||
|
||||
public RedisDataRepository(Region region, Class<T> elementType, String elementLabel)
|
||||
@ -56,7 +56,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
|
||||
|
||||
public String getElementSetKey()
|
||||
{
|
||||
return concatenate("data", elementLabel, _region.toString());
|
||||
return concatenate("data", _elementLabel, _region.toString());
|
||||
}
|
||||
|
||||
public String generateKey(T element)
|
||||
@ -317,7 +317,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
|
||||
|
||||
protected T deserialize(String serializedData)
|
||||
{
|
||||
return Utility.deserialize(serializedData, elementType);
|
||||
return Utility.deserialize(serializedData, _elementType);
|
||||
}
|
||||
|
||||
protected String serialize(T element)
|
||||
|
@ -1,10 +1,12 @@
|
||||
package mineplex.serverdata;
|
||||
|
||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||
|
||||
public abstract class ServerCommand
|
||||
{
|
||||
|
||||
// The names of servers targetted to receive this ServerCommand.
|
||||
private String[] targetServers;
|
||||
private String[] _targetServers;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
@ -12,13 +14,16 @@ public abstract class ServerCommand
|
||||
*/
|
||||
public ServerCommand(String... targetServers)
|
||||
{
|
||||
this.targetServers = targetServers;
|
||||
_targetServers = targetServers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the command on it's destination target server.
|
||||
*/
|
||||
public abstract void run();
|
||||
public void run()
|
||||
{
|
||||
// Not yet implemented in base
|
||||
}
|
||||
|
||||
/**
|
||||
* @param serverName - the name of the server to be checked for whether they are a target
|
||||
@ -27,10 +32,10 @@ public abstract class ServerCommand
|
||||
*/
|
||||
public boolean isTargetServer(String serverName)
|
||||
{
|
||||
if (targetServers == null || targetServers.length == 0) // Targets all online servers
|
||||
if (_targetServers == null || _targetServers.length == 0) // Targets all online servers
|
||||
return true;
|
||||
|
||||
for (String targetServer : targetServers)
|
||||
for (String targetServer : _targetServers)
|
||||
{
|
||||
if (targetServer.equalsIgnoreCase(serverName))
|
||||
{
|
||||
|
@ -3,6 +3,7 @@ package mineplex.serverdata;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import mineplex.serverdata.transfers.TransferCommand;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
@ -11,23 +12,30 @@ public class ServerCommandManager
|
||||
{
|
||||
|
||||
// The singleton instance of ServerCommandManager
|
||||
private static ServerCommandManager instance;
|
||||
private static ServerCommandManager _instance;
|
||||
|
||||
public final String SERVER_COMMANDS_CHANNEL = "commands.server";
|
||||
|
||||
private JedisPool _jedisPool;
|
||||
private Map<String, Class<? extends ServerCommand>> commandTypes;
|
||||
private Map<String, CommandType> _commandTypes;
|
||||
|
||||
private String _localServerName;
|
||||
public void initializeServer(String serverName) { _localServerName = serverName; }
|
||||
public boolean isServerInitialized() { return _localServerName != null; }
|
||||
|
||||
/**
|
||||
* Private class constructor to prevent non-singleton instances.
|
||||
*/
|
||||
private ServerCommandManager()
|
||||
{
|
||||
this._jedisPool = new JedisPool(new JedisPoolConfig(), ServerManager.DEFAULT_REDIS_HOST,
|
||||
_jedisPool = new JedisPool(new JedisPoolConfig(), ServerManager.DEFAULT_REDIS_HOST,
|
||||
ServerManager.DEFAULT_REDIS_PORT);
|
||||
this.commandTypes = new HashMap<String, Class<? extends ServerCommand>>();
|
||||
_commandTypes = new HashMap<String, CommandType>();
|
||||
|
||||
initialize();
|
||||
|
||||
// Register default command types
|
||||
registerCommandType(TransferCommand.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,14 +100,27 @@ public class ServerCommandManager
|
||||
*/
|
||||
public void handleCommand(String commandType, String serializedCommand)
|
||||
{
|
||||
if (commandTypes.containsKey(commandType))
|
||||
if (!isServerInitialized())
|
||||
{
|
||||
ServerCommand serverCommand = Utility.deserialize(serializedCommand, commandTypes.get(commandType));
|
||||
// TODO: Log un-initialized server receiving command?
|
||||
return;
|
||||
}
|
||||
|
||||
if (serverCommand.isTargetServer("THIS SERVER NAME HERE")) // TODO: Find server name ref
|
||||
if (_commandTypes.containsKey(commandType))
|
||||
{
|
||||
Class<? extends ServerCommand> commandClazz = _commandTypes.get(commandType).getCommandType();
|
||||
ServerCommand serverCommand = Utility.deserialize(serializedCommand, commandClazz);
|
||||
|
||||
if (serverCommand.isTargetServer(_localServerName))
|
||||
{
|
||||
// TODO: Run synchronously?
|
||||
serverCommand.run(); // Run the server command
|
||||
CommandCallback callback = _commandTypes.get(commandType).getCallback();
|
||||
serverCommand.run(); // Run server command without callback
|
||||
|
||||
if (callback != null)
|
||||
{
|
||||
callback.run(serverCommand); // Run callback
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -108,14 +129,22 @@ public class ServerCommandManager
|
||||
* Register a new type of {@link ServerCommand}.
|
||||
* @param commandType - the {@link ServerCommand} type to register.
|
||||
*/
|
||||
public void registerCommandType(Class<? extends ServerCommand> commandType)
|
||||
public void registerCommandType(Class<? extends ServerCommand> commandType, CommandCallback callback)
|
||||
{
|
||||
String commandName = commandType.toString();
|
||||
|
||||
if (!commandTypes.containsKey(commandName))
|
||||
if (_commandTypes.containsKey(commandName))
|
||||
{
|
||||
commandTypes.put(commandName, commandType);
|
||||
// Log overwriting of command type?
|
||||
}
|
||||
|
||||
CommandType cmdType = new CommandType(commandType, callback);
|
||||
_commandTypes.put(commandName, cmdType);
|
||||
}
|
||||
|
||||
public void registerCommandType(Class<? extends ServerCommand> commandType)
|
||||
{
|
||||
registerCommandType(commandType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,11 +152,11 @@ public class ServerCommandManager
|
||||
*/
|
||||
public static ServerCommandManager getInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
if (_instance == null)
|
||||
{
|
||||
instance = new ServerCommandManager();
|
||||
_instance = new ServerCommandManager();
|
||||
}
|
||||
|
||||
return instance;
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
package mineplex.core.portal;
|
||||
package mineplex.serverdata.transfers;
|
||||
|
||||
public class ServerTransfer
|
||||
{
|
||||
|
||||
// The name of the player who is being transferred.
|
||||
private String playerName;
|
||||
public String getPlayerName() { return playerName; }
|
||||
private String _playerName;
|
||||
public String getPlayerName() { return _playerName; }
|
||||
|
||||
// The name of the destination server in this ServerTransfer.
|
||||
private String serverName;
|
||||
public String getServerName() { return serverName; }
|
||||
private String _serverName;
|
||||
public String getServerName() { return _serverName; }
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
@ -18,7 +18,7 @@ public class ServerTransfer
|
||||
*/
|
||||
public ServerTransfer(String playerName, String serverName)
|
||||
{
|
||||
this.playerName = playerName;
|
||||
this.serverName = serverName;
|
||||
_playerName = playerName;
|
||||
_serverName = serverName;
|
||||
}
|
||||
}
|
@ -1,7 +1,4 @@
|
||||
package mineplex.core.portal;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
package mineplex.serverdata.transfers;
|
||||
|
||||
import mineplex.serverdata.ServerCommand;
|
||||
|
||||
@ -15,7 +12,8 @@ public class TransferCommand extends ServerCommand
|
||||
{
|
||||
|
||||
// The ServerTransfer to be sent to another server for enactment
|
||||
private ServerTransfer transfer;
|
||||
private ServerTransfer _transfer;
|
||||
public ServerTransfer getTransfer() { return _transfer; }
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
@ -23,18 +21,13 @@ public class TransferCommand extends ServerCommand
|
||||
*/
|
||||
public TransferCommand(ServerTransfer transfer)
|
||||
{
|
||||
this.transfer = transfer;
|
||||
_transfer = transfer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
Player player = Bukkit.getPlayer(transfer.getPlayerName());
|
||||
|
||||
if (player != null && player.isOnline())
|
||||
{
|
||||
Portal.getInstance().SendPlayerToServer(player, transfer.getServerName());
|
||||
}
|
||||
// Utilitizes a callback functionality to seperate dependencies
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user