After refractor

This commit is contained in:
Jaime Martínez Rincón 2017-12-26 02:00:18 +01:00
parent 4f8840c765
commit 0e5ee0e2e2
6 changed files with 168 additions and 166 deletions

View File

@ -1,6 +1,7 @@
package com.jaimemartz.playerbalancer;
import com.google.common.reflect.TypeToken;
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
import com.jaimemartz.playerbalancer.commands.MainCommand;
import com.jaimemartz.playerbalancer.commands.ManageCommand;
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;
@ -10,7 +11,6 @@ import com.jaimemartz.playerbalancer.manager.PasteHelper;
import com.jaimemartz.playerbalancer.manager.PlayerLocker;
import com.jaimemartz.playerbalancer.ping.StatusManager;
import com.jaimemartz.playerbalancer.section.SectionManager;
import com.jaimemartz.playerbalancer.services.FallbackService;
import com.jaimemartz.playerbalancer.settings.SettingsHolder;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener;
@ -34,7 +34,8 @@ public class PlayerBalancer extends Plugin {
private SectionManager sectionManager;
private NetworkManager networkManager;
private ConfigurationLoader<CommentedConfigurationNode> loader;
private FallbackService fallbackService;
private FallbackCommand fallbackCommand;
private Command mainCommand, manageCommand;
private Listener connectListener, kickListener, reloadListener, pluginMessageListener;
@ -117,10 +118,9 @@ public class PlayerBalancer extends Plugin {
statusManager.start();
}
fallbackService = new FallbackService(this, settings.getFallbackCommandProps().getCommand());
if (settings.getFallbackCommandProps().isEnabled()) {
getProxy().getPluginManager().registerCommand(this, fallbackService);
fallbackCommand = new FallbackCommand(this);
getProxy().getPluginManager().registerCommand(this, fallbackCommand);
}
connectListener = new ServerConnectListener(this);
@ -129,8 +129,6 @@ public class PlayerBalancer extends Plugin {
if (settings.getGeneralProps().isPluginMessaging()) {
getProxy().registerChannel("PlayerBalancer");
getProxy().getPluginManager().registerListener(this, fallbackService);
getProxy().getPluginManager().registerListener(this, statusManager);
pluginMessageListener = new PluginMessageListener(this);
@ -186,14 +184,11 @@ public class PlayerBalancer extends Plugin {
}
}
if (fallbackService != null) {
if (settings.getFallbackCommandProps().isEnabled()) {
getProxy().getPluginManager().unregisterCommand(fallbackService);
if (settings.getFallbackCommandProps().isEnabled()) {
if (fallbackCommand != null) {
getProxy().getPluginManager().unregisterCommand(fallbackCommand);
fallbackCommand = null;
}
getProxy().getPluginManager().unregisterListener(fallbackService);
fallbackService = null;
}
if (settings.getKickHandlerProps().isEnabled()) {
@ -262,4 +257,8 @@ public class PlayerBalancer extends Plugin {
public NetworkManager getNetworkManager() {
return networkManager;
}
public FallbackCommand getFallbackCommand() {
return fallbackCommand;
}
}

View File

@ -0,0 +1,58 @@
package com.jaimemartz.playerbalancer.commands;
import com.google.common.collect.Iterables;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.settings.props.MessagesProps;
import com.jaimemartz.playerbalancer.settings.props.shared.CommandProps;
import com.jaimemartz.playerbalancer.utils.MessageUtils;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
public abstract class AbstractMoveCommand extends Command {
private final PlayerBalancer plugin;
private final MessagesProps messages;
public AbstractMoveCommand(PlayerBalancer plugin, CommandProps commandProps) {
super(commandProps.getName(), commandProps.getPermission(), commandProps.getAliasesArray());
this.messages = plugin.getSettings().getMessagesProps();
this.plugin = plugin;
}
@Override
public void execute(CommandSender sender, String[] args) {
if (sender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) sender;
ServerSection target = getSection(player);
if (target != null) {
if (args.length == 1) {
try {
int number = Integer.parseInt(args[0]);
if (number <= 0) {
MessageUtils.send(player, messages.getInvalidInputMessage());
} else if (number > target.getServers().size()) {
MessageUtils.send(player, messages.getInvalidInputMessage());
} else {
ServerInfo server = Iterables.get(target.getServers(), number - 1);
ConnectionIntent.direct(plugin, player, server, (response, throwable) -> {});
}
} catch (NumberFormatException e) {
MessageUtils.send(player, messages.getInvalidInputMessage());
}
} else {
ConnectionIntent.simple(plugin, player, target);
}
}
} else {
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
}
}
public abstract ServerSection getSection(ProxiedPlayer player);
}

View File

@ -0,0 +1,57 @@
package com.jaimemartz.playerbalancer.commands;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.settings.props.MessagesProps;
import com.jaimemartz.playerbalancer.settings.props.features.FallbackCommandProps;
import com.jaimemartz.playerbalancer.utils.MessageUtils;
import net.md_5.bungee.api.connection.ProxiedPlayer;
public class FallbackCommand extends AbstractMoveCommand {
private final PlayerBalancer plugin;
private final MessagesProps messages;
private final FallbackCommandProps props;
public FallbackCommand(PlayerBalancer plugin) {
super(plugin, plugin.getSettings().getFallbackCommandProps().getCommand());
this.props = plugin.getSettings().getFallbackCommandProps();
this.messages = plugin.getSettings().getMessagesProps();
this.plugin = plugin;
}
@Override
public ServerSection getSection(ProxiedPlayer player) {
ServerSection current = plugin.getSectionManager().getByPlayer(player);
if (current != null) {
if (props.getExcludedSections().contains(current.getName())) {
MessageUtils.send(player, messages.getUnavailableServerMessage());
return null;
}
ServerSection target = plugin.getSectionManager().getBind(props.getRules(), current)
.orElse(current.getParent());
if (target == null) {
MessageUtils.send(player, messages.getUnavailableServerMessage());
return null;
}
if (props.isRestrictive()) {
if (current.getPosition() >= 0 && target.getPosition() < 0) {
MessageUtils.send(player, messages.getUnavailableServerMessage());
return null;
}
}
return target;
} else {
if (plugin.getSettings().getBalancerProps().isDefaultPrincipal()) {
return plugin.getSectionManager().getPrincipal();
} else {
MessageUtils.send(player, messages.getUnavailableServerMessage());
}
}
return null;
}
}

View File

@ -219,6 +219,44 @@ public class PluginMessageListener implements Listener {
}
break;
}
case "FallbackPlayer": {
if (event.getReceiver() instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
ServerSection target = plugin.getFallbackCommand().getSection(player);
if (target == null)
break;
ConnectionIntent.simple(
plugin,
player,
target
);
}
break;
}
case "FallbackOtherPlayer": {
ProxiedPlayer player = plugin.getProxy().getPlayer(in.readUTF());
if (player == null)
break;
ServerSection target = plugin.getFallbackCommand().getSection(player);
if (target == null)
break;
ConnectionIntent.simple(
plugin,
player,
target
);
break;
}
}
}
}

