Skip to content

Commit df083f5

Browse files
committed
Plugin message helper for the spigot plugin
1 parent 16b05eb commit df083f5

File tree

7 files changed

+220
-6
lines changed

7 files changed

+220
-6
lines changed

.circleci/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ jobs:
4444
path: target/surefire-reports
4545

4646
- store_artifacts:
47-
path: target/PlayerBalancer.jar
47+
paths:
48+
- target/PlayerBalancer.jar
49+
- target/PlayerBalancerAddon.jar

Main Plugin/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
</execution>
3737
</executions>
3838
</plugin>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-jar-plugin</artifactId>
42+
<version>2.3.1</version>
43+
<configuration>
44+
<outputDirectory>../target</outputDirectory>
45+
</configuration>
46+
</plugin>
3947
</plugins>
4048
</build>
4149

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ public void onPluginMessage(PluginMessageEvent event) {
111111
sender.sendData("PlayerBalancer", stream.toByteArray());
112112
break;
113113
}
114+
115+
case "GetSectionOfPlayer": {
116+
if (event.getReceiver() instanceof ProxiedPlayer) {
117+
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
118+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
119+
DataOutputStream out = new DataOutputStream(stream);
120+
121+
ServerSection section = plugin.getSectionManager().getByPlayer(player);
122+
if (section == null) {
123+
return;
124+
}
125+
126+
try {
127+
String output = gson.toJson(section);
128+
out.writeUTF(output);
129+
} catch (IOException e) {
130+
e.printStackTrace();
131+
}
132+
133+
sender.sendData("PlayerBalancer", stream.toByteArray());
134+
}
135+
break;
136+
}
114137
}
115138
}
116139
}

Spigot Addon/pom.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,43 @@
1212
<name>PlayerBalancer Addon</name>
1313
<artifactId>playerbalancer-addon</artifactId>
1414

15+
<build>
16+
<finalName>PlayerBalancerAddon</finalName>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-jar-plugin</artifactId>
21+
<version>2.3.1</version>
22+
<configuration>
23+
<outputDirectory>../target</outputDirectory>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
1529
<repositories>
1630
<repository>
1731
<id>spigot-repo</id>
1832
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
1933
</repository>
34+
<repository>
35+
<id>placeholderapi</id>
36+
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
37+
</repository>
2038
</repositories>
2139

22-
<build>
23-
<finalName>PlayerBalancerAddon</finalName>
24-
</build>
25-
2640
<dependencies>
2741
<dependency>
2842
<groupId>org.spigotmc</groupId>
2943
<artifactId>spigot-api</artifactId>
3044
<version>1.12.2-R0.1-SNAPSHOT</version>
3145
<scope>provided</scope>
3246
</dependency>
47+
<dependency>
48+
<groupId>me.clip</groupId>
49+
<artifactId>placeholderapi</artifactId>
50+
<version>2.8.2</version>
51+
<scope>provided</scope>
52+
</dependency>
3353
</dependencies>
3454
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.jaimemartz.playerbalanceraddon;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
7+
public class MainCommand implements CommandExecutor {
8+
private final PlayerBalancerAddon plugin;
9+
10+
public MainCommand(PlayerBalancerAddon plugin) {
11+
this.plugin = plugin;
12+
}
13+
14+
@Override
15+
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
16+
return false;
17+
}
18+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
import org.bukkit.plugin.java.JavaPlugin;
44

