[ERROR] NEED HELP!

Discussion in 'Plugin Development' started by mactown21, Jun 3, 2014.

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

    mactown21

    Im not understanding whats the issue with this.
    Code:
    package me.legitsoulja.playerwarp;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class playerwarp extends JavaPlugin implements Listener
    {
        String prefix = ChatColor.GRAY+"["+ChatColor.DARK_PURPLE+"PW"+ChatColor.GRAY+"] ";
        public void LoadConfiguration()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        @Override
        public void onEnable()
        {
            getLogger().info(">-=-={Loading Configuration}=-=-<");
            LoadConfiguration();
            getLogger().info(">-=-={Done Loading!}=-=-<");
            getServer().getPluginManager().registerEvents(this, this);
        }@Override public void onDisable(){}
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){}
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(sender instanceof Player)
            {
                Player player = (Player)sender;
                String pname = player.getName();
                String warpcount = getConfig().getString("player." + pname + ".warps");
                String name = args[1];
                String owner = player.getName();
                String token = getConfig().getString("player." + owner);
                int max = getConfig().getInt("maxcreate");
               
                if(label.equalsIgnoreCase("pwarp"))
                {
                    player.sendMessage(prefix + "Hey, " + pname + ". You have, " + warpcount + " total warps");
                    player.sendMessage(prefix + "/pwarp || Main Command");
                    player.sendMessage(prefix + "/pwarp create || Create Player Warp!");
                    player.sendMessage(prefix + "/pwarp delete || Delete Your Warp!");
                    player.sendMessage(prefix + "/pwarp <warp_name> || Teleport to <warp_name>");
                    if(player.hasPermission("pwarp.admin") | player.isOp())
                    {
                        player.sendMessage(prefix + "/pwarp reload || Reloads Config");
                        return true;
                    }
                }
                if(player.hasPermission("pwarp.create") | player.isOp())
                {
                    if(token.equals(max))
                    {
                        if(args[0].equalsIgnoreCase("create"))
                        {
                            World world = Bukkit.getWorld(getConfig().getString("warp." + name + ".world"));
                            Location loc = player.getLocation();
                            /**/
                            getConfig().set("warp." + name + ".world", world);
                            getConfig().set("player." + owner, getConfig().getInt("player." + owner) + 1);
                            getConfig().set("warp." + name + ".owner", owner);
                            getConfig().set("warp." + name + ".x", loc.getX());
                            getConfig().set("warp." + name + ".y", loc.getY());
                            getConfig().set("warp." + name + ".x", loc.getX());
                            /**/
                            player.sendMessage(prefix + name + ", Has been created! now do /pwarp " + name);
                            return true;
                        }
                    }else
                    {
                        player.sendMessage(prefix + "You have reached your max warp claim!");
                    }
                }
                if(player.hasPermission("pwarp.delete") | player.isOp())
                {
                    if(args[0].equalsIgnoreCase("delete"))
                    {
                        if(getConfig().getString("warp." + name + ".owner").equals(owner) | player.isOp())
                        {
                            getConfig().set("warp." + name + ".world", null);
                            getConfig().set("warp." + name + ".owner", null);
                            getConfig().set("warp." + name + ".x", null);
                            getConfig().set("warp." + name + ".y", null);
                            getConfig().set("warp." + name + ".x", null);
                            player.sendMessage(prefix + "Warp, " + name + " Has been deleted!");
                        }else
                        {
                            player.sendMessage(prefix + "Your not allowed to delete a warp thats not yours!");
                        }
                    }
                }else
                {
                    player.sendMessage(prefix + "You have no permissions to delete a warp!");
                }
                if(player.hasPermission("pwarp.warp") | player.isOp())
                {
                    if(args[0].equalsIgnoreCase("pwarp") && !args[1].equalsIgnoreCase(""))
                    {
                        World world = Bukkit.getWorld("warp." + name + ".world");
                        int x = getConfig().getInt("warp." + name + ".x");
                        int y = getConfig().getInt("warp." + name + ".y");
                        int z = getConfig().getInt("warp." + name + ".z");
                       
                        Location location = new Location(world, x, y, z);
                       
                        player.teleport(location);
                        return true;
                    }else
                    {
                        return true;
                    }
                }else
                {
                    player.sendMessage(prefix + "You dont have permission to warp anywhere!");
                }
                if(args[0].equalsIgnoreCase("reload"))
                {
                    reloadConfig();
                    player.sendMessage(prefix + "Plugin reloaded!");
                    System.out.println(prefix + "Plugin reloaded!");
                }
            }
            return false;
        }
    }
     
  2. Offline

    Go Hard

  3. Offline

    bdubz4552

    mactown21 I counted the lines. Line 38 is
    Code:java
    1. String name = args[1];
    Now when you say this after the onCommand() void, you are telling it to use args[1], regardless of whether or not it exists. You need an if statement to check if args[1] exists before you can use it.

    Also, arrays start counting at zero. So in a command, lets say "/throw <player> <distance>", "throw" is the label, player is args[0], and distance is args[1].
     
  4. Offline

    flaaghara

    mactown21 ArrayIndexOutOfBoundsException

    You're assuming that args[1] is not null when you declare it to a variable.

    Code:
    String name = args[1];
    Then you attempt to use it in a boolean expression.

    Code:
    if(args[0].equalsIgnoreCase("pwarp") && !args[1].equalsIgnoreCase(""))
    I know what you're trying to do here. Instead, just check if:

    Code:
    args[1] != null
    EDIT: bdubz4552 Ninja'd
     
  5. Offline

    mactown21

    bdubz4552 yea for example so for my name just get rid of that string and just change all the name variables to args[1]?
    because /pwarp create args[1]

    flaaghara and ok, i will just return the args if its null.

    and so mainly everything is crashing because of the args right? flaaghara bdubz4552 Go Hard

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  6. Offline

    bdubz4552

    mactown21
    And yes, the args[] is the problem. Don't forget to start it on 0, not 1.
    Code:java
    1. package me.legitsoulja.playerwarp;
    2. //Imports and all that other stuff
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    4. {
    5. if(sender instanceof Player)
    6. {
    7. Player player = (Player)sender;
    8. String pname = player.getName();
    9. String warpcount = getConfig().getString("player." + pname + ".warps");
    10.  
    11. //This is what you should have.
    12. if (args[1] != null) {
    13. String name = args[1];
    14. } else {
    15. player.sendMessage(//some message about bad arguments);
    16. }
    17.  
    18. String owner = player.getName();
    19. String token = getConfig().getString("player." + owner);
    20. int max = getConfig().getInt("maxcreate");
    21.  
    22. if(label.equalsIgnoreCase("pwarp"))
    23. {
    24. player.sendMessage(prefix + "Hey, " + pname + ". You have, " + warpcount + " total warps");
    25. player.sendMessage(prefix + "/pwarp || Main Command");
    26. player.sendMessage(prefix + "/pwarp create || Create Player Warp!");
    27. player.sendMessage(prefix + "/pwarp delete || Delete Your Warp!");
    28. player.sendMessage(prefix + "/pwarp <warp_name> || Teleport to <warp_name>");
    29. if(player.hasPermission("pwarp.admin") | player.isOp())
    30. {
    31. player.sendMessage(prefix + "/pwarp reload || Reloads Config");
    32. return true;
    33. }
    34. }
    35. if(player.hasPermission("pwarp.create") | player.isOp())
    36. {
    37. if(token.equals(max))
    38. {
    39. if(args[0].equalsIgnoreCase("create"))
    40. {
    41. World world = Bukkit.getWorld(getConfig().getString("warp." + name + ".world"));
    42. Location loc = player.getLocation();
    43. /**/
    44. getConfig().set("warp." + name + ".world", world);
    45. getConfig().set("player." + owner, getConfig().getInt("player." + owner) + 1);
    46. getConfig().set("warp." + name + ".owner", owner);
    47. getConfig().set("warp." + name + ".x", loc.getX());
    48. getConfig().set("warp." + name + ".y", loc.getY());
    49. getConfig().set("warp." + name + ".x", loc.getX());
    50. /**/
    51. player.sendMessage(prefix + name + ", Has been created! now do /pwarp " + name);
    52. return true;
    53. }
    54. }else
    55. {
    56. player.sendMessage(prefix + "You have reached your max warp claim!");
    57. }
    58. }
    59. if(player.hasPermission("pwarp.delete") | player.isOp())
    60. {
    61. if(args[0].equalsIgnoreCase("delete"))
    62. {
    63. if(getConfig().getString("warp." + name + ".owner").equals(owner) | player.isOp())
    64. {
    65. getConfig().set("warp." + name + ".world", null);
    66. getConfig().set("warp." + name + ".owner", null);
    67. getConfig().set("warp." + name + ".x", null);
    68. getConfig().set("warp." + name + ".y", null);
    69. getConfig().set("warp." + name + ".x", null);
    70. player.sendMessage(prefix + "Warp, " + name + " Has been deleted!");
    71. }else
    72. {
    73. player.sendMessage(prefix + "Your not allowed to delete a warp thats not yours!");
    74. }
    75. }
    76. }else
    77. {
    78. player.sendMessage(prefix + "You have no permissions to delete a warp!");
    79. }
    80. if(player.hasPermission("pwarp.warp") | player.isOp())
    81. {
    82. if(args[0].equalsIgnoreCase("pwarp") && !args[1].equalsIgnoreCase(""))
    83. {
    84. World world = Bukkit.getWorld("warp." + name + ".world");
    85. int x = getConfig().getInt("warp." + name + ".x");
    86. int y = getConfig().getInt("warp." + name + ".y");
    87. int z = getConfig().getInt("warp." + name + ".z");
    88.  
    89. Location location = new Location(world, x, y, z);
    90.  
    91. player.teleport(location);
    92. return true;
    93. }else
    94. {
    95. return true;
    96. }
    97. }else
    98. {
    99. player.sendMessage(prefix + "You dont have permission to warp anywhere!");
    100. }
    101. if(args[0].equalsIgnoreCase("reload"))
    102. {
    103. reloadConfig();
    104. player.sendMessage(prefix + "Plugin reloaded!");
    105. System.out.println(prefix + "Plugin reloaded!");
    106. }
    107. }
    108. return false;
    109. }
    110. }
     
  7. Offline

    mactown21

    bdubz4552 on line 82 should i keep this?
    !args[1].equalsIgnoreCase(""))
     
  8. Offline

    bdubz4552

    mactown21 If args[1] does not exist, that will give an ArrayIndexOutOfBounds exception as well. My personal suggestion would be that if however many args you need are not present, you halt the method.
    Code:java
    1. public boolean onCommand(<insert parameters... Meh>) {
    2. if (args.length != 2) {
    3. sender.sendMessage("Learn to use yer arguments!");
    4. return true;
    5. }
    6. /**
    7.   * Now as long as there are two arguments, this method will run.
    8.   * If not, the method dies at the return.
    9.   */
    10. }
     
Thread Status:
Not open for further replies.

Share This Page