fix subchannel/bungee issues

This commit is contained in:
dalbodeule 2020-01-21 00:45:55 +09:00
parent a6555b171c
commit 0015a3bcb7
No known key found for this signature in database
GPG Key ID: 2FCD836D79E6D226
8 changed files with 145 additions and 38 deletions

View File

@ -18,7 +18,9 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
public CraftBukkit compat = null;
private Settings settings;
public HashMap<OfflinePlayer, String> PlayerDestiMap = new HashMap<>();
public String channelName = "mc:advancedportals";
// public HashMap<OfflinePlayer, String> PlayerDestiMap = new HashMap<>();
public void onEnable() {
@ -110,6 +112,9 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
private void setupBungee() {
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new BungeeListener(this));
this.getServer().getMessenger().registerOutgoingPluginChannel(this, channelName);
this.getServer().getMessenger().registerIncomingPluginChannel(this, channelName, new PluginMessageReceiver(this));
}

View File

@ -19,27 +19,11 @@ public class BungeeListener implements PluginMessageListener {
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("AdvancedPortals")) {
if (!channel.equals("BungeeCord")) {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
plugin.getLogger().fine(subchannel);
if (subchannel.equals("BungeePortal")) {
String targetPlayerUUID = in.readUTF();
String targetDestination = in.readUTF();
OfflinePlayer msgPlayer = plugin.getServer().getOfflinePlayer(UUID.fromString(targetPlayerUUID));
plugin.PlayerDestiMap.put(msgPlayer, targetDestination);
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () ->
plugin.PlayerDestiMap.remove(msgPlayer),
20L*10
);
}
// some codes
}
/**

View File

@ -66,11 +66,11 @@ public class Listeners implements Listener {
public void onJoinEvent(PlayerJoinEvent event) {
Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
if (plugin.PlayerDestiMap.containsKey(event.getPlayer())) {
/* if (plugin.PlayerDestiMap.containsKey(event.getPlayer())) {
String desti = plugin.PlayerDestiMap.get(event.getPlayer());
Destination.warp(event.getPlayer(), desti);
}
} */
}
@EventHandler

View File

