Signed-off-by: Etzelia <etzelia@hotmail.com>
discord
Etzelia 2020-06-22 23:27:10 -05:00
parent e5a6d7ecc6
commit f3a8438904
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
8 changed files with 209 additions and 33 deletions

View File

@ -38,13 +38,11 @@
<groupId>us.dynmap</groupId> <groupId>us.dynmap</groupId>
<artifactId>dynmap-api</artifactId> <artifactId>dynmap-api</artifactId>
<version>1.9.4</version> <version>1.9.4</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.discordsrv</groupId> <groupId>com.discordsrv</groupId>
<artifactId>discordsrv</artifactId> <artifactId>discordsrv</artifactId>
<version>1.19.0</version> <version>1.19.0</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>xyz.etztech</groupId> <groupId>xyz.etztech</groupId>

View File

@ -12,6 +12,7 @@ import xyz.etztech.core.api.IMinecraftManager;
import xyz.etztech.deluxegroups.command.CommandGroup; import xyz.etztech.deluxegroups.command.CommandGroup;
import xyz.etztech.deluxegroups.command.CommandMain; import xyz.etztech.deluxegroups.command.CommandMain;
import xyz.etztech.deluxegroups.listeners.AsyncPlayerChatListener; import xyz.etztech.deluxegroups.listeners.AsyncPlayerChatListener;
import xyz.etztech.deluxegroups.listeners.DGListener;
import xyz.etztech.deluxegroups.listeners.DiscordSRVListener; import xyz.etztech.deluxegroups.listeners.DiscordSRVListener;
import xyz.etztech.deluxegroups.listeners.SessionListener; import xyz.etztech.deluxegroups.listeners.SessionListener;
@ -79,11 +80,11 @@ public class DeluxeGroups extends JavaPlugin {
CommandGroup cmdGroup = new CommandGroup(this); CommandGroup cmdGroup = new CommandGroup(this);
this.getCommand("group").setExecutor(cmdGroup); this.getCommand("group").setExecutor(cmdGroup);
// Add Listeners // Add Listeners
chatListener = new AsyncPlayerChatListener(this); chatListener = new AsyncPlayerChatListener(this);
getServer().getPluginManager().registerEvents(chatListener, this); getServer().getPluginManager().registerEvents(chatListener, this);
DGListener dgListener = new DGListener(this);
getServer().getPluginManager().registerEvents(dgListener, this);
} }
} }

View File

@ -0,0 +1,47 @@
package xyz.etztech.deluxegroups.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class GlobalChatEvent extends Event implements Cancellable {
private Player author;
private String message;
public GlobalChatEvent(Player author, String message) {
this.author = author;
this.message = message;
}
public Player getAuthor() {
return author;
}
public String getMessage() {
return message;
}
//
// - - - HandlerList boilerplate - - -
//
public static final HandlerList HANDLERS = new HandlerList();
@Override
public HandlerList getHandlers() { return HANDLERS; }
public static HandlerList getHandlerList() { return HANDLERS; }
//
// - - - Cancellable boilerplate - - -
//
private boolean isCancelled = false;
@Override
public boolean isCancelled() { return isCancelled; }
@Override
public void setCancelled(boolean cancelled) { isCancelled = cancelled; }
}

View File

@ -0,0 +1,55 @@
package xyz.etztech.deluxegroups.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import xyz.etztech.deluxegroups.DeluxeGroup;
import xyz.etztech.deluxegroups.DeluxeUtil;
public class GroupChatEvent extends Event implements Cancellable {
private Player author;
private DeluxeGroup group;
private String message;
public GroupChatEvent(Player author, DeluxeGroup group, String message) {
this.author = author;
this.group = group;
this.message = message;
}
public Player getAuthor() {
return author;
}
public DeluxeGroup getGroup() {
return group;
}
public String getMessage() {
return message;
}
//
// - - - HandlerList boilerplate - - -
//
public static final HandlerList HANDLERS = new HandlerList();
@Override
public HandlerList getHandlers() { return HANDLERS; }
public static HandlerList getHandlerList() { return HANDLERS; }
//
// - - - Cancellable boilerplate - - -
//
private boolean isCancelled = false;
@Override
public boolean isCancelled() { return isCancelled; }
@Override
public void setCancelled(boolean cancelled) { isCancelled = cancelled; }
}

View File

