I want to code a plugin for a plugin's bug.(ChestShop). When player right click sign(Sign's line 2 is "B Free" or "Free" and line 3 is "Iron_Ingot") cancel event and send message.
public void onPlayerInteract(PlayerInteractEvent e){
Player player = e.getPlayer();
if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.LEFT_CLICK_BLOCK) ){
if(e.getClickedBlock().getState() instanceof Sign){
Sign sign = (Sign) e.getClickedBlock().getState();
if(sign.getLine(2).equals("B Free") || sign.getLine(2).equals("Free")){
if(sign.getLine(3).equals("Iron_Ingot"))
e.setCancelled(true);
e.getPlayer().sendMessage("You can not click this sign");
}
}
}
}
I tried this but it didn't work, which part of the code is wrong? What methods can I use?
答案 0 :(得分:1)
您的方法onPlayerInteract所在的类必须实现te接口Listener。您还必须使用EventHandler注释并在插件中注册该事件。
public class PlayerInteract implements Listener {
// priority is when the listener is called.
// LOWEST is called first, then LOW, NORMAL, HIGH, HIGHEST, MONITOR.
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerInteract(PlayerInteractEvent event) {
// ...
}
}
在Main类的onEnable中(扩展了JavaPlugin):
getServer().getPluginManager().registerEvents(new PlayerInteract(),this);