Casting Problems?

Discussion in 'Plugin Development' started by 22vortex22, Jan 10, 2014.

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

    22vortex22

    Hey peoplez I've been currently trying to add sign integration to my plugin. I have however gotten alot of trouble.

    Here is the code for the event that is given me trouble:

    Code:java
    1. @EventHandler
    2. public void onSignClick(PlayerInteractEvent event){
    3. Block block = event.getClickedBlock();
    4. Player player = event.getPlayer();
    5. Sign sign = (Sign) block.getState();
    6. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    7. if (block.getType() == Material.SIGN_POST || block.getType() == Material.SIGN || block.getType() == Material.WALL_SIGN){
    8. if (player.hasPermission("EasyPvpKits.Sign.Use")){
    9. if (sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_BLUE + "Pvp")){
    10. player.performCommand("Pvp");
    11. }
    12. if (sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_BLUE + "Archer")){
    13. player.performCommand("Archer");
    14. }
    15. if (sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_BLUE + "Heavy")){
    16. player.performCommand("Heavy");
    17. }
    18. if (sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_BLUE + "Assassin")){
    19. player.performCommand("Assassin");
    20. }
    21. if (sign.getLine(1).equalsIgnoreCase(ChatColor.DARK_BLUE + "Pyro")){
    22. player.performCommand("Pyro");
    23. }
    24. }
    25. }
    26. }
    27. }


    Whenever I click a block that isn't a sign it gives me

    Code:java
    1. Caused by: java.lang.NullPointerException
    2. at com.gmail.codervortex.EasyPvpKits.onSignClick(EasyPvpKits.java:131) ~


    Line 131 is:

    Code:java
    1. Sign sign = (Sign) block.getState();


    And when I do click a sign it gives me this:
    Code:java
    1. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftBlockState cannot be cast to org.bukkit.block.Sign



    Can someone please help me? I know its one line and probably a second to fix but I have really busy lately and it is right in front of my nose. I feel really stupid for not knowing how to fix it. Anyways thanks in advance.
     
  2. Offline

    rangersmash

    I don't know much about casting, but possibly because you set a variable called Block then try to send it to sign? Try this. Let me know if I can help anymore.

    Sign sign = (Sign) event.getClickedBlock().getState();
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    22vortex22 Casting is not a magic that allows you to turn any type into another type.

    Casting only lets you tell the compiler which does not have full information, that this BlockState is really a Sign. The JVM at runtime will make sure that the BlockState is indeed a Sign. If you are wrong however, the JVM will throw a ClassCastException.
     
  4. Offline

    rangersmash

  5. Offline

    22vortex22



    Now, when I don't click a sign it gives me
    Code:java
    1. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftBlockState cannot be cast to org.bukkit.block.Sign
     
  6. Offline

    Not2EXceL

    22vortex22 Did you even read what Sagavious_Zed said? (didnt tag since its a useless tag)

    You're casting the block before even checking if the block is a sign.
     
  7. Offline

    22vortex22


    Sorry my internet is slow here so by the time it got sent he already replied.
     
Thread Status:
Not open for further replies.

Share This Page