Plugin message helper for the spigot plugin

This commit is contained in:
Jaime Martínez Rincón 2017-10-07 20:38:02 +02:00
parent 16b05eb9d3
commit df083f56f6
7 changed files with 220 additions and 6 deletions

View File

@ -44,4 +44,6 @@ jobs:
path: target/surefire-reports path: target/surefire-reports
- store_artifacts: - store_artifacts:
path: target/PlayerBalancer.jar paths:
- target/PlayerBalancer.jar
- target/PlayerBalancerAddon.jar

View File

@ -36,6 +36,14 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>../target</outputDirectory>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@ -111,6 +111,29 @@ public class PluginMessageListener implements Listener {
sender.sendData("PlayerBalancer", stream.toByteArray()); sender.sendData("PlayerBalancer", stream.toByteArray());
break; break;
} }
case "GetSectionOfPlayer": {
if (event.getReceiver() instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(stream);
ServerSection section = plugin.getSectionManager().getByPlayer(player);
if (section == null) {
return;
}
try {
String output = gson.toJson(section);
out.writeUTF(output);
} catch (IOException e) {
e.printStackTrace();
}
sender.sendData("PlayerBalancer", stream.toByteArray());
}
break;
}
} }
} }
} }

View File

@ -12,17 +12,31 @@
<name>PlayerBalancer Addon</name> <name>PlayerBalancer Addon</name>
<artifactId>playerbalancer-addon</artifactId> <artifactId>playerbalancer-addon</artifactId>
<build>
<finalName>PlayerBalancerAddon</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>../target</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository>
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories> </repositories>
<build>
<finalName>PlayerBalancerAddon</finalName>
</build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
@ -30,5 +44,11 @@
<version>1.12.2-R0.1-SNAPSHOT</version> <version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.8.2</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,18 @@
package com.jaimemartz.playerbalanceraddon;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class MainCommand implements CommandExecutor {
private final PlayerBalancerAddon plugin;
public MainCommand(PlayerBalancerAddon plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
return false;
}
}

View File

@ -3,13 +3,20 @@ package com.jaimemartz.playerbalanceraddon;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class PlayerBalancerAddon extends JavaPlugin { public class PlayerBalancerAddon extends JavaPlugin {
private PluginMessageManager manager;
@Override @Override
public void onDisable() { public void onDisable() {
manager = new PluginMessageManager(this);
getCommand("balancer").setExecutor(new MainCommand(this));
} }
@Override @Override
public void onEnable() { public void onEnable() {
} }
public PluginMessageManager getManager() {
return manager;
}
} }

View File

@ -0,0 +1,136 @@
package com.jaimemartz.playerbalanceraddon;
import com.google.common.collect.ArrayListMultimap;
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.function.Consumer;
public class PluginMessageManager implements PluginMessageListener {
private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = LinkedListMultimap.create();
private final PlayerBalancerAddon plugin;
public PluginMessageManager(PlayerBalancerAddon plugin) {
this.plugin = plugin;
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "PlayerBalancer", this);
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "PlayerBalancer");
//In case we need to use BungeeCord channels
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this);
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (channel.equals("PlayerBalancer")) {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
contexts.get(new MessageContext(channel, subchannel, player))
.stream().findFirst().ifPresent(a -> a.accept(in));
}
}
public void connectPlayer(Player player, String section) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(section);
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
}
public boolean getSectionByName(String section, Consumer<String> consumer) {
Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null);
if (player == null) {
return false;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionByName");
out.writeUTF(section);
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionByName",
player
), ByteArrayDataInput::readUTF);
return true;
}
public boolean getSectionByServer(String server, Consumer<String> consumer) {
Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null);
if (player == null) {
return false;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionByServer");
out.writeUTF(server);
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionByServer",
player
), ByteArrayDataInput::readUTF);
return true;
}
public void getSectionOfPlayer(Player player, Consumer<String> consumer) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetSectionOfPlayer");
out.writeUTF(player.getName());
player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray());
contexts.put(new MessageContext(
"PlayerBalancer",
"GetSectionOfPlayer",
player
), ByteArrayDataInput::readUTF);
}
private final class MessageContext {
private final String channel;
private final String subchannel;
private final Player player;
public MessageContext(String channel, String subchannel, Player player) {
this.channel = channel;
this.subchannel = subchannel;
this.player = player;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MessageContext that = (MessageContext) o;
if (channel != null ? !channel.equals(that.channel) : that.channel != null) return false;
if (subchannel != null ? !subchannel.equals(that.subchannel) : that.subchannel != null) return false;
return player != null ? player.equals(that.player) : that.player == null;
}
@Override
public int hashCode() {
int result = channel != null ? channel.hashCode() : 0;
result = 31 * result + (subchannel != null ? subchannel.hashCode() : 0);
result = 31 * result + (player != null ? player.hashCode() : 0);
return result;
}
}
}