Interact and Place event errors

Discussion in 'Plugin Development' started by Sweatyyyy, Dec 14, 2013.

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

    Sweatyyyy

    I am trying to make a plugin where when you right click a sign with [Heal] on it, it heals the player if they have permissions, and the sign with will change to [§1Heal§0].
    Code:
    Code:java
    1. package me.louie.HealSign;
    2.  
    3. import org.bukkit.block.Block;
    4. import org.bukkit.block.Sign;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.block.BlockPlaceEvent;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class HealSign extends JavaPlugin implements Listener{
    14. @Override
    15. public void onEnable(){
    16. this.getServer().getPluginManager().registerEvents(this, this);
    17. }
    18. @EventHandler
    19. public void onInteract(PlayerInteractEvent event){
    20. Player player = event.getPlayer();
    21. double health = player.getMaxHealth();
    22. Block block = event.getClickedBlock();
    23. Sign sign = (Sign) block;
    24. if(player.hasPermission("HealSign.use")){
    25. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    26. if(sign.getLine(0).equalsIgnoreCase("[Heal]")){
    27. player.setHealth(health);
    28. }
    29. }
    30. }
    31. }
    32.  
    33. @EventHandler
    34. public void onPlace(BlockPlaceEvent event){
    35. Block block = event.getBlockPlaced();
    36. Sign sign = (Sign) block;
    37. if(sign.getLine(0).equalsIgnoreCase("[Heal]")){
    38. sign.setLine(0, "[§1Heal§0]");
    39. sign.update();
    40. }
    41. }
    42.  
    43. }
    44.  

    Error:
    http://gyazo.com/e4bdfbc6dd966426bfe86cb0277bb512
    http://gyazo.com/251487bd7c0a591181a84951e9bffff5
    Them errors show up when I place a sign
     
  2. Offline

    random_username

    Try using the SignChangeEvent instead of the BlockPlaceEvent. The error for your interact event says you are casting the sign, before making sure the block is a sign. So, you are assuming the block interacted is a sign, first you should try
    Code:java
    1. if(event.getClickedBlock().getState() instanceof Sign){

    and then you can do
    Code:java
    1. Sign sign = (Sign) event.getClickedBlock().getState();

    Also in the playerInteractEvent instead of
    Code:java
    1. if(sign.getLine(0).equalsIgnoreCase("[Heal]")){

    use:
    Code:java
    1. if(sign.getLine(0).equalsIgnoreCase("[§1Heal§0]")){
     
    Sweatyyyy likes this.
  3. Offline

    Sweatyyyy

  4. Offline

    Developing

    Sweatyyyy You are doing it for someone in the plugin requests.... Remember to compile it with Java 6.
     
  5. It would probably be better to use chatColor.COLOR instead of The color symbol, for code readability.
     
Thread Status:
Not open for further replies.

Share This Page