Solved [Help]Message when right click a block

Discussion in 'Plugin Development' started by Beekie, Jun 4, 2014.

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

    Beekie

    Hey,

    I want it to when a player looks at a specified block, like stone(this isn't in the code, can someone also help with that?), he gets a message.
    I got this code:

    Code:java
    1. package me.Lucas.WatchBlocks;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin {
    12.  
    13. public void onEnable() {
    14. getLogger().info("Plugin started and enabled!!");
    15. getServer().getPluginManager().registerEvents(new Main(), this);
    16. }
    17. public void onDisable() {
    18. getLogger().info("Plugin stopped and disabled!");
    19. }
    20. @EventHandler
    21. public void onPlayerInteract(PlayerInteractEvent event) {
    22. Player player = this.getPlayer();
    23. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    24. event.getPlayer().sendMessage(ChatColor.AQUA + "Well done");
    25. }
    26. }
    27. }


    But I get an error at
    Code:java
    1. getServer().getPluginManager().registerEvents(new Main(), this);

    and at
    Code:java
    1. Player player = this.getPlayer();


    What am I doing wrong? Can someone help me? :)

    Oh and yes, this is all the code I have.
     
  2. Beekie new Main() - don't initialise your main class. Use the keyword "this" instead
    this.getPlayer() - Get it from the event, not the plugin instance

    Certain block - event.getBlock().getType() returns the type of block. See the Material enum.
     
  3. Offline

    Beekie

    AdamQpzm Now I have this, but it still doesn't work..
    Code:java
    1. package me.Lucas.WatchBlocks;
    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.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin {
    13.  
    14. public void onEnable() {
    15. getLogger().info("Plugin started and enabled!!");
    16. getServer().getPluginManager().registerEvents(this, this);
    17. }
    18. public void onDisable() {
    19. getLogger().info("Plugin stopped and disabled!");
    20. }
    21. @EventHandler
    22. public void onPlayerInteract(PlayerInteractEvent event) {
    23. Player player = event.getPlayer();
    24. if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
    25. if (event.getClickedBlock().getType() == Material.STONE);
    26. event.getPlayer().sendMessage(ChatColor.AQUA + "Well done");
    27. }
    28. }
    29.  


    The error is at
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);


    Am I being stupid or..?
     
  4. Beekie Your main class class doesn't implement Listener. Also
    Code:
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
    if (event.getClickedBlock().getType() == Material.STONE);
    event.getPlayer().sendMessage(ChatColor.AQUA + "Well done");
    Means "if the action was a RIGHT_CLICK_BLOCK and the block clicked was stone, then do nothing. Afterwards, regardless of the two previous things, send the player a well done message."

    I think you need to cover some Java basics. :)
     
  5. Offline

    Beekie

    AdamQpzm It worked :D A big thanks to you!
     
    AdamQpzm likes this.
  6. Offline

    99storm2

    If everything is working and fixed can you please add a Solved prefix to the thread. Thanks Beekie
     
  7. Offline

    Beekie

    AdamQpzm I got 1 more question, is it possible that it only sends the message when the player is holding a specified item?
     
  8. Beekie player.getItemInHand() returns an ItemStack of the item they're holding - which has a getType() method like block does. However, it will return null if they're not holding anything, so be sure to check that it isn't null before using getType() or else you'll get NullPointerExceptions :)
     
  9. Offline

    Beekie

    AdamQpzm Thanks :D
    And now I got another question.... Hope you don't mind :)

    How do I give the player points when he right clicks the block?
    And when the player gets 10 points he receives an item
     
  10. Beekie Look into HashMaps :)
     
  11. Offline

    Beekie

    AdamQpzm Alright I will do that, thanks for helping me out :)
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page