Done some thingies

This commit is contained in:
Jaime Martínez Rincón 2017-10-16 18:02:24 +02:00
parent ffa628442a
commit e94b39fdf0
16 changed files with 239 additions and 50 deletions

View File

@ -1,11 +1,11 @@
package com.jaimemartz.playerbalancer;
import com.google.common.reflect.TypeToken;
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
import com.jaimemartz.playerbalancer.services.FallbackService;
import com.jaimemartz.playerbalancer.commands.MainCommand;
import com.jaimemartz.playerbalancer.commands.ManageCommand;
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;
import com.jaimemartz.playerbalancer.listener.*;
import com.jaimemartz.playerbalancer.listeners.*;
import com.jaimemartz.playerbalancer.manager.NetworkManager;
import com.jaimemartz.playerbalancer.manager.PasteHelper;
import com.jaimemartz.playerbalancer.manager.PlayerLocker;
@ -35,7 +35,8 @@ public class PlayerBalancer extends Plugin {
private SectionManager sectionManager;
private NetworkManager networkManager;
private ConfigurationLoader<CommentedConfigurationNode> loader;
private Command fallbackCommand, mainCommand, manageCommand;
private FallbackService fallbackService;
private Command mainCommand, manageCommand;
private Listener connectListener, kickListener, messageListener, reloadListener;
@Override
@ -104,11 +105,14 @@ public class PlayerBalancer extends Plugin {
statusManager.start();
}
fallbackService = new FallbackService(this, settings.getFallbackCommandProps().getCommand());
if (settings.getFallbackCommandProps().isEnabled()) {
fallbackCommand = new FallbackCommand(this, settings.getFallbackCommandProps().getCommand());
getProxy().getPluginManager().registerCommand(this, fallbackCommand);
getProxy().getPluginManager().registerCommand(this, fallbackService);
}
getProxy().getPluginManager().registerListener(this, fallbackService);
connectListener = new ServerConnectListener(this);
getProxy().getPluginManager().registerListener(this, connectListener);
@ -161,10 +165,13 @@ public class PlayerBalancer extends Plugin {
}
if (settings.getFallbackCommandProps().isEnabled()) {
getProxy().getPluginManager().unregisterCommand(fallbackCommand);
fallbackCommand = null;
getProxy().getPluginManager().unregisterCommand(fallbackService);
}
getProxy().getPluginManager().unregisterListener(fallbackService);
fallbackService = null;
if (settings.getKickHandlerProps().isEnabled()) {
getProxy().getPluginManager().unregisterListener(kickListener);
kickListener = null;

View File

@ -46,7 +46,7 @@ public class ManageCommand extends Command {
if (sender instanceof ProxiedPlayer) {
ConnectionIntent.simple(plugin, (ProxiedPlayer) sender, section);
} else {
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
sender.sendMessage(new ComponentBuilder("This command variant can only be executed by a player").color(ChatColor.RED).create());
}
}
} else {

View File

@ -72,9 +72,18 @@ public enum ProviderType {
@Override
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
AbstractProvider provider = section.getExternalProvider();
if (provider == null) {
plugin.getLogger().warning("Target requested to the EXTERNAL provider with the section not having a provider instance, falling back to RANDOM...");
return RANDOM.requestTarget(plugin, section, servers, player);
}
return provider.requestTarget(plugin, section, servers, player);
}
};
public abstract ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player);
public abstract ServerInfo requestTarget(
PlayerBalancer plugin,
ServerSection section,
List<ServerInfo> servers,
ProxiedPlayer player
);
}

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.listener;
package com.jaimemartz.playerbalancer.listeners;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;

View File

