diff --git a/src/core/SubCommand.java b/src/core/SubCommand.java new file mode 100644 index 0000000..c490c06 --- /dev/null +++ b/src/core/SubCommand.java @@ -0,0 +1,64 @@ +package core; + +import dataStructures.*; +import utils.GlobalLog; +import utils.LogFilter; + +public abstract class SubCommand +{ + private KittyRole roleLevel; + private KittyRating contentRating; + + + public SubCommand(KittyRole roleLevel, KittyRating contentRating) + { + this.roleLevel = roleLevel; + this.contentRating = contentRating; + } + + private void Reject(KittyUser user, String reason) + { + GlobalLog.Warn(LogFilter.Command, this.getClass().getSimpleName() + " from " + user.name + " rejected due to command's " + reason); + } + + private boolean CanCall(KittyGuild guild, KittyChannel channel, KittyUser user) + { + if(guild.contentRating.getValue() < contentRating.getValue()) + { + Reject(user, "content rating"); + return false; + } + + //TODO: ADD CHANNEL CHECK HERE + + if(user.GetRole().getValue() >= roleLevel.getValue()) + { + return true; + } + + Reject(user, "permissions"); + return false; + } + + // Called by the Command manager - this will run the command + // if the issuing user has the permission to do so! + protected final void Invoke(KittyGuild guild, KittyChannel channel, KittyUser user, String input) + { + if(!CanCall(guild, channel, user)) + return; + + OnRun(guild, channel, user, input); + } + + public KittyRating Rating() + { + return contentRating; + } + + public KittyRole RequiredRole() + { + return roleLevel; + } + + public abstract SubCommandFormattable OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, String input); +} \ No newline at end of file diff --git a/src/core/SubCommandFormattable.java b/src/core/SubCommandFormattable.java new file mode 100644 index 0000000..6507847 --- /dev/null +++ b/src/core/SubCommandFormattable.java @@ -0,0 +1,40 @@ +package core; + +import dataStructures.KittyEmbed; +import dataStructures.Response; + +// Only one isn't null! +public class SubCommandFormattable +{ + public final KittyEmbed resEmbed; + public final String resString; + + // Default constructor is hidden and disabled. + // If somehow it is called, defaults to an empty string and no embed. + @SuppressWarnings("unused") + private SubCommandFormattable() + { + this.resEmbed = null; + this.resString = ""; + } + + public SubCommandFormattable(String res) + { + this.resEmbed = null; + this.resString = res; + } + + public SubCommandFormattable(KittyEmbed embed) + { + this.resEmbed = embed; + this.resString = null; + } + + public void Call(Response res) + { + if(resEmbed == null) + res.Call(resString); + else + res.CallEmbed(resEmbed); + } +} diff --git a/src/core/SubCommandFramework.java b/src/core/SubCommandFramework.java new file mode 100644 index 0000000..8a62ef2 --- /dev/null +++ b/src/core/SubCommandFramework.java @@ -0,0 +1,33 @@ +package core; + +import java.util.HashMap; + +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyUser; +import utils.GlobalLog; + +public class SubCommandFramework +{ + HashMap commands = new HashMap(); + + public SubCommandFramework() + { + + } + + public void addCommand(String name, SubCommand command) + { + name = name.toLowerCase(); + if(commands.put(name, command) != null) + GlobalLog.Error("Multiple registration of a name with name '" + name + "'!"); + + GlobalLog.Log("Registered " + name); + } + + public SubCommandFormattable run(KittyGuild guild, KittyChannel channel, KittyUser user, String input) + { + SubCommandFormattable res = commands.get(input.split(" ")[0].toLowerCase()).OnRun(guild, channel, user, input.substring(input.indexOf(" "))); + return res; + } +}