Solved Sign Click Event

Discussion in 'Plugin Development' started by RedEliteGamer, Jan 9, 2017.

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

    RedEliteGamer

    Hey guys
    I am newer developer. And i have a problem with one of my first plugin's. I want a java method triggered if a player hits a sign with a specific text.

    I have looked around and i have found some posts on how to do this but they seem to be older and not work when i tried to do them.

    If you could help me out with this I would appreciate it greatly.
    thanks,
     
  2. Offline

    Caedus

    1) Post your previous code? Even if it's faulty, perhaps you can be steered in the right direction.

    2) Use PlayerInteractEvent, Then check if the interaction was right clicking a block (if thats what you want), then check if the block is a sign.
     
  3. Offline

    RedEliteGamer

    Previous code I was Trying:
    Code:
        public void onInteract(PlayerInteractEvent Event) {
          Player player = Event.getPlayer();
          Block block = Event.getBlock();
          if(block.getState() instanceof Sign) {
          Sign sign = block.getState;
          String line1 = sign.getLine(1);
          player.sendMessage(line1); 
    Sorry I am a new coder so Im not sure what you mean by that, could you explain it a little more?
     
  4. Offline

    Zombie_Striker

    @RedEliteGamer
    1. Did you register your class?
    2. Do you have the @EventHandler tag?
    3. Are you sure block is not null? You never check.
    4. You forgot the "()" at the end of "getState" along with the cast. That would never have been able to be compiled.
    5. You never check if the line1 is equal to anything. You only print it out.
    6. Event starts with a capital letter. All variable names should start with a lowercase letter.
     
  5. Offline

    RedEliteGamer

    @Zombie_Striker
    1. I think so
    2. Yes
    3. Im not sure how to check
    4.I added the () but how would I add the cast
    5. That is just because I was testing to see if it can find the sign
    6.Fixed it

    Here is all of my code (Sorry if i missed something).
    Code:
    package me.carter.SignTesting;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Sign extends JavaPlugin{
        Logger myPluginLogger = Bukkit.getLogger();
      public void onEnable(){   
          myPluginLogger.info("SignTest Plugin Is Starting"); 
      }
      public void onDisable(){
          myPluginLogger.info("SignTest Plugin Is Starting");
      }
     
      @EventHandler
      public void onInteract(PlayerInteractEvent event) {
         
          Player player = event.getPlayer();
          Block block = event.getBlock();
          if(block.getState() instanceof Sign) {
          Sign sign = block.getState();
          String line1 = sign.getLine(1);
          player.sendMessage(line1);
          }
      }
    }
     
    Last edited: Jan 10, 2017
  6. Offline

    Zombie_Striker

    No you did not.

    To register the class, let the class implement Listener, import listener, and then add this line to the onEnable
    Code:
    getServer().getPluginManager().registerEvents(this,this);
    The first "this" is the Listener (the class with the events), and the second "this" is the main class (telling bukkit which plugin is registering this class)
     
  7. Offline

    RedEliteGamer

    @Zombie_Striker
    Sorry i feel like a Noob lol, but how would I do this?
    And so i just replace the "This" with what you said?
     
  8. Offline

    Zombie_Striker

    @RedEliteGamer
    No. You don't need to replace anything. Just copy that line into the onEnable and let your class implement Listener.
     
  9. Offline

    RedEliteGamer

    @Zombie_Striker
    Ok so I have added the Listener But its still not working, Here is code:
    Code:
    package me.carter.SignTesting;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Sign extends JavaPlugin implements Listener{
        Logger myPluginLogger = Bukkit.getLogger();
      public void onEnable(){  
          myPluginLogger.info("SignTest Plugin Is Starting");
          getServer().getPluginManager().registerEvents(this,this);
      }
      public void onDisable(){
          myPluginLogger.info("SignTest Plugin Is Starting");
      }
    
      @EventHandler
      public void onInteract(PlayerInteractEvent event) {
        
          Player player = event.getPlayer();
          Block block = event.getBlock();
          if(block.getState() instanceof Sign) {
          Sign sign = block.getState();
          String line1 = sign.getLine(1);
          player.sendMessage(line1);
          }
      }
    }
    RedPart Is underlined Red:

    Player player = event.getPlayer();
    Block block = event.getBlock();
    if(block.getState() instanceof Sign) {
    Sign sign = block.getState();
    String line1 = sign.getLine(1);
    player.sendMessage(line1);
     
  10. Offline

    Zombie_Striker

    @RedEliteGamer
    1. It is getClickedBlock, not getBlock
    2. You are not casting the state to a Sign
    3. I will have to review all the methods, but it may be that you have to use another method.
     
  11. Offline

    RedEliteGamer

    @Zombie_Striker
    Ok so i have fixed what you said and here is my code:
    Code:
    package me.carter.SignTesting;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Sign extends JavaPlugin implements Listener{
        Logger myPluginLogger = Bukkit.getLogger();
      public void onEnable(){  
          myPluginLogger.info("SignTest Plugin Is Starting");
          getServer().getPluginManager().registerEvents(this,this);
      }
      public void onDisable(){
          myPluginLogger.info("SignTest Plugin Is Starting");
      }
    
      @EventHandler
      public void onInteract(PlayerInteractEvent event) {
        
          Player player = event.getPlayer();
          Block block = event.getClickedBlock();
          if(block.getState() instanceof Sign) {
          Sign sign = (Sign) block.getState();
          String line1 = sign.getLine(0);
          player.sendMessage(line1);
          }
      }
    }
    Now the only red item is the ".getLine" part

    EDIT: Ok so i added "Bukkit.broadcastMessage("You Have Clicked A Sign");" after the " if(block.getState() instanceof Sign) {" too see if it recognized that I clicked the sign and it did not display the broadcast so I am assuming that the methods that check if the sign has been clicked are wrong. (if that helps at all)
     
    Last edited: Jan 11, 2017
  12. Offline

    Zombie_Striker

    What does the error say?
     
  13. Offline

    RedEliteGamer

    @Zombie_Striker
    When I start up my server it shows no errors loading the plugin in the Command prompt, but when I test the plugin it does not work.
     
  14. Offline

    MrGriefer_

    @RedEliteGamer

    Try checking if its Material.WALL_SIGN or if its Material.SIGN_POST.
    And cancel the event after sending the message.
     
  15. Offline

    RedEliteGamer

    @MrGriefer_ @Zombie_Striker
    Ok so i got the main sign click detecting part to work using a different method but the .getline part in the code doesn't work. Im using spigot/minecraft 1.10.2 so would they have removed the .getline in that version?
     
  16. Offline

    Zombie_Striker

    @RedEliteGamer
    Nope. Just checked. It still there.

    What is the error you are getting?
     
  17. Offline

    RedEliteGamer

    Last edited: Jan 12, 2017
  18. Offline

    Payless

    Alright i have one thing to tell you as a Java Developer. Java is so sensitive to the point if you dont caps something or write it correctly it will break.

    your problem there: getline(0); needs to be getLine(0);
     
  19. Offline

    MrGriefer_

    In his code he wrote getLine(0); with caps otherwise it's getLine(0); instead of getline(0);
     
  20. Offline

    RedEliteGamer

    @Payless @MrGriefer_
    Ok i tried it with caps and without caps but it is still not doing anything
    could someone try putting this into eclipse and trying it for themselves?
     
  21. Offline

    MrGriefer_

    Well in your last code, your just sending the player line 1 but your not checking if line 1 contains a specific text! And did you try canceling the event?
     
  22. Offline

    RedEliteGamer

    @MrGriefer_
    The reason I was just sending line 1 was too see if the code was working, And what do you mean by Try canceling the event?

    @Zombie_Striker
    Did you manage to figure out the problem yet?
     
  23. Offline

    Zombie_Striker

    @RedEliteGamer
    [Edit] You named your main class sign. That is your issue. Java thinks you are casting the blockstate to the main class, not the sign blockstate. You will need to change the name of your main class to something else (I would recommend changing it to the name of your plugin, since that is convention) and import the Sign class.
     
  24. Offline

    MrGriefer_

    event#setCancelled(true);
     
  25. Offline

    RedEliteGamer

Thread Status:
Not open for further replies.

Share This Page