Plugin Problems!

Discussion in 'Plugin Development' started by MCFunCubes, Dec 20, 2015.

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

    MCFunCubes

    Help! My plugin, for some reason, throws out "null" for the prefix, when you do /te without a subcommand it says an internal error has occurred, and and when I do /te with a subcommand it does the thing it is supposed to do, but it also says "Usage: /te <help|reload|high>". Here is my code:
    tinyEssentials/src/main/java/com.gmail.awbkerbal.tinyEssentials/TinyEssentials.java
    Code:
    package com.gmail.awbkerbal.tinyEssentials;
    //Javaplugin
    import org.bukkit.plugin.java.JavaPlugin;
    //Chatcolors
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    //command crap
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    //player
    import org.bukkit.entity.Player;
    public final class TinyEssentials extends JavaPlugin {
        @Override
        public void onEnable() {
            // Log to console
            getLogger().info("[TE] Loading config");
            loadConfiguration();
            getLogger().info("[TE] Config loaded!");
            getLogger().info("[TE] Plugin has started successfully.");
        }
        public void loadConfiguration(){
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
        @Override
        public void onDisable() {
            //end
            this.saveConfig();
            getLogger().info("[TE] Plugin STOPPED!");
            //ended
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("te")) {
            String prefix = this.getConfig().getString("te.prefix");
            if(sender instanceof Player) {
                Player p = (Player) sender;
                        if ((p.hasPermission("te.help") == true) && (args[0].equalsIgnoreCase("help") == true)) {
                            p.sendMessage(prefix + ChatColor.BLUE + "tinyEssentials v1.0 help");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te help " + ChatColor.GOLD + "tinyEssentials help");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te kill " + ChatColor.GOLD + "Kill yourself");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te high [number of blocks]" + ChatColor.GOLD + "Teleports you to the specified number of blocks above you");
                            }
                        else if ((p.hasPermission("te.reload") == true) && (args[0].equalsIgnoreCase("reload") == true)) {
                            this.saveConfig();
                            this.reloadConfig();
                            p.sendMessage(prefix + ChatColor.GREEN + "TinyEssentials reloaded!");
                        }
                        else if ((p.hasPermission("te.help") == false) && (args[0].equalsIgnoreCase("help") == true)) {
                            p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                        }
                        else if ((p.hasPermission("te.high") == true) && (args[0].equalsIgnoreCase("high")) == true) {
                            String number = args[1];
                            int integer = Integer.parseInt(number);
                            if (integer >= 0) {
                            Location loc = p.getLocation();
                            int x = loc.getBlockX();
                            int y = (loc.getBlockY() + integer);
                            int z = loc.getBlockZ();
                            World coolWorld = p.getWorld();
                            Location location = new Location(coolWorld, x, y, z);
                            //TODO: block under player
                            p.teleport(location);
                            }
                            else if ((p.hasPermission("te.high") == false) && (args[0].equalsIgnoreCase("high")) == true) {
                                p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                            }
                            else {
                                p.sendMessage(prefix + ChatColor.AQUA + "TinyEssentials v1.0.3 by Andrew Bilotti");
                                p.sendMessage(prefix + ChatColor.AQUA + "Type /te help for help");
                            }
                        }
             }
            getLogger().info("[TE] You must be a player to run this command!");
            }
            return false;
           }//end OnCommand
          }//End public final class extends javaPlugin
                  
           
    
            
    tinyEssentials/src/main/java/src/main/resources/plugin.yml
    Code:
    main: com.gmail.awbkerbal.tinyEssentials.TinyEssentials
    name: tinyEssentials
    version: 1.0.1
    author: Andrew Bilotti
    description: Essentials, but more useful, bigger config, and smaller size!
    commands:
      te:
        description: 'TinyEssentials help'
        usage: '/te <help|reload>'
        permission: te
      te help:
        description: 'TinyEssentials help'
        usage: '/te help'
        permission: te.help
      te reload:
        description: 'Reload TinyEssentials'
        usage: '/te reload'
        permission: te.reload
      te high:
        description: 'go up x amount of blocks'
        usage: '/te high [number]'
        permission: te.high
    tinyEssentials/src/main/java/src/main/resources/config.yml
    Code:
    #TinyEssentials config
    tinyEssentials:
    #NO COLOR CODES YET
    te:
    prefix: [TinyEssentials]
    Please help!
     
  2. Offline

    Zombie_Striker

    Since there are no longs that say "useage /te....", I think this may be a problem with another plugin.
     
  3. Offline

    MCFunCubes

    ok

    wait it says usage: /te <help|reload> actually :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2015
  4. Offline

    Zombie_Striker

    @MCFunCubes
    I'm assuming this is what you're referring to. The only time this can run is if the player has the permission and the first arg is "help".

    Here are a few notes that I see already:
    • You don't need "== true". If that boolean (p.hasPerm()) is true, there is not need to test if "true==true"
    • You don't seem tot test how many args actually exist.
    • You are misusing the "else" statements. You can have it like the following:
      Code:
      [*]if(args[0].equalIgnoreCase("help")){
      
      [*]If (Player.hasPerm(help)){
      [*]//do stuff with perm
      [*]}else{
      [*]//do stuff without perm
      [*]}else if(args[0].equalIgnoreCase("other command")){
      
      [*]

    Did you debug?
     
    MCFunCubes likes this.
  5. Offline

    MCFunCubes

    ok, ill try that.

    can I still do
    Code:
    if ((true == true) && (true == true))
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. Offline

    ItsBRZY

    ehh what?
     
  7. Offline

    Zombie_Striker

    @MCFunCubes
    Heres the basic idea

    if(boolean){
    //Use this method if you're testing if something is true.

    If(! boolean){
    //Use this method when testing if something is false

    if(boolean == boolean){//what you're currently doing
    //Use then when what you want it to be equal to can change. When you can't tell if it should be true or false.
     
    MCFunCubes likes this.
  8. Offline

    WolfMage1

    Uhmmmm, just a question

    Why're you doing if(p.hasPermission("permission") == true)

    the if is already testing if it's true, the == true bit is just unnecessary.

    Just do at this guy said ^ if(p.hasPermission("permission") or if(!p.hasPermission("permission")

    Also, Your indentation is terrible. (It might be bukkit's website doing it but it's never happened to me so, I was just asking)

    And, from what I can see there's a lot of unnecessary if's
     
    Last edited: Dec 20, 2015
    MCFunCubes likes this.
  9. Offline

    MCFunCubes

    This?
    Code:
    package com.gmail.awbkerbal.tinyEssentials;
    //Javaplugin
    import org.bukkit.plugin.java.JavaPlugin;
    //Chatcolors
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    //command crap
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    //player
    import org.bukkit.entity.Player;
    public final class TinyEssentials extends JavaPlugin {
        @Override
        public void onEnable() {
            // Log to console
            getLogger().info("[TE] Loading config");
            loadConfiguration();
            getLogger().info("[TE] Config loaded!");
            getLogger().info("[TE] Plugin has started successfully.");
        }
        public void loadConfiguration(){
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
        @Override
        public void onDisable() {
            //end
            this.saveConfig();
            getLogger().info("[TE] Plugin STOPPED!");
            //ended
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("te")) {
            String prefix = this.getConfig().getString("te.prefix");
            if(sender instanceof Player) {
                Player p = (Player) sender;
                            if (args[0].equalsIgnoreCase("help")) {
                            if (p.hasPermission("te.help")) {
                            p.sendMessage(prefix + ChatColor.BLUE + "tinyEssentials v1.0 help");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te help " + ChatColor.GOLD + "tinyEssentials help");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te kill " + ChatColor.GOLD + "Kill yourself");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te high [number of blocks]" + ChatColor.GOLD + "Teleports you to the specified number of blocks above you");
                            }else {
                                p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                            }}
                        if (args[0].equalsIgnoreCase("reload")) {
                            if (p.hasPermission("te.reload")) {
                            this.saveConfig();
                            this.reloadConfig();
                            p.sendMessage(prefix + ChatColor.GREEN + "TinyEssentials reloaded!");
                        } else {
                            p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                        }
                            }
                        if (args[0].equalsIgnoreCase("high")) {
                            if (p.hasPermission("te.high")) {
                            String number = args[1];
                            int integer = Integer.parseInt(number);
                            if (integer >= 0) {
                            Location loc = p.getLocation();
                            int x = loc.getBlockX();
                            int y = (loc.getBlockY() + integer);
                            int z = loc.getBlockZ();
                            World coolWorld = p.getWorld();
                            Location location = new Location(coolWorld, x, y, z);
                            //TODO: block under player
                            p.teleport(location);
                            }} else {
                                p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                            }
                            }
                        else {
                                p.sendMessage(prefix + ChatColor.AQUA + "TinyEssentials v1.0.3 by Andrew Bilotti");
                                p.sendMessage(prefix + ChatColor.AQUA + "Type /te help for help");
                            }
                       
             }
            getLogger().info("[TE] You must be a player to run this command!");
            }
            return false;
           }//end OnCommand
          }//End public final class extends javaPlugin
                  
           
    
            
    I don't care about indentation :D

    it still gives me a syntax error when I just do /te and null for prefix...
    what does null mean?!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  10. Offline

    Irantwomiles

    @MCFunCubes I believe that if you don't specify how long the argument is it will throw a null pointer exception. Somewhere in your code you need to check if the args are less than the number of arguments you are going to have and if they are send the player a message saying "Not enough arguments, /te ...." Or something like that.

    EDIT: I think that you are getting null for your prefix because you dont have ' <-- around your string.
     
    MCFunCubes likes this.
  11. Offline

    MCFunCubes

    @Irantwomiles how do I do dat?

    Examples, examples :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  12. Offline

    Irantwomiles

    Please just use the edit function and I'll give you an example but you have to implement it in your code because spoon feeding wont teach you anything.

    Code:
    example:
    
    if(args.length < (how ever long your argument is going to be)) {
     //if it is that length you want to continue...
    
    return true;
    } else {
    player.sendmessage("Not enough Arguments, /te... (what ever your command is)");
    return true;
    }
    
     
    MCFunCubes likes this.
  13. Offline

    MCFunCubes

    That makes sense... whats the less than or equal operator?
     
  14. Offline

    Irantwomiles

    The arrow is pointing to ' idk what its called lol

    EDIT: here is a picture http://prntscr.com/9gijgd

    <=

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    EDIT: /\ rekt by moderator after I just told someone not to double post!
     
    MCFunCubes likes this.
  15. Offline

    MCFunCubes

    ok :D

    ' = apostrophe

    then this should work :D
    Code:
    package com.gmail.awbkerbal.tinyEssentials;
    //Javaplugin
    import org.bukkit.plugin.java.JavaPlugin;
    //Chatcolors
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    //command crap
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    //player
    import org.bukkit.entity.Player;
    public final class TinyEssentials extends JavaPlugin {
        @Override
        public void onEnable() {
            // Log to console
            getLogger().info("[TE] Loading config");
            loadConfiguration();
            getLogger().info("[TE] Config loaded!");
            getLogger().info("[TE] Plugin has started successfully.");
        }
        public void loadConfiguration(){
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
        @Override
        public void onDisable() {
            //end
            this.saveConfig();
            getLogger().info("[TE] Plugin STOPPED!");
            //ended
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("te")) {
            String prefix = this.getConfig().getString("te.prefix");
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (args.length >= 4){
                            if (args[0].equalsIgnoreCase("help")) {
                            if (p.hasPermission("te.help")) {
                            p.sendMessage(prefix + ChatColor.BLUE + "tinyEssentials v1.0 help");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te help " + ChatColor.GOLD + "tinyEssentials help");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te kill " + ChatColor.GOLD + "Kill yourself");
                            p.sendMessage(prefix + ChatColor.GREEN + "/te high [number of blocks]" + ChatColor.GOLD + "Teleports you to the specified number of blocks above you");
                            }else {
                                p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                            }}
                        if (args[0].equalsIgnoreCase("reload")) {
                            if (p.hasPermission("te.reload")) {
                            this.saveConfig();
                            this.reloadConfig();
                            p.sendMessage(prefix + ChatColor.GREEN + "TinyEssentials reloaded!");
                        } else {
                            p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                        }
                            }
                        if (args[0].equalsIgnoreCase("high")) {
                            if (p.hasPermission("te.high")) {
                            String number = args[1];
                            int integer = Integer.parseInt(number);
                            if (integer >= 0) {
                            Location loc = p.getLocation();
                            int x = loc.getBlockX();
                            int y = (loc.getBlockY() + integer);
                            int z = loc.getBlockZ();
                            World coolWorld = p.getWorld();
                            Location location = new Location(coolWorld, x, y, z);
                            //TODO: block under player
                            p.teleport(location);
                            }} else {
                                p.sendMessage(prefix + ChatColor.RED + "You dont have permission!");
                            }
                            }}
                        else{
                                p.sendMessage(prefix + ChatColor.AQUA + "TinyEssentials v1.0.3 by Andrew Bilotti");
                                p.sendMessage(prefix + ChatColor.AQUA + "Type /te help for help");
                            }
                      
             }
            getLogger().info("[TE] You must be a player to run this command!");
            }
            return false;
           }//end OnCommand
          }//End public final class extends javaPlugin
    btw it is bukkit who makes the tabs bad :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2015
  16. Offline

    Irantwomiles

    @MCFunCubes I would also check if the args are <=. Also did you try the apostrophe in the config?
     
  17. Offline

    MCFunCubes

  18. Offline

    WolfMage1

    Honestly, as much as I would happily fix your code, I hate spoon-feeding code.

    You really should get more of a understanding of java if you want to proceed in making plugins, because your code just leaves something to be desired.

    And indentation is everything :),

    http://mrbool.com/importance-of-code-indentation/29079

    You really, really, really need to indent your code.

    And if you dont know why something doesnt work, take it appart piece by piece until you understand it propperly, add system.out.println messages saying I did this here, I did that there and see where it goes wrong and find out why it did
     
  19. Offline

    MCFunCubes

    It is indented! :D
     
  20. Offline

    WolfMage1

    *Properly indent your code
     
  21. Offline

    MCFunCubes

    @WolfMage1
    I already said, I indented the code. Where is the dislike post button?
     
  22. Offline

    Zombie_Striker

    @MCFunCubes
    You did not indent properly. That is the issue.
     
    WolfMage1 likes this.
  23. Offline

    MCFunCubes

    ??? I thought that spacing doesn't matter!!!!
     
  24. Offline

    Zombie_Striker

    @MCFunCubes
    To the Java decoder, it does not matter. To the people that have to read your code, then it matters. I believe we already posted why it matters.

    Besides, how hard is it to use ctrl-shift-F to format your code for you?
     
  25. Offline

    MCFunCubes

  26. Offline

    WolfMage1

    You could write all of your code on 1 line, and the java decompiler wouldn't care as long as it was working code.

    But the whole point of indentation is that it is readable for the programmer or those helping the programmer

    Code:
    public boolean myboolean(String string1, String string2, String string3)
                              {
        if(string1.equals
        ("string1"))
                          {
        System.out.println("String 1");
    }
        if(string2.equals
        ("string2"))
                          {
        System.outprintln("
                    String 2");
    }
        if(
                                     string3.equals
        ("
    string3"))
                          {
        System.
       out.println(
                           "String 1");
    }
    
    
    
    
            return
                            false;
    }
    whilst java doesn't care that your code looks like that because all it reads is 0's and 1's, it's not nice to read is it? that is what indentation is for, so instead of it looking like that, it looks like this

    Code:
    public boolean myBoolean(String string1, String string2, String string3) {
        if(string1.equals("String 1") {
            System.out.println("String 1");
        }
    
        if(string2.equals("String 2") {
            System.out.println("String 2");
        }
    
        if(string3.equals("String 3") {
            System.out.println("String 3");
        }
        return true;
    }
    Although they both work in the exact same way. what's easier to read?

    Can I just say, that I just saw how many plugins you've made, it must be a pain when it comes to updating them xD
     
    Zombie_Striker likes this.
  27. Offline

    Zombie_Striker

    @WolfMage1
    --Offtopic--
    I make sure they only use the API, because the API stays the same over all updates and all alternative bukkits (e.g. Works from 1.5 to the new 1.9 and from Bukkit to Spigot)
     
  28. Offline

    WolfMage1

    --Offtopic--
    If I had any intention of making a vast amount of plugins I'd make my own api xD, because they're very likely to consist of similar methods and code (which is why I love interfaces)
     
  29. Offline

    MCFunCubes

    What?
     
  30. Offline

    MCFunCubes

    Kinda dead :D
     
Thread Status:
Not open for further replies.

Share This Page