Add JDA Implementation - untested - not ready yet!

Hoping @dumptruckman can finish this up
This commit is contained in:
Aikar
2018-01-22 22:03:22 -05:00
parent 77751ad906
commit 8667970216
14 changed files with 482 additions and 0 deletions
+2
View File
@@ -12,6 +12,7 @@
<module name="acf-bungee" />
<module name="acf-core" />
<module name="acf-example" />
<module name="acf-jda" />
<module name="acf-paper" />
<module name="acf-sponge" />
<module name="commands" />
@@ -24,6 +25,7 @@
<module name="acf-bungee" target="1.8" />
<module name="acf-core" target="1.8" />
<module name="acf-example" target="1.8" />
<module name="acf-jda" target="1.8" />
<module name="acf-paper" target="1.8" />
<module name="acf-parent" target="1.8" />
<module name="acf-sponge" target="1.8" />
+1
View File
@@ -6,6 +6,7 @@
<file url="file://$PROJECT_DIR$/bungee" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/example" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jda" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/paper" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/sponge" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
+93
View File
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
~
~ Permission is hereby granted, free of charge, to any person obtaining
~ a copy of this software and associated documentation files (the
~ "Software"), to deal in the Software without restriction, including
~ without limitation the rights to use, copy, modify, merge, publish,
~ distribute, sublicense, and/or sell copies of the Software, and to
~ permit persons to whom the Software is furnished to do so, subject to
~ the following conditions:
~
~ The above copyright notice and this permission notice shall be
~ included in all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
~ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
~ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
~ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
~ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
~ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
~ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>co.aikar</groupId>
<artifactId>acf-parent</artifactId>
<version><!--VERSION-->0.5.0-SNAPSHOT<!--VERSION--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>acf-jda</artifactId>
<version><!--VERSION-->0.5.0-SNAPSHOT<!--VERSION--></version>
<name>ACF (JDA)</name>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>co.aikar</groupId>
<artifactId>acf-core</artifactId>
<version><!--VERSION-->0.5.0-SNAPSHOT<!--VERSION--></version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.5.0_327</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
<!-- when downloading via Maven we can pull depends individually -->
<shadedArtifactAttached>true</shadedArtifactAttached>
<relocations>
</relocations>
</configuration>
</plugin>
</plugins>
<resources>
<!--resource>
<directory>${project.basedir}/../languages/minecraft/</directory>
</resource-->
</resources>
</build>
</project>
@@ -0,0 +1,50 @@
package co.aikar.commands;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
public class CommandEvent implements CommandIssuer {
private MessageReceivedEvent event;
private JDACommandManager manager;
public CommandEvent(JDACommandManager manager, MessageReceivedEvent event) {
this.manager = manager;
this.event = event;
}
@Override
public MessageReceivedEvent getIssuer() {
return event;
}
@Override
public CommandManager getManager() {
return this.manager;
}
@Override
public boolean isPlayer() {
return false;
}
@Override
public boolean hasPermission(String permission) {
// TODO: Permission Resolving
return false;
}
@Override
public void sendMessageInternal(String message) {
this.event.getChannel().sendMessage(message).queue();
}
public void sendMessage(Message message) {
this.event.getChannel().sendMessage(message).queue();
}
public void sendMessage(MessageEmbed message) {
this.event.getChannel().sendMessage(message).queue();
}
}
@@ -0,0 +1,41 @@
package co.aikar.commands;
import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class JDACommandCompletions extends CommandCompletions<CommandCompletionContext<?>> {
private boolean initialized;
public JDACommandCompletions(CommandManager manager) {
super(manager);
this.initialized = true;
}
@Override
public CommandCompletionHandler registerCompletion(String id, CommandCompletionHandler<CommandCompletionContext<?>> handler) {
if (initialized) {
throw new UnsupportedOperationException("JDA Doesn't support Command Completions");
}
return null;
}
@Override
public CommandCompletionHandler registerAsyncCompletion(String id, AsyncCommandCompletionHandler<CommandCompletionContext<?>> handler) {
if (initialized) {
throw new UnsupportedOperationException("JDA Doesn't support Command Completions");
}
return null;
}
@NotNull
@Override
List<String> of(RegisteredCommand command, CommandIssuer sender, String[] completionInfo, String[] args, boolean isAsync) {
return ImmutableList.of();
}
@Override
List<String> getCompletionValues(RegisteredCommand command, CommandIssuer sender, String completion, String[] args, boolean isAsync) {
return ImmutableList.of();
}
}
@@ -0,0 +1,7 @@
package co.aikar.commands;
public class JDACommandContexts extends CommandContexts<JDACommandExecutionContext> {
JDACommandContexts(CommandManager manager) {
super(manager);
}
}
@@ -0,0 +1,11 @@
package co.aikar.commands;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
public class JDACommandExecutionContext extends CommandExecutionContext<JDACommandExecutionContext,CommandEvent> {
JDACommandExecutionContext(RegisteredCommand cmd, Parameter param, CommandEvent sender, List<String> args, int index, Map<String, Object> passedArgs) {
super(cmd, param, sender, args, index, passedArgs);
}
}
@@ -0,0 +1,170 @@
package co.aikar.commands;
import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
import com.google.common.collect.Maps;
import jdk.nashorn.internal.ir.IfNode;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JDACommandManager extends CommandManager<
MessageReceivedEvent,
CommandEvent,
String,
MessageFormatter<String>,
JDACommandExecutionContext,
JDAConditionContext
> {
private final JDA jda;
private String startsWith;
private Logger logger;
protected JDACommandCompletions completions;
protected JDACommandContexts contexts;
protected JDALocales locales;
protected Map<String, JDARootCommand> commands = Maps.newHashMap();
public JDACommandManager(JDA jda) {
this(jda, "!");
}
public JDACommandManager(JDA jda, String startsWith) {
this.jda = jda;
jda.addEventListener(new JDAListener(this));
this.startsWith = startsWith;
this.completions = new JDACommandCompletions(this);
this.logger = Logger.getLogger(this.getClass().getSimpleName());
}
public JDA getJDA() {
return jda;
}
public Logger getLogger() {
return logger;
}
public void setLogger(Logger logger) {
this.logger = logger;
}
public String getStartsWith() {
return startsWith;
}
public void setStartsWith(String startsWith) {
this.startsWith = startsWith;
}
@Override
public CommandContexts<?> getCommandContexts() {
if (this.contexts == null) {
this.contexts = new JDACommandContexts(this);
}
return null;
}
@Override
public CommandCompletions<?> getCommandCompletions() {
return this.completions;
}
@Override
public void registerCommand(BaseCommand command) {
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String commandName = entry.getKey().toLowerCase();
JDARootCommand cmd = (JDARootCommand) entry.getValue();
if (!cmd.isRegistered) {
cmd.isRegistered = true;
commands.put(commandName, cmd);
}
}
}
@Override
public boolean hasRegisteredCommands() {
return !this.commands.isEmpty();
}
@Override
public boolean isCommandIssuer(Class<?> type) {
return CommandEvent.class.isAssignableFrom(type);
}
@Override
public CommandEvent getCommandIssuer(Object issuer) {
if (!(issuer instanceof MessageReceivedEvent)) {
throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Message Received Event.");
}
return new CommandEvent(this, (MessageReceivedEvent) issuer);
}
@Override
public RootCommand createRootCommand(String cmd) {
return new JDARootCommand(this, cmd);
}
@Override
public Locales getLocales() {
if (this.locales == null) {
this.locales = new JDALocales(this);
this.locales.loadLanguages();
}
return null;
}
@Override
public CommandExecutionContext createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
return new JDACommandExecutionContext(command, parameter, (CommandEvent) sender, args, i, passedArgs);
}
@Override
public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
// Not really going to be used;
//noinspection unchecked
return new CommandCompletionContext(command, sender, input, config, args);
}
@Override
public void log(LogLevel level, String message, Throwable throwable) {
Level logLevel = level == LogLevel.INFO ? Level.INFO : Level.SEVERE;
logger.log(logLevel, LogLevel.LOG_PREFIX + message);
if (throwable != null) {
for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
logger.log(logLevel, LogLevel.LOG_PREFIX + line);
}
}
}
void dispatchEvent(MessageReceivedEvent event) {
Message message = event.getMessage();
String msg = message.getContentDisplay();
if (!msg.startsWith(this.startsWith)) {
return;
}
String[] args = ACFPatterns.SPACE.split(msg.substring(1), -1);
if (args.length == 0) {
return;
}
String cmd = args[0].toLowerCase();
JDARootCommand rootCommand = this.commands.get(cmd);
if (rootCommand == null) {
return;
}
if (args.length > 1) {
args = Arrays.copyOfRange(args, 1, args.length);
} else {
args = new String[0];
}
rootCommand.execute(this.getCommandIssuer(event), cmd, args);
}
}
@@ -0,0 +1,7 @@
package co.aikar.commands;
public class JDAConditionContext extends ConditionContext<CommandEvent> {
JDAConditionContext(CommandEvent issuer, String config) {
super(issuer, config);
}
}
@@ -0,0 +1,33 @@
package co.aikar.commands;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
public class JDAListener extends ListenerAdapter {
private final JDACommandManager manager;
JDAListener(JDACommandManager manager) {
this.manager = manager;
}
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
super.onGuildMessageReceived(event);
}
@Override
public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) {
super.onPrivateMessageReceived(event);
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.isFromType(ChannelType.TEXT) || event.isFromType(ChannelType.PRIVATE)) {
this.manager.dispatchEvent(event);
}
}
}
@@ -0,0 +1,7 @@
package co.aikar.commands;
public class JDALocales extends Locales {
public JDALocales(CommandManager manager) {
super(manager);
}
}
@@ -0,0 +1,8 @@
package co.aikar.commands;
public class JDAMessageFormatter extends MessageFormatter<String> {
@Override
String format(String color, String message) {
return message;
}
}
@@ -0,0 +1,51 @@
package co.aikar.commands;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import java.util.ArrayList;
import java.util.List;
public class JDARootCommand implements RootCommand {
private JDACommandManager manager;
private final String name;
private BaseCommand defCommand;
private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
private List<BaseCommand> children = new ArrayList<>();
boolean isRegistered = false;
JDARootCommand(JDACommandManager manager, String name) {
this.manager = manager;
this.name = name;
}
@Override
public void addChild(BaseCommand command) {
if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
this.defCommand = command;
}
addChildShared(this.children, this.subCommands, command);
}
@Override
public CommandManager getManager() {
return this.manager;
}
@Override
public SetMultimap<String, RegisteredCommand> getSubCommands() {
return this.subCommands;
}
@Override
public List<BaseCommand> getChildren() {
return this.children;
}
@Override
public String getCommandName() {
return this.name;
}
}
+1
View File
@@ -147,5 +147,6 @@
<module>paper</module>
<module>bungee</module>
<module>sponge</module>
<module>jda</module>
</modules>
</project>