Add component forwarding

This commit is contained in:
Draycia committed 2020-06-26 04:03:20 -07:00
1 parent 95cfa7db52
commit dc9d7c84c0
7 files changed
+107 -13

No files matched your search

@@ -1,6 +1,7 @@
package net.draycia.simplechat.channels;
import net.draycia.simplechat.SimpleChat;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@@ -100,6 +101,13 @@ public abstract class ChatChannel {
*/
public abstract boolean canPlayerUse(Player player);
/**
* @return If the channel should forward its formatting / formatted message to other servers
*/
public boolean shouldForwardFormatting() {
return true;
}
/**
* Parses the specified message, calls a {@link net.draycia.simplechat.events.ChannelChatEvent}, and sends the message to everyone who can view this channel.
* @param player The player who is saying the message.
@@ -107,6 +115,8 @@ public abstract class ChatChannel {
*/
public abstract void sendMessage(OfflinePlayer player, String message);
public abstract void sendComponent(OfflinePlayer player, Component component);
public abstract void processDiscordMessage(MessageCreateEvent event);
public static abstract class Builder {
@@ -123,6 +133,7 @@ public abstract class ChatChannel {
public abstract ChatChannel.Builder setSwitchMessage(String switchMessage);
public abstract ChatChannel.Builder setToggleOffMessage(String toggleOffMessage);
public abstract ChatChannel.Builder setToggleOnMessage(String toggleOnMessage);
public abstract ChatChannel.Builder setShouldForwardFormatting(boolean forwardFormatting);
}
}
@@ -19,8 +19,8 @@ import java.util.Map;
public class AllianceChatChannel extends SimpleChatChannel {
AllianceChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
AllianceChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, simpleChat);
}
@Override
@@ -20,8 +20,8 @@ import java.util.Map;
public class NationChatChannel extends SimpleChatChannel {
NationChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
NationChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, simpleChat);
}
@Override
@@ -17,8 +17,8 @@ import java.util.Map;
public class PartyChatChannel extends SimpleChatChannel {
PartyChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
PartyChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, simpleChat);
}
@Override
@@ -36,6 +36,7 @@ public class SimpleChatChannel extends ChatChannel {
private String switchMessage;
private String toggleOffMessage;
private String toggleOnMessage;
private boolean forwardFormatting;
private DiscordWebhook discordWebhook = null;
@@ -43,7 +44,7 @@ public class SimpleChatChannel extends ChatChannel {
private SimpleChatChannel() { }
SimpleChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, SimpleChat simpleChat) {
SimpleChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, SimpleChat simpleChat) {
this.color = color;
this.id = id;
this.formats = formats;
@@ -55,6 +56,7 @@ public class SimpleChatChannel extends ChatChannel {
this.switchMessage = switchMessage;
this.toggleOffMessage = toggleOffMessage;
this.toggleOnMessage = toggleOnMessage;
this.forwardFormatting = forwardFormatting;
this.simpleChat = simpleChat;
@@ -164,11 +166,29 @@ public class SimpleChatChannel extends ChatChannel {
System.out.println(LegacyComponentSerializer.legacy().serialize(formattedMessage));
if (player.isOnline()) {
sendMessageToBungee(player.getPlayer(), message);
if (shouldForwardFormatting()) {
sendMessageToBungee(player.getPlayer(), component);
} else {
sendMessageToBungee(player.getPlayer(), message);
}
sendMessageToDiscord(player, message);
}
}
@Override
public void sendComponent(OfflinePlayer player, Component component) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (!canUserSeeMessage(player, onlinePlayer)) {
continue;
}
simpleChat.getPlatform().player(onlinePlayer).sendMessage(component);
}
System.out.println(LegacyComponentSerializer.legacy().serialize(component));
}
public void sendMessageToDiscord(OfflinePlayer player, String message) {
if (discordWebhook == null) {
return;
@@ -185,6 +205,10 @@ public class SimpleChatChannel extends ChatChannel {
}
}
public void sendMessageToBungee(Player player, Component component) {
simpleChat.getPluginMessageManager().sendComponent(this, player, component);
}
public void sendMessageToBungee(Player player, String message) {
simpleChat.getPluginMessageManager().sendMessage(this, player, message);
}
@@ -272,6 +296,11 @@ public class SimpleChatChannel extends ChatChannel {
return toggleOnMessage;
}
@Override
public boolean shouldForwardFormatting() {
return forwardFormatting;
}
public static SimpleChatChannel.Builder builder(String name) {
return new SimpleChatChannel.Builder(name);
}
@@ -289,6 +318,7 @@ public class SimpleChatChannel extends ChatChannel {
private String switchMessage = "<gray>You are now in <color><channel> <gray>chat!";
private String toggleOffMessage = "<gray>You can no longer see <color><channel> <gray>chat!";
private String toggleOnMessage = "<gray>You can now see <color><channel> <gray>chat!";
private boolean forwardFormatting = true;
private Builder() { }
@@ -298,7 +328,7 @@ public class SimpleChatChannel extends ChatChannel {
@Override
public SimpleChatChannel build(SimpleChat simpleChat) {
return new SimpleChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
return new SimpleChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, simpleChat);
}
@Override
@@ -382,6 +412,13 @@ public class SimpleChatChannel extends ChatChannel {
return this;
}
@Override
public Builder setShouldForwardFormatting(boolean forwardFormatting) {
this.forwardFormatting = forwardFormatting;
return this;
}
}
}
@@ -20,8 +20,8 @@ import java.util.Map;
public class TownChatChannel extends SimpleChatChannel {
TownChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, simpleChat);
TownChatChannel(TextColor color, long id, Map<String, String> formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, SimpleChat simpleChat) {
super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, simpleChat);
}
@Override
@@ -3,8 +3,11 @@ package net.draycia.simplechat.managers;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import me.minidigger.minimessage.text.MiniMessageParser;
import me.minidigger.minimessage.text.MiniMessageSerializer;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@@ -25,10 +28,14 @@ public class PluginMessageManager implements PluginMessageListener {
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("simplechat:message")) {
return;
if (channel.equals("simplechat:message")) {
onMessageReceived(channel, player, message);
} else if (channel.equals("simplechat:component")) {
onComponentReceived(channel, player, message);
}
}
private void onMessageReceived(String channel, Player player, byte[] message) {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String chatChannelName = in.readUTF();
@@ -57,6 +64,35 @@ public class PluginMessageManager implements PluginMessageListener {
});
}
public void onComponentReceived(String channel, Player player, byte[] message) {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String chatChannelName = in.readUTF();
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
if (chatChannel == null) {
return;
}
UUID playerUUID;
try {
playerUUID = UUID.fromString(in.readUTF());
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
OfflinePlayer messageAuthor = Bukkit.getOfflinePlayer(playerUUID);
String chatMessage = in.readUTF();
Bukkit.getScheduler().scheduleAsyncDelayedTask(simpleChat, () -> {
chatChannel.sendComponent(messageAuthor, MiniMessageParser.parseFormat(chatMessage));
});
}
public void sendMessage(ChatChannel chatChannel, Player player, String message) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
@@ -66,4 +102,14 @@ public class PluginMessageManager implements PluginMessageListener {
player.sendPluginMessage(simpleChat, "simplechat:message", out.toByteArray());
}
public void sendComponent(ChatChannel chatChannel, Player player, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(chatChannel.getName());
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(MiniMessageSerializer.serialize(component));
player.sendPluginMessage(simpleChat, "simplechat:component", out.toByteArray());
}
}