+Added SubCommand system
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<String, SubCommand> commands = new HashMap<String, SubCommand>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user