mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-21 18:32:17 +00:00
[WIP] Multiple Profile Handlers
This commit is contained in:
1 parent
b2b9248c55
commit
bf604774c0
11 files changed
+750
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package kamkeel.command.profile;
|
||||
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.util.ChatComponentTranslation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class CommandHelpProfile extends CommandProfileBase {
|
||||
private CommandProfile parent;
|
||||
|
||||
public CommandHelpProfile(CommandProfile parent){
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "help [command]";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
|
||||
if(args.length == 0){
|
||||
sendMessage(sender, "\u00A78------ \u00A7aProfile Commands \u00A78------");
|
||||
for(Entry<String, CommandProfileBase> entry : parent.map.entrySet()){
|
||||
sendMessage(sender, "\u00A77> " + "\u00A7e" + entry.getKey() + "\u00A78: \u00A77" + entry.getValue().getCommandUsage(sender));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
CommandProfileBase command = parent.getCommand(args);
|
||||
if(command == null){
|
||||
sendError(sender, "Unknown command " + args[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
if(command.subcommands.isEmpty()){
|
||||
sender.addChatMessage(new ChatComponentTranslation(command.getCommandUsage(sender)));
|
||||
return;
|
||||
}
|
||||
|
||||
Method m = null;
|
||||
if(args.length > 1){
|
||||
m = command.subcommands.get(args[1].toLowerCase());
|
||||
}
|
||||
if(m == null){
|
||||
sendMessage(sender, "\u00A78------ \u00A7a" + command.getCommandName().toUpperCase() + " SubCommands \u00A78------");
|
||||
sendMessage(sender, "\u00A77Usage: \u00A76" + command.getUsage());
|
||||
for(Entry<String, Method> entry : command.subcommands.entrySet()){
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A77> " + "\u00A7e" + entry.getKey() + "\u00A78: \u00A77" + entry.getValue().getAnnotation(SubCommand.class).desc()));
|
||||
}
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A78Permission:\u00A77 " + CommandProfile.getCommandPermission(command.getCommandName())));
|
||||
}
|
||||
else{
|
||||
sendMessage(sender, "\u00A78------ \u00A7b" + command.getCommandName().toUpperCase() + "." + args[1].toUpperCase() + " Command \u00A78------");
|
||||
SubCommand sc = m.getAnnotation(SubCommand.class);
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A77" + sc.desc()));
|
||||
if(!sc.usage().isEmpty())
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A77Usage: \u00A76" + sc.usage()));
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A78Permission:\u00A77 " + getSubCommandPermission(args[1])));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package kamkeel.command.profile;
|
||||
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import noppes.npcs.CustomNpcsPermissions;
|
||||
import noppes.npcs.scripted.CustomNPCsException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandProfile extends CommandBase {
|
||||
|
||||
public Map<String, CommandProfileBase> map = new HashMap<String, CommandProfileBase>();
|
||||
public CommandHelpProfile help = new CommandHelpProfile(this);
|
||||
public String[] alias = {"profile"};
|
||||
|
||||
public CommandProfile(){
|
||||
registerCommand(help);
|
||||
}
|
||||
|
||||
public void registerCommand(CommandProfileBase command){
|
||||
String name = command.getCommandName().toLowerCase();
|
||||
if(map.containsKey(name))
|
||||
throw new CustomNPCsException("Already a subcommand with the name: " + name);
|
||||
map.put(name, command);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "profile";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List getCommandAliases()
|
||||
{
|
||||
return Arrays.asList(alias);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandUsage(ICommandSender sender) {
|
||||
return "Use as /profile subcommand";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
|
||||
if(args.length == 0){
|
||||
help.processCommand(sender, args);
|
||||
return;
|
||||
}
|
||||
|
||||
CommandProfileBase command = getCommand(args);
|
||||
if(command == null)
|
||||
throw new CommandException("Unknown command " + args[0]);
|
||||
|
||||
args = Arrays.copyOfRange(args, 1, args.length);
|
||||
if(command.subcommands.isEmpty() || !command.runSubCommands()){
|
||||
if(!canSendCommand(sender, command))
|
||||
throw new CommandException("You are not allowed to use this command: " + command);
|
||||
command.canRun(sender, command.getUsage(), args);
|
||||
command.processCommand(sender, args);
|
||||
return;
|
||||
}
|
||||
|
||||
if(args.length == 0){
|
||||
help.processCommand(sender, new String[]{command.getCommandName()});
|
||||
return;
|
||||
}
|
||||
|
||||
command.processSubCommand(sender, args[0], Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List addTabCompletionOptions(ICommandSender sender, String[] args){
|
||||
if(args.length == 1)
|
||||
return CommandBase.getListOfStringsMatchingLastWord(args, map.keySet().toArray(new String[map.size()]));
|
||||
CommandProfileBase command = getCommand(args);
|
||||
if(command == null)
|
||||
return null;
|
||||
if(args.length == 2 && command.runSubCommands())
|
||||
return CommandBase.getListOfStringsMatchingLastWord(args, command.subcommands.keySet().toArray(new String[command.subcommands.keySet().size()]));
|
||||
String[] useArgs = command.getUsage().split(" ");
|
||||
if(command.runSubCommands()) {
|
||||
Method m = command.subcommands.get(args[1].toLowerCase());
|
||||
if(m != null) {
|
||||
useArgs = m.getAnnotation(CommandProfileBase.SubCommand.class).usage().split(" ");
|
||||
}
|
||||
}
|
||||
if(args.length <= useArgs.length + 2) {
|
||||
if(args.length - 3 >= 0){
|
||||
String usage = useArgs[args.length - 3];
|
||||
if(usage.equals("<player>") || usage.equals("[player]")) {
|
||||
return CommandBase.getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames());
|
||||
}
|
||||
}
|
||||
}
|
||||
return command.addTabCompletionOptions(sender, Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
|
||||
public CommandProfileBase getCommand(String[] args){
|
||||
if(args.length == 0)
|
||||
return null;
|
||||
return map.get(args[0].toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequiredPermissionLevel(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static String getCommandPermission(String command){
|
||||
return "profile.kamkeel." + command.toLowerCase();
|
||||
}
|
||||
|
||||
public static String getUniversalPermission(){
|
||||
return "profile.kamkeel.*";
|
||||
}
|
||||
|
||||
public static boolean canSendCommand(ICommandSender sender, CommandProfileBase command){
|
||||
if(sender.canCommandSenderUseCommand(command.getRequiredPermissionLevel(), getUniversalPermission())){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(sender.canCommandSenderUseCommand(command.getRequiredPermissionLevel(), getCommandPermission(command.getCommandName()))){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(sender instanceof EntityPlayer){
|
||||
return CustomNpcsPermissions.hasCustomPermission((EntityPlayer) sender, getCommandPermission(command.getCommandName()));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
package kamkeel.command.profile;
|
||||
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.ChatComponentTranslation;
|
||||
import noppes.npcs.CustomNpcsPermissions;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class CommandProfileBase extends CommandBase{
|
||||
public Map<String, Method> subcommands = new HashMap<String, Method>();
|
||||
|
||||
public CommandProfileBase(){
|
||||
for (Method m : this.getClass().getDeclaredMethods()) {
|
||||
SubCommand sc = m.getAnnotation(SubCommand.class);
|
||||
if (sc != null) {
|
||||
String name = sc.name();
|
||||
if (name.equals(""))
|
||||
name = m.getName();
|
||||
subcommands.put(name.toLowerCase(), m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandUsage(ICommandSender sender) {
|
||||
return getDescription();
|
||||
}
|
||||
|
||||
public abstract String getDescription();
|
||||
|
||||
/**
|
||||
* @return Should return a string in the format of <arg> <arg2> <arg3> [arg4] where <> is a required parameter and [] optional
|
||||
*/
|
||||
public String getUsage(){
|
||||
return "";
|
||||
}
|
||||
|
||||
public boolean runSubCommands(){
|
||||
return !subcommands.isEmpty();
|
||||
}
|
||||
|
||||
protected static void sendMessage(ICommandSender sender, String message, Object... obs) {
|
||||
sender.addChatMessage(new ChatComponentTranslation(message, obs));
|
||||
}
|
||||
|
||||
protected static void sendResult(ICommandSender sender, String message, Object... obs) {
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A76[\u00A7eCNPC+\u00A76]\u00A77 " + message, obs));
|
||||
}
|
||||
|
||||
protected static void sendError(ICommandSender sender, String message, Object... obs) {
|
||||
sender.addChatMessage(new ChatComponentTranslation("\u00A76[\u00A7eCNPC+\u00A76]\u00A74 Error:\u00A7c " + message, obs));
|
||||
}
|
||||
|
||||
@Retention(value = RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface SubCommand {
|
||||
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* @return Should return a string in the format of <arg> <arg2> <arg3> [arg4] where <> is a required parameter and [] optional
|
||||
*/
|
||||
String usage() default "";
|
||||
|
||||
String desc();
|
||||
|
||||
int permission() default 2;
|
||||
}
|
||||
|
||||
public void processSubCommand(ICommandSender sender, String command, String[] args) throws CommandException {
|
||||
Method m = subcommands.get(command.toLowerCase());
|
||||
if(m == null)
|
||||
throw new CommandException("Unknown subcommand " + command);
|
||||
|
||||
SubCommand sc = m.getAnnotation(SubCommand.class);
|
||||
if(!canSendCommand(sender, sc, command))
|
||||
throw new CommandException("You are not allowed to use this command: " + command);
|
||||
|
||||
canRun(sender, sc.usage(), args);
|
||||
try {
|
||||
m.invoke(this, sender, args);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void canRun(ICommandSender sender, String usage, String[] args) throws CommandException{
|
||||
String[] np = usage.split(" ");
|
||||
List<String> required = new ArrayList<String>();
|
||||
for(int i = 0; i < np.length; i++){
|
||||
String command = np[i];
|
||||
if(command.startsWith("<")){
|
||||
required.add(command);
|
||||
}
|
||||
if(command.equals("<player>") && args.length > i){
|
||||
CommandBase.getPlayer(sender, args[i]); //throws PlayerNotFoundException if no player is found
|
||||
}
|
||||
|
||||
}
|
||||
if (args.length < required.size()) {
|
||||
throw new CommandException("Missing parameter: " + required.get(args.length));
|
||||
}
|
||||
}
|
||||
|
||||
public int getRequiredPermissionLevel(){
|
||||
return 2;
|
||||
}
|
||||
|
||||
public String getSubCommandPermission(String subCommand){
|
||||
return "cnpc.kamkeel." + getCommandName().toLowerCase() + "." + subCommand.toLowerCase();
|
||||
}
|
||||
|
||||
public String getSubUniversalPermission(){
|
||||
return "cnpc.kamkeel." + getCommandName().toLowerCase() + "*";
|
||||
}
|
||||
|
||||
public boolean canSendCommand(ICommandSender sender, SubCommand command, String subCommand){
|
||||
if(sender.canCommandSenderUseCommand(command.permission(), getSubUniversalPermission())){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(sender.canCommandSenderUseCommand(command.permission(), getSubCommandPermission(subCommand))){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(sender instanceof EntityPlayer){
|
||||
return CustomNpcsPermissions.hasCustomPermission((EntityPlayer) sender, getSubCommandPermission(subCommand));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user