Solved Returns usage

Discussion in 'Plugin Development' started by KarimAKL, Apr 1, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    RcExtract

    Have u registered the CommandHacker? Main.getCommand("hacker").setExecutor(new CommandHacker(Main))
     
  2. Offline

    KarimAKL

    @RcExtract I have this in my onEnable "new CommandHacker(this);", is that what you mean?
     
  3. Offline

    MightyOne

    No. Register it as the executor for your command. Otherwise Bukkit doesn't know that ComnandHacker shall handle the "hack" command and won't call it's onCommand() method in that case.
     
  4. Offline

    KarimAKL

    @MightyOne Oh, i thought i only needed to do the "if (cmd.getName().equalsIgnoreCase("hacker")) {" So i would just need to add this then?:
    Code:
    plugin.getCommand("hacker").setExecutor(this);
    
    So the code would look like this then?
    Code:
    public class CommandHacker implements CommandExecutor {
      
        public static final Set<UUID> hackerList = new HashSet<UUID>();
      
        private Main plugin;
      
        public CommandHacker(Main plugin) {
            this.plugin = plugin;
          
            plugin.getCommand("hacker").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("hacker")) {
                if (plugin.getConfig().getBoolean("Hacker.enabled")) {
                    String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    if (sender instanceof Player && !(sender.hasPermission("hacker.use"))) {
                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
                        return true;
                    } else {
                        if (args.length == 1) {
                            Player target = Bukkit.getPlayer(args[0]);
                            if (target != null) {
                            boolean added = hackerList.add(target.getUniqueId());
                            if (!(added)) hackerList.remove(target.getUniqueId());
                                sender.sendMessage(added ? addHacker : removeHacker);
                                if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                    if (target.hasPermission("hacker.see")) {
                                        if (target != sender) {
                                            target.sendMessage(added ? broadcastAdd : broadcastRemove);
                                            return true;
                                        } else {
                                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
                                            return true;
                                        }
                                    }
                                }
                            } else {
                                sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                                return true;
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
                            return true;
                        }
                    }
                } else {
                    return false;
                }
            }
            return false;
        }
    
    }
    
    EDIT: I just did this but when i started the server with this plugin and tried the command it came with this error then it begins to return the usage again. :(
    Code:
    >hacker
    [17:47:49 ERROR]: Cannot load plugins\Hacker\config.yml
    org.bukkit.configuration.InvalidConfigurationException: while parsing a block mapping
     in 'string', line 5, column 3:
          enabled: true
          ^
    expected <block end>, but found Scalar
     in 'string', line 52, column 34:
          add-yourself-error: '&cYou can't set yourself as a hacker!'
                                         ^
    
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:57) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:226) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:169) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:180) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:188) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at me.karim.hacker.commands.CommandHacker.onCommand(CommandHacker.java:31) [Hacker.jar:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping
     in 'string', line 5, column 3:
          enabled: true
          ^
    expected <block end>, but found Scalar
     in 'string', line 52, column 34:
          add-yourself-error: '&cYou can't set yourself as a hacker!'
                                         ^
    
            at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:570) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:143) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:224) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:229) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:120) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:369) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 15 more
    [17:47:49 ERROR]: [Hacker] Cannot load configuration from jar
    org.bukkit.configuration.InvalidConfigurationException: while parsing a block mapping
     in 'string', line 5, column 3:
          enabled: true
          ^
    expected <block end>, but found Scalar
     in 'string', line 52, column 34:
          add-yourself-error: '&cYou can't set yourself as a hacker!'
                                         ^
    
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:57) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:214) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at me.karim.hacker.commands.CommandHacker.onCommand(CommandHacker.java:31) [Hacker.jar:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping
     in 'string', line 5, column 3:
          enabled: true
          ^
    expected <block end>, but found Scalar
     in 'string', line 52, column 34:
          add-yourself-error: '&cYou can't set yourself as a hacker!'
                                         ^
    
            at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:570) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:143) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:224) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:229) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:120) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:369) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 12 more
    
    I've tried to read it and see if i understood it but i don't quite understand it. :/
     
    Last edited by a moderator: Apr 15, 2018
  5. Offline

    MightyOne

    surely but does it work? thats more important
     
  6. Offline

    KarimAKL

    @MightyOne No it doesn't work. :/ I edited the other message so you can see the error there.
     
  7. Offline

    RcExtract

    U lack programming logics.

    Bukkit will not invest a lot of resources into developing native CraftBukkit so it will never know the actual statements. Therefore, "if (cmd.getName().equalsIgnoreCase("hacker"))" only ensures that onCommand is run when command name is "hacker", but not able to tell Bukkit to run this onCommand when hacker command is executed.

    Besides, you cannot register with "new CommandHacker(this)". It is just an instantiation (creation of an object of a class), same problem: Bukkit does not have native CraftBukkit, and it will never know that the object created there is used for handling "hacker" commands, probably does not even know that the object is created (though the creation can be checked with Thread.getStackTrace() or if the CommandHacker is annotated with sun.reflect.CallerSensitive, which is not accessible outside of java package).

    You can do "plugin.getCommand("hacker").setExecutor(this)" inside constructor of CommandHacker, but it is not recommended because you will limit the CommandHacker to be registered to command "hacker" immediately after creation of an object of CommandHacker.

    A ParseException was thrown because you have invalid characters inside ur configuration file, which is
    Code:
    '
    s, as shown obviously in the stack trace. To use escape characters like ', surround the string with double quotation marks which includes the escape characters. For example,
    Code:
    "You can't perform this command!"
    is valid while
    Code:
    You can't perform this command!
    is invalid.
     
  8. Offline

    KarimAKL

    @RcExtract So i should replace my 'You can't perform this command!' with "You can't perform this command!" and then i don't understand what you mean with the "new CommandHacker(this)", should i remove it or what? If i remember correctly the last time i removed the "new CommandHacker(this)" the class didn't work (wasn't registered i think), so what should i do? I'll try replacing my ' with " and see how it works now, thanks.
    EDIT: Okay so i just replaced it and it still comes with errors, though it's not as many, here is the error:
    Code:
    >hacker
    [14:17:09 WARN]: Unexpected exception while parsing console command "hacker"
    org.bukkit.command.CommandException: Unhandled exception executing command 'hacker' in plugin Hacker v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.karim.hacker.commands.CommandHacker.onCommand(CommandHacker.java:32) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 8 more
    
    I don't quite think i understand what you said. :/ (I have tried reading this aswell but can't seem to understand it :( )
    EDIT2: Btw, what does the "... 8 more" mean? Like there is 8 more error lines or what? If so then how would i get it to show them?
     
    Last edited by a moderator: Apr 16, 2018
  9. Offline

    MightyOne

    The console just shows the different levels from where a method was called that threw an exception. You should just be interested in the Caused by:
    The ArrayIndexOutOfBound exception indicates that you tried to get an element of an array beyond the arrays size. You do not check the args.length before initializing these 4 strings
     
  10. Offline

    RcExtract

    Registering command executor to command is a feature in Bukkit API, while it does not have native codes, which means it is nearly impossible to detect your instantiation of CommandHacker. Even Bukkit successfully detects instantiation of any CommandExecutor, including CommandHacker (which is implementation of CommandExecutor), it does not know which command should have the created CommandExecutor as its executor. From these conditions, we can know that Bukkit will not want you to register CommandHacker by only instantiating it. We want to register it with methods too. To know how to register it, you can search on internet or ask here, which we helped you in the previous posts about which methods should be used to register your CommandHacker, and it is Plugin.getCommand(String).setExecutor(CommandExecutor).

    In ur situation, you put the CommandExecutor registration statement into the constructor, therefore simply doing "new CommandHacker(Plugin)" is enough.

    ArrayIndexOutOfBoundsException occurs when u are trying to access to a value in an array by an index where it is out of the range of it.

    Lets say below are some containers within an array:
    ["hi"] ["bye"] ["lol"]
    The index of an array starts from 0, so the indexs of the containers are 0, 1, and 2 respectively.
    In ur code, before "if(args.length == 1) {...} else if {args.length <1) {...} else if (args.length >1) {...}", you have already used arg[0] multiple times for replacing strings obtained from your configuration file. Therefore, when the CommandSender simply executes command "hacker" without any arguments following, args[0] will be out of range because there will be no arguments. The solution is to put the four variables about messages back into if (args.length ==1) {HERE}".

    Good Question. And, you are right, ... 8 more means the exception thrower can be caught to 8 more layers deeper, but you should rarely need the information. If u ask me how to display it, i will say idk :).
     
  11. Offline

    MightyOne

    @RcExtract dude why do you always write about half a page? You know when I had problems like these I was just happy when someone said what was missing. It's not necessary to tell what BukkitAPI can or cannot not or how the observer pattern works. The problem was the command executor registration and its done now. I mean you even write in your profile that learning means practicing.
     
  12. Offline

    KarimAKL

    @MightyOne I do check the "args.length". @RcExtract Oh, so i should just move these:
    Code:
    String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
    String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
    String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
    String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
    
    Under the "if (args.length == 1) {" then? Oh, okay so i was right about the "8 more", anyway thanks, i'll try moving them now. :)
    EDIT: I just finished moving it and testing it and it works perfectly now, thanks for all the help, i'll change this to solved now. :D
    EDIT2: Small extra question: If i want to add more commands in that 1 class i would just do like this:
    Code:
    plugin.getCommand("command1").setExecutor(this);
    plugin.getCommand("command2").setExecutor(this);
    
    Right? And then of course check if the command is equal to that with:
    Code:
    if (cmd.getName().equalsIgnoreCase("command1")) {
    if (cmd.getName().equalsIgnoreCase("command2")) {
    
    Or what?
     
    Last edited by a moderator: Apr 16, 2018
  13. Offline

    MightyOne

    Yes... I believe :D shouldn't be hard to find out. You'll get it work
     
  14. Offline

    KarimAKL

    @MightyOne Okay, when i need it i'll see if that's how, thanks again. :)
     
Thread Status:
Not open for further replies.

Share This Page