55
public class PlayerBalancerAddon extends JavaPlugin {
6+
private PluginMessageManager manager;
7+
68
@Override
79
public void onDisable() {
8-
10+
manager = new PluginMessageManager(this);
11+
getCommand("balancer").setExecutor(new MainCommand(this));
912
}
1013

1114
@Override
1215
public void onEnable() {
1316

1417
}
18+
19+
public PluginMessageManager getManager() {
20+
return manager;
21+
}
1522
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.jaimemartz.playerbalanceraddon;
2+
3+
import com.google.common.collect.ArrayListMultimap;
4+
import com.google.common.collect.Iterables;
5+
import com.google.common.collect.LinkedListMultimap;
6+
import com.google.common.collect.Multimap;
7+
import com.google.common.io.ByteArrayDataInput;
8+
import com.google.common.io.ByteArrayDataOutput;
9+
import com.google.common.io.ByteStreams;
10+
import org.bukkit.Bukkit;
11+
import org.bukkit.entity.Player;
12+
import org.bukkit.plugin.messaging.Messenger;
13+
import org.bukkit.plugin.messaging.PluginMessageListener;
14+
15+
import java.io.ByteArrayOutputStream;
16+
import java.io.DataInputStream;
17+
import java.util.*;
18+
import java.util.function.Consumer;
19+
20+
public class PluginMessageManager implements PluginMessageListener {
21+
private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = LinkedListMultimap.create();
22+
private final PlayerBalancerAddon plugin;
23+
24+
public PluginMessageManager(PlayerBalancerAddon plugin) {
25+
this.plugin = plugin;
26+
27+
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "PlayerBalancer", this);
28+
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "PlayerBalancer");
29+
30+
//In case we need to use BungeeCord channels
31+
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this);
32+
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
33+
}
34+
35+
@Override
36+
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
37+
if (channel.equals("PlayerBalancer")) {
38+
ByteArrayDataInput in = ByteStreams.newDataInput(message);
39+
String subchannel = in.readUTF();
40+
41+
contexts.get(new MessageContext(channel, subchannel, player))
42+
.stream().findFirst().ifPresent(a -> a.accept(in));
43+
}
44+
}
45+
46+
public void connectPlayer(Player player, String section) {
47+
ByteArrayDataOutput out = ByteStreams.newDataOutput();
48+
out.writeUTF("Connect");
49+
out.writeUTF(section);
50+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
51+
}
52+
53+
public boolean getSectionByName(String section, Consumer<String> consumer) {
54+
Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null);
55+
if (player == null) {
56+
return false;
57+
}
58+
59+
ByteArrayDataOutput out = ByteStreams.newDataOutput();
60+
out.writeUTF("GetSectionByName");
61+
out.writeUTF(section);
62+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
63+
64+
contexts.put(new MessageContext(
65+
"PlayerBalancer",
66+
"GetSectionByName",
67+
player
68+
), ByteArrayDataInput::readUTF);
69+
70+
return true;
71+
}
72+
73+
public boolean getSectionByServer(String server, Consumer<String> consumer) {
74+
Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null);
75+
if (player == null) {
76+
return false;
77+
}
78+
79+
ByteArrayDataOutput out = ByteStreams.newDataOutput();
80+
out.writeUTF("GetSectionByServer");
81+
out.writeUTF(server);
82+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
83+
84+
contexts.put(new MessageContext(
85+
"PlayerBalancer",
86+
"GetSectionByServer",
87+
player
88+
), ByteArrayDataInput::readUTF);
89+
return true;
90+
}
91+
92+
public void getSectionOfPlayer(Player player, Consumer<String> consumer) {
93+
ByteArrayDataOutput out = ByteStreams.newDataOutput();
94+
out.writeUTF("GetSectionOfPlayer");
95+
out.writeUTF(player.getName());
96+
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
97+
98+
contexts.put(new MessageContext(
99+
"PlayerBalancer",
100+
"GetSectionOfPlayer",
101+
player
102+
), ByteArrayDataInput::readUTF);
103+
}
104+
105+
private final class MessageContext {
106+
private final String channel;
107+
private final String subchannel;
108+
private final Player player;
109+
110+
public MessageContext(String channel, String subchannel, Player player) {
111+
this.channel = channel;
112+
this.subchannel = subchannel;
113+
this.player = player;
114+
}
115+
116+
@Override
117+
public boolean equals(Object o) {
118+
if (this == o) return true;
119+
if (o == null || getClass() != o.getClass()) return false;
120+
121+
MessageContext that = (MessageContext) o;
122+
123+
if (channel != null ? !channel.equals(that.channel) : that.channel != null) return false;
124+
if (subchannel != null ? !subchannel.equals(that.subchannel) : that.subchannel != null) return false;
125+
return player != null ? player.equals(that.player) : that.player == null;
126+
}
127+
128+
@Override
129+
public int hashCode() {
130+
int result = channel != null ? channel.hashCode() : 0;
131+
result = 31 * result + (subchannel != null ? subchannel.hashCode() : 0);
132+
result = 31 * result + (player != null ? player.hashCode() : 0);
133+
return result;
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)