mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Add cross server whispering
This commit is contained in:
1 parent
faf3f34f0d
commit
16d98d35cc
3 files changed
+53
-25
No files matched your search
@@ -3,6 +3,7 @@ package net.draycia.simplechat.managers;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
|
||||
import net.draycia.simplechat.SimpleChat;
|
||||
import net.draycia.simplechat.channels.ChatChannel;
|
||||
import net.draycia.simplechat.storage.ChatUser;
|
||||
@@ -18,12 +19,15 @@ import java.util.UUID;
|
||||
public class PluginMessageManager implements PluginMessageListener {
|
||||
|
||||
private SimpleChat simpleChat;
|
||||
private BungeeChannelApi api;
|
||||
|
||||
public PluginMessageManager(SimpleChat simpleChat) {
|
||||
this.simpleChat = simpleChat;
|
||||
|
||||
simpleChat.getServer().getMessenger().registerOutgoingPluginChannel(simpleChat, "BungeeCord");
|
||||
simpleChat.getServer().getMessenger().registerIncomingPluginChannel(simpleChat, "BungeeCord", this);
|
||||
|
||||
api = BungeeChannelApi.of(simpleChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,6 +49,8 @@ public class PluginMessageManager implements PluginMessageListener {
|
||||
onMessageReceived(msg);
|
||||
} else if (subchannel.equals("simplechat:component")) {
|
||||
onComponentReceived(msg);
|
||||
} else if (subchannel.equals("simplechat:whisper")) {
|
||||
onWhisperReceived(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,41 +108,54 @@ public class PluginMessageManager implements PluginMessageListener {
|
||||
});
|
||||
}
|
||||
|
||||
private void onWhisperReceived(ByteArrayDataInput in) {
|
||||
UUID targetUUID;
|
||||
|
||||
try {
|
||||
targetUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
ChatUser target = simpleChat.getUserService().wrap(targetUUID);
|
||||
target.sendMessage(MiniMessage.get().parse(chatMessage));
|
||||
}
|
||||
|
||||
public void sendMessage(ChatChannel chatChannel, Player player, String message) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
|
||||
out.writeUTF("Forward");
|
||||
out.writeUTF("ALL");
|
||||
out.writeUTF("simplechat:message");
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(message);
|
||||
|
||||
out.writeShort(msg.toByteArray().length);
|
||||
out.write(msg.toByteArray());
|
||||
|
||||
player.sendPluginMessage(simpleChat, "BungeeCord", out.toByteArray());
|
||||
api.forward("ALL", "simplechat:message", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponent(ChatChannel chatChannel, Player player, Component component) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
|
||||
out.writeUTF("Forward");
|
||||
out.writeUTF("ALL");
|
||||
out.writeUTF("simplechat:component");
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getName());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(component));
|
||||
|
||||
out.writeShort(msg.toByteArray().length);
|
||||
out.write(msg.toByteArray());
|
||||
api.forward("ALL", "simplechat:component", msg.toByteArray());
|
||||
}
|
||||
|
||||
player.sendPluginMessage(simpleChat, "BungeeCord", out.toByteArray());
|
||||
public void sendComponentToPlayer(Player sender, ChatUser target, Component component) {
|
||||
api.getPlayerList("ALL").whenComplete((result, error) -> {
|
||||
if (!result.contains(target.asOfflinePlayer().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(target.getUUID().toString());
|
||||
msg.writeUTF(MiniMessage.get().serialize(component));
|
||||
|
||||
api.forward("ALL", "simplechat:whisper", msg.toByteArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -190,8 +191,8 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
this.playSound(Sound.of(key, source, volume, pitch));
|
||||
}
|
||||
} else {
|
||||
// TODO: cross server msg support, don't forget to include /ignore support
|
||||
} else if (sender.isOnline()) {
|
||||
simpleChat.getPluginMessageManager().sendComponentToPlayer(sender.asPlayer(), this, fromPlayerComponent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user