Add DiscordSRV integration

Signed-off-by: Etzelia <etzelia@hotmail.com>
discord
Etzelia 2020-06-21 22:34:32 -05:00
parent ab702eccf2
commit 9345ad73d3
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
9 changed files with 147 additions and 27 deletions

15
pom.xml
View File

@ -31,13 +31,20 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.14.3-R0.1-SNAPSHOT</version> <version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<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>
<groupId>com.discordsrv</groupId>
<artifactId>discordsrv</artifactId>
<version>1.19.0</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>xyz.etztech</groupId> <groupId>xyz.etztech</groupId>
@ -62,6 +69,10 @@
<id>dynmap-repo</id> <id>dynmap-repo</id>
<url>http://repo.mikeprimm.com</url> <url>http://repo.mikeprimm.com</url>
</repository> </repository>
<repository>
<id>discordsrv-repo</id>
<url>https://nexus.scarsz.me/content/groups/public/</url>
</repository>
<repository> <repository>
<id>etztech-repo</id> <id>etztech-repo</id>
<url>http://repo.etztech.xyz</url> <url>http://repo.etztech.xyz</url>
@ -72,7 +83,7 @@
</repository> </repository>
<repository> <!-- This repo fixes issues with transitive dependencies --> <repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id> <id>jcenter</id>
<url>http://jcenter.bintray.com</url> <url>https://jcenter.bintray.com</url>
</repository> </repository>
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>

View File

@ -130,12 +130,11 @@ public class DeluxeGroup {
} }
public void sendMessage(String message) { public void sendMessage(String message) {
//DeluxeUtil.log(this.log, message);
Player on; Player on;
for (OfflinePlayer player : this.groupList) { for (OfflinePlayer player : this.groupList) {
if (player.isOnline()) { if (player.isOnline()) {
on = Bukkit.getPlayer(player.getName()); on = Bukkit.getPlayer(player.getName());
on.sendMessage(ChatColor.AQUA + message); on.sendMessage(message);
} }
} }
} }

View File

@ -1,23 +1,21 @@
package xyz.etztech.deluxegroups; package xyz.etztech.deluxegroups;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.util.DiscordUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapAPI; import org.dynmap.DynmapAPI;
import xyz.etztech.core.api.IMinecraftManager; 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.DiscordSRVListener;
import xyz.etztech.deluxegroups.listeners.SessionListener; import xyz.etztech.deluxegroups.listeners.SessionListener;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -32,13 +30,13 @@ public class DeluxeGroups extends JavaPlugin {
// Objects that can be reloaded // Objects that can be reloaded
private AsyncPlayerChatListener chatListener; private AsyncPlayerChatListener chatListener;
// Dynmap API // Dynmap APIDynmapAPI
private static DynmapAPI dynmap = null; private static DynmapAPI dynmap = null;
// MinecraftManager API // MinecraftManager API
private static IMinecraftManager minecraftManager = null; private static IMinecraftManager minecraftManager = null;
private static boolean discord = false;
public void onEnable() { public void onEnable() {
@ -69,6 +67,12 @@ public class DeluxeGroups extends JavaPlugin {
minecraftManager.logOverride(true); minecraftManager.logOverride(true);
} }
// DiscordSRV integration
if (Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")) {
discord = true;
DiscordSRV.api.subscribe(new DiscordSRVListener(this));
}
// Add Commands // Add Commands
CommandMain cmdMain = new CommandMain(this); CommandMain cmdMain = new CommandMain(this);
this.getCommand("deluxegroups").setExecutor(cmdMain); this.getCommand("deluxegroups").setExecutor(cmdMain);
@ -154,5 +158,8 @@ public class DeluxeGroups extends JavaPlugin {
return minecraftManager; return minecraftManager;
} }
public static boolean getDiscord() {
return discord;
}
} }

View File

@ -51,4 +51,28 @@ public class DeluxeUtil {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "IMPORTANT: Disabling Dynmap Game->Web Chat. All web chat will be handled by DeluxeGroups."); Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "IMPORTANT: Disabling Dynmap Game->Web Chat. All web chat will be handled by DeluxeGroups.");
((DynmapAPI) dynmap).setDisableChatToWebProcessing(true); ((DynmapAPI) dynmap).setDisableChatToWebProcessing(true);
} }
public static boolean isGroupChat(List<String> prefixes, String message) {
boolean groupChat = false;
for (String prefix : prefixes) {
// If chat starts with 1 prefix, it is group chat.
if (message.startsWith(prefix)) {
groupChat = true;
message = message.substring(1);
// If chat still starts with the prefix, it means there were two and we are delegating to normal chat
if (message.startsWith(prefix)) {
return false;
}
break;
}
}
return groupChat;
}
public static List<String> trimmed(List<String> list) {
for (int i = 0; i < list.size(); i++) {
list.set(i, list.get(i).trim());
}
return list;
}
} }

View File

