Question

I am simply trying to call a method, but I'm a bit lost on how to do so with the parameters I need to use from the Bukkit API.

The method I'm calling:

@EventHandler   
    private void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        Location location = loc.get(player.getName());
        if (player.getLocation().distance(location) >= 65) {
            player.sendMessage(ChatColor.AQUA + "Out of reach.");
        }
    }

The method I'm calling it from:

public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
    this.onPlayerMove(PlayerMoveEvent);
}

Any help is greatly appreciated!

Was it helpful?

Solution

You don't need to call events, they're automatically triggered when the event happens. For example,

@EventHandler   
public void onPlayerMove(PlayerMoveEvent event){
}

Would be called when a player moves. You will NEVER need to call an event.

If what you're trying to do is teleport a player, you could use:

Player p; //make sure that 'p' is assigned to something
Location l = new Location(world, x-coord, y-coord, z-coord);

p.teleport(l);

So, what you should really be doing is, considering this is all in your main file:

@Override
public void onEnable(){
    //plugin enabled
    this.getServer().getPluginManager().registerEvents(this, this);
}

@Override
public void onDisable(){
    //plugin disabled
}

@EventHandler   
public void onPlayerMove(PlayerMoveEvent event){ //NEVER make events with the private modifier
    Player player = event.getPlayer();
    Location location = loc.get(player.getName()); //I really don't know what your trying to do here
    //if your trying to create a new location, check out the code above
    if(player.getLocation().distance(location) >= 65) {
        player.sendMessage(ChatColor.AQUA + "Out of reach.");
    }
}

Your onPlayerMove method will be called whenever a player moves, no extra work necessary!

Also, make sure the player.getLocation().distance(location) actually works, as it isn't a method in Bukkit... I'm assuming you get the distance between the player's location, and location, by checking the players X, Y, and Z coords...

Just make sure that the class in which you're putting the event in implements Listener

Also, If you would like to put the event in a class other than your Main file, to register events in your onEnable(), use this:

this.getServer().getPluginManager().registerEvents(new classNameHere(), this);

If it's in your main file, you could replace new classNameHere() with this

One last time though, and I can't stress how important this is, you NEVER have to call your event methods. As long as the events are registered (done in the code above), and the methods have @EventHandler above them. Events will ALWAYS be called automatically whenever the event happens

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top