Solved Event not working!

Discussion in 'Plugin Development' started by Techno, Mar 13, 2014.

Thread Status:
Not open for further replies.
  1. Hey, I am working on a new plugin and what it does is: When a player right clicks with a diamond, it's supposed to teleport them to their /home, a command from essentials.

    Code:
    Code:java
    1.  
    2. package noah.plugins.pluginx;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    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.plugin.java.JavaPlugin;
    12.  
    13. public class PluginX extends JavaPlugin implements Listener {
    14.  
    15. // To Register
    16. public void onEnable() {
    17. getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. public void onDisable() {
    21.  
    22. }
    23.  
    24. // Lets start
    25. @EventHandler
    26. public void onPlayerInteract(PlayerInteractEvent e) {
    27. if (e.getPlayer().hasPermission("hometp.tphome")) {
    28. if (e.getPlayer().getItemInHand().getType() == Material.DIAMOND) {
    29. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    30. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "home");
    31. e.getPlayer().sendMessage("Hi");
    32. }
    33. }
    34. } else {
    35. e.getPlayer().sendMessage(ChatColor.RED + "You don't have permission to teleport home!");
    36. }
    37. }
    38.  
    39.  
    40. }
    41.  
     
  2. Offline

    Wout_

    This code will execute "home" from the CONSOLE perspective, and because the CONSOLE has no home it doesnt work. Try to set the player as the commandsender. So replace Bukkit.getConsoleSender() with it.
     
  3. Offline

    FabeGabeMC

    Try this:


    Code:java
    1. // Lets start
    2.  
    3. @EventHandler
    4.  
    5. public void onPlayerInteract(PlayerInteractEvent e) {
    6.  
    7. Player p = e.getPlayer();
    8.  
    9. if (e.getPlayer().hasPermission("hometp.tphome")) {
    10.  
    11. if (e.getPlayer().getItemInHand().getType() == Material.DIAMOND) {
    12.  
    13. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    14.  
    15. Bukkit.getServer().dispatchCommand(p, "home");
    16.  
    17. e.getPlayer().sendMessage("Hi");
    18.  
    19. }
    20.  
    21. }
    22.  
    23. } else {
    24.  
    25. e.getPlayer().sendMessage(ChatColor.RED + "You don't have permission to teleport home!");
    26.  
    27. }
    28.  
    29. }
    30.  
    31.  
    32.  
    33.  
    34.  
    35. }



     
  4. FabeGabeMC
    Why feed him code, when you can simply tell him what's wrong, especially if it only requires a minor change?

    Techno
    In your Bukkit.dispatchCommand() simply change Bukkit.getConsoleSender() to e.getPlayer().
     
    The Fancy Whale and Dpasi314 like this.
  5. Offline

    Gater12

    Techno
    Player object also has it's built in method of performing a command from the player called performCommand
     
  6. Offline

    FabeGabeMC

  7. FabeGabeMC
    I'm not doubting that. I'm just saying that you don't need to provide a giant block of code when you can simply tell him exactly what the problem is.
     
  8. Offline

    The Fancy Whale

    The Gaming Grunts FabeGabeMC Or just give Techno one of the best tricks for finding issues...
    Put broadcast messages throughout the code to see what fires and what doesn't then you can see where the code stops working and fix from there :D
     
  9. Offline

    iiHeroo

    Or simply do p.performCommand because you know that's what it was made for.
     
  10. Thanks everyone! You all really helped!
     
  11. Offline

    FabeGabeMC

Thread Status:
Not open for further replies.

Share This Page