From 9b5ae3371d11c0a2d80c4905f29b3d3551dca72b Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Sat, 14 Sep 2019 00:56:45 -0700 Subject: [PATCH] + Highly experimental last message info now saved --- src/commands/dew/CommandDew.java | 10 +- src/commands/dew/CommandDewCapture.java | 28 ++++ src/commands/dew/CommandDewInput.java | 5 +- src/commands/dew/CommandDewRealm.java | 93 +----------- src/commands/dew/core/data/DewMapTile.java | 2 - src/commands/dew/core/impl/DewCore.java | 163 ++++++++++++++++++++- src/commands/dew/core/impl/DewLuaCore.java | 1 - src/commands/music/SubCommandAddTrack.java | 4 - src/core/SubCommandFramework.java | 7 +- src/dataStructures/Response.java | 24 +-- src/dataStructures/UserInput.java | 21 +++ src/main/Main.java | 7 + src/main/Superintendent.java | 1 + 13 files changed, 252 insertions(+), 114 deletions(-) create mode 100644 src/commands/dew/CommandDewCapture.java diff --git a/src/commands/dew/CommandDew.java b/src/commands/dew/CommandDew.java index 9354dff..d81cef0 100644 --- a/src/commands/dew/CommandDew.java +++ b/src/commands/dew/CommandDew.java @@ -1,5 +1,6 @@ package commands.dew; +import commands.dew.core.impl.DewCore; import core.Command; import core.LocStrings; import core.SubCommandFramework; @@ -11,8 +12,10 @@ import dataStructures.KittyUser; import dataStructures.Response; import dataStructures.UserInput; -public class CommandDew extends Command { - +public class CommandDew extends Command +{ + @SuppressWarnings("unused") + public static DewCore core; SubCommandFramework framework = new SubCommandFramework(); @Override @@ -28,11 +31,14 @@ public class CommandDew extends Command { { super(level, rating); + core = new DewCore(framework); + framework.addCommand("luatest", new CommandDewLuaTest(KittyRole.Dev, KittyRating.Safe)); framework.addCommand("realm", new CommandDewRealm(KittyRole.Dev, KittyRating.Safe)); framework.addCommand("w", new CommandDewInput("w", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("a", new CommandDewInput("a", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("s", new CommandDewInput("s", KittyRole.Dev, KittyRating.Safe)); framework.addCommand("d", new CommandDewInput("d", KittyRole.Dev, KittyRating.Safe)); + framework.addCommand("capture", new CommandDewCapture(KittyRole.Dev, KittyRating.Safe)); } } diff --git a/src/commands/dew/CommandDewCapture.java b/src/commands/dew/CommandDewCapture.java new file mode 100644 index 0000000..0527eb4 --- /dev/null +++ b/src/commands/dew/CommandDewCapture.java @@ -0,0 +1,28 @@ +package commands.dew; + +import commands.dew.adapter.KittyAdapterDewPlayer; +import commands.dew.core.impl.DewCore; +import core.SubCommand; +import core.SubCommandFormattable; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRating; +import dataStructures.KittyRole; +import dataStructures.KittyUser; +import dataStructures.UserInput; + +public class CommandDewCapture extends SubCommand +{ + public CommandDewCapture(KittyRole roleLevel, KittyRating contentRating) + { + super(roleLevel, contentRating); + } + + @Override + public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) + { + KittyAdapterDewPlayer player = DewCore.getOrCreate(user); + DewCore.playersCaptured.add(player); + return null;//new SubCommandFormattable("captured input for " + user.name); + } +} diff --git a/src/commands/dew/CommandDewInput.java b/src/commands/dew/CommandDewInput.java index 52e3f8a..ff213b3 100644 --- a/src/commands/dew/CommandDewInput.java +++ b/src/commands/dew/CommandDewInput.java @@ -1,6 +1,7 @@ package commands.dew; import commands.dew.adapter.KittyAdapterDewPlayer; +import commands.dew.core.impl.DewCore; import core.SubCommand; import core.SubCommandFormattable; import dataStructures.KittyChannel; @@ -22,7 +23,7 @@ public class CommandDewInput extends SubCommand @Override public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) { - KittyAdapterDewPlayer player = CommandDewRealm.getOrCreate(user); + KittyAdapterDewPlayer player = DewCore.getOrCreate(user); int x = player.getRealmX(); int y = player.getRealmY(); @@ -35,6 +36,6 @@ public class CommandDewInput extends SubCommand case "d": player.setRealmPos(x + 1, y); break; } - return new SubCommandFormattable(CommandDewRealm.drawWorld(player)); + return new SubCommandFormattable(DewCore.drawWorld(player)); } } diff --git a/src/commands/dew/CommandDewRealm.java b/src/commands/dew/CommandDewRealm.java index fd7ae0e..dddc563 100644 --- a/src/commands/dew/CommandDewRealm.java +++ b/src/commands/dew/CommandDewRealm.java @@ -1,11 +1,7 @@ package commands.dew; -import java.util.ArrayList; -import java.util.List; - import commands.dew.adapter.KittyAdapterDewPlayer; -import commands.dew.core.data.DewMapTile; -import commands.dew.core.data.DewRealm; +import commands.dew.core.impl.DewCore; import core.SubCommand; import core.SubCommandFormattable; import dataStructures.KittyChannel; @@ -17,100 +13,17 @@ import dataStructures.UserInput; public class CommandDewRealm extends SubCommand { - // TODO: CHANGE THIS! THIS IS TEMP STATIC, DO NOT CONTINUE TO DO THIS - private static DewRealm realm; - public static List players; - public CommandDewRealm(KittyRole roleLevel, KittyRating contentRating) { super(roleLevel, contentRating); - - realm = new DewRealm(); - players = new ArrayList(); - - for(int y = 0; y < realm.map.length; ++y) - { - for(int x = 0; x < realm.map[y].length; ++x) - { - realm.map[y][x] = DewMapTile.Grass.getValue(); - } - } - } - - public static KittyAdapterDewPlayer getOrCreate(KittyUser user) - { - for(KittyAdapterDewPlayer player : players) - { - if(player.getUniqueID() == user.uniqueID) - { - return player; - } - } - - KittyAdapterDewPlayer player = new KittyAdapterDewPlayer(realm, user, 10, 10); - players.add(player); - return player; - } - - public static String drawWorld(KittyAdapterDewPlayer player) - { - // Build empty world buffer - String[][] worldBuffer = new String[realm.map.length][realm.map[0].length]; - - // Build world - for(int y = 0; y < realm.map.length; ++y) - { - for(int x = 0; x < realm.map[y].length; ++x) - { - switch(DewMapTile.values()[realm.map[y][x]]) - { - case Grass: - worldBuffer[y][x] = ","; - break; - - default: - worldBuffer[y][x] = " "; - break; - } - } - } - - // Build players - for(KittyAdapterDewPlayer p : players) - { - worldBuffer[p.getRealmY()][p.getRealmX()] = p.getVisual(); - } - - // Draw world - String out = ""; - out += "**Realm ID - ** `" + realm.id + "`\n"; - - for(int y = 0; y < worldBuffer.length; ++y) - { - out += "`"; - - for(int x = 0; x < worldBuffer[y].length; ++x) - { - out += worldBuffer[y][x]; - } - - out += "`"; - out += "\n"; - } - - out += "**Stats**\n"; - out += "`Player Name` - `" + player.getName() + "`\n"; - out += "`Player Icon` - `" + player.getVisual() + "`\n"; - - return out; } @Override public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) { - KittyAdapterDewPlayer player = getOrCreate(user); + KittyAdapterDewPlayer player = DewCore.getOrCreate(user); - String out = drawWorld(player); + String out = DewCore.drawWorld(player); return new SubCommandFormattable(out); } diff --git a/src/commands/dew/core/data/DewMapTile.java b/src/commands/dew/core/data/DewMapTile.java index ea302c1..cc674ae 100644 --- a/src/commands/dew/core/data/DewMapTile.java +++ b/src/commands/dew/core/data/DewMapTile.java @@ -1,7 +1,5 @@ package commands.dew.core.data; -import commands.dew.core.api.IDewLuaSerializable; - public enum DewMapTile { // Assign multiples of 2 for flag purposes diff --git a/src/commands/dew/core/impl/DewCore.java b/src/commands/dew/core/impl/DewCore.java index d169b0f..a6c811e 100644 --- a/src/commands/dew/core/impl/DewCore.java +++ b/src/commands/dew/core/impl/DewCore.java @@ -1,11 +1,170 @@ package commands.dew.core.impl; -import commands.dew.core.api.IDewPlayer; +import java.util.ArrayList; +import java.util.List; + +import commands.dew.adapter.KittyAdapterDewPlayer; +import commands.dew.core.data.DewMapTile; +import commands.dew.core.data.DewRealm; +import core.SubCommandFormattable; +import core.SubCommandFramework; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyUser; +import dataStructures.Response; +import dataStructures.UserInput; +import utils.GlobalLog; +import utils.LogFilter; public class DewCore { - public DewCore(IDewPlayer player) + // TODO: Make realm and players both per-realm + public static DewRealm realm; + public static List players; + public static List playersCaptured; + private static SubCommandFramework framework; + + public DewCore(SubCommandFramework framework) { + DewCore.framework = framework; + DewCore.realm = new DewRealm(); + DewCore.players = new ArrayList(); + DewCore.playersCaptured = new ArrayList(); + for(int y = 0; y < realm.map.length; ++y) + { + for(int x = 0; x < realm.map[y].length; ++x) + { + realm.map[y][x] = DewMapTile.Grass.getValue(); + } + } + } + + // Returns false if not captured, true if captured. + public static boolean ProcessIfCaptured(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput baseInput, Response res) + { + // Search for a captured player. + KittyAdapterDewPlayer player = null; + for(KittyAdapterDewPlayer p : playersCaptured) + { + if(p.getUniqueID() == user.uniqueID) + { + player = p; + break; + } + } + + // If we didn't find that the person is captured, return false since we don't need to do anything. + if(player == null) + return false; + + // See if we run anything - also, check to see if we need to exit. + boolean didRun = false; + try + { + UserInput toEdit = new UserInput(baseInput); + String key = toEdit.message.toLowerCase().trim(); + toEdit.key = key; + toEdit.args = key; + switch(key) + { + case "cap": + case "capture": + case "uncapture": + case "stop": + case "quit": + case "exit": + playersCaptured.remove(player); + return false; + } + + SubCommandFormattable f = framework.run(guild, channel, user, toEdit); + + if(f != null) + { + Response.LastSendMessage.editMessage(f.resString).complete(); + //f.Call(res); + didRun = true; + } + } + catch(Exception e) + { + GlobalLog.error(LogFilter.Command, e); + } + + if(didRun) + { + baseInput.deleteOriginInput(); + } + + return didRun; + } + + public static KittyAdapterDewPlayer getOrCreate(KittyUser user) + { + for(KittyAdapterDewPlayer player : players) + { + if(player.getUniqueID() == user.uniqueID) + { + return player; + } + } + + KittyAdapterDewPlayer player = new KittyAdapterDewPlayer(realm, user, 10, 10); + players.add(player); + return player; + } + + public static String drawWorld(KittyAdapterDewPlayer player) + { + // Build empty world buffer + String[][] worldBuffer = new String[realm.map.length][realm.map[0].length]; + + // Build world + for(int y = 0; y < realm.map.length; ++y) + { + for(int x = 0; x < realm.map[y].length; ++x) + { + switch(DewMapTile.values()[realm.map[y][x]]) + { + case Grass: + worldBuffer[y][x] = ","; + break; + + default: + worldBuffer[y][x] = " "; + break; + } + } + } + + // Build players + for(KittyAdapterDewPlayer p : players) + { + worldBuffer[p.getRealmY()][p.getRealmX()] = p.getVisual(); + } + + // Draw world + String out = ""; + out += "**Realm ID - ** `" + realm.id + "`\n"; + + for(int y = 0; y < worldBuffer.length; ++y) + { + out += "`"; + + for(int x = 0; x < worldBuffer[y].length; ++x) + { + out += worldBuffer[y][x]; + } + + out += "`"; + out += "\n"; + } + + out += "**Stats**\n"; + out += "`Player Name` - `" + player.getName() + "`\n"; + out += "`Player Icon` - `" + player.getVisual() + "`\n"; + + return out; } } diff --git a/src/commands/dew/core/impl/DewLuaCore.java b/src/commands/dew/core/impl/DewLuaCore.java index aa19d10..208698a 100644 --- a/src/commands/dew/core/impl/DewLuaCore.java +++ b/src/commands/dew/core/impl/DewLuaCore.java @@ -1,7 +1,6 @@ package commands.dew.core.impl; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import org.luaj.vm2.Globals; import org.luaj.vm2.LuaValue; diff --git a/src/commands/music/SubCommandAddTrack.java b/src/commands/music/SubCommandAddTrack.java index 1c19498..84c6a55 100644 --- a/src/commands/music/SubCommandAddTrack.java +++ b/src/commands/music/SubCommandAddTrack.java @@ -1,7 +1,5 @@ package commands.music; -import com.google.api.services.youtube.YouTube; - import core.SubCommand; import core.SubCommandFormattable; import dataStructures.KittyChannel; @@ -13,13 +11,11 @@ import dataStructures.UserInput; public class SubCommandAddTrack extends SubCommand { - public SubCommandAddTrack(KittyRole level, KittyRating rating) { super(level, rating); } @Override public SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input) { - return null; } } \ No newline at end of file diff --git a/src/core/SubCommandFramework.java b/src/core/SubCommandFramework.java index b4277c1..b3d746e 100644 --- a/src/core/SubCommandFramework.java +++ b/src/core/SubCommandFramework.java @@ -7,6 +7,7 @@ import dataStructures.KittyGuild; import dataStructures.KittyUser; import dataStructures.UserInput; import utils.GlobalLog; +import utils.LogFilter; public class SubCommandFramework { @@ -41,10 +42,14 @@ public class SubCommandFramework } catch(Exception e) { + GlobalLog.error(LogFilter.Command, e); + String help = Stats.instance.getHelpText(input.key); + if(help != null) return new SubCommandFormattable(help); - return new SubCommandFormattable("Something has gone very wrong."); + + return new SubCommandFormattable("Whoops! Internal subcommand error!"); } } } \ No newline at end of file diff --git a/src/dataStructures/Response.java b/src/dataStructures/Response.java index 8e8ba10..a2f5f1d 100644 --- a/src/dataStructures/Response.java +++ b/src/dataStructures/Response.java @@ -5,6 +5,7 @@ import java.io.InputStream; import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.MessageBuilder; +import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.events.message.guild.*; import net.dv8tion.jda.core.EmbedBuilder; @@ -16,6 +17,9 @@ import utils.LogFilter; // commands aren't exposed to the GuildMessageReceivedEvent then we should be fine. public class Response { + // Last message + public static Message LastSendMessage = null; + // Variables private GuildMessageReceivedEvent event; private final int discordMessageMax = 2000; @@ -75,7 +79,7 @@ public class Response File thumbImage = new File(thumbName); if(thumbImage.exists()) { - event.getChannel().sendFile(thumbImage, thumbName, message.build()).queue(); + LastSendMessage = event.getChannel().sendFile(thumbImage, thumbName, message.build()).complete(); } } else @@ -83,7 +87,7 @@ public class Response if(embedInfo.thumbnailURL != null) embed.setThumbnail(embedInfo.thumbnailURL); - event.getChannel().sendMessage(embed.build()).queue(); + LastSendMessage = event.getChannel().sendMessage(embed.build()).complete(); } } @@ -93,11 +97,11 @@ public class Response GlobalLog.log(LogFilter.Response, "Sending response: " + toRespondWith); if(toRespondWith.length() > discordMessageMax) { - event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").queue(); + LastSendMessage = event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").complete(); } else { - event.getChannel().sendMessage(toRespondWith).queue(); + LastSendMessage = event.getChannel().sendMessage(toRespondWith).complete(); } } @@ -108,11 +112,11 @@ public class Response channel = kitty.getTextChannelById(Long.parseLong(channelID)); if(toRespondWith.length() > discordMessageMax) { - channel.sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").queue(); + LastSendMessage = channel.sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").complete(); } else { - channel.sendMessage(toRespondWith).queue(); + LastSendMessage = channel.sendMessage(toRespondWith).complete(); } } @@ -122,11 +126,11 @@ public class Response GlobalLog.log(LogFilter.Response, "Sending immediate response: " + toRespondWith); if(toRespondWith.length() > discordMessageMax) { - event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!"); + LastSendMessage = event.getChannel().sendMessage(toRespondWith.substring(0, kittyMessageMax) + "\n\nI think that's enough!").complete(); } else { - event.getChannel().sendMessage(toRespondWith).complete(); + LastSendMessage = event.getChannel().sendMessage(toRespondWith).complete(); } } @@ -134,13 +138,13 @@ public class Response public void sendFile(File toRespondWith, String extension) { GlobalLog.log(LogFilter.Response, "Sending file response"); - event.getChannel().sendFile(toRespondWith, "return." + extension).queue(); + LastSendMessage = event.getChannel().sendFile(toRespondWith, "return." + extension).complete(); } // Queues an input stream response to the channel that issued the command. public void sendFile(InputStream in, String extension) { GlobalLog.log(LogFilter.Response, "Sending input stream response"); - event.getChannel().sendFile(in, "return." + extension).queue(); + LastSendMessage = event.getChannel().sendFile(in, "return." + extension).complete(); } } diff --git a/src/dataStructures/UserInput.java b/src/dataStructures/UserInput.java index 328d27a..133079d 100644 --- a/src/dataStructures/UserInput.java +++ b/src/dataStructures/UserInput.java @@ -4,6 +4,7 @@ import java.util.List; import core.ObjectBuilderFactory; import net.dv8tion.jda.core.entities.Member; +import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; public class UserInput @@ -14,6 +15,7 @@ public class UserInput public KittyUser [] mentions; private boolean isValid; public String message; + private Message internalJDAMessage; // NOTE(wisp): Designed to parse a string as it's constructed. // If a user enters the command "!ping some stuff here :D" @@ -25,6 +27,7 @@ public class UserInput // The CommandIndicator and spaces between the key and args are dropped. public UserInput(GuildMessageReceivedEvent event, KittyGuild guildContext) { + this.internalJDAMessage = event.getMessage(); String toParse = event.getMessage().getContentRaw(); message = toParse; @@ -61,6 +64,18 @@ public class UserInput } } + // Copy Constructor + public UserInput(UserInput other) + { + this.key = other.key; + this.args = other.args; + this.mentions = other.mentions; + this.isValid = other.isValid; + this.message = other.message; + this.internalJDAMessage = other.internalJDAMessage; + } + + // Used for injecting direct commands from plugins public UserInput(String input, KittyGuild contextGuild) { @@ -75,6 +90,12 @@ public class UserInput return isValid; } + // This deletes the input that was originally sent + public void deleteOriginInput() + { + internalJDAMessage.delete().queue(); + } + // Finds first whitespace in the string private int findFirstWhitespace(String str) { diff --git a/src/main/Main.java b/src/main/Main.java index 06b562b..d9bea61 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -4,6 +4,7 @@ import java.util.List; import javax.security.auth.login.LoginException; +import commands.dew.core.impl.DewCore; import core.CharacterManager; import core.CommandEnabler; import core.CommandManager; @@ -89,6 +90,10 @@ public class Main extends ListenerAdapter if(!Superintendent.perCommandUpkeepPre(pluginManager)) return; + // TODO: Condense outer if somehow... + if(!DewCore.ProcessIfCaptured(guild, channel, user, input, response)) + { + // Attempt to spin up a command. If the command doesn't exist. // Run plugins right before invoking the commands but after all other setup. if(commandManager.invokeOnNewThread(guild, channel, user, input, response) == false) @@ -111,6 +116,8 @@ public class Main extends ListenerAdapter } } + } + // Run any upkeep in post we need to if(!Superintendent.perCommandUpkeepPost(kittyCore, databaseManager)) return; diff --git a/src/main/Superintendent.java b/src/main/Superintendent.java index 427b02f..b8b5244 100644 --- a/src/main/Superintendent.java +++ b/src/main/Superintendent.java @@ -2,6 +2,7 @@ package main; import java.util.concurrent.atomic.AtomicInteger; +import commands.dew.core.impl.DewCore; import core.Config; import core.DatabaseManager; import core.RPManager;