@ -1,13 +1,12 @@
package com.jaimemartz.playerbalancer.listener;
package com.jaimemartz.playerbalancer.listeners;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.*;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.json.ServerInfoAdapter;
import com.jaimemartz.playerbalancer.utils.ServerInfoAdapter;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
@ -18,6 +17,9 @@ import net.md_5.bungee.event.EventHandler;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.stream.Collectors;
public class PluginMessageListener implements Listener {
private final PlayerBalancer plugin;
@ -26,7 +28,11 @@ public class PluginMessageListener implements Listener {
public PluginMessageListener(PlayerBalancer plugin) {
this.plugin = plugin;
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(ServerInfo.class, new ServerInfoAdapter());
builder.registerTypeAdapter(ServerInfo.class, (JsonSerializer<ServerInfo>) (server, type, context) ->
context.serialize(server.getName())
);
builder.serializeNulls();
gson = builder.create();
}
@ -44,9 +50,8 @@ public class PluginMessageListener implements Listener {
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
if (section == null) {
return;
}
if (section == null)
break;
ConnectionIntent.simple(plugin, player, section);
}
@ -55,14 +60,14 @@ public class PluginMessageListener implements Listener {
case "ConnectOther": {
ProxiedPlayer player = plugin.getProxy().getPlayer(in.readUTF());
if (player == null) {
return;
}
if (player == null)
break;
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
if (section == null) {
return;
}
if (section == null)
break;
ConnectionIntent.simple(plugin, player, section);
break;
@ -73,9 +78,9 @@ public class PluginMessageListener implements Listener {
DataOutputStream out = new DataOutputStream(stream);
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
if (section == null) {
return;
}
if (section == null)
break;
try {
String output = gson.toJson(section);
@ -94,14 +99,14 @@ public class PluginMessageListener implements Listener {
DataOutputStream out = new DataOutputStream(stream);
ServerInfo server = plugin.getProxy().getServerInfo(in.readUTF());
if (server == null) {
return;
}
if (server == null)
break;
ServerSection section = plugin.getSectionManager().getByServer(server);
if (section == null) {
return;
}
if (section == null)
break;
try {
String output = gson.toJson(section);
@ -122,9 +127,9 @@ public class PluginMessageListener implements Listener {
DataOutputStream out = new DataOutputStream(stream);
ServerSection section = plugin.getSectionManager().getByPlayer(player);
if (section == null) {
return;
}
if (section == null)
break;
try {
String output = gson.toJson(section);
@ -138,6 +143,27 @@ public class PluginMessageListener implements Listener {
}
break;
}
case "GetSectionPlayerCount": {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(stream);
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
if (section == null)
break;
try {
out.writeUTF("GetSectionPlayerCount");
out.writeInt(section.getServers().stream()
.mapToInt(a -> a.getPlayers().size()).sum());
} catch (IOException e) {
e.printStackTrace();
}
sender.sendData("PlayerBalancer", stream.toByteArray());
break;
}
}
}
}

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.listener;
package com.jaimemartz.playerbalancer.listeners;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import net.md_5.bungee.api.event.ProxyReloadEvent;

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.listener;
package com.jaimemartz.playerbalancer.listeners;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.listener;
package com.jaimemartz.playerbalancer.listeners;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;

View File

@ -1,10 +1,10 @@
package com.jaimemartz.playerbalancer.section;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
import com.jaimemartz.playerbalancer.services.FallbackService;
import net.md_5.bungee.api.connection.ProxiedPlayer;
public class SectionCommand extends FallbackCommand {
public class SectionCommand extends FallbackService {
private final ServerSection section;
public SectionCommand(PlayerBalancer plugin, ServerSection section) {

View File

@ -1,6 +1,8 @@
package com.jaimemartz.playerbalancer.commands;
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;
@ -13,14 +15,18 @@ 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 FallbackCommand extends Command {
public class FallbackService extends Command implements Listener {
protected final PlayerBalancer plugin;
protected final MessagesProps messages;
private final FallbackCommandProps props;
public FallbackCommand(PlayerBalancer plugin, CommandProps props) {
public FallbackService(PlayerBalancer plugin, CommandProps props) {
super(props.getName(), props.getPermission(), props.getAliasesArray());
this.props = plugin.getSettings().getFallbackCommandProps();
this.messages = plugin.getSettings().getMessagesProps();
@ -91,4 +97,53 @@ public class FallbackCommand extends Command {
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;
}
}
}
}
}

View File

@ -1,4 +1,4 @@
package com.jaimemartz.playerbalancer.json;
package com.jaimemartz.playerbalancer.utils;
import com.google.gson.*;
import net.md_5.bungee.api.config.ServerInfo;

View File

@ -53,6 +53,7 @@ features {
# PROGRESSIVE: Returns the first server found that is not full
# FILLER: Returns the server with the most players online that is not full
# EXTERNAL: Returns the server determined by a provider created by other plugin
sections {
auth-lobbies {
provider=RANDOM

View File

@ -23,18 +23,18 @@ public class MainCommand implements CommandExecutor {
case "connect": {
if (args.length >= 2) {
String input = args[1];
if (args.length == 3) {
if (args.length >= 3) {
Player player = plugin.getServer().getPlayer(args[2]);
if (player != null) {
plugin.getManager().connectPlayer(player, input);
} else {
sender.spigot().sendMessage(new ComponentBuilder("There is no player with that name connected to this proxy").color(ChatColor.RED).create());
sender.spigot().sendMessage(new ComponentBuilder("There is no player with that name connected to this server").color(ChatColor.RED).create());
}
} else {
if (sender instanceof Player) {
plugin.getManager().connectPlayer((Player) sender, input);
} else {
sender.spigot().sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
sender.spigot().sendMessage(new ComponentBuilder("This command variant can only be executed by a player").color(ChatColor.RED).create());
}
}
} else {
@ -43,6 +43,25 @@ public class MainCommand implements CommandExecutor {
break;
}
case "fallback": {
if (args.length >= 2) {
Player player = plugin.getServer().getPlayer(args[1]);
if (player != null) {
plugin.getManager().fallbackPlayer((Player) sender);
} else {
sender.spigot().sendMessage(new ComponentBuilder("There is no player with that name connected to this server").color(ChatColor.RED).create());
}
} else {
if (sender instanceof Player) {
plugin.getManager().fallbackPlayer((Player) sender);
} else {
sender.spigot().sendMessage(new ComponentBuilder("This command variant can only be executed by a player").color(ChatColor.RED).create());
}
}
break;
}
/*
case "info": {
plugin.getManager().getSectionOfPlayer((Player) sender, (a) -> {
System.out.println(a);
@ -50,12 +69,13 @@ public class MainCommand implements CommandExecutor {
});
break;
}
*/
}
} else {
sender.spigot().sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
sender.spigot().sendMessage(new ComponentBuilder("Available commands:").color(ChatColor.GRAY).create());
sender.spigot().sendMessage(new ComponentBuilder("/spb connect <section> [player]").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Connects you or the specified player to that section").color(ChatColor.RED).create());
sender.spigot().sendMessage(new ComponentBuilder("/spb fallback").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Connects you to the parent section").color(ChatColor.RED).create());
sender.spigot().sendMessage(new ComponentBuilder("/spb fallback [player]").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Connects you or the specified player to the parent of the current section").color(ChatColor.RED).create());
sender.spigot().sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
}
}

View File

@ -0,0 +1,39 @@
package com.jaimemartz.playerbalanceraddon;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.external.EZPlaceholderHook;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class PlaceholderHandler extends EZPlaceholderHook {
private final Map<String, Integer> sectionPlayerCounts = new LinkedHashMap<>();
private final PlayerBalancerAddon plugin;
public PlaceholderHandler(PlayerBalancerAddon plugin) {
super(plugin, "balancer");
this.plugin = plugin;
}
@Override
public String onPlaceholderRequest(Player player, String identifier) {
if (identifier.startsWith("playercount_")) {
String section = identifier.split("playercount_")[1];
if (section == null)
return null;
//For the first call this placeholder will return 0
//For the next one, the result of the previous one
plugin.getManager().getSectionPlayerCount(section, (count) -> {
sectionPlayerCounts.put(section, count);
});
return String.valueOf(sectionPlayerCounts.get(section));
}
return null;
}
}

View File

@ -4,11 +4,17 @@ import org.bukkit.plugin.java.JavaPlugin;
public class PlayerBalancerAddon extends JavaPlugin {
private PluginMessageManager manager;
private PlaceholderHandler handler;
@Override
public void onEnable() {
manager = new PluginMessageManager(this);
getCommand("spb").setExecutor(new MainCommand(this));
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
handler = new PlaceholderHandler(this);
handler.hook();
}
}
@Override

View File

@ -67,7 +67,6 @@ public class PluginMessageManager implements PluginMessageListener {
), (response) -> consumer.accept(response.readUTF()));
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
return true;
}
@ -91,6 +90,26 @@ public class PluginMessageManager implements PluginMessageListener {
return true;
}
public boolean getSectionPlayerCount(String section, Consumer<Integer> consumer) {
Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null);
if (player == null) {
return false;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionPlayerCount");
out.writeUTF(section);
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionPlayerCount",
player.getUniqueId()
), (response) -> consumer.accept(response.readInt()));
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
return true;
}
public void getSectionOfPlayer(Player player, Consumer<String> consumer) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionOfPlayer");
@ -105,6 +124,13 @@ public class PluginMessageManager implements PluginMessageListener {
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
}
public void fallbackPlayer(Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("FallbackPlayer");
out.writeUTF(player.getName());
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
}
private final class MessageContext {
private final String channel;
private final String subchannel;