Browse Source

Used consumer interface

pull/2/head
Paul Glaß 4 years ago
parent
commit
0a4c858b06
1 changed files with 18 additions and 18 deletions
  1. +18
    -18
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java

+ 18
- 18
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java View File

@ -5,6 +5,8 @@ import de.yannicpunktdee.yoshibot.command.commands.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/** /**
* Unterscheidet nach der spezifizierten Action welche YoshiCommand-Kindklasse zum Ausführen des Kommandos verwendet * Unterscheidet nach der spezifizierten Action welche YoshiCommand-Kindklasse zum Ausführen des Kommandos verwendet
@ -13,7 +15,7 @@ import java.util.Map;
* @author Yannic Link * @author Yannic Link
*/ */
public class YoshiCommandDistributor { public class YoshiCommandDistributor {
public static final String COMMAND_HELP = "help"; public static final String COMMAND_HELP = "help";
public static final String COMMAND_JOKE = "joke"; public static final String COMMAND_JOKE = "joke";
public static final String COMMAND_SAY = "say"; public static final String COMMAND_SAY = "say";
@ -22,19 +24,19 @@ public class YoshiCommandDistributor {
public static final String COMMAND_PAT = "pat"; public static final String COMMAND_PAT = "pat";
public static final String COMMAND_BONK = "bonk"; public static final String COMMAND_BONK = "bonk";
public static final String COMMAND_WIKIPEDIA = "wikipedia"; public static final String COMMAND_WIKIPEDIA = "wikipedia";
private static final Map<String, Class<?>> commands = new HashMap<>();
public static void init(){
commands.put(COMMAND_HELP, HelpCommand.class);
commands.put(COMMAND_JOKE, JokeCommand.class);
commands.put(COMMAND_SAY, SayCommand.class);
commands.put(COMMAND_PLAY, PlayCommand.class);
commands.put(COMMAND_SAUCE, SauceCommand.class);
commands.put(COMMAND_PAT, PatCommand.class);
commands.put(COMMAND_BONK, BonkCommand.class);
commands.put(COMMAND_WIKIPEDIA, WikipediaCommand.class);
private static final Map<String, Consumer<YoshiCommandContext>> commands = new HashMap<>();
public static void init() {
commands.put(COMMAND_HELP, HelpCommand::new);
commands.put(COMMAND_JOKE, JokeCommand::new);
commands.put(COMMAND_SAY, SayCommand::new);
commands.put(COMMAND_PLAY, PlayCommand::new);
commands.put(COMMAND_SAUCE, SauceCommand::new);
commands.put(COMMAND_PAT, PatCommand::new);
commands.put(COMMAND_BONK, BonkCommand::new);
commands.put(COMMAND_WIKIPEDIA, WikipediaCommand::new);
} }
/** /**
@ -63,16 +65,14 @@ public class YoshiCommandDistributor {
break; break;
} }
Class<?> commandClass = commands.get(context.getAction());
if(commandClass == null){
if (!commands.containsKey(context.getAction())) {
context.getEvent().getTextChannel().sendMessage("Diese Aktion existiert nicht.").queue(); context.getEvent().getTextChannel().sendMessage("Diese Aktion existiert nicht.").queue();
return; return;
} }
try { try {
((YoshiCommand) commandClass.getConstructor(YoshiCommandContext.class).newInstance(context)).execute();
commands.get(context.getAction()).accept(context);
} catch (Exception e) { } catch (Exception e) {
context.getEvent().getTextChannel().sendMessage("Konnte Aktion nicht zuordnen.").queue(); context.getEvent().getTextChannel().sendMessage("Konnte Aktion nicht zuordnen.").queue();
return;
} }
} }


Loading…
Cancel
Save