@ -0,0 +1,69 @@
package com.sekwah.advancedportals.bukkit.listeners;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.bukkit.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.bukkit.destinations.Destination;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.UUID;
public class PluginMessageReceiver implements PluginMessageListener {
private AdvancedPortalsPlugin plugin;
public PluginMessageReceiver(AdvancedPortalsPlugin plugin) {
this.plugin = plugin;
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
// plugin.getLogger().info(""+channel.equals(plugin.channelName));
if (!channel.equals(plugin.channelName)) {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
// plugin.getLogger().info("bukkit plugin received: " + subchannel);
if (subchannel.equals("BungeePortal")) {
String targetPlayerUUID = in.readUTF();
String targetDestination = in.readUTF();
OfflinePlayer msgPlayer = plugin.getServer().getOfflinePlayer(UUID.fromString(targetPlayerUUID));
Destination.warp(msgPlayer.getPlayer(), targetDestination);
/* plugin.PlayerDestiMap.put(msgPlayer, targetDestination);
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () ->
plugin.PlayerDestiMap.remove(msgPlayer),
20L*10
); */
}
}
/**
* Example forward packet.
*
* Construct like the forge packets.
*
* out.writeUTF("Forward"); // So BungeeCord knows to forward it
out.writeUTF("ALL");
out.writeUTF("MyChannel"); // The channel name to check if this your data
ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
DataOutputStream msgout = new DataOutputStream(msgbytes);
msgout.writeUTF("Some kind of data here"); // You can do anything you want with msgout
msgout.writeShort(123);
out.writeShort(msgbytes.toByteArray().length);
out.write(msgbytes.toByteArray());
*
*/
}

View File

@ -414,7 +414,7 @@ public class Portal {
outForList.writeUTF(player.getUniqueId().toString());
outForList.writeUTF(portal.getDestiation());
player.sendPluginMessage(plugin, "AdvancedPortals", outForList.toByteArray());
player.sendPluginMessage(plugin, plugin.channelName, outForList.toByteArray());
ByteArrayDataOutput outForSend = ByteStreams.newDataOutput();
outForSend.writeUTF("Connect");

View File

@ -1,13 +1,28 @@
package com.sekwah.advancedportals.bungee;
import com.sekwah.advancedportals.bungee.listener.EventListener;
import com.sekwah.advancedportals.bungee.listener.PluginMessageReceiver;
import net.md_5.bungee.api.plugin.Plugin;
import java.util.HashMap;
public class AdvancedPortalsPlugin extends Plugin {
public String channelName = "mc:advancedportals";
public HashMap<String, String[]> PlayerDestiMap = new HashMap<>();
// key: UUID (string)
// value: [0] targetServer, [1] targetDestination
@Override
public void onEnable() {
getProxy().registerChannel("AdvancedPortals");
getProxy().registerChannel(channelName);
getProxy().getPluginManager().registerListener(this, new PluginMessageReceiver(this));
getProxy().getPluginManager().registerListener(this, new EventListener(this));
getLogger().info("\u00A7aAdvanced portals have been successfully enabled!");
}
}
@Override
public void onDisable() {

View File

@ -0,0 +1,36 @@
package com.sekwah.advancedportals.bungee.listener;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.bungee.AdvancedPortalsPlugin;
import net.md_5.bungee.api.event.ServerSwitchEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
public class EventListener implements Listener {
private AdvancedPortalsPlugin plugin;
public EventListener(AdvancedPortalsPlugin plugin) { this.plugin = plugin; }
@EventHandler
public void onPlayerSwitchServer(ServerSwitchEvent event) {
String uuid = event.getPlayer().getUniqueId().toString();
if (plugin.PlayerDestiMap.containsKey(uuid)) {
String[] val = plugin.PlayerDestiMap.get(uuid);
// key: UUID (string)
// value: [0] targetServer, [1] targetDestination
if (event.getPlayer().getServer().getInfo().getName().equalsIgnoreCase(val[0])) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("BungeePortal");
out.writeUTF(uuid);
out.writeUTF(val[1]);
event.getPlayer().getServer().getInfo().sendData(plugin.channelName, out.toByteArray());
}
}
}
}

View File

@ -1,43 +1,41 @@
package com.sekwah.advancedportals.bungee.listener;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.bungee.AdvancedPortalsPlugin;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import java.util.concurrent.TimeUnit;
public class PluginMessageReceiver implements Listener {
private AdvancedPortalsPlugin plugin;
public PluginMessageReceiver (AdvancedPortalsPlugin plugin) {
this.plugin = plugin;
}
public PluginMessageReceiver(AdvancedPortalsPlugin plugin) { this.plugin = plugin; }
@EventHandler
public void onMessageReceived(PluginMessageEvent event) {
if(!event.getTag().equalsIgnoreCase("AdvancedPortals")) return;
if(!event.getTag().equalsIgnoreCase(plugin.channelName)) return;
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
String subChannel = in.readUTF();
plugin.getProxy().getLogger().info("bungee plugin received: " + subChannel);
if (subChannel.equalsIgnoreCase("PortalEnter")) {
String targetServer = in.readUTF();
String targetPlayerUUID = in.readUTF();
String targetDestination = in.readUTF();
ServerInfo server = plugin.getProxy().getServerInfo(targetServer);
// plugin.getProxy().getLogger().info(targetServer + " " + targetPlayerUUID + " " + targetDestination);
if (server != null) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("BungeePortal");
out.writeUTF(targetPlayerUUID);
out.writeUTF(targetDestination);
plugin.PlayerDestiMap.put(targetPlayerUUID, new String[]{targetServer, targetDestination});
server.sendData("AdvancedPortals", out.toByteArray());
plugin.getProxy().getScheduler().schedule(plugin, () -> {
if (plugin.PlayerDestiMap.containsKey(targetPlayerUUID)) {
plugin.PlayerDestiMap.remove(targetPlayerUUID);
}
}, 20, TimeUnit.SECONDS);
}
}
}