Right click sign to run command

Discussion in 'Plugin Development' started by Glass_Eater84, Aug 21, 2014.

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

    Glass_Eater84

    Hey guys,

    I'm trying to make it when you right click a sign it runs the command /feather however it is not working. This is my code. Any help is much appreciated!

    Code:java
    1.  
    2. import java.util.logging.Logger;
    3.  
    4. import org.bukkit.block.Sign;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.block.SignChangeEvent;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10.  
    11. public class SignCommand implements Listener {
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13.  
    14.  
    15. @EventHandler
    16. public void onSignChange(SignChangeEvent e) {
    17. if (e.getLine(0).equalsIgnoreCase("[Feather]")) {
    18. e.setLine(0, "§3=========================");
    19. e.setLine(1, "§3Click here to");
    20. e.setLine(2, "§3get a feather!");
    21. e.setLine(3, "§3=========================");
    22. }
    23. }
    24.  
    25. @EventHandler
    26. public void onPlayerInteract(PlayerInteractEvent e) {
    27. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK));
    28. if (e.getClickedBlock().getState() instanceof Sign) {
    29. Sign s = (Sign) e.getClickedBlock().getState();
    30. if (s.getLine(0).equalsIgnoreCase("[Feather]")){
    31. e.getPlayer().chat("feather");
    32. }
    33. }
    34. }
    35. }


    Thanks,
    Glass
     
  2. Offline

    Dragonphase

  3. Offline

    Glass_Eater84

    Dragonphase Thanks, I tried but had no success. This is my updated code.
    SignCommad class:
    Code:java
    1. public class SignCommand implements Listener {
    2.  
    3.  
    4.  
    5. @EventHandler
    6. public void onSignChange(SignChangeEvent e) {
    7. if (e.getLine(0).equalsIgnoreCase("[Feather]")) {
    8. e.setLine(0, "§3=========================");
    9. e.setLine(1, "§3Click here to");
    10. e.setLine(2, "§3get a feather!");
    11. e.setLine(3, "§3=========================");
    12. }
    13. }
    14.  
    15. @EventHandler
    16. public void onPlayerInteract(PlayerInteractEvent e) {
    17. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK));
    18. if (e.getClickedBlock().getState() instanceof Sign) {
    19. Sign s = (Sign) e.getClickedBlock().getState();
    20. if (s.getLine(0).equalsIgnoreCase("[Feather]")){
    21. Bukkit.dispatchCommand(e.getPlayer(), "feather");
    22. }
    23. }
    24. }
    25. }


    And heres my registered events: (Main Class)
    Code:java
    1. public class Main extends JavaPlugin
    2. {
    3. public void onDisable()
    4. {
    5. System.out.println(Messages.shoutdown);
    6. }
    7. public final Logger plugin = Logger.getLogger("Minecraft");
    8. public void onEnable()
    9. {
    10. Bukkit.getServer().getPluginManager().registerEvents(new SignCommand(), this);
     
  4. Offline

    Dragonphase

    Glass_Eater84

    When you create a sign with "[Feather]" as the first line, you're replacing it in your onSignChange EventHandler. So your conditional check on line 20 will always return false.
     
  5. Offline

    Glass_Eater84

    Dragonphase I put this check on line 20 because the first line(Feather) wasn't getting replaced. However, neither the command function nor the replacement is working, this is why I though there might be a bug in the registerEvents
     
  6. Offline

    timtower Administrator Administrator Moderator

    Glass_Eater84 Try to debug to find out to where the event is running, might not even reach the place what you want it to reach.
    And try to send a message when they dispatch the command. Useful for debugging.
     
  7. Offline

    Glass_Eater84

    timtower How would I do that, specifically?
     
  8. Offline

    timtower Administrator Administrator Moderator

    Glass_Eater84 player.sendMessage, System.out.println, plugin.getLogger().info
    Anything to know where it is doing stuff will do
     
  9. Offline

    Glass_Eater84

    timtower Im not getting any errors nor is it showing up...
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    stormneo7

    There are more than one type of signs. Add multiple checks for these.

    Code:java
    1. Material.SIGN
    2. Material.WALL_SIGN
    3. Material.SIGN_POST


    are the ones I believe. Not 100% sure.
     
  12. Offline

    Glass_Eater84

    timtower
    Code:java
    1.  
    2. public class SignCommand implements Listener {
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4.  
    5.  
    6. @EventHandler
    7. public void onSignChange(SignChangeEvent e) {
    8. if (e.getLine(0).equalsIgnoreCase("[Feather]")) {
    9. e.setLine(0, "§3=========================");
    10. e.setLine(1, "§3Click here to");
    11. e.setLine(2, "§3get a feather!");
    12. e.setLine(3, "§3=========================");
    13. }
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerInteract(PlayerInteractEvent e) {
    18. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    19. if (e.getClickedBlock().getState() instanceof Sign) {
    20. Sign s = (Sign) e.getClickedBlock().getState();
    21. if (s.getLine(0).equalsIgnoreCase("[Feather]")){
    22. e.getPlayer().chat("feather");
    23.  
    24. }
    25. }
    26. }
    27. }
     
  13. Offline

    timtower Administrator Administrator Moderator

  14. Offline

    Glass_Eater84

    timtower wrong code I undid what I did because it didnt work updated
    here it is:
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent e) {
    4. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    5. if (e.getClickedBlock().getState() instanceof Sign) {
    6. Sign s = (Sign) e.getClickedBlock().getState();
    7. if (s.getLine(0).equalsIgnoreCase("[Feather]")){
    8. e.getPlayer().chat("feather");
    9. System.out.println(Messages.debug);
    10. }
    11.  
    12. }
    13. }
    14. }
    15.  
     
  15. Offline

    stormneo7

    Glass_Eater84
    I'm just curious of what you're trying to do.
    Your SignChangeEvent changes signs with line[0] "[Feather]" to a random panel.
    Your PlayerInteractEvent triggers when a player interacts with a sign that has "[Feather]" in line[0] but that'll never occur due to your SignChangeEvent.
     
  16. Offline

    Glass_Eater84

    stormneo7 Well, what I was trying to do is make it so whenever I type [Feather] on the sign, and someone clicks it, it will give run the command /feather
     
  17. Offline

    Xyplo

    Glass_Eater84 I'm going to update your code to something that I would probably use. I hope this helps in some way. (As per usual this is NOT tested.)
    SignCommand
    Code:java
    1. public class SignCommand implements Listener {
    2.  
    3. Main m;
    4. public SignCommand(Main instance)
    5. {
    6. m= instance;
    7. }
    8.  
    9. @EventHandler
    10. public void onSignChange(SignChangeEvent e) {
    11. if (e.getLine(0).equalsIgnoreCase("[Feather]")) {
    12. e.setLine(0, "§3=========================");
    13. e.setLine(1, "§3Click here to");
    14. e.setLine(2, "§3get a feather!");
    15. e.setLine(3, "§3=========================");
    16. }
    17. }
    18.  
    19. @EventHandler
    20. public void onPlayerInteract(PlayerInteractEvent e) {
    21. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK));
    22. if (e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST)
    23. Sign s = (Sign) e.getClickedBlock().getState();
    24. if (s.getLine(0).equalsIgnoreCase("[Feather]")){
    25. Bukkit.dispatchCommand(e.getPlayer(), "feather");
    26. }
    27. }
    28. }
    29. }


    And for your Main class, the following

    Code:java
    1. public class Main extends JavaPlugin
    2. {
    3. public void onDisable()
    4. {
    5. System.out.println(Messages.shoutdown);
    6. }
    7. public final Logger plugin = Logger.getLogger("Minecraft");
    8. public void onEnable()
    9. {
    10. Bukkit.getServer().getPluginManager().registerEvents(new SignCommand(this), this); // This is what i've changed.
    11. }
    12. } // end of Class
     
  18. Offline

    stormneo7

    I know how making multiple classes show a sign of organization but in this case, it's just two listeners. Just do it in the main class lol...

    Also, wtf does this do?
    Code:java
    1. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK));


    I also don't get this insanity... The first event PREVENTS signs to BECOME [Feather] so the 2nd event will NEVER trigger.
     
  19. Offline

    Xyplo


    I agree, you could just put it in one class, but in this case he's using two classes so we have to work with them two classes.

    and for
    Code:java
    1. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK));
    [/quote] I am with you on that one, I just left that in, I guess he could remove it to see if it works. (OR TRY LEFT CLICKING!!!)
     
  20. Offline

    Glass_Eater84

    Xyplo Thanks. However, I am still getting an error here:
    Code:java
    1. Sign s = (Sign) e.getClickedBlock().getState();
    2. if (s.getLine(0).equalsIgnoreCase("[Feather]")){
    3. Bukkit.dispatchCommand(e.getPlayer(), "feather");
    4. }

    "Sign can not be resolved as a type"
    Also, I removed
    Code:java
    1. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK));
     
Thread Status:
Not open for further replies.

Share This Page