View File

@ -1,11 +1,10 @@
package com.jaimemartz.playerbalancer.section;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.services.FallbackService;
import com.jaimemartz.playerbalancer.commands.AbstractMoveCommand;
import net.md_5.bungee.api.connection.ProxiedPlayer;
//This is unsafe, make it extend just the command, not the listener too
public class SectionCommand extends FallbackService {
public class SectionCommand extends AbstractMoveCommand {
private final ServerSection section;
public SectionCommand(PlayerBalancer plugin, ServerSection section) {

View File

@ -1,149 +0,0 @@
package com.jaimemartz.playerbalancer.services;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.settings.props.MessagesProps;
import com.jaimemartz.playerbalancer.settings.props.features.FallbackCommandProps;
import com.jaimemartz.playerbalancer.settings.props.shared.CommandProps;
import com.jaimemartz.playerbalancer.utils.MessageUtils;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
public class FallbackService extends Command implements Listener {
protected final PlayerBalancer plugin;
protected final MessagesProps messages;
private final FallbackCommandProps props;
public FallbackService(PlayerBalancer plugin, CommandProps props) {
super(props.getName(), props.getPermission(), props.getAliasesArray());
this.props = plugin.getSettings().getFallbackCommandProps();
this.messages = plugin.getSettings().getMessagesProps();
this.plugin = plugin;
}
@Override
public void execute(CommandSender sender, String[] args) {
if (sender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) sender;
ServerSection target = this.getSection(player);
if (target != null) {
if (args.length == 1) {
try {
int number = Integer.parseInt(args[0]);
if (number <= 0) {
MessageUtils.send(player, messages.getInvalidInputMessage());
} else if (number > target.getServers().size()) {
MessageUtils.send(player, messages.getInvalidInputMessage());
} else {
ServerInfo server = Iterables.get(target.getServers(), number - 1);
ConnectionIntent.direct(plugin, player, server, (response, throwable) -> {});
}
} catch (NumberFormatException e) {
MessageUtils.send(player, messages.getInvalidInputMessage());
}
} else {
ConnectionIntent.simple(plugin, player, target);
}
}
} else {
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
}
}
public ServerSection getSection(ProxiedPlayer player) {
ServerSection current = plugin.getSectionManager().getByPlayer(player);
if (current != null) {
if (props.getExcludedSections().contains(current.getName())) {
MessageUtils.send(player, messages.getUnavailableServerMessage());
return null;
}
ServerSection target = plugin.getSectionManager().getBind(props.getRules(), current)
.orElse(current.getParent());
if (target == null) {
MessageUtils.send(player, messages.getUnavailableServerMessage());
return null;
}
if (props.isRestrictive()) {
if (current.getPosition() >= 0 && target.getPosition() < 0) {
MessageUtils.send(player, messages.getUnavailableServerMessage());
return null;
}
}
return target;
} else {
if (plugin.getSettings().getBalancerProps().isDefaultPrincipal()) {
return plugin.getSectionManager().getPrincipal();
} else {
MessageUtils.send(player, messages.getUnavailableServerMessage());
}
}
return null;
}
@EventHandler
public void onPluginMessage(PluginMessageEvent event) {
if (event.getTag().equals("PlayerBalancer") && event.getSender() instanceof Server) {
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
String request = in.readUTF();
ServerInfo sender = ((Server) event.getSender()).getInfo();
switch (request) {
case "FallbackPlayer": {
if (event.getReceiver() instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
ServerSection target = getSection(player);
if (target == null)
break;
ConnectionIntent.simple(
plugin,
player,
target
);
}
break;
}
case "FallbackOtherPlayer": {
ProxiedPlayer player = plugin.getProxy().getPlayer(in.readUTF());
if (player == null)
break;
ServerSection target = getSection(player);
if (target == null)
break;
ConnectionIntent.simple(
plugin,
player,
target
);
break;
}
}
}
}
}