如何检测某人是否在Bukkit中右键单击了块(例如,石头)?我尝试过很多建议,但没有任何回复。
Main.java
package me.ben.test;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
@Override
public void onEnable() {
this.getServer().getPluginManager().registerEvents(new Click(), this);
getLogger().info("The Plugin Has Been Enabled!");
}
@Override
public void onDisable() {
getLogger().info("The Plugin Has Been Disabled!");
}
public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) {
if (cmd.getName().equalsIgnoreCase("hello") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("Hello, " + player.getName() + "!");
return true;
} else if (cmd.getName().equalsIgnoreCase("isonline")
&& args.length == 1) {
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.AQUA + "Player " + args[0]
+ " is not online.");
return true;
} else if (target != null) {
sender.sendMessage(ChatColor.AQUA + "Player " + args[0]
+ " is online.");
return true;
} else return false;
}
return false;
}
}
Click.java
package me.ben.test;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Click extends JavaPlugin implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendMessage("Heyyyyyyy");
}
}
我尝试了其他的但是这个是我尝试的最后一个,这似乎是最简单的。
我将该行添加到onEnable
部分。
答案 0 :(得分:1)
您可以使用Event
并检查玩家何时互动:
@EventHandler //you NEED to have @EventHandler before any event
public void playerInteract(PlayerInteractEvent e){
//this will be called automatically by bukkit whenever a player interacts
if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
//the player right-clicked a block
Material m = e.getClickedBlock().getType(); //get the block type clicked
if(m.equals(Material.STONE){
//the block clicked was stone.
}
}
}
然后你需要确保这个类位于implements Listener
:
public class MyListenerClass implements Listener{
//events & other methods
}
然后您需要在Main类的onEnable()
方法中注册事件(extends JavaPlugin
)
public class MyMainClass extends JavaPlugin{
@Override
public void onEnable(){
//enabled
this.getServer().getPluginManager().registerEvents(new MyListenerClass(), this));
}
}