mirror of
https://github.com/BGHDDevelopment/PlayerBalancer.git
synced 2024-09-17 10:14:10 +02:00
Plugin message helper for the spigot plugin
This commit is contained in:
parent
16b05eb9d3
commit
df083f56f6
@ -44,4 +44,6 @@ jobs:
|
||||
path: target/surefire-reports
|
||||
|
||||
- store_artifacts:
|
||||
path: target/PlayerBalancer.jar
|
||||
paths:
|
||||
- target/PlayerBalancer.jar
|
||||
- target/PlayerBalancerAddon.jar
|
@ -36,6 +36,14 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</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>
|
||||
</build>
|
||||
|
||||
|
@ -111,6 +111,29 @@ public class PluginMessageListener implements Listener {
|
||||
sender.sendData("PlayerBalancer", stream.toByteArray());
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,17 +12,31 @@
|
||||
<name>PlayerBalancer Addon</name>
|
||||
<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>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
<finalName>PlayerBalancerAddon</finalName>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
@ -30,5 +44,11 @@
|
||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.8.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -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;
|
||||
}
|
||||
}
|
@ -3,13 +3,20 @@ package com.jaimemartz.playerbalanceraddon;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class PlayerBalancerAddon extends JavaPlugin {
|
||||
private PluginMessageManager manager;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
manager = new PluginMessageManager(this);
|
||||
getCommand("balancer").setExecutor(new MainCommand(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
}
|
||||
|
||||
public PluginMessageManager getManager() {
|
||||
return manager;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user