Having trouble with right click player listener

Discussion in 'Plugin Development' started by kmccmk9, Nov 21, 2011.

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

    kmccmk9

    Hello all! I'm developing a plugin that first detects when a user right clicks on a fence gate. However, I can not get this simple task to work. Any help would be appreciated

    TrainStation:
    Code:java
    1. package com.kmccmk9.TrainStation;
    2.  
    3. import org.bukkit.Server;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.block.BlockFace;
    6. import org.bukkit.block.Sign;
    7. import org.bukkit.event.Event;
    8. import org.bukkit.event.Event.Priority;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. import com.kmccmk9.TrainStation.TrainStationPlayerListener;
    14.  
    15. /**
    16.  * TrainStation for Bukkit
    17.  *
    18.  * @author kmccmk9
    19.  */
    20.  
    21. public class TrainStation extends JavaPlugin {
    22. //Variables
    23. Server server;
    24. private TrainStationPlayerListener playerListener;
    25. BlockFace SignBlockFace;
    26. //OnDisable
    27. public void onDisable() {
    28. PluginDescriptionFile pdfFile = this.getDescription();
    29. System.out.println( pdfFile.getName() + " is disabled!" );
    30. }
    31. //OnEnable
    32. public void onEnable() {
    33. PluginDescriptionFile pdfFile = this.getDescription();
    34. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    35. server = this.getServer();
    36. PluginManager pm = getServer().getPluginManager();
    37. pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Priority.Normal, this);
    38. }
    39. //IsBlockGate
    40. public boolean blockIsValidGateBlock(Block block) {
    41. // TODO: Make this exception more specific
    42. try {
    43. int GateBlock = block.getTypeId();
    44. if (GateBlock == 107)
    45. {
    46. server.broadcastMessage("You clicked a gateblock");
    47. return true;
    48. }
    49. } catch (Exception e) {
    50.  
    51. }
    52. server.broadcastMessage("You did not click a gateblock");
    53. return false;
    54. }
    55. //IsSignTrain
    56. public String getTrainStationSignName(Block block) {
    57. // TODO: Make this exception more specific
    58. try {
    59. Sign sign = (Sign)block.getRelative(block.getX(), block.getY(), block.getZ()+2);
    60. server.broadcastMessage(sign.getLine(1));
    61. return sign.getLine(1);
    62. } catch (Exception e) {
    63.  
    64. }
    65. server.broadcastMessage("No Name");
    66. return "No Name";
    67. }
    68. //TrainCost
    69. public String getTrainStationCost(Block block) {
    70. // TODO: Make this exception more specific
    71. try {
    72. Sign sign = (Sign)block.getRelative(block.getX(), block.getY(), block.getZ()+2);
    73. server.broadcastMessage(sign.getLine(2));
    74. return sign.getLine(2);
    75. } catch (Exception e) {
    76.  
    77. }
    78. server.broadcastMessage("No Cost");
    79. return "No Cost";
    80. }
    81. }


    TrainStationPlayerListener:
    Code:java
    1. package com.kmccmk9.TrainStation;
    2.  
    3. import org.bukkit.block.Block;
    4. import org.bukkit.block.Sign;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.block.Action;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.event.player.PlayerListener;
    9.  
    10. public class TrainStationPlayerListener extends PlayerListener {
    11. private TrainStation plugin;
    12. public TrainStationPlayerListener(final TrainStation plugin) {
    13. this.plugin = plugin;
    14. }
    15. public void onPlayerInteract(PlayerInteractEvent event) {
    16. Player p = event.getPlayer();
    17. String TrainStationName;
    18. int Cost;
    19. if (event.hasBlock() && event.getClickedBlock().getState() instanceof Block && event.getAction() == Action.RIGHT_CLICK_BLOCK)
    20. {
    21. if ((plugin).blockIsValidGateBlock(event.getClickedBlock()))
    22. {
    23. TrainStationName = (plugin).getTrainStationSignName(event.getClickedBlock());
    24. if (TrainStationName == "[TrainStation]")
    25. {
    26. Cost = Integer.parseInt((plugin).getTrainStationCost(event.getClickedBlock()));
    27. }
    28. }
    29. else
    30. {
    31. plugin.server.broadcastMessage("Not a Gate Block");
    32. }
    33. }
    34. }
    35. }
     
  2. Offline

    coldandtired

    You never create the TrainStationPlayerListener instance. Change line 24 in the main class to private TrainStationPlayerListener playerListener = new TrainStationPlayerInstance(this);

    It might also need to be public instead of private, I can't remember.
     
  3. Offline

    DDoS

    Here's a fix for line 19 of your Listener:

    PHP:
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    That's all you need to check for a right click of a block.
     
  4. Offline

    kmccmk9

    Thanks for the replies. It now notices I clicked the gate. However, it does not recognize the sign two blocks above it

    Would this be because I am using the relative block function with the variable of a blockface that is set to nothing?
     
Thread Status:
Not open for further replies.

Share This Page