(MC bukkit 服务器)右键单击特定项目以触发事件
(MC bukkit server) Right-clicking with specific item to trigger an event
public void arrowstick(PlayerInteractEvent event) {
Player p = event.getPlayer();
if(p.getInventory().getItemInMainHand().getType().equals(Material.STICK)){
if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
Arrow ar = p.getWorld().spawn(p.getLocation(), Arrow.class);
ar.setShooter(p);
}
}
}
我想在这里做的是,如果玩家拿着一根棍子右击,就会射出一支箭。我似乎无法为此找到解决方案,有没有人知道我可能做错了什么?对不起,如果它是一个小而愚蠢的错误,它现在凌晨 2 点在这里,所以是的,希望你们能帮助
我在这里为你编码:
public class Arrow extends JavaPlugin implements Listener {
public void onEnable() {
this.getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();
if (p.getInventory().getItemInHand().getType() == Material.STICK) {
if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
p.launchProjectile(Arrow.class);
}
}
}
}
如果您只是在新 class 中复制监听器,请不要忘记将其设为 implements Listener
并注册监听器。
public void arrowstick(PlayerInteractEvent event) {
Player p = event.getPlayer();
if(p.getInventory().getItemInMainHand().getType().equals(Material.STICK)){
if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
Arrow ar = p.getWorld().spawn(p.getLocation(), Arrow.class);
ar.setShooter(p);
}
}
}
我想在这里做的是,如果玩家拿着一根棍子右击,就会射出一支箭。我似乎无法为此找到解决方案,有没有人知道我可能做错了什么?对不起,如果它是一个小而愚蠢的错误,它现在凌晨 2 点在这里,所以是的,希望你们能帮助
我在这里为你编码:
public class Arrow extends JavaPlugin implements Listener {
public void onEnable() {
this.getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();
if (p.getInventory().getItemInHand().getType() == Material.STICK) {
if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
p.launchProjectile(Arrow.class);
}
}
}
}
如果您只是在新 class 中复制监听器,请不要忘记将其设为 implements Listener
并注册监听器。