@ -3,6 +3,7 @@ package xyz.etztech.deluxegroups.listeners;
import github.scarsz.discordsrv.DiscordSRV; import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel; import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -15,6 +16,8 @@ import xyz.etztech.core.api.IMinecraftManager;
import xyz.etztech.deluxegroups.DeluxeGroup; import xyz.etztech.deluxegroups.DeluxeGroup;
import xyz.etztech.deluxegroups.DeluxeGroups; import xyz.etztech.deluxegroups.DeluxeGroups;
import xyz.etztech.deluxegroups.DeluxeUtil; import xyz.etztech.deluxegroups.DeluxeUtil;
import xyz.etztech.deluxegroups.events.GlobalChatEvent;
import xyz.etztech.deluxegroups.events.GroupChatEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -96,15 +99,10 @@ public class AsyncPlayerChatListener implements Listener {
// DiscordSRV // DiscordSRV
if (DeluxeGroups.getDiscord()) { if (DeluxeGroups.getDiscord()) {
TextChannel groupChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(group.getName()); String finalChat = chat;
if (groupChannel != null) { plugin.getServer().getScheduler().runTask(plugin, () ->
String discordFormat = plugin.getConfig().getString("mtd", "<author> > <message>"); Bukkit.getPluginManager().callEvent(new GroupChatEvent(sender, group, finalChat))
String message = discordFormat. );
replace("<group>", group.getName()).
replace("<message>", event.getMessage()).
replace("<author>", event.getPlayer().getName());
groupChannel.sendMessage(message);
}
} }
return; return;
} }
@ -122,6 +120,9 @@ public class AsyncPlayerChatListener implements Listener {
if (minecraftManager != null) { if (minecraftManager != null) {
minecraftManager.globalLog(sender, chat); minecraftManager.globalLog(sender, chat);
} }
plugin.getServer().getScheduler().runTask(plugin, () ->
Bukkit.getPluginManager().callEvent(new GlobalChatEvent(sender, chat))
);
} }
} }

View File

@ -0,0 +1,85 @@
package xyz.etztech.deluxegroups.listeners;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.dependencies.commons.lang3.StringUtils;
import github.scarsz.discordsrv.dependencies.dev.vankka.mcdiscordreserializer.discord.DiscordSerializer;
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
import github.scarsz.discordsrv.dependencies.net.kyori.text.serializer.legacy.LegacyComponentSerializer;
import github.scarsz.discordsrv.hooks.VaultHook;
import github.scarsz.discordsrv.hooks.world.MultiverseCoreHook;
import github.scarsz.discordsrv.util.DiscordUtil;
import github.scarsz.discordsrv.util.LangUtil;
import github.scarsz.discordsrv.util.PlaceholderUtil;
import github.scarsz.discordsrv.util.TimeUtil;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import xyz.etztech.deluxegroups.DeluxeGroups;
import xyz.etztech.deluxegroups.events.GlobalChatEvent;
import xyz.etztech.deluxegroups.events.GroupChatEvent;
import static org.bukkit.Bukkit.getServer;
public class DGListener implements Listener {
private DeluxeGroups plugin;
public DGListener(DeluxeGroups deluxeGroups) {
this.plugin = deluxeGroups;
}
@EventHandler
public void onGlobalChat(GlobalChatEvent event) {
TextChannel mainChannel = DiscordSRV.getPlugin().getMainTextChannel();
// FIXME Do we really need to copy/paste DiscordSRV code??
String channel = mainChannel.getName();
String message = event.getMessage();
Player player = event.getAuthor();
String userPrimaryGroup = VaultHook.getPrimaryGroup(player);
boolean hasGoodGroup = StringUtils.isNotBlank(userPrimaryGroup);
if (hasGoodGroup) {
userPrimaryGroup = userPrimaryGroup.substring(0, 1).toUpperCase() + userPrimaryGroup.substring(1);
}
boolean reserializer = DiscordSRV.config().getBoolean("Experiment_MCDiscordReserializer_ToDiscord");
String username = DiscordUtil.strip(player.getName());
if (!reserializer) {
username = DiscordUtil.escapeMarkdown(username);
}
String discordMessage = (hasGoodGroup ? LangUtil.Message.CHAT_TO_DISCORD.toString() : LangUtil.Message.CHAT_TO_DISCORD_NO_PRIMARY_GROUP.toString()).replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%channelname%", channel != null ? channel.substring(0, 1).toUpperCase() + channel.substring(1) : "").replace("%primarygroup%", userPrimaryGroup).replace("%username%", username).replace("%world%", player.getWorld().getName()).replace("%worldalias%", DiscordUtil.strip(MultiverseCoreHook.getWorldAlias(player.getWorld().getName())));
discordMessage = PlaceholderUtil.replacePlaceholdersToDiscord(discordMessage, player);
String displayName = DiscordUtil.strip(player.getDisplayName());
if (reserializer) {
message = DiscordSerializer.INSTANCE.serialize(LegacyComponentSerializer.legacy().deserialize(message));
} else {
displayName = DiscordUtil.escapeMarkdown(displayName);
}
discordMessage = discordMessage.replace("%displayname%", displayName).replace("%message%", message);
if (!reserializer) {
discordMessage = DiscordUtil.strip(discordMessage);
}
if (DiscordSRV.config().getBoolean("DiscordChatChannelTranslateMentions")) {
discordMessage = DiscordUtil.convertMentionsFromNames(discordMessage, DiscordSRV.getPlugin().getMainGuild());
} else {
discordMessage = discordMessage.replace("@", "@\u200b");
}
DiscordUtil.sendMessage(mainChannel, discordMessage);
}
@EventHandler
public void onGroupChat(GroupChatEvent event) {
TextChannel groupChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(event.getGroup().getName());
if (groupChannel != null) {
String discordFormat = plugin.getConfig().getString("discord.mtd", "<author> > <message>");
String message = discordFormat.
replace("<group>", event.getGroup().getName()).
replace("<message>", ChatColor.stripColor(event.getMessage())).
replace("<author>", event.getAuthor().getName());
groupChannel.sendMessage(message).queue();
}
}
}

