Sign Click event?

Discussion in 'Plugin Development' started by PolarCraft, Oct 16, 2013.

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

    PolarCraft

    I do not get how to make a sign click event. If anyone can explain how to that would be great!
    Main class:
    Code:java
    1. package net.tc.minecraft;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.SignChangeEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class main extends JavaPlugin
    12. implements Listener {
    13. public void onEnable()
    14. {
    15. System.out.println("TimeCraft Test v.1 enabled!");
    16. getServer().getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. public void onDisable() {
    20. System.out.println("TimeCraft Test v.1 disabled!!");
    21. }
    22.  
    23. @EventHandler(priority=EventPriority.HIGHEST)
    24. public void TimeGate(SignChangeEvent sign) {
    25. Player player = sign.getPlayer();
    26. if (sign.getLine(0).equalsIgnoreCase("tg")) {
    27. sign.setLine(0, ChatColor.GOLD + "[TimeGate]");
    28. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    29. }
    30. }
    31. }


    plugin.yml:
    Code:java
    1. name: TimeCraft
    2. main: net.tc.minecraft.main
    3. version: 10-13-13
     
  2. Offline

    The_Doctor_123

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event)
    3. {
    4. if (event.getClickedBlock().getState() instanceof Sign)
    5. {
    6. // Do Stuff
    7. }
    8. }
     
  3. Offline

    PolarCraft

    The_Doctor_123 how would i make it so that the block of the sign could be clicked when it is on a block or on a post?
     
  4. Offline

    thecrystalflame

    you should be able to do womething like this
    Code:java
    1.  
    2. @EventHandler
    3. public void playerInteractEvent(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (event.getClickedBlock().getType() == material.SIGN) {
    6. //do something
    7. }
    8. }
    9. }
    10.  
    11.  



    you should really check that the events action is right_click_block
    other wise everytime the player interacts with something that isnt right clicking a block then your if statement would throw an error, null.getState() doesnt quite work

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

    PolarCraft

    thecrystalflame That did not work. Says:
    sign cannot be resolved
    Current code:
    Code:java
    1. package net.tc.minecraft;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.EventPriority;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.block.SignChangeEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class main extends JavaPlugin
    15. implements Listener {
    16. public void onEnable()
    17. {
    18. System.out.println("TimeCraft Test v.1 enabled!");
    19. getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. public void onDisable() {
    23. System.out.println("TimeCraft Test v.1 disabled!!");
    24. }
    25.  
    26. @EventHandler(priority=EventPriority.HIGHEST)
    27. public void playerInteractEvent(PlayerInteractEvent event) {
    28. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    29. if (event.getClickedBlock().getType() == Material.SIGN) {
    30. Player player = sign.getPlayer();
    31. if (sign.getLine(0).equalsIgnoreCase("tg")) {
    32. sign.setLine(0, ChatColor.GOLD + "[TimeGate]");
    33. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    34. }
    35. }
    36. }
    37. }
    38. }
    39.  
     
  6. Offline

    thecrystalflame

    sorry but where has sign been initialized?
     
  7. Offline

    PolarCraft

    thecrystalflame it should be "Player player = sign.getPlayer();" but i take it it is not.
     
  8. Offline

    thecrystalflame

    event.getPlayer()

    you need to initialize a Sign first, add this into your code inside the if statement that checks if the block is a sign
    Code:java
    1.  
    2. Sign sign = (Sign)event.getClickedBlock().getState;
    3.  


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

    PolarCraft

    thecrystalflame Once i did that the sign variable is not being recognized.
     
  10. Offline

    thecrystalflame

    can you please repaste the code now?
     
  11. Offline

    PolarCraft

    Code:java
    1. package net.tc.minecraft;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.EventPriority;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.block.SignChangeEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class main extends JavaPlugin
    15. implements Listener {
    16. public void onEnable()
    17. {
    18. System.out.println("TimeCraft Test v.1 enabled!");
    19. getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. public void onDisable() {
    23. System.out.println("TimeCraft Test v.1 disabled!!");
    24. }
    25.  
    26. @EventHandler(priority=EventPriority.HIGHEST)
    27. public void playerInteractEvent(PlayerInteractEvent event) {
    28. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    29. if (event.getClickedBlock().getType() == Material.SIGN) {
    30. Player player = event.getPlayer();
    31. if (sign.getLine(0).equalsIgnoreCase("tg")) {
    32. sign.setLine(0, ChatColor.GOLD + "[TimeGate]");
    33. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    34. }
    35. }
    36. }
    37. }
    38. }
     
  12. Offline

    thecrystalflame

    this is what worked for me

    Code:java
    1.  
    2. @EventHandler
    3. public void playerInteractEvent2(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (event.getClickedBlock().getType() == Material.SIGN) {
    6. Sign sign = (Sign) event.getClickedBlock().getState(); //this line is required
    7. Player player = event.getPlayer();
    8. if (sign.getLine(0).equalsIgnoreCase("tg")) {
    9. sign.setLine(0, ChatColor.GOLD + "[TimeGate]");
    10. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    11. }
    12. }
    13. }
    14. }
    15.  
     
  13. Offline

    PolarCraft

    thecrystalflame If i wanted to put more signs how would i do so? Like when i do the exact same code but change the variables it does not want to work.
     
  14. Offline

    thecrystalflame

    you could do else ifs or a switch- case
     
  15. Offline

    PolarCraft

    so? example:
    Code:java
    1. @EventHandler
    2. public void playerInteractEvent2(PlayerInteractEvent event) {
    3. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    4. if (event.getClickedBlock().getType() == Material.SIGN) {
    5. Sign sign = (Sign) event.getClickedBlock().getState(); //this line is required
    6. Player player = event.getPlayer();
    7. if (sign.getLine(0).equalsIgnoreCase("tg")) {
    8. sign.setLine(0, ChatColor.GOLD + "[TimeGate]");
    9. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    10. }
    11. }
    12. }
    13. }
    14. @EventHandler
    15. public void playerInteractEvent3(PlayerInteractEvent event) {
    16. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    17. if (event.getClickedBlock().getType() == Material.SIGN) {
    18. Sign sign2 = (Sign) event.getClickedBlock().getState(); //this line is required
    19. Player player = event.getPlayer();
    20. if (sign2.getLine(0).equalsIgnoreCase("w")) {
    21. sign2.setLine(0, ChatColor.DARK_RED + "[Warning]");
    22. if (sign2.getLine(1).equalsIgnoreCase("5sd")) {
    23. sign2.setLine(0, ChatColor.DARK_RED+ "5 second delay!");
    24. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    25. }
    26. }
    27. }
    28. }
    29. }
    30. }
     
  16. Offline

    thecrystalflame

    no like this

    Code:java
    1.  
    2. @EventHandler
    3. public void playerInteractEvent3(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (event.getClickedBlock().getType() == Material.SIGN) {
    6. Sign sign = (Sign) event.getClickedBlock().getState(); //this line is required
    7. Player player = event.getPlayer();
    8. if (sign.getLine(0).equalsIgnoreCase("w")) {
    9. sign.setLine(0, ChatColor.DARK_RED + "[Warning]");
    10. if (sign.getLine(1).equalsIgnoreCase("5sd")) {
    11. sign.setLine(0, ChatColor.DARK_RED+ "5 second delay!");
    12. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    13. }
    14. } else if (sign.getLine(0).equalsIgnoreCase("tg")) {
    15. sign.setLine(0, ChatColor.GOLD + "[TimeGate]");
    16. player.sendMessage(ChatColor.DARK_AQUA + "[TimeGate] " + ChatColor.GREEN + "Sign Created!");
    17. }
    18. }
    19. }
    20. }
    21.  
     
Thread Status:
Not open for further replies.

Share This Page