From 0a4c858b06571a42da4226a9daa61848a4e91e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Gla=C3=9F?= Date: Mon, 26 Apr 2021 19:29:23 +0200 Subject: [PATCH] Used consumer interface --- .../command/YoshiCommandDistributor.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java index 9e01090..a805ae3 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java @@ -5,6 +5,8 @@ import de.yannicpunktdee.yoshibot.command.commands.*; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; 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 @@ -13,7 +15,7 @@ import java.util.Map; * @author Yannic Link */ public class YoshiCommandDistributor { - + public static final String COMMAND_HELP = "help"; public static final String COMMAND_JOKE = "joke"; 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_BONK = "bonk"; public static final String COMMAND_WIKIPEDIA = "wikipedia"; - - private static final Map> 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> 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; } - Class commandClass = commands.get(context.getAction()); - if(commandClass == null){ + if (!commands.containsKey(context.getAction())) { context.getEvent().getTextChannel().sendMessage("Diese Aktion existiert nicht.").queue(); return; } try { - ((YoshiCommand) commandClass.getConstructor(YoshiCommandContext.class).newInstance(context)).execute(); + commands.get(context.getAction()).accept(context); } catch (Exception e) { context.getEvent().getTextChannel().sendMessage("Konnte Aktion nicht zuordnen.").queue(); - return; } }