View File

@ -1,8 +1,10 @@
package xyz.etztech.deluxegroups.listeners; package xyz.etztech.deluxegroups.listeners;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.api.Subscribe; import github.scarsz.discordsrv.api.Subscribe;
import github.scarsz.discordsrv.api.events.DiscordGuildMessagePreProcessEvent; import github.scarsz.discordsrv.api.events.DiscordGuildMessagePreProcessEvent;
import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent; import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import xyz.etztech.deluxegroups.DeluxeGroup; import xyz.etztech.deluxegroups.DeluxeGroup;
import xyz.etztech.deluxegroups.DeluxeGroups; import xyz.etztech.deluxegroups.DeluxeGroups;
@ -30,7 +32,7 @@ public class DiscordSRVListener {
public void onDiscordMessage(DiscordGuildMessagePreProcessEvent event) { public void onDiscordMessage(DiscordGuildMessagePreProcessEvent event) {
if (DeluxeGroups.getDatabase().groupExists(event.getChannel().getName())) { if (DeluxeGroups.getDatabase().groupExists(event.getChannel().getName())) {
DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(event.getChannel().getName()); DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(event.getChannel().getName());
String format = plugin.getConfig().getString("dtm", "[DISCORD | <group>] <player> > <message>"); String format = plugin.getConfig().getString("discord.dtm", "[DISCORD | <group>] <author> > <message>");
String chat = format. String chat = format.
replace("<group>", group.getName()). replace("<group>", group.getName()).
replace("<message>", event.getMessage().getContentStripped()). replace("<message>", event.getMessage().getContentStripped()).
@ -39,27 +41,14 @@ public class DiscordSRVListener {
group.sendMessage(chat); group.sendMessage(chat);
event.setCancelled(true); event.setCancelled(true);
} }
if (!event.getChannel().getId().equals(DiscordSRV.getPlugin().getMainTextChannel().getId())) {
event.setCancelled(true);
}
} }
@Subscribe @Subscribe
public void onMinecraftMessage(GameChatMessagePreProcessEvent event) { public void onMinecraftMessage(GameChatMessagePreProcessEvent event) {
String chat = event.getMessage(); // Handled with custom events
boolean groupChat = false;
for (String prefix : this.groupPrefixes) {
// If chat starts with 1 prefix, it is group chat.
if (chat.startsWith(prefix)) {
groupChat = true;
chat = chat.substring(1);
// If chat still starts with the prefix, it means there were two and we are delegating to normal chat
if (chat.startsWith(prefix)) {
event.setMessage(chat);
return;
}
break;
}
}
if (groupChat) {
event.setCancelled(true); event.setCancelled(true);
} }
}
} }

View File

@ -16,6 +16,6 @@ format:
discord: discord:
# These have the same variables available as custom, but also <author> for the name of the speaker # These have the same variables available as custom, but also <author> for the name of the speaker
# Discord to Minecraft. Can include color codes # Discord to Minecraft. Can include color codes
dtm: '[DISCORD | <group>] <author> > <message>' dtm: '[&bDiscord &f| &b<group>&f] <author> > <message>'
# Minecraft to Discord # Minecraft to Discord
mtd: '<author> > <message>' mtd: '<author> > <message>'