Solved Sign Changing Plugin always returns false

Discussion in 'Plugin Development' started by dingus007, Jul 30, 2013.

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

    dingus007

    So whenever I try to run my command with my plugin with all of the arguments correct, all the plugin gives me in chat is the usage from plugin.yml. If anyone could help me, feel free. Remember that I just started mod/plugin development yesterday and I'm still learning.

    Code:java
    1. package com.macleisure.am; //my real package is my email address, so I changed it to this for privacy.
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.block.BlockState;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class AdamsCommands extends JavaPlugin {
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("onEnable has been invoked");
    18. }
    19.  
    20. @Override
    21. public void onDisable(){
    22. getLogger().info("onDisable has been invoked!");
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    26.  
    27. Player player = (Bukkit.getServer().getPlayer(sender.getName()));
    28. Block block = player.getTargetBlock(null, 200);
    29.  
    30. if (args.length == 1) {
    31. if (cmd.getName().equalsIgnoreCase("changeline")) {
    32. if (sender instanceof Player == false) {
    33. sender.sendMessage("This command can only be run by a player!");
    34. } else {
    35. if (block.equals(Material.SIGN_POST)) {
    36. BlockState state = block.getState();
    37. Sign sign = (Sign)state;
    38.  
    39. sign.setLine(Integer.parseInt(args[0]), args[1]);
    40. sign.update(true);
    41. } else { return false; }
    42. }
    43. } else { return false; }
    44. } else { return false; }
    45. return true;
    46. }
    47. }
    48.  
     
  2. Offline

    Tarestudio

    dingus007
    Block is not equal to Material, use block.getType().equals(Material.SIGN_POST)
    Also, there is Material.SIGN_WALL, too, maybe you want to check this as well.
    And you can merge line 36 and 37 to Sign sign = (Sign)Block.getState();

    Edit: And you should check that block is not null
     
  3. Offline

    Taketheword

    And instead of using this:
    Code:
    Player player = (Bukkit.getServer().getPlayer(sender.getName()));
    Just cast it like this:
    Code:
    Player player = (Player) sender;
     
  4. Offline

    dingus007

    Thanks, guys I will test out the changes when I get back to my computer..
     
  5. Offline

    CubieX

    Code:
      if (args.length == 1) {
    You are using args[0] and [1], so you need to check for args.length == 2 here, as you have 2 arguments for this command.

    Code:
    Integer.parseInt(args[0])
    This will throw a NumberFormatException, if the player entered no valid interger value as first argument.
    So better use a try{} catch{} block to handle the exception.

    Code:
    Player player = (Player) sender;
    This is fine. But before casting "sender" to "Player", check if sender actually is a Player,
    by using
    Code:
    if (sender instanceof Player)
    {
      // cast to Player here
    }
    Because "sender" could also be the console, which would cause an error here, if you cast it to Player.

    You should "return true;" if the player entered a valid command. Even if the other checks fail. (like if he has not targeted a sign, but the command was correct)
    If it was not valid, (wrong command, wrong parameters) "return false;".

    It's actually called "Material.WALL_SIGN". ;)
     
  6. Offline

    dingus007


    So I edited my code to most of your changes, but plugin.yml still kicks in by saying /changeline [line] [text] and the command doesn't run even though I'm looking at a sign and put in two arguments.

    Code:java
    1. package com.macleisure.am
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.Material;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.block.BlockState;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class AdamsCommands extends JavaPlugin {
    13.  
    14. @Override
    15. public void onEnable(){
    16. getLogger().info("onEnable has been invoked");
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. getLogger().info("onDisable has been invoked!");
    22. }
    23.  
    24. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    25.  
    26. // /changeline command
    27.  
    28.  
    29.  
    30. if (cmd.getName().equalsIgnoreCase("changeline") & sender instanceof Player) {
    31. Player player = (Player) sender;
    32. Block block = player.getTargetBlock(null, 200);
    33. if (args.length == 2 & block.equals(Material.WALL_SIGN)) {
    34. BlockState state = block.getState();
    35. Sign sign = (Sign)state;
    36.  
    37. sign.setLine(Integer.parseInt(args[0]), args[1]);
    38. sign.update(true);
    39. return true;
    40. } else { sender.sendMessage(ChatColor.RED + "Command formatted wrong!"); return false;}
    41. }
    42. return true;
    43. }
    44. }


    If anyone could help, please do!
     
  7. Offline

    CubieX

    Code:
    if (args.length == 2 & block.equals(Material.WALL_SIGN))
     
    if (cmd.getName().equalsIgnoreCase("changeline") & sender instanceof Player)
    
    Use "&&". You accidently used "&" in both lines, which will do something different.
     
Thread Status:
Not open for further replies.

Share This Page