Skip to content

Commit d4cea35

Browse files
committed
Plugin message kinda works, next up: creating more subchannels on the bungee side, creating the command and creating the placeholders
1 parent 3b11db7 commit d4cea35

File tree

9 files changed

+104
-31
lines changed

9 files changed

+104
-31
lines changed

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MainCommand extends Command {
1212
private final PlayerBalancer plugin;
1313

1414
public MainCommand(PlayerBalancer plugin) {
15-
super("balancer");
15+
super("balancer", "", "playerbalancer", "pb");
1616
this.plugin = plugin;
1717
}
1818

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jaimemartz.playerbalancer.json;
2+
3+
import com.google.gson.*;
4+
import net.md_5.bungee.api.config.ServerInfo;
5+
6+
import java.lang.reflect.Type;
7+
8+
public class ServerInfoAdapter implements JsonSerializer<ServerInfo> {
9+
@Override
10+
public JsonElement serialize(ServerInfo server, Type type, JsonSerializationContext context) {
11+
return context.serialize(server.getName());
12+
}
13+
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listener/PluginMessageListener.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.jaimemartz.playerbalancer.PlayerBalancer;
88
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
99
import com.jaimemartz.playerbalancer.section.ServerSection;
10+
import com.jaimemartz.playerbalancer.json.ServerInfoAdapter;
1011
import net.md_5.bungee.api.config.ServerInfo;
1112
import net.md_5.bungee.api.connection.ProxiedPlayer;
1213
import net.md_5.bungee.api.connection.Server;
@@ -24,10 +25,9 @@ public class PluginMessageListener implements Listener {
2425

2526
public PluginMessageListener(PlayerBalancer plugin) {
2627
this.plugin = plugin;
27-
2828
GsonBuilder builder = new GsonBuilder();
29+
builder.registerTypeAdapter(ServerInfo.class, new ServerInfoAdapter());
2930
builder.serializeNulls();
30-
builder.excludeFieldsWithoutExposeAnnotation();
3131
gson = builder.create();
3232
}
3333

@@ -37,6 +37,7 @@ public void onPluginMessage(PluginMessageEvent event) {
3737
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
3838
String request = in.readUTF();
3939
ServerInfo sender = ((Server) event.getSender()).getInfo();
40+
4041
switch (request) {
4142
case "Connect": {
4243
if (event.getReceiver() instanceof ProxiedPlayer) {
@@ -78,6 +79,7 @@ public void onPluginMessage(PluginMessageEvent event) {
7879

7980
try {
8081
String output = gson.toJson(section);
82+
out.writeUTF("GetSectionByName");
8183
out.writeUTF(output);
8284
} catch (IOException e) {
8385
e.printStackTrace();
@@ -103,6 +105,7 @@ public void onPluginMessage(PluginMessageEvent event) {
103105

104106
try {
105107
String output = gson.toJson(section);
108+
out.writeUTF("GetSectionByServer");
106109
out.writeUTF(output);
107110
} catch (IOException e) {
108111
e.printStackTrace();
@@ -125,6 +128,7 @@ public void onPluginMessage(PluginMessageEvent event) {
125128

126129
try {
127130
String output = gson.toJson(section);
131+
out.writeUTF("GetSectionOfPlayer");
128132
out.writeUTF(output);
129133
} catch (IOException e) {
130134
e.printStackTrace();

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/section/ServerSection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public class ServerSection {
1919
private ServerSection parent;
2020
private int position;
2121

22-
private ServerInfo server;
23-
private SectionCommand command;
24-
private NavigableSet<ServerInfo> servers;
25-
private AbstractProvider externalProvider;
22+
private transient ServerInfo server;
23+
private transient SectionCommand command;
24+
private transient AbstractProvider externalProvider;
25+
private Set<ServerInfo> servers;
2626

2727
private boolean valid = false;
2828

@@ -109,7 +109,7 @@ public void setCommand(SectionCommand command) {
109109
this.command = command;
110110
}
111111

112-
public NavigableSet<ServerInfo> getServers() {
112+
public Set<ServerInfo> getServers() {
113113
return servers;
114114
}
115115

Spigot Addon/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<plugin>
1919
<groupId>org.apache.maven.plugins</groupId>
2020
<artifactId>maven-jar-plugin</artifactId>
21-
<version>2.3.1</version>
21+
<version>3.0.2</version>
2222
<configuration>
2323
<outputDirectory>../target</outputDirectory>
2424
</configuration>
@@ -32,7 +32,7 @@
3232
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
3333
</repository>
3434
<repository>
35-
<id>placeholderapi</id>
35+
<id>papi-repo</id>
3636
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
3737
</repository>
3838
</repositories>

Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/MainCommand.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bukkit.command.Command;
77
import org.bukkit.command.CommandExecutor;
88
import org.bukkit.command.CommandSender;
9+
import org.bukkit.entity.Player;
910

1011
public class MainCommand implements CommandExecutor {
1112
private final PlayerBalancerAddon plugin;
@@ -16,12 +17,48 @@ public MainCommand(PlayerBalancerAddon plugin) {
1617

1718
@Override
1819
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
19-
if (cmd.getName().equalsIgnoreCase("balanceraddon"))
20+
if (cmd.getName().equalsIgnoreCase("spb")) {
21+
if (args.length != 0) {
22+
switch (args[0].toLowerCase()) {
23+
case "connect": {
24+
if (args.length >= 2) {
25+
String input = args[1];
26+
if (args.length == 3) {
27+
Player player = plugin.getServer().getPlayer(args[2]);
28+
if (player != null) {
29+
plugin.getManager().connectPlayer(player, input);
30+
} else {
31+
sender.spigot().sendMessage(new ComponentBuilder("There is no player with that name connected to this proxy").color(ChatColor.RED).create());
32+
}
33+
} else {
34+
if (sender instanceof Player) {
35+
plugin.getManager().connectPlayer((Player) sender, input);
36+
} else {
37+
sender.spigot().sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
38+
}
39+
}
40+
} else {
41+
sender.spigot().sendMessage(new ComponentBuilder("Usage: /section connect <section> [player]").color(ChatColor.RED).create());
42+
}
43+
break;
44+
}
2045

21-
sender.spigot().sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
22-
sender.spigot().sendMessage(new ComponentBuilder("Available commands:").color(ChatColor.GRAY).create());
23-
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());
24-
sender.spigot().sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
46+
case "info": {
47+
plugin.getManager().getSectionOfPlayer((Player) sender, (a) -> {
48+
System.out.println(a);
49+
sender.sendMessage(a);
50+
});
51+
break;
52+
}
53+
}
54+
} else {
55+
sender.spigot().sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
56+
sender.spigot().sendMessage(new ComponentBuilder("Available commands:").color(ChatColor.GRAY).create());
57+
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());
58+
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());
59+
sender.spigot().sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
60+
}
61+
}
2562
return false;
2663
}
2764
}

Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/PlayerBalancerAddon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class PlayerBalancerAddon extends JavaPlugin {
88
@Override
99
public void onEnable() {
1010
manager = new PluginMessageManager(this);
11-
getCommand("balanceraddon").setExecutor(new MainCommand(this));
11+
getCommand("spb").setExecutor(new MainCommand(this));
1212
}
1313

1414
@Override

Spigot Addon/src/main/java/com/jaimemartz/playerbalanceraddon/PluginMessageManager.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package com.jaimemartz.playerbalanceraddon;
22

3-
import com.google.common.collect.ArrayListMultimap;
3+
import com.google.common.collect.HashMultimap;
44
import com.google.common.collect.Iterables;
55
import com.google.common.collect.LinkedListMultimap;
66
import com.google.common.collect.Multimap;
77
import com.google.common.io.ByteArrayDataInput;
88
import com.google.common.io.ByteArrayDataOutput;
99
import com.google.common.io.ByteStreams;
10-
import org.bukkit.Bukkit;
1110
import org.bukkit.entity.Player;
12-
import org.bukkit.plugin.messaging.Messenger;
1311
import org.bukkit.plugin.messaging.PluginMessageListener;
1412

15-
import java.io.ByteArrayOutputStream;
16-
import java.io.DataInputStream;
1713
import java.util.*;
14+
import java.util.concurrent.Future;
1815
import java.util.function.Consumer;
1916

2017
public class PluginMessageManager implements PluginMessageListener {
21-
private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = LinkedListMultimap.create();
18+
private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = HashMultimap.create();
2219
private final PlayerBalancerAddon plugin;
2320

2421
public PluginMessageManager(PlayerBalancerAddon plugin) {
@@ -38,8 +35,14 @@ public void onPluginMessageReceived(String channel, Player player, byte[] messag
3835
ByteArrayDataInput in = ByteStreams.newDataInput(message);
3936
String subchannel = in.readUTF();
4037

41-
contexts.get(new MessageContext(channel, subchannel, player.getUniqueId()))
42-
.stream().findFirst().ifPresent(a -> a.accept(in));
38+
Iterator<Consumer<ByteArrayDataInput>> iterator = contexts.get(
39+
new MessageContext(channel, subchannel, player.getUniqueId())
40+
).iterator();
41+
42+
if (iterator.hasNext()) {
43+
iterator.next().accept(in);
44+
iterator.remove();
45+
}
4346
}
4447
}
4548

@@ -59,13 +62,14 @@ public boolean getSectionByName(String section, Consumer<String> consumer) {
5962
ByteArrayDataOutput out = ByteStreams.newDataOutput();
6063
out.writeUTF("GetSectionByName");
6164
out.writeUTF(section);
62-
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
6365

6466
contexts.put(new MessageContext(
6567
"PlayerBalancer",
6668
"GetSectionByName",
6769
player.getUniqueId()
68-
), ByteArrayDataInput::readUTF);
70+
), (response) -> consumer.accept(response.readUTF()));
71+
72+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
6973

7074
return true;
7175
}
@@ -79,27 +83,29 @@ public boolean getSectionByServer(String server, Consumer<String> consumer) {
7983
ByteArrayDataOutput out = ByteStreams.newDataOutput();
8084
out.writeUTF("GetSectionByServer");
8185
out.writeUTF(server);
82-
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
8386

8487
contexts.put(new MessageContext(
8588
"PlayerBalancer",
8689
"GetSectionByServer",
8790
player.getUniqueId()
88-
), ByteArrayDataInput::readUTF);
91+
), (response) -> consumer.accept(response.readUTF()));
92+
93+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
8994
return true;
9095
}
9196

9297
public void getSectionOfPlayer(Player player, Consumer<String> consumer) {
9398
ByteArrayDataOutput out = ByteStreams.newDataOutput();
9499
out.writeUTF("GetSectionOfPlayer");
95100
out.writeUTF(player.getName());
96-
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
97101

98102
contexts.put(new MessageContext(
99103
"PlayerBalancer",
100104
"GetSectionOfPlayer",
101105
player.getUniqueId()
102-
), ByteArrayDataInput::readUTF);
106+
), (response) -> consumer.accept(response.readUTF()));
107+
108+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
103109
}
104110

105111
private final class MessageContext {
@@ -132,5 +138,14 @@ public int hashCode() {
132138
result = 31 * result + (player != null ? player.hashCode() : 0);
133139
return result;
134140
}
141+
142+
@Override
143+
public String toString() {
144+
return "MessageContext{" +
145+
"channel='" + channel + '\'' +
146+
", subchannel='" + subchannel + '\'' +
147+
", player=" + player +
148+
'}';
149+
}
135150
}
136151
}

Spigot Addon/src/main/resources/plugin.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ name: PlayerBalancerAddon
22
main: com.jaimemartz.playerbalanceraddon.PlayerBalancerAddon
33
version: ${project.version}
44
author: jaime29010
5+
softdepend: [PlaceholderAPI]
56

6-
softdepend: [PlaceholderAPI]
7+
commands:
8+
spb:
9+
description: Command to interact with the bungee side of PlayerBalancer
10+
permission: playerbalancer.admin

0 commit comments

Comments
 (0)