Solved Sign Interaction

Discussion in 'Plugin Development' started by TheMcScavenger, Sep 11, 2013.

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

    TheMcScavenger

    Hi,

    I've got this code, can't find any errors in it, but it doesn't work. Whenever I click a sign, it just doesn't do anything. Eclipse doesn't give me errors, the plugin loads fine, and gives no errors in the console.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    4. Block clickedblock = event.getClickedBlock();
    5. Player player = event.getPlayer();
    6. if(clickedblock.getTypeId() == Material.SIGN.getId() || clickedblock.getTypeId() == Material.SIGN_POST.getId()){
    7. Sign sign = (Sign) clickedblock;
    8. String Line1 = sign.getLine(0);
    9. String Line3 = sign.getLine(2);
    10. String Line4 = sign.getLine(3);
    11.  
    12. if(Line1 == "[Shop]"){
    13. if(Line3 == "1"){
    14. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "1");
    15. }if(Line3 == "2"){
    16. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "2");
    17. }if(Line3 == "3"){
    18. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "3");
    19. }if(Line3 == "4"){
    20. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "4");
    21. }if(Line3 == "5"){
    22. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "5");
    23. }if(Line3 == "6"){
    24. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "6");
    25. }if(Line3 == "7"){
    26. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "7");
    27. }if(Line3 == "8"){
    28. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "8");
    29. }if(Line3 == "9"){
    30. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "9");
    31. }
    32. }
    33. }
    34. }
    35. }
     
  2. Offline

    KanTe

    In line 6 try "instanceof" instead ==
     
  3. Offline

    Hoolean

    KanTe

    Nope, that's not an issue.

    TheMcScavenger

    Put in a few debug messages to see what is being called.

    Perhaps you have not registered your Listener?
     
  4. Offline

    TheMcScavenger

    Put in a few debug messages to see what is being called.
    ??

    Perhaps you have not registered your Listener?
    Haha, I have. I've made that mistake before :p
     
  5. Offline

    RainoBoy97

    Try on line 7
    Sign sign = (Sign) clickedblock.getState();

    :)
     
  6. Offline

    TheMcScavenger

    No, that didn't work either :(

    After some error searching, I found a number of mistakes. All fixed, and now it works:

    Code:java
    1. if(clickedblock.getTypeId() == Material.SIGN.getId() || clickedblock.getTypeId() == Material.SIGN_POST.getId()){

    Well, I was clicking a sign on the wall, so it should've been Material.WALL_SIGN.getId()... I feel
    like such a noob right now :D

    Code:java
    1. if(Line1 == "[Shop]"){
    2. if(Line3 == "1"){
    3. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "1");
    4. }

    I used ==, which doesn't work with strings. I had to use .equals() to get it to work:

    Code:java
    1. if(Line1.equals("[Shop]")){
    2. if(Line3.equals("1")){
    3. player.sendMessage(ChatColor.GRAY + "Opening Shop: " + ChatColor.GREEN + "1");
    4. }


    Thanks for everyones help!

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

    Loog

    TheMcScavenger It won't really make a difference but it would be better to use getType() and the Material.SIGN enum instead of getting the id of the sign through the Material enum. Or you could just do
    getTypeId() == 323 || 68 || 63

    Sorry for bad code, in on my phone
     
  8. Offline

    TheMcScavenger

    I actually had that before, but when it didn't work, I went on a rampage and changed about all the lines in the code. I guess I could change it back... But I probably won't :p
     
Thread Status:
Not open for further replies.

Share This Page