PlayerInteractEvent issues

Discussion in 'Plugin Development' started by ColaCraft, Jun 14, 2013.

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

    ColaCraft

    Hey guys, the code seems to not have any big (red underlined) errors.

    It's supposed to do stuff when you right click a sign, but nothing happens when I right click a sign.


    Here is my code, could you check it over for errors please?

    Code:java
    1. package me.PorterK.ColaSigns;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.BlockState;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.block.Sign;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. import java.util.logging.Logger;
    17.  
    18. public class main extends JavaPlugin {
    19.  
    20. private final Logger log = Logger.getLogger("Minecraft");
    21. private final String logTag = "[ColaSigns] " + ChatColor.RESET;
    22.  
    23. private FileConfiguration config;
    24. private PluginDescriptionFile pdfFile;
    25.  
    26. @Override
    27. public void onEnable(){
    28. this.pdfFile = this.getDescription();
    29. this.config = this.getConfig();
    30. log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " enabled!");
    31. }
    32.  
    33. @Override
    34. public void onDisable(){
    35. log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " disabled!");
    36. }
    37. @EventHandler
    38. public void onPlayerInteract(PlayerInteractEvent event){
    39. if(event.getClickedBlock().getType() == Material.SIGN_POST || event.getClickedBlock().getType() == Material.WALL_SIGN || event.getClickedBlock().getType() == Material.SIGN) {
    40.  
    41. BlockState state = event.getClickedBlock().getState();
    42.  
    43. if (state instanceof Sign) {
    44. final Sign s = (Sign)event.getClickedBlock().getState();
    45. Player player = event.getPlayer();
    46. if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
    47.  
    48. if(s.getLine(0).contains("[TDM]")){
    49.  
    50. player.sendMessage(ChatColor.GREEN + "Joining TDM");
    51.  
    52. }
    53. if(s.getLine(3).contains("2")){
    54. Bukkit.getServer().dispatchCommand(player, "pa join fallen");
    55. }
    56. if(s.getLine(0).contains("[What is]")){
    57. if(s.getLine(1).contains("Factions")){
    58. player.sendMessage(ChatColor.RED + "This plugin will allow the players on the server to create factions/guilds. The factions can claim territory that will be protected from non-members. Factions can forge alliances and declare themselves enemies with others. Land may be taken from other factions through war.");
    59. }
    60. if(s.getLine(1).contains("Skyblock")){
    61. player.sendMessage(ChatColor.RED + "Skyblock is a type of survival that is challenge orientated. You spawn on an island in the sky with limited (but enough) resources and must expand your island to become an empire. The original idea has been around for a while now, and can be played in single player mode by downloading a map, or on multiplayer servers like this one!");
    62. }
    63. if(s.getLine(1).contains("Mingames")){
    64. player.sendMessage(ChatColor.RED + "Minigames include Spleef, Ultimate PVP, TDM, & Parkour! You can play these for fun if you get bored of any of the other worlds ColaCraft has to offer!");
    65. }
    66. if("Hunger Games".equalsIgnoreCase(s.getLine(1))){
    67. player.sendMessage(ChatColor.RED + "Hunger Games is a minecraft minigame based off of the popular movie & book series, The Hunger Games. There are several arenas & you spawn with minimal supplies. Everyone gets equal oppertunity- it's just fun! I encourage you to join if you're getting bored of any other worlds!");
    68. }
    69. }
    70. }
    71. }
    72. }
    73. }
    74. }
    75.  


    Thanks.
     
  2. Offline

    Ultimate_n00b

    Make sure to register your main class as a listener.
     
    ColaCraft likes this.
  3. Offline

    ColaCraft


    I did that & I'm still having the same issue.

    Here is the updated code.

    Code:java
    1. package me.PorterK.ColaSigns;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.BlockState;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.block.Sign;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. import java.util.logging.Logger;
    18.  
    19. public class main extends JavaPlugin implements Listener {
    20.  
    21. private final Logger log = Logger.getLogger("Minecraft");
    22. private final String logTag = "[ColaSigns] " + ChatColor.RESET;
    23.  
    24. private FileConfiguration config;
    25. private PluginDescriptionFile pdfFile;
    26.  
    27. @Override
    28. public void onEnable(){
    29. this.pdfFile = this.getDescription();
    30. this.config = this.getConfig();
    31. log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " enabled!");
    32. }
    33.  
    34. @Override
    35. public void onDisable(){
    36. log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " disabled!");
    37. }
    38. @EventHandler
    39. public void onPlayerInteract(PlayerInteractEvent event){
    40. if(event.getClickedBlock().getType() == Material.SIGN_POST || event.getClickedBlock().getType() == Material.WALL_SIGN || event.getClickedBlock().getType() == Material.SIGN) {
    41.  
    42. BlockState state = event.getClickedBlock().getState();
    43.  
    44. if (state instanceof Sign) {
    45. final Sign s = (Sign)event.getClickedBlock().getState();
    46. Player player = event.getPlayer();
    47. if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
    48.  
    49. if(s.getLine(0).contains("[TDM]")){
    50.  
    51. player.sendMessage(ChatColor.GREEN + "Joining TDM");
    52.  
    53. }
    54. if(s.getLine(3).contains("2")){
    55. Bukkit.getServer().dispatchCommand(player, "pa join fallen");
    56. }
    57. if(s.getLine(0).contains("[What is]")){
    58. if(s.getLine(1).contains("Factions")){
    59. player.sendMessage(ChatColor.RED + "This plugin will allow the players on the server to create factions/guilds. The factions can claim territory that will be protected from non-members. Factions can forge alliances and declare themselves enemies with others. Land may be taken from other factions through war.");
    60. }
    61. if(s.getLine(1).contains("Skyblock")){
    62. player.sendMessage(ChatColor.RED + "Skyblock is a type of survival that is challenge orientated. You spawn on an island in the sky with limited (but enough) resources and must expand your island to become an empire. The original idea has been around for a while now, and can be played in single player mode by downloading a map, or on multiplayer servers like this one!");
    63. }
    64. if(s.getLine(1).contains("Mingames")){
    65. player.sendMessage(ChatColor.RED + "Minigames include Spleef, Ultimate PVP, TDM, & Parkour! You can play these for fun if you get bored of any of the other worlds ColaCraft has to offer!");
    66. }
    67. if("Hunger Games".equalsIgnoreCase(s.getLine(1))){
    68. player.sendMessage(ChatColor.RED + "Hunger Games is a minecraft minigame based off of the popular movie & book series, The Hunger Games. There are several arenas & you spawn with minimal supplies. Everyone gets equal oppertunity- it's just fun! I encourage you to join if you're getting bored of any other worlds!");
    69. }
    70. }
    71. }
    72. }
    73. }
    74. }
    75. }
    76.  


    EDIT:

    Figured it out. Thanks for the help Ultimate_N00b :) I've given you a like.
     
  4. Offline

    geNAZt

    Well the onEnable does not set the Plugin as Listener so you have not appended it to the Listener.

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


    In your onEnable function to do so.

    //Edit: But you should create a new File for each Listener to keep the code clean
     
Thread Status:
Not open for further replies.

Share This Page