Add cross server whispering

This commit is contained in:
Draycia committed 2020-07-06 07:26:02 -07:00
1 parent faf3f34f0d
commit 16d98d35cc
3 files changed
+53 -25

No files matched your search

+12 -4
View File
@@ -70,10 +70,6 @@
<pattern>org.checkerframework</pattern>
<shadedPattern>net.draycia.simplechat.libs.checkerframework</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>net.draycia.simplechat.libs.apache</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
@@ -156,6 +152,12 @@
<url>https://dl.bintray.com/ichbinjoe/public/</url>
</repository>
<!-- BungeeChannelAPI -->
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
</repository>
<!-- mcMMO and MiniMessage must be compiled locally -->
</repositories>
@@ -262,5 +264,11 @@
<artifactId>HikariCP</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>io.github.leonardosnt</groupId>
<artifactId>bungeechannelapi</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
@@ -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);
}
}