Plugin message kinda works, next up: creating more subchannels on the bungee side, creating the command and creating the placeholders

This commit is contained in:
Jaime Martínez Rincón 2017-10-10 22:56:17 +02:00
parent 3b11db7b2a
commit d4cea35eb0
9 changed files with 104 additions and 31 deletions

View File

@ -12,7 +12,7 @@ public class MainCommand extends Command {
private final PlayerBalancer plugin;
public MainCommand(PlayerBalancer plugin) {
super("balancer");
super("balancer", "", "playerbalancer", "pb");
this.plugin = plugin;
}

View File

@ -0,0 +1,13 @@
package com.jaimemartz.playerbalancer.json;
import com.google.gson.*;
import net.md_5.bungee.api.config.ServerInfo;
import java.lang.reflect.Type;
public class ServerInfoAdapter implements JsonSerializer<ServerInfo> {
@Override
public JsonElement serialize(ServerInfo server, Type type, JsonSerializationContext context) {
return context.serialize(server.getName());
}
}

View File

@ -7,6 +7,7 @@ import com.google.gson.GsonBuilder;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.json.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;
@ -24,10 +25,9 @@ public class PluginMessageListener implements Listener {
public PluginMessageListener(PlayerBalancer plugin) {
this.plugin = plugin;
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(ServerInfo.class, new ServerInfoAdapter());
builder.serializeNulls();
builder.excludeFieldsWithoutExposeAnnotation();
gson = builder.create();
}
@ -37,6 +37,7 @@ public class PluginMessageListener implements Listener {
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
String request = in.readUTF();
ServerInfo sender = ((Server) event.getSender()).getInfo();
switch (request) {
case "Connect": {
if (event.getReceiver() instanceof ProxiedPlayer) {
@ -78,6 +79,7 @@ public class PluginMessageListener implements Listener {
try {
String output = gson.toJson(section);
out.writeUTF("GetSectionByName");
out.writeUTF(output);
} catch (IOException e) {
e.printStackTrace();
@ -103,6 +105,7 @@ public class PluginMessageListener implements Listener {
try {
String output = gson.toJson(section);
out.writeUTF("GetSectionByServer");
out.writeUTF(output);
} catch (IOException e) {
e.printStackTrace();
@ -125,6 +128,7 @@ public class PluginMessageListener implements Listener {
try {
String output = gson.toJson(section);
out.writeUTF("GetSectionOfPlayer");
out.writeUTF(output);
} catch (IOException e) {
e.printStackTrace();

View File

@ -19,10 +19,10 @@ public class ServerSection {
private ServerSection parent;
private int position;
private ServerInfo server;
private SectionCommand command;
private NavigableSet<ServerInfo> servers;
private AbstractProvider externalProvider;
private transient ServerInfo server;
private transient SectionCommand command;
private transient AbstractProvider externalProvider;
private Set<ServerInfo> servers;
private boolean valid = false;
@ -109,7 +109,7 @@ public class ServerSection {
this.command = command;
}
public NavigableSet<ServerInfo> getServers() {
public Set<ServerInfo> getServers() {
return servers;
}

View File

@ -18,7 +18,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<version>3.0.2</version>
<configuration>
<outputDirectory>../target</outputDirectory>
</configuration>
@ -32,7 +32,7 @@
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>placeholderapi</id>
<id>papi-repo</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>

View File

@ -6,6 +6,7 @@ import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class MainCommand implements CommandExecutor {
private final PlayerBalancerAddon plugin;
@ -16,12 +17,48 @@ public class MainCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("balanceraddon"))
if (cmd.getName().equalsIgnoreCase("spb")) {
if (args.length != 0) {
switch (args[0].toLowerCase()) {
case "connect": {
if (args.length >= 2) {
String input = args[1];
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());
}
} 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());
}
}
} else {
sender.spigot().sendMessage(new ComponentBuilder("Usage: /section connect <section> [player]").color(ChatColor.RED).create());
}
break;
}
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("/section 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(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
case "info": {
plugin.getManager().getSectionOfPlayer((Player) sender, (a) -> {
System.out.println(a);
sender.sendMessage(a);
});
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(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
}
}
return false;
}
}

View File

@ -8,7 +8,7 @@ public class PlayerBalancerAddon extends JavaPlugin {
@Override
public void onEnable() {
manager = new PluginMessageManager(this);
getCommand("balanceraddon").setExecutor(new MainCommand(this));
getCommand("spb").setExecutor(new MainCommand(this));
}
@Override

View File

@ -1,24 +1,21 @@
package com.jaimemartz.playerbalanceraddon;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.util.*;
import java.util.concurrent.Future;
import java.util.function.Consumer;
public class PluginMessageManager implements PluginMessageListener {
private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = LinkedListMultimap.create();
private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = HashMultimap.create();
private final PlayerBalancerAddon plugin;
public PluginMessageManager(PlayerBalancerAddon plugin) {
@ -38,8 +35,14 @@ public class PluginMessageManager implements PluginMessageListener {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
contexts.get(new MessageContext(channel, subchannel, player.getUniqueId()))
.stream().findFirst().ifPresent(a -> a.accept(in));
Iterator<Consumer<ByteArrayDataInput>> iterator = contexts.get(
new MessageContext(channel, subchannel, player.getUniqueId())
).iterator();
if (iterator.hasNext()) {
iterator.next().accept(in);
iterator.remove();
}
}
}
@ -59,13 +62,14 @@ public class PluginMessageManager implements PluginMessageListener {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionByName");
out.writeUTF(section);
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionByName",
player.getUniqueId()
), ByteArrayDataInput::readUTF);
), (response) -> consumer.accept(response.readUTF()));
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
return true;
}
@ -79,13 +83,14 @@ public class PluginMessageManager implements PluginMessageListener {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionByServer");
out.writeUTF(server);
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionByServer",
player.getUniqueId()
), ByteArrayDataInput::readUTF);
), (response) -> consumer.accept(response.readUTF()));
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
return true;
}
@ -93,13 +98,14 @@ public class PluginMessageManager implements PluginMessageListener {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionOfPlayer");
out.writeUTF(player.getName());
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionOfPlayer",
player.getUniqueId()
), ByteArrayDataInput::readUTF);
), (response) -> consumer.accept(response.readUTF()));
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
}
private final class MessageContext {
@ -132,5 +138,14 @@ public class PluginMessageManager implements PluginMessageListener {
result = 31 * result + (player != null ? player.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "MessageContext{" +
"channel='" + channel + '\'' +
", subchannel='" + subchannel + '\'' +
", player=" + player +
'}';
}
}
}

View File

@ -2,5 +2,9 @@ name: PlayerBalancerAddon
main: com.jaimemartz.playerbalanceraddon.PlayerBalancerAddon
version: ${project.version}
author: jaime29010
softdepend: [PlaceholderAPI]
commands:
spb:
description: Command to interact with the bungee side of PlayerBalancer
permission: playerbalancer.admin