Another version of the events stuff
It directly calls the methods in the modules Also made a fun module
This commit is contained in:
parent
e29e6f1682
commit
7a6b79365f
8 changed files with 273 additions and 237 deletions
|
@ -1,6 +1,7 @@
|
||||||
package buttondevteam.discordplugin.commands;
|
package buttondevteam.discordplugin.commands;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
|
import buttondevteam.discordplugin.mcchat.MCChatCommand;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import sx.blah.discord.handle.obj.IMessage;
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
|
||||||
|
|
86
src/main/java/buttondevteam/discordplugin/fun/FunModule.java
Normal file
86
src/main/java/buttondevteam/discordplugin/fun/FunModule.java
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package buttondevteam.discordplugin.fun;
|
||||||
|
|
||||||
|
import buttondevteam.core.ComponentManager;
|
||||||
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.architecture.Component;
|
||||||
|
import buttondevteam.lib.architecture.ConfigData;
|
||||||
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FunModule extends Component {
|
||||||
|
private static FunModule mod;
|
||||||
|
|
||||||
|
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali
|
||||||
|
"Between now and the heat-death of the universe.", // Ghostise
|
||||||
|
"Soon™", "Ask again this time next month", // Ghostise
|
||||||
|
"In about 3 seconds", // Nicolai
|
||||||
|
"After we finish 8 plugins", // Ali
|
||||||
|
"Tomorrow.", // Ali
|
||||||
|
"After one tiiiny feature", // Ali
|
||||||
|
"Next commit", // Ali
|
||||||
|
"After we finish strangling Towny", // Ali
|
||||||
|
"When we kill every *fucking* bug", // Ali
|
||||||
|
"Once the server stops screaming.", // Ali
|
||||||
|
"After HL3 comes out", // Ali
|
||||||
|
"Next time you ask", // Ali
|
||||||
|
"When will *you* be open?" // Ali
|
||||||
|
};
|
||||||
|
|
||||||
|
private ConfigData<Boolean> serverReady() {
|
||||||
|
return getData("serverReady", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConfigData<List<String>> serverReadyAnswers() {
|
||||||
|
return getData("serverReadyAnswers", Arrays.asList(serverReadyStrings),
|
||||||
|
data -> (List<String>) data, data -> data); //TODO: Test
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String[] serverReadyQuestions = new String[]{"when will the server be open",
|
||||||
|
"when will the server be ready", "when will the server be done", "when will the server be complete",
|
||||||
|
"when will the server be finished", "when's the server ready", "when's the server open",
|
||||||
|
"Vhen vill ze server be open?"};
|
||||||
|
|
||||||
|
private static final Random serverReadyRandom = new Random();
|
||||||
|
private static final ArrayList<Short> usableServerReadyStrings = new ArrayList<Short>(serverReadyStrings.length) {
|
||||||
|
private static final long serialVersionUID = 2213771460909848770L;
|
||||||
|
|
||||||
|
{
|
||||||
|
createUsableServerReadyStrings(this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private static void createUsableServerReadyStrings(ArrayList<Short> list) {
|
||||||
|
for (short i = 0; i < serverReadyStrings.length; i++)
|
||||||
|
list.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void enable() {
|
||||||
|
mod = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void disable() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean executeMemes(IMessage message) {
|
||||||
|
if (!ComponentManager.isEnabled(FunModule.class)) return false;
|
||||||
|
if (mod.serverReady().get()) {
|
||||||
|
if (!TBMCCoreAPI.IsTestServer()
|
||||||
|
&& Arrays.stream(serverReadyQuestions).anyMatch(s -> message.getContent().toLowerCase().contains(s))) {
|
||||||
|
int next;
|
||||||
|
if (usableServerReadyStrings.size() == 0)
|
||||||
|
createUsableServerReadyStrings(usableServerReadyStrings);
|
||||||
|
next = usableServerReadyStrings.remove(serverReadyRandom.nextInt(usableServerReadyStrings.size()));
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), serverReadyStrings[next]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,27 +1,73 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.AsyncDiscordEvent;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import org.bukkit.event.EventHandler;
|
import buttondevteam.discordplugin.commands.DiscordCommandBase;
|
||||||
import org.bukkit.event.Listener;
|
import sx.blah.discord.handle.obj.IChannel;
|
||||||
import sx.blah.discord.api.events.Event;
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MentionEvent;
|
|
||||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
|
||||||
|
|
||||||
public class CommandListener implements Listener {
|
public class CommandListener {
|
||||||
@SuppressWarnings("unchecked")
|
/**
|
||||||
@EventHandler
|
* Runs a ChromaBot command. If mentionedonly is false, it will only execute the command if it was in #bot with the correct prefix or in private.
|
||||||
public <T extends Event> void onEvent(AsyncDiscordEvent<T> event_) {
|
*
|
||||||
if (event_.getEvent() instanceof MentionEvent)
|
* @param message The Discord message
|
||||||
onMention((AsyncDiscordEvent<MentionEvent>) event_);
|
* @param mentionedonly Only run the command if ChromaBot is mentioned at the start of the message
|
||||||
else if (event_.getEvent() instanceof MessageReceivedEvent)
|
* @return Whether it ran the command
|
||||||
onMessageReceived((AsyncDiscordEvent<MessageReceivedEvent>) event_);
|
*/
|
||||||
|
public static boolean runCommand(IMessage message, boolean mentionedonly) {
|
||||||
|
final IChannel channel = message.getChannel();
|
||||||
|
if (mentionedonly) {
|
||||||
|
if (!channel.getStringID().equals(DiscordPlugin.botchannel.getStringID())
|
||||||
|
&& !message.getContent().contains("channelcon")) //Allow channelcon in other servers
|
||||||
|
return false; //Private chat is handled without mentions
|
||||||
|
} else {
|
||||||
|
if (!message.getChannel().isPrivate()
|
||||||
|
&& !(message.getContent().startsWith("/")
|
||||||
|
&& channel.getStringID().equals(DiscordPlugin.botchannel.getStringID()))) //
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
message.getChannel().setTypingStatus(true); // Fun
|
||||||
|
final StringBuilder cmdwithargs = new StringBuilder(message.getContent());
|
||||||
|
final String mention = DiscordPlugin.dc.getOurUser().mention(false);
|
||||||
|
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
|
||||||
|
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
|
||||||
|
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
|
||||||
|
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().filter(r -> DiscordPlugin.dc.getOurUser().hasRole(r)).map(r -> r.mention())::iterator)
|
||||||
|
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
|
||||||
|
if (mentionedonly && !gotmention) {
|
||||||
|
message.getChannel().setTypingStatus(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
message.getChannel().setTypingStatus(true);
|
||||||
|
String cmdwithargsString = cmdwithargs.toString().trim(); //Remove spaces between mention and command
|
||||||
|
int index = cmdwithargsString.indexOf(" ");
|
||||||
|
String cmd;
|
||||||
|
String args;
|
||||||
|
if (index == -1) {
|
||||||
|
cmd = cmdwithargsString;
|
||||||
|
args = "";
|
||||||
|
} else {
|
||||||
|
cmd = cmdwithargsString.substring(0, index);
|
||||||
|
args = cmdwithargsString.substring(index + 1).trim(); //In case there are multiple spaces
|
||||||
|
}
|
||||||
|
DiscordCommandBase.runCommand(cmd.toLowerCase(), args, message);
|
||||||
|
message.getChannel().setTypingStatus(false);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onMention(AsyncDiscordEvent<MentionEvent> event) {
|
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) {
|
||||||
//TODO: Can't use priorities with this
|
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
|
||||||
|
if (cmdwithargs.length() > mention.length() + 1)
|
||||||
|
cmdwithargs.delete(0,
|
||||||
|
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length());
|
||||||
|
else
|
||||||
|
cmdwithargs.replace(0, cmdwithargs.length(), "help");
|
||||||
|
else {
|
||||||
|
if (cmdwithargs.length() > 0 && cmdwithargs.charAt(0) == '/')
|
||||||
|
cmdwithargs.deleteCharAt(0); //Don't treat / as mention, mentions can be used in public mcchat
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
if (cmdwithargs.length() == 0)
|
||||||
private void onMessageReceived(AsyncDiscordEvent<MessageReceivedEvent> event) {
|
cmdwithargs.replace(0, cmdwithargs.length(), "help");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.AsyncDiscordEvent;
|
|
||||||
import buttondevteam.discordplugin.DPUtils;
|
import buttondevteam.discordplugin.DPUtils;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.commands.DiscordCommandBase;
|
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
|
||||||
import buttondevteam.discordplugin.mcchat.MCChatCustom;
|
import buttondevteam.lib.architecture.Component;
|
||||||
import buttondevteam.discordplugin.mcchat.MCChatPrivate;
|
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import sx.blah.discord.api.events.Event;
|
|
||||||
import sx.blah.discord.api.events.IListener;
|
import sx.blah.discord.api.events.IListener;
|
||||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MentionEvent;
|
import sx.blah.discord.handle.impl.events.guild.channel.message.MentionEvent;
|
||||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||||
|
@ -17,103 +13,63 @@ import sx.blah.discord.handle.impl.events.guild.role.RoleCreateEvent;
|
||||||
import sx.blah.discord.handle.impl.events.guild.role.RoleDeleteEvent;
|
import sx.blah.discord.handle.impl.events.guild.role.RoleDeleteEvent;
|
||||||
import sx.blah.discord.handle.impl.events.guild.role.RoleUpdateEvent;
|
import sx.blah.discord.handle.impl.events.guild.role.RoleUpdateEvent;
|
||||||
import sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent;
|
import sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent;
|
||||||
import sx.blah.discord.handle.obj.IChannel;
|
|
||||||
import sx.blah.discord.handle.obj.IMessage;
|
|
||||||
import sx.blah.discord.handle.obj.StatusType;
|
import sx.blah.discord.handle.obj.StatusType;
|
||||||
import sx.blah.discord.util.EmbedBuilder;
|
import sx.blah.discord.util.EmbedBuilder;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class CommonListeners {
|
public class CommonListeners {
|
||||||
|
|
||||||
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali
|
/*private static ArrayList<Object> dcListeners=new ArrayList<>();
|
||||||
"Between now and the heat-death of the universe.", // Ghostise
|
|
||||||
"Soon™", "Ask again this time next month", // Ghostise
|
|
||||||
"In about 3 seconds", // Nicolai
|
|
||||||
"After we finish 8 plugins", // Ali
|
|
||||||
"Tomorrow.", // Ali
|
|
||||||
"After one tiiiny feature", // Ali
|
|
||||||
"Next commit", // Ali
|
|
||||||
"After we finish strangling Towny", // Ali
|
|
||||||
"When we kill every *fucking* bug", // Ali
|
|
||||||
"Once the server stops screaming.", // Ali
|
|
||||||
"After HL3 comes out", // Ali
|
|
||||||
"Next time you ask", // Ali
|
|
||||||
"When will *you* be open?" // Ali
|
|
||||||
};
|
|
||||||
|
|
||||||
private static final String[] serverReadyQuestions = new String[]{"when will the server be open",
|
public static void registerDiscordListener(DiscordListener listener) {
|
||||||
"when will the server be ready", "when will the server be done", "when will the server be complete",
|
//Step 1: Get all events that are handled by us
|
||||||
"when will the server be finished", "when's the server ready", "when's the server open",
|
//Step 2: Find methods that handle these
|
||||||
"Vhen vill ze server be open?"};
|
//...or just simply call the methods in the right order
|
||||||
|
|
||||||
private static final Random serverReadyRandom = new Random();
|
|
||||||
private static final ArrayList<Short> usableServerReadyStrings = new ArrayList<Short>(serverReadyStrings.length) {
|
|
||||||
private static final long serialVersionUID = 2213771460909848770L;
|
|
||||||
|
|
||||||
{
|
|
||||||
createUsableServerReadyStrings(this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private static void createUsableServerReadyStrings(ArrayList<Short> list) {
|
|
||||||
for (short i = 0; i < serverReadyStrings.length; i++)
|
|
||||||
list.add(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void callDiscordEvent(Event event) {
|
private static void callDiscordEvent(Event event) {
|
||||||
MCListener.callEventExcluding(new AsyncDiscordEvent(event), true, "DiscordPlugin");
|
String name=event.getClass().getSimpleName();
|
||||||
|
name=Character.toLowerCase(name.charAt(0))+name.substring(1);
|
||||||
|
for (Object listener : dcListeners) {
|
||||||
|
listener.getClass().getMethods(name, AsyncDiscordEvent.class);
|
||||||
}
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
private static long lasttime = 0;
|
private static long lasttime = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
MentionEvent:
|
||||||
|
- CommandListener (starts with mention, in #bot unless 'channelcon')
|
||||||
|
|
||||||
|
MessageReceivedEvent:
|
||||||
|
- Minecraft chat (is enabled in the channel and message isn't [/]mcchat)
|
||||||
|
- CommandListener (with the correct prefix in #bot, or in private)
|
||||||
|
*/
|
||||||
public static IListener<?>[] getListeners() {
|
public static IListener<?>[] getListeners() {
|
||||||
return new IListener[]{new IListener<MentionEvent>() {
|
return new IListener[]{new IListener<MentionEvent>() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(MentionEvent event) {
|
public void handle(MentionEvent event) {
|
||||||
if (DiscordPlugin.SafeMode)
|
if (DiscordPlugin.SafeMode)
|
||||||
return;
|
return;
|
||||||
callDiscordEvent(event);
|
|
||||||
if (event.getMessage().getAuthor().isBot())
|
if (event.getMessage().getAuthor().isBot())
|
||||||
return;
|
return;
|
||||||
final IChannel channel = event.getMessage().getChannel();
|
CommandListener.runCommand(event.getMessage(), true);
|
||||||
if (!channel.getStringID().equals(DiscordPlugin.botchannel.getStringID())
|
|
||||||
&& (!event.getMessage().getContent().contains("channelcon") || MCChatCustom.hasCustomChat(channel))) //Allow channelcon in other servers but avoid double handling when it's enabled
|
|
||||||
return;
|
|
||||||
if (channel.getStringID().equals(DiscordPlugin.chatchannel.getStringID()))
|
|
||||||
return; // The chat code already handles this - Right now while testing botchannel is the same as chatchannel
|
|
||||||
event.getMessage().getChannel().setTypingStatus(true); // Fun
|
|
||||||
runCommand(event.getMessage(), true);
|
|
||||||
}
|
}
|
||||||
}, new IListener<MessageReceivedEvent>() {
|
}, new IListener<MessageReceivedEvent>() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(MessageReceivedEvent event) {
|
public void handle(MessageReceivedEvent event) {
|
||||||
if (DiscordPlugin.SafeMode)
|
if (DiscordPlugin.SafeMode)
|
||||||
return;
|
return;
|
||||||
final String msglowercase = event.getMessage().getContent().toLowerCase();
|
|
||||||
if (!TBMCCoreAPI.IsTestServer()
|
|
||||||
&& Arrays.stream(serverReadyQuestions).anyMatch(s -> msglowercase.contains(s))) {
|
|
||||||
int next;
|
|
||||||
if (usableServerReadyStrings.size() == 0)
|
|
||||||
createUsableServerReadyStrings(usableServerReadyStrings);
|
|
||||||
next = usableServerReadyStrings.remove(serverReadyRandom.nextInt(usableServerReadyStrings.size()));
|
|
||||||
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), serverReadyStrings[next]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!event.getMessage().getChannel().isPrivate()
|
|
||||||
&& !(event.getMessage().getContent().startsWith("/")
|
|
||||||
&& event.getChannel().getStringID().equals(DiscordPlugin.botchannel.getStringID()))) //
|
|
||||||
return;
|
|
||||||
if (MCChatPrivate.isMinecraftChatEnabled(event.getAuthor().toString()))
|
|
||||||
if (!event.getMessage().getContent().equalsIgnoreCase("mcchat"))
|
|
||||||
return;
|
|
||||||
if (event.getMessage().getAuthor().isBot())
|
if (event.getMessage().getAuthor().isBot())
|
||||||
return;
|
return;
|
||||||
runCommand(event.getMessage(), false);
|
boolean handled = false;
|
||||||
|
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
|
||||||
|
if (mcchat != null && mcchat.isEnabled())
|
||||||
|
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event);
|
||||||
|
if (!handled)
|
||||||
|
handled = CommandListener.runCommand(event.getMessage(), false);
|
||||||
}
|
}
|
||||||
}, new IListener<sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent>() {
|
}, new IListener<sx.blah.discord.handle.impl.events.user.PresenceUpdateEvent>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -164,49 +120,6 @@ public class CommonListeners {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs a ChromaBot command.
|
|
||||||
*
|
|
||||||
* @param message The Discord message
|
|
||||||
* @param mentionedonly Only run the command if ChromaBot is mentioned at the start of the message
|
|
||||||
* @return Whether it ran the command (always true if mentionedonly is false)
|
|
||||||
*/
|
|
||||||
public static boolean runCommand(IMessage message, boolean mentionedonly) {
|
|
||||||
debug("A");
|
|
||||||
if (DiscordPlugin.SafeMode)
|
|
||||||
return true;
|
|
||||||
debug("B");
|
|
||||||
final StringBuilder cmdwithargs = new StringBuilder(message.getContent());
|
|
||||||
final String mention = DiscordPlugin.dc.getOurUser().mention(false);
|
|
||||||
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
|
|
||||||
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
|
|
||||||
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
|
|
||||||
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().filter(r -> DiscordPlugin.dc.getOurUser().hasRole(r)).map(r -> r.mention())::iterator)
|
|
||||||
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
|
|
||||||
debug("C");
|
|
||||||
if (mentionedonly && !gotmention) {
|
|
||||||
message.getChannel().setTypingStatus(false);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
debug("D");
|
|
||||||
message.getChannel().setTypingStatus(true);
|
|
||||||
String cmdwithargsString = cmdwithargs.toString().trim(); //Remove spaces between mention and command
|
|
||||||
int index = cmdwithargsString.indexOf(" ");
|
|
||||||
String cmd;
|
|
||||||
String args;
|
|
||||||
if (index == -1) {
|
|
||||||
cmd = cmdwithargsString;
|
|
||||||
args = "";
|
|
||||||
} else {
|
|
||||||
cmd = cmdwithargsString.substring(0, index);
|
|
||||||
args = cmdwithargsString.substring(index + 1).trim(); //In case there are multiple spaces
|
|
||||||
}
|
|
||||||
debug("E");
|
|
||||||
DiscordCommandBase.runCommand(cmd.toLowerCase(), args, message);
|
|
||||||
message.getChannel().setTypingStatus(false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean debug = false;
|
private static boolean debug = false;
|
||||||
|
|
||||||
public static void debug(String debug) {
|
public static void debug(String debug) {
|
||||||
|
@ -217,21 +130,4 @@ public class CommonListeners {
|
||||||
public static boolean debug() {
|
public static boolean debug() {
|
||||||
return debug = !debug;
|
return debug = !debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) {
|
|
||||||
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
|
|
||||||
if (cmdwithargs.length() > mention.length() + 1)
|
|
||||||
cmdwithargs.delete(0,
|
|
||||||
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length());
|
|
||||||
else
|
|
||||||
cmdwithargs.replace(0, cmdwithargs.length(), "help");
|
|
||||||
else {
|
|
||||||
if (cmdwithargs.length() > 0 && cmdwithargs.charAt(0) == '/')
|
|
||||||
cmdwithargs.deleteCharAt(0); //Don't treat / as mention, mentions can be used in public mcchat
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (cmdwithargs.length() == 0)
|
|
||||||
cmdwithargs.replace(0, cmdwithargs.length(), "help");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
|
public interface DiscordListener {
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
package buttondevteam.discordplugin.commands;
|
package buttondevteam.discordplugin.mcchat;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.DiscordPlayer;
|
import buttondevteam.discordplugin.DiscordPlayer;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.mcchat.MCChatPrivate;
|
import buttondevteam.discordplugin.commands.DiscordCommandBase;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import sx.blah.discord.handle.obj.IMessage;
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ public class MCChatCommand extends DiscordCommandBase {
|
||||||
return "mcchat";
|
return "mcchat";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override //TODO: Only register if module is enabled
|
||||||
public boolean run(IMessage message, String args) {
|
public boolean run(IMessage message, String args) {
|
||||||
if (!message.getChannel().isPrivate()) {
|
if (!message.getChannel().isPrivate()) {
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
|
@ -5,7 +5,6 @@ import buttondevteam.discordplugin.DPUtils;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.DiscordSender;
|
import buttondevteam.discordplugin.DiscordSender;
|
||||||
import buttondevteam.discordplugin.DiscordSenderBase;
|
import buttondevteam.discordplugin.DiscordSenderBase;
|
||||||
import buttondevteam.discordplugin.listeners.CommonListeners;
|
|
||||||
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
||||||
import buttondevteam.lib.TBMCChatEvent;
|
import buttondevteam.lib.TBMCChatEvent;
|
||||||
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
||||||
|
@ -22,7 +21,6 @@ import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
import sx.blah.discord.api.events.IListener;
|
|
||||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||||
import sx.blah.discord.handle.obj.IChannel;
|
import sx.blah.discord.handle.obj.IChannel;
|
||||||
|
@ -43,7 +41,7 @@ import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
public class MCChatListener implements Listener {
|
||||||
private BukkitTask sendtask;
|
private BukkitTask sendtask;
|
||||||
private LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>();
|
private LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>();
|
||||||
private Runnable sendrunnable;
|
private Runnable sendrunnable;
|
||||||
|
@ -231,27 +229,25 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
private Runnable recrun;
|
private Runnable recrun;
|
||||||
private static Thread recthread;
|
private static Thread recthread;
|
||||||
|
|
||||||
@Override // Discord - TODO: Call from the common listener
|
// Discord
|
||||||
public void handle(MessageReceivedEvent ev) {
|
public boolean handleDiscord(MessageReceivedEvent ev) {
|
||||||
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
|
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
|
||||||
return;
|
return false;
|
||||||
val author = ev.getMessage().getAuthor();
|
val author = ev.getMessage().getAuthor();
|
||||||
if (author.isBot())
|
|
||||||
return;
|
|
||||||
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getChannel());
|
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getChannel());
|
||||||
if (!ev.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
|
if (ev.getMessage().getChannel().getLongID() != DiscordPlugin.chatchannel.getLongID()
|
||||||
&& !(ev.getMessage().getChannel().isPrivate() && MCChatPrivate.isMinecraftChatEnabled(author.getStringID()))
|
&& !(ev.getMessage().getChannel().isPrivate() && MCChatPrivate.isMinecraftChatEnabled(author.getStringID()))
|
||||||
&& !hasCustomChat)
|
&& !hasCustomChat)
|
||||||
return;
|
return false; //Chat isn't enabled on this channel
|
||||||
if (ev.getMessage().getContent().equalsIgnoreCase("mcchat"))
|
if (hasCustomChat && ev.getMessage().getContent().length() < "/mcchat<>".length()
|
||||||
return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommonListeners)
|
&& ev.getMessage().getContent().replace("/", "")
|
||||||
if (CommonListeners.runCommand(ev.getMessage(), true))
|
.equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat
|
||||||
return;
|
return false; //Allow disabling the chat if needed
|
||||||
MCChatUtils.resetLastMessage(ev.getChannel());
|
MCChatUtils.resetLastMessage(ev.getChannel());
|
||||||
lastlist++;
|
lastlist++;
|
||||||
recevents.add(ev);
|
recevents.add(ev);
|
||||||
if (rectask != null)
|
if (rectask != null)
|
||||||
return;
|
return true;
|
||||||
recrun = () -> { //Don't return in a while loop next time
|
recrun = () -> { //Don't return in a while loop next time
|
||||||
recthread = Thread.currentThread();
|
recthread = Thread.currentThread();
|
||||||
processDiscordToMC();
|
processDiscordToMC();
|
||||||
|
@ -259,6 +255,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
|
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
|
||||||
};
|
};
|
||||||
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
|
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processDiscordToMC() {
|
private void processDiscordToMC() {
|
||||||
|
|
|
@ -3,13 +3,19 @@ package buttondevteam.discordplugin.mcchat;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.architecture.Component;
|
import buttondevteam.lib.architecture.Component;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
public class MinecraftChatModule extends Component {
|
public class MinecraftChatModule extends Component {
|
||||||
|
private @Getter MCChatListener listener;
|
||||||
|
|
||||||
|
public MCChatListener getListener() { //It doesn't want to generate
|
||||||
|
return listener;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void enable() {
|
protected void enable() {
|
||||||
MCChatListener mcchat = new MCChatListener();
|
listener = new MCChatListener();
|
||||||
DiscordPlugin.dc.getDispatcher().registerListener(mcchat);
|
DiscordPlugin.dc.getDispatcher().registerListener(listener);
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, getPlugin());
|
TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin());
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), getPlugin());
|
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), getPlugin());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue