PlayerInteractEvent Signs...

Discussion in 'Plugin Development' started by Chibbey, Nov 5, 2013.

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

    Chibbey

    Hello, I was testing out the sign feature for a kitpvp plugin im making. And I have to use the PlayerInteractEvent to know when a player interacts with a sign. So I tried and looked up videos and stuff and it would work. The SignChangeEvent works but not the Interact with signs for me here my code if you could tell me what I did wrong


    Code:java
    1. package me.chibbey.signtest;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class SignInteract extends JavaPlugin implements Listener {
    15.  
    16. @EventHandler
    17. public void onSignInteract(PlayerInteractEvent e) {
    18. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    19. if (!(e.getClickedBlock().getType() == Material.SIGN_POST) && !(e.getClickedBlock().getType() == Material.WALL_SIGN)) return;
    20. Sign s = (Sign) e.getClickedBlock().getState();
    21. if (s.getLine(0).equalsIgnoreCase("[i]")) {
    22. e.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    23. e.getPlayer().sendMessage(ChatColor.DARK_GREEN + "You got a Diamond!");
    24.  
    25. }
    26. }
    27.  
    28.  
    29. public void onEnable() {
    30. Bukkit.getServer().getPluginManager().registerEvents(new SignChange(), this);
    31. }
    32.  
    33. }
    34. [/i]

    Also I tried this
    Code:java
    1. package me.chibbey.signtest;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class SignInteract extends JavaPlugin implements Listener {
    15.  
    16. @EventHandler
    17. public void onSignInteract(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("[i]")) {
    22. e.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    23. e.getPlayer().sendMessage(ChatColor.DARK_GREEN + "You got a Diamond!");
    24. }
    25. }
    26. }
    27.  
    28.  
    29. public void onEnable() {
    30. Bukkit.getServer().getPluginManager().registerEvents(new SignChange(), this);
    31. }
    32.  
    33. }
    34. [/i]
     
  2. Offline

    wizzinangel

    Chibbey are you saying in your code if the action doesn't equal with the ! In there?

    Code:java
    1.  
    2. public void onSignInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    4. if (!(e.getClickedBlock().getState() instanceof Sign) {
    5.  


    Should be:

    Code:java
    1.  
    2. public void onSignInteract(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    4. if (e.getClickedBlock().getState() instanceof Sign) {
    5. //do stuff for sign here
    6.  


    So now your code reads: If action is right click on the block, and the clicked block is an instance of a sign. Then do something.
     
    Mathias Eklund likes this.
  3. Offline

    Mathias Eklund

    Yeah those !'s after the if statements are not supposed to be there i assume.
     
  4. Offline

    Chibbey

  5. Offline

    Mathias Eklund

    add
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);
     
  6. Offline

    Chibbey

  7. Offline

    Mathias Eklund

    Chibbey Yes i did, if i am not mistaken you are only registering an event in a different class.
     
  8. Offline

    Chibbey

    Mathias Eklund
    Wow, you don't know much, Im registering the events in my other class and this class...
     
  9. Offline

    jimuskin

    Chibbey be nicer to people, we don't have to help you you know.

    Anyway, by telling us "I tried its not working" doesn't help us in any way. What doesn't work? What does the console output?
     
  10. Offline

    drtshock

    lolwut. Why are you creating a new instance of your main class? Just registerEvents(this, this);
     
    jimuskin likes this.
  11. Offline

    Chibbey

    Anyways I figured it out I did need to put the ! before the Action and I registered the other class because I got 2 classes, It worked because the register events wasn't before the event it was after so I put the onEnable() on top and it worked :)
     
Thread Status:
Not open for further replies.

Share This Page