@ -119,7 +119,7 @@ public class CommandGroup
DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(args[1]); DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(args[1]);
if (args.length == 2) { if (args.length == 2) {
if (!group.hasPassword()) { if (!group.hasPassword()) {
group.sendMessage(player.getName() + " has joined the group!"); group.sendMessage(ChatColor.AQUA + player.getName() + " has joined the group!");
DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!");
DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId()); DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId());
} else { } else {
@ -127,11 +127,11 @@ public class CommandGroup
} }
} else if ( args.length == 3) { } else if ( args.length == 3) {
if (group.hasPassword() && args[2].equals(group.getPassword())) { if (group.hasPassword() && args[2].equals(group.getPassword())) {
group.sendMessage(player.getName() + " has joined the group!"); group.sendMessage(ChatColor.AQUA + player.getName() + " has joined the group!");
DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!");
DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId()); DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId());
} else if (!group.hasPassword()) { } else if (!group.hasPassword()) {
group.sendMessage(player.getName() + " has joined the group!"); group.sendMessage(ChatColor.AQUA + player.getName() + " has joined the group!");
DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!");
DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId()); DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId());
} else { } else {

View File

@ -1,5 +1,7 @@
package xyz.etztech.deluxegroups.listeners; package xyz.etztech.deluxegroups.listeners;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,6 +14,7 @@ import org.dynmap.DynmapCommonAPI;
import xyz.etztech.core.api.IMinecraftManager; 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 java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -29,7 +32,7 @@ public class AsyncPlayerChatListener implements Listener {
public void reload() { public void reload() {
this.groupPrefixes = new ArrayList<>(); this.groupPrefixes = new ArrayList<>();
this.groupPrefixes.addAll(trimmed(this.plugin.getConfig().getStringList("prefix"))); this.groupPrefixes.addAll(DeluxeUtil.trimmed(this.plugin.getConfig().getStringList("prefix")));
} }
@EventHandler(priority=EventPriority.HIGH) @EventHandler(priority=EventPriority.HIGH)
@ -90,6 +93,19 @@ public class AsyncPlayerChatListener implements Listener {
} }
} }
event.setMessage(chat); event.setMessage(chat);
// DiscordSRV
if (DeluxeGroups.getDiscord()) {
TextChannel groupChannel = DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(group.getName());
if (groupChannel != null) {
String discordFormat = plugin.getConfig().getString("mtd", "<author> > <message>");
String message = discordFormat.
replace("<group>", group.getName()).
replace("<message>", event.getMessage()).
replace("<author>", event.getPlayer().getName());
groupChannel.sendMessage(message);
}
}
return; return;
} }
} }
@ -109,14 +125,4 @@ public class AsyncPlayerChatListener implements Listener {
} }
} }
private List<String> trimmed(List<String> list) {
for (int i = 0; i < list.size(); i++) {
list.set(i, list.get(i).trim());
}
return list;
}
} }

View File

@ -0,0 +1,65 @@
package xyz.etztech.deluxegroups.listeners;
import github.scarsz.discordsrv.api.Subscribe;
import github.scarsz.discordsrv.api.events.DiscordGuildMessagePreProcessEvent;
import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent;
import org.bukkit.ChatColor;
import xyz.etztech.deluxegroups.DeluxeGroup;
import xyz.etztech.deluxegroups.DeluxeGroups;
import xyz.etztech.deluxegroups.DeluxeUtil;
import java.util.ArrayList;
import java.util.List;
public class DiscordSRVListener {
private DeluxeGroups plugin;
private List<String> groupPrefixes;
public DiscordSRVListener(DeluxeGroups deluxeGroups) {
this.plugin = deluxeGroups;
reload();
}
public void reload() {
this.groupPrefixes = new ArrayList<>();
this.groupPrefixes.addAll(DeluxeUtil.trimmed(this.plugin.getConfig().getStringList("prefix")));
}
@Subscribe
public void onDiscordMessage(DiscordGuildMessagePreProcessEvent event) {
event.setCancelled(true);
if (DeluxeGroups.getDatabase().groupExists(event.getChannel().getName())) {
DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(event.getChannel().getName());
String format = plugin.getConfig().getString("dtm", "[DISCORD | <group>] <player> > <message>");
String chat = format.
replace("<group>", group.getName()).
replace("<message>", event.getMessage().getContentStripped()).
replace("<author>", event.getAuthor().getName());
chat = ChatColor.translateAlternateColorCodes('&', chat);
group.sendMessage(chat);
}
}
@Subscribe
public void onMinecraftMessage(GameChatMessagePreProcessEvent event) {
String chat = event.getMessage();
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);
}
}
}

View File

@ -10,4 +10,12 @@ format:
color: '' color: ''
# Custom format. The variables <group> and <message> are available and will be # Custom format. The variables <group> and <message> are available and will be
# replaced by the group name and chat message respectively # replaced by the group name and chat message respectively
custom: '' custom: ''
# Optional formatting for DiscordSRV integration
discord:
# These have the same variables available as custom, but also <author> for the name of the speaker
# Discord to Minecraft. Can include color codes
dtm: '[DISCORD | <group>] <author> > <message>'
# Minecraft to Discord
mtd: '<author> > <message>'

View File

@ -4,7 +4,7 @@ description: ${description}
author: ${author} author: ${author}
website: ${url} website: ${url}
main: ${mainClass} main: ${mainClass}
softdepend: [dynmap, MinecraftManager] softdepend: [dynmap, MinecraftManager, DiscordSRV]
commands: commands:
deluxegroups: deluxegroups:
description: Base DeluxeGroups command description: Base DeluxeGroups command