Solved SignChangeEvent and PlayerInteractEvent

Discussion in 'Plugin Development' started by random_username, Nov 1, 2013.

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

    random_username

    Hello, I'm having some trouble with my plugin. Each time I create a sign, I get an error on the console. Here is my SignChangeEvent and PlayerInteractEvents:
    Code:java
    1. @EventHandler
    2. public void onsignCreate(SignChangeEvent sign){
    3. Player player = sign.getPlayer();
    4. if(sign.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix"))){
    5. if(player.hasPermission("ecore.signs.admin")){
    6. sign.setLine(0, "§b" + getConfig().getString("Signprefix"));
    7. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.BLUE + "Sign has been created!");
    8. if(sign.getLine(1).equalsIgnoreCase("Premium")){
    9. sign.setLine(1, "§aPremium");
    10. }if(sign.getLine(1).equalsIgnoreCase("VIP")){
    11. sign.setLine(1, "§aVIP");
    12. }if(sign.getLine(1).equalsIgnoreCase("Legend")){
    13. sign.setLine(1, "§aLegend");
    14. }if(sign.getLine(1).equalsIgnoreCase("StoreInfo")){
    15. sign.setLine(1, "§aStore Info");
    16. }
    17. }else{
    18. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.RED + "You do not have permission to do this!");
    19. sign.setCancelled(true);
    20. }
    21. }
    22. }
    23.  
    24. @EventHandler(priority = EventPriority.LOWEST)
    25. public void onPlayerInteract(PlayerInteractEvent event){
    26. Player player = event.getPlayer();
    27. Action action = event.getAction();
    28. Sign s = (Sign) event.getClickedBlock();
    29. Block block = event.getClickedBlock();
    30. if(action == Action.RIGHT_CLICK_BLOCK){
    31. if(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.SIGN){
    32. if(s.getLine(0).equalsIgnoreCase("§b" + getConfig().getString("Signprefix"))){
    33. if(player.hasPermission("ecore.signs.use")){
    34. if(s.getLine(1).equalsIgnoreCase("§aPremium")){
    35. for(Object o : getConfig().getList("Premium"))
    36. {
    37. if(o instanceof String)
    38. {
    39. String a = (String)o;
    40. player.sendMessage(a.replace("&","§"));
    41. }
    42. }
    43. }if(s.getLine(1).equalsIgnoreCase("§aVIP")){
    44. for(Object o : getConfig().getList("VIP"))
    45. {
    46. if(o instanceof String)
    47. {
    48. String a = (String)o;
    49. player.sendMessage(a.replace("&","§"));
    50. }
    51. }
    52. }if(s.getLine(1).equalsIgnoreCase("§aLegend")){
    53. for(Object o : getConfig().getList("Legend"))
    54. {
    55. if(o instanceof String)
    56. {
    57. String a = (String)o;
    58. player.sendMessage(a.replace("&","§"));
    59. }
    60. }
    61. }if(s.getLine(1).equalsIgnoreCase("§aStore Info")){
    62. for(Object o : getConfig().getList("Store"))
    63. {
    64. if(o instanceof String)
    65. {
    66. String a = (String)o;
    67. player.sendMessage(a.replace("&","§"));
    68. }
    69. }
    70. }
    71. }else{
    72. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.RED + "You do not have permission to do this!");
    73. }
    74. }
    75.  
    76. }
    77.  
    78. }
    79.  
    80. }

    This is the error I get: http://pastebin.com/KQGbywPn
    I tried removing the getState(); from line 119, in the playerinteractevent, and used this:
    Code:java
    1. Sign s = (Sign) event.getClickedBlock();

    When I try this in Game, sign messages won't appear, and I get this error: http://pastebin.com/fRcYhGCW

    Any Idea on how to fix this? Thanks for the help :) .
     
  2. Offline

    Drkmaster83

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.block.Cr
    aftBlockState cannot be cast to org.bukkit.block.Sign
    
    I believe that one issue is this:
    Code:
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerInteract(PlayerInteractEvent event){
    Player player = event.getPlayer();
    Action action = event.getAction();
    Sign s = (Sign) event.getClickedBlock();
    
    Casting the clicked block to a sign before even doing the if statement.

    Also, something that I believe may be helpful to you when doing this: A while ago (I can't remember exactly when), Bukkit made it to where you must update the sign entity when you modify it. So, after you've made all your changes, add sign.update(); (where sign is a Sign block) to the end.
     
  3. Offline

    random_username

    I'm no longer getting errors, thanks :) . The only problem is that when I right click the signs, I don't get the messages, and also, when I break the sign, I dont get any message(I just added blockbreakevent) Any idea how to fix this? :) My new code:
    Code:java
    1. @EventHandler
    2. public void onsignCreate(SignChangeEvent sign){
    3. Player player = sign.getPlayer();
    4. if(sign.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix"))){
    5. if(player.hasPermission("ecore.signs.admin")){
    6. sign.setLine(0, ChatColor.AQUA + getConfig().getString("Signprefix"));
    7. if(!(sign.getLine(1).equalsIgnoreCase("Premium") || sign.getLine(1).equalsIgnoreCase("VIP") || sign.getLine(1).equalsIgnoreCase("Legend") || sign.getLine(1).equalsIgnoreCase("Store"))){
    8. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.RED + "Invalid Sign!");
    9. sign.setCancelled(true);
    10. }else{
    11. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.BLUE + "Sign has been created!");
    12. }
    13. if(sign.getLine(1).equalsIgnoreCase("Premium")){
    14. sign.setLine(1, ChatColor.GREEN + "Premium");
    15. }if(sign.getLine(1).equalsIgnoreCase("VIP")){
    16. sign.setLine(1, ChatColor.GREEN + "VIP");
    17. }if(sign.getLine(1).equalsIgnoreCase("Legend")){
    18. sign.setLine(1, ChatColor.GREEN + "Legend");
    19. }if(sign.getLine(1).equalsIgnoreCase("StoreInfo")){
    20. sign.setLine(1, ChatColor.GREEN + "Store Info");
    21. }
    22. }else{
    23. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.RED + "You do not have permission to do this!");
    24. sign.setCancelled(true);
    25. }
    26. }
    27. }
    28.  
    29. @EventHandler
    30. public void onBlockBreak(BlockBreakEvent event){
    31. Player player = event.getPlayer();
    32. if(event.getBlock().getState() instanceof Sign){
    33. Sign s = (Sign) event.getBlock().getState();
    34. if(s.getLine(0).equalsIgnoreCase(getConfig().getString("signprefix"))){
    35. if(player.hasPermission("ecore.signs.admin")){
    36. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.AQUA + "This Enstopia sign has been destroyed!");
    37. }else{
    38. event.setCancelled(true);
    39. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.RED + "You do not have permission to break an Enstopia Sign!");
    40. }
    41. }
    42. }
    43. }
    44.  
    45. @EventHandler
    46. public void onPlayerInteract(PlayerInteractEvent event){
    47. Player player = event.getPlayer();
    48. Action action = event.getAction();;
    49. if(action == Action.RIGHT_CLICK_BLOCK){
    50. if(event.getClickedBlock().getState() instanceof Sign){
    51. Sign s = (Sign) event.getClickedBlock().getState();
    52. if(s.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix"))){
    53. if(player.hasPermission("ecore.signs.use")){
    54. if(s.getLine(1).equalsIgnoreCase("Premium")){
    55. for(Object o : getConfig().getList("Premium"))
    56. {
    57. if(o instanceof String)
    58. {
    59. String a = (String)o;
    60. player.sendMessage(a.replace("&","§"));
    61. }
    62. }
    63. }if(s.getLine(1).equalsIgnoreCase("VIP")){
    64. for(Object o : getConfig().getList("VIP"))
    65. {
    66. if(o instanceof String)
    67. {
    68. String a = (String)o;
    69. player.sendMessage(a.replace("&","§"));
    70. }
    71. }
    72. }if(s.getLine(1).equalsIgnoreCase("Legend")){
    73. for(Object o : getConfig().getList("Legend"))
    74. {
    75. if(o instanceof String)
    76. {
    77. String a = (String)o;
    78. player.sendMessage(a.replace("&","§"));
    79. }
    80. }
    81. }if(s.getLine(1).equalsIgnoreCase("Store Info")){
    82. for(Object o : getConfig().getList("Store"))
    83. {
    84. if(o instanceof String)
    85. {
    86. String a = (String)o;
    87. player.sendMessage(a.replace("&","§"));
    88. }
    89. }
    90. }
    91. }else{
    92. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.RED + "You do not have permission to do this!");
    93. }
    94. }
    95.  
    96. }
    97.  
    98. }
    99.  
    100. }


    For some reason, on playerInteractevent, signs would send the messages when I casted the sign before the if statement, which threw an error. Now that I fixed that error, messages won't appear. any ideas on how to solve this? My code is in my post above :)

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

    Drkmaster83

    Fixed/optimized code... should work:
    Code:
    String prefix = replaceColors(getConfig().getString("Prefix"));
        
    @EventHandler
    public void onsignCreate(SignChangeEvent sign)
    {
      Player player = sign.getPlayer();
      if(sign.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix")))
      {
        if(player.hasPermission("ecore.signs.admin"))
        {
          sign.setLine(0, ChatColor.AQUA + getConfig().getString("Signprefix"));
          if(!(sign.getLine(1).equalsIgnoreCase("Premium") || sign.getLine(1).equalsIgnoreCase("VIP") || sign.getLine(1).equalsIgnoreCase("Legend") || sign.getLine(1).equalsIgnoreCase("Store")))
          {
            player.sendMessage(prefix + ChatColor.RED + "Invalid Sign!");
            sign.setCancelled(true);
          }
          else
          {
            player.sendMessage(prefix + ChatColor.BLUE + "Sign has been created!");
          }
          if(sign.getLine(1).equalsIgnoreCase("Premium")) sign.setLine(1, ChatColor.GREEN + "Premium");
          else if(sign.getLine(1).equalsIgnoreCase("VIP")) sign.setLine(1, ChatColor.GREEN + "VIP");
          else if(sign.getLine(1).equalsIgnoreCase("Legend")) sign.setLine(1, ChatColor.GREEN + "Legend");
          else if(sign.getLine(1).equalsIgnoreCase("StoreInfo")) sign.setLine(1, ChatColor.GREEN + "Store Info");
        }
        else
        {
          player.sendMessage(prefix + ChatColor.RED + "You do not have permission to do this!");
          sign.setCancelled(true);
        }
      }
    }
    
    @EventHandler
    public void onBlockBreak(BlockBreakEvent event)
    {
      Player player = event.getPlayer();
      if(event.getBlock() instanceof Sign)
      {
        Sign s = (Sign) event.getBlock().getState();
        if(s.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix")))
        {
          if(player.hasPermission("ecore.signs.admin"))
          {
            player.sendMessage(prefix + ChatColor.AQUA + "This Enstopia sign has been destroyed!");
          }
          else
          {
            event.setCancelled(true);
            player.sendMessage(prefix + ChatColor.RED + "You do not have permission to break an Enstopia Sign!");
          }
        }
      }
    }
    
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event)
    {
      Player player = event.getPlayer();
      Action action = event.getAction();
      if(action == Action.RIGHT_CLICK_BLOCK)
      {
        if(event.getClickedBlock() instanceof Sign)
        {
          Sign s = (Sign) event.getClickedBlock().getState();
          if(s.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix")))
          {
            if(player.hasPermission("ecore.signs.use"))
            {
              if(s.getLine(1).equalsIgnoreCase("Premium"))
              {
                for(String string : getConfig().getStringList("Premium"))
                {
                  player.sendMessage(replaceColors(string));
                }
              }
              if(s.getLine(1).equalsIgnoreCase("VIP"))
              {
                for(String string : getConfig().getStringList("VIP"))
                {
                  player.sendMessage(replaceColors(string));
                }
              }
              if(s.getLine(1).equalsIgnoreCase("Legend"))
              {
                for(String string : getConfig().getStringList("Legend"))
                {
                  player.sendMessage(replaceColors(string));
                }
              }
              if(s.getLine(1).equalsIgnoreCase("Store Info"))
              {
                for(String string : getConfig().getStringList("Store"))
                {
                  player.sendMessage(replaceColors(string));
                }
              }
            }
            else
            {
              player.sendMessage(prefix + ChatColor.RED + "You do not have permission to do this!");
            }
          }
        }
      }
    }
    
    public String replaceColors(String s)
    {
      s.replaceAll("(&([a-fk-or0-9A-FK-OR]))", "\u00A7$2");
      return s;
    }
    
     
  5. Offline

    random_username

    Hello, thanks for the reply, I'm still not getting the messages :) Is there any other solution, thanks for helping btw :D
     
  6. Offline

    Drkmaster83

    Post your config.yml.
     
  7. Offline

    random_username

  8. Offline

    Drkmaster83

  9. Offline

    random_username

    Where do I find the method to initialize it? you mean the on in the onenable? (Sorry for dumb question, I'm new to bukkit coding)
     
  10. Offline

    Drkmaster83

    Alright, I got the code to work:
    Code:
    @Override
    public void onEnable()
    {
        if(!(new File(getDataFolder() + File.separator + "config.yml").exists()))
        {
            saveDefaultConfig();
        }
        getServer().getPluginManager().registerEvents(this, this);
    }
     
    @EventHandler
    public void onSignCreate(SignChangeEvent event)
    {
        String prefix = replaceColors(getConfig().getString("Prefix"));
        Player player = event.getPlayer();
        Sign sign = (Sign) event.getBlock().getState();
        if(sign.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix")))
        {
            if(player.hasPermission("ecore.signs.admin"))
            {
                sign.setLine(0, ChatColor.AQUA + getConfig().getString("Signprefix"));
                if(!(sign.getLine(1).equalsIgnoreCase("Premium") || sign.getLine(1).equalsIgnoreCase("VIP") || sign.getLine(1).equalsIgnoreCase("Legend") || sign.getLine(1).equalsIgnoreCase("Store")))
                {
                    player.sendMessage(prefix + ChatColor.RED + "Invalid Sign!");
                    event.setCancelled(true);
                }
                else
                {
                    player.sendMessage(prefix + ChatColor.BLUE + "Sign has been created!");
                }
                if(sign.getLine(1).equalsIgnoreCase("Premium")) sign.setLine(1, ChatColor.GREEN + "Premium");
                else if(sign.getLine(1).equalsIgnoreCase("VIP")) sign.setLine(1, ChatColor.GREEN + "VIP");
                else if(sign.getLine(1).equalsIgnoreCase("Legend")) sign.setLine(1, ChatColor.GREEN + "Legend");
                else if(sign.getLine(1).equalsIgnoreCase("StoreInfo")) sign.setLine(1, ChatColor.GREEN + "Store Info");
            }
            else
            {
                player.sendMessage(prefix + ChatColor.RED + "You do not have permission to do this!");
                event.setCancelled(true);
            }
        }
    }
     
    @EventHandler
    public void onBlockBreak(BlockBreakEvent event)
    {
        String prefix = replaceColors(getConfig().getString("Prefix"));
        Player player = event.getPlayer();
        if(event.getBlock().getState() instanceof Sign)
        {
            Sign s = (Sign) event.getBlock().getState();
            if(s.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix")))
            {
                if(player.hasPermission("ecore.signs.admin"))
                {
                    player.sendMessage(prefix + ChatColor.AQUA + "This Enstopia sign has been destroyed!");
                }
                else
                {
                    event.setCancelled(true);
                    player.sendMessage(prefix + ChatColor.RED + "You do not have permission to break an Enstopia Sign!");
                }
            }
        }
    }
     
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event)
    {
        String prefix = replaceColors(getConfig().getString("Prefix"));
        Player player = event.getPlayer();
        Action action = event.getAction();
        if(action == Action.RIGHT_CLICK_BLOCK)
        {
            if(event.getClickedBlock().getState() instanceof Sign)
            {
                Sign s = (Sign) event.getClickedBlock().getState();
                if(s.getLine(0).equalsIgnoreCase(getConfig().getString("Signprefix")))
                {
                    if(player.hasPermission("ecore.signs.use"))
                    {
                        if(s.getLine(1).equalsIgnoreCase("Premium"))
                        {
                            for(String string : getConfig().getStringList("Premium"))
                            {
                                player.sendMessage(replaceColors(string));
                            }
                        }
                        if(s.getLine(1).equalsIgnoreCase("VIP"))
                        {
                            for(String string : getConfig().getStringList("VIP"))
                            {
                                player.sendMessage(replaceColors(string));
                            }
                        }
                        if(s.getLine(1).equalsIgnoreCase("Legend"))
                        {
                            for(String string : getConfig().getStringList("Legend"))
                            {
                                player.sendMessage(replaceColors(string));
                            }
                        }
                        if(s.getLine(1).equalsIgnoreCase("Store Info"))
                        {
                            for(String string : getConfig().getStringList("Store"))
                            {
                                player.sendMessage(replaceColors(string));
                            }
                        }
                    }
                    else
                    {
                        player.sendMessage(prefix + ChatColor.RED + "You do not have permission to do this!");
                    }
                }
            }
        }
    }
     
    public String replaceColors(String s)
    {
        s = s.replaceAll("(&([a-fk-or0-9A-FK-OR]))", "\u00A7$2");
        return s;
    }
    
    With this config.yml:
    Code:
    Prefix: "&b[Enstopia] "
    Signprefix: "[Enstopia]"
    noperm: "&3You do not have permission to do this."
    Premium:
    - "&3Premium costs $20.00 and its features are:"
    - "&aAccess to Donor Lounge and arenas/maps."
    - "&aSet two Homes."
    - "&a/clear"
    - "&a$2,000 In game Money."
    - "&a/recipe"
    - "&a/heal (1 hour cooldown)."
    - "&a/fly"
    - "&a/kit premium (once per day)."
    - "&a/nick"
    - "&6[Premium]&a Tag in chat."
    VIP:
    - "&3VIP costs $40.00 and its features are all premium perks plus:"
    - "&aAccess to Donor Lounge and arenas/maps."
    - "&aSet fourHomes."
    - "&a/clear"
    - "&a$4,000 In game Money."
    - "&a/recipe"
    - "&a/heal (40 minute Cooldown)."
    - "&a/fly"
    - "&a/kit VIP (once per day)."
    - "&a/tptoggle"
    - "&a/near"
    - "&a/realname"
    - "&a/nick (with colors)."
    - "&a/workbench"
    - "&aColor Codes in Chat."
    - "&aDisguise as Mobs."
    - "&6[VIP] &aTag in chat."
    Legend:
    - "&3Legend costs $75.00 and its features are all Elite perks plus:"
    - "&aAccess to Donor Lounge and arenas/maps."
    - "&aSet ten Homes."
    - "&a/clear"
    - "&a$10,000 In game Money."
    - "&a/near"
    - "&a/realname"
    - "&a/recipe"
    - "&a/tptoggle"
    - "&a/heal (20 minute Cooldown)."
    - "&a/fly"
    - "&a/kit Legend (once per day)."
    - "&a/nick (with colors and format!)."
    - "&a/workbench"
    - "&aColor Codes and format Codes in Chat."
    - "&aMagic in Chat."
    - "&aMagic in Signs."
    - "&aFormat and magic in signs."
    - "&aColor in signs."
    - "&aDisguise as Mobs."
    - "&a/head <player> <quantity>"
    - "&a/starve"
    - "&6[Legend]&a Tag in chat."
    donation:
    - "&aThe command is used to get info of donor ranks. Commands are:"
    - "&3/donation Premium"
    - "&3/donation VIP"
    - "&3/donation Legend"
    Store:
    - "&aYou can find our store at http://enstopia.com/shop We offer:"
    - "&3Premium for $20.00"
    - "&3VIP for $40.00"
    - "&3Legend for $60.00"
    - "&3For more info, /warp Donations"
    
    It all worked, and I'm sorry that I mislead you on the if statements, as Signs apparently respond better to if they're a State when they're a Block class...

    I also had a flaw in my replaceColors() method where I didn't set the new String equal to the replaced String. Terribly sorry.

    EDIT: Once more, terribly sorry for taking a while to respond, as I was getting frustrated because the if statements with the Signs weren't passing, so I was debugging furiously.
     
  11. Offline

    random_username

    It worked, Thanks so much! :D
     
Thread Status:
Not open for further replies.

Share This Page