【问题标题】:PlayerInteractEvent breaking when hitting Spigot击中 Spigot 时 PlayerInteractEvent 中断
【发布时间】:2016-12-06 14:46:10
【问题描述】:

提醒,这个插件本身可以工作,只是点击时出错。

我正在尝试彻底改变箱子在我的世界中的工作方式。在我的插件中,当你打开它们时,它们会给你一个项目然后消失,这一直在工作,但有一个问题。

我调用PlayerInteractEvent并使用if语句查看方块类型是否为箱子,如果是则取消事件,使箱子消失并给玩家一个物品。但是当主类将事件传递给 ChestRewards 类时,它似乎也传递了预期的来自 PlayerInteractEvent 的击球事件或出拳动作,但这就是为什么我使用 if 语句仅使用如果块传递的事件类型是胸部。但它似乎在控制台中出错,因为无法传递事件,请帮助!

    @EventHandler
    public void catchChestOpen(PlayerInteractEvent event) {

    Entity p = event.getPlayer();
    Player player = (Player) p;

    if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.PHYSICAL || event.getAction() == Action.LEFT_CLICK_BLOCK){
        // TODO: 2016-12-06 Nothing
    }

    if (event.getClickedBlock().getType() == Material.CHEST) {


        event.getClickedBlock().setType(Material.AIR);

        event.setCancelled(true);

        int rando = (int) (Math.random() * 10);


        //---------------------------------------------------------------------------------
        if (rando == 1) {
            ItemStack Axe = new ItemStack(Material.DIAMOND_AXE, 1);
            ItemMeta meta = Axe.getItemMeta();
            List<String> lores = new ArrayList<String>();
            lores.add(ChatColor.DARK_PURPLE + "The axe of a long lost survivor");

            meta.setDisplayName(ChatColor.DARK_RED + "Survivor Axe");
            meta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
            meta.addEnchant(Enchantment.DURABILITY, 2, true);
            meta.setLore(lores);
            Axe.setItemMeta(meta);
            Axe.setDurability((short) 800);
            player.getInventory().addItem(Axe);
            player.sendMessage(ChatColor.GOLD + "You opened a" + ChatColor.AQUA + " RARE " + ChatColor.GOLD + "loot chest!");
        }
        //---------------------------------------------------------------------------------
        if (rando > 1) {
            ItemStack IronSword = new ItemStack(Material.IRON_SWORD, 1);
            ItemMeta meta2 = IronSword.getItemMeta();
            List<String> lores2 = new ArrayList<String>();
            lores2.add(ChatColor.DARK_PURPLE + "A reliable iron sword");

            meta2.setDisplayName(ChatColor.DARK_RED + "Reliable Iron Sword");
            meta2.addEnchant(Enchantment.DURABILITY, 3, true);
            meta2.setLore(lores2);
            IronSword.setItemMeta(meta2);
            IronSword.setDurability((short) 800);
            player.getInventory().addItem(IronSword);
            player.sendMessage(ChatColor.GOLD + "You opened a loot chest!");
        }
        //---------------------------------------------------------------------------------



    }
}

这是错误本身

[09:32:00 ERROR]: Could not pass event PlayerInteractEvent to Apocalypse v1.0

【问题讨论】:

  • 您能否发布消息“无法将事件 PlayerInteractEvent 传递给 Apocalypse v1.0”之后的堆栈跟踪?

标签: java minecraft bukkit


【解决方案1】:

如果Action 不是RIGHT_CLICK_BLOCKLEFT_CLICK_BLOCK,您不会停止执行代码,那么当您调用block.getType() 以将其与a 进行比较时,event.getClickedBlock() 将为空玩家左键或右键点击空气时的箱子。

我建议您添加检查以查看该操作是否与块有关。下面的 sn-p 将检查玩家是左击还是右击一个箱子:

if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || 
    event.getAction() == Action.LEFT_CLICK_BLOCK) && 
    event.getClickedBlock().getType() == Material.CHEST) {

【讨论】:

  • 效果很好!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
  • 2013-02-09
相关资源
最近更新 更多