为什么我的Minecraft GUI不能清除剑并在设置时设置弓?

时间:2015-11-28 01:12:06

标签: java bukkit

我是Minecraft服务器的插件开发人员,当我点击“接受”时,我遇到了移除GUI中的钻石剑并将弓放在GUI中的问题。我最终需要做的就是拿出那把钻石剑,在接受换购时清除它,然后将弓加入相应的槽中,让玩家随后拿走。

private Inventory i = Bukkit.createInventory(null, 9, ChatColor.BOLD + "Trade-Up"); //Creating an inventory for everything to work around

public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
}//Registering the event

private void openGUI(Player player) {
    player.closeInventory();

    ItemStack Anvil = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 3);
    ItemMeta AnvilMeta = Anvil.getItemMeta();

    AnvilMeta.setDisplayName(ChatColor.DARK_AQUA + "Trade Up");
    Anvil.setItemMeta(AnvilMeta);

    ItemStack AnvilAccept = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
    ItemMeta AnvilAcceptMeta = AnvilAccept.getItemMeta();

    AnvilAcceptMeta.setDisplayName(ChatColor.GREEN + "Accept");
    AnvilAccept.setItemMeta(AnvilAcceptMeta);

    ItemStack AnvilFirst = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
    ItemMeta AnvilFirstMeta = AnvilFirst.getItemMeta();

    AnvilFirstMeta.setDisplayName(ChatColor.GRAY + "Input Items Here");
    AnvilFirst.setItemMeta(AnvilFirstMeta);

    ItemStack AnvilSecond = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
    ItemMeta AnvilSecondMeta = AnvilSecond.getItemMeta();

    AnvilSecondMeta.setDisplayName(ChatColor.GRAY + "Output Items Here");
    AnvilSecond.setItemMeta(AnvilSecondMeta);

    i.setItem(0, Anvil);
    i.setItem(1, Anvil);
    i.setItem(3, AnvilFirst);
    i.setItem(4, AnvilAccept);
    i.setItem(5, AnvilSecond);
    i.setItem(7, Anvil);
    i.setItem(8, Anvil);

    player.openInventory(i);
}//Opens the trade-up inventory when the player type the command

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(sender instanceof Player) {
        if(cmd.getName().equalsIgnoreCase("trade-up")) {
            Player player = (Player) sender;
            openGUI(player);
        }//GUI openning command
    }
    return false;
}

@EventHandler
public void inventoryClickEvent(InventoryClickEvent e) {

    ItemStack Anvil = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 3);
    ItemMeta AnvilMeta = Anvil.getItemMeta();

    AnvilMeta.setDisplayName(ChatColor.DARK_AQUA + "Trade Up");
    Anvil.setItemMeta(AnvilMeta);

    ItemStack AnvilAccept = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
    ItemMeta AnvilAcceptMeta = AnvilAccept.getItemMeta();

    AnvilAcceptMeta.setDisplayName(ChatColor.GREEN + "Accept");
    AnvilAccept.setItemMeta(AnvilAcceptMeta);

    ItemStack AnvilFirst = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
    ItemMeta AnvilFirstMeta = AnvilFirst.getItemMeta();

    AnvilFirstMeta.setDisplayName(ChatColor.GRAY + "Input Items Here");
    AnvilFirst.setItemMeta(AnvilFirstMeta);

    ItemStack AnvilSecond = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
    ItemMeta AnvilSecondMeta = AnvilSecond.getItemMeta();

    AnvilSecondMeta.setDisplayName(ChatColor.GRAY + "Output Items Here");
    AnvilSecond.setItemMeta(AnvilSecondMeta);

    ItemStack is = e.getCurrentItem();

    if(is.equals(Anvil) || is.equals(AnvilFirst) || is.equals(AnvilSecond)) {
        e.setCancelled(true);//Making sure that the player doesn't take the glass panes
    }

    if(is.equals(AnvilAccept)) {
        Player p = (Player) e.getWhoClicked();

        ItemStack air = new ItemStack(Material.AIR);

        ItemStack chests = new ItemStack(Material.DIAMOND_SWORD);
        ItemStack chestsConvert = new ItemStack(Material.BOW);

        if(i.contains(chests, 2)) {
            i.setItem(6, chestsConvert);
            i.clear(2);
            i.setItem(0, Anvil);
            i.setItem(1, Anvil);
            i.setItem(3, AnvilFirst);
            i.setItem(4, AnvilAccept);
            i.setItem(5, AnvilSecond);
            i.setItem(7, Anvil);
            i.setItem(8, Anvil);
            p.closeInventory();
            p.openInventory(i);
            p.sendMessage(ChatColor.GREEN + "Your items have been Traded-Up!");//Removes the sword and gives them a bow instead
        }else if(i.contains(air, 2)) {
            p.sendMessage("There is no items to trade up!");
        } //Makes sure there is an item to trade-up

    e.setCancelled(true);//Makes so the player can't take the "Accept" pane.
    }
}

该插件没有记录插槽中的任何东西,也没有移除并用弓替换钻石剑。

1 个答案:

答案 0 :(得分:0)

contains(ItemStack, int)方法不会检查ItemStack是否位于int位(我看到您使用它的方式),但会检查该广告资源的全部内容是否包含至少整数ItemStack。您的代码正在检查自定义库存是否包含两把钻石剑(如果您将它们放在第二和第六槽中就可以完成),而不是检查是否已将一把钻石剑放入第二槽(这是我假设您正试图这样做。

要解决此问题,请使用inventory.getItem(2).equals(chests)检查插槽2是否包含剑。请注意,上述方法在空插槽的情况下不会返回ItemStack类型Material.AIR,而是返回null

以下是一些示例代码:

ItemStack input = i.getItem(2); // Get the ItemStack at slot two

if (input == null) { // If there is nothing in that slot
    player.sendMessage("There are no items to trade up!");
} else if (input.equals(chests)) { // If it contains the sword
    // Execute the trade
}
// Optionally send message if it is the wrong item

您定义和初始化自定义InventoryItemStack的方式可以颠倒过来。请注意,多个人可以查看同一个广告资源并与之互动,因此您现在就可以使用“换购”界面编写插件的方式,每个人都会看到相同的广告资源,并能够获取其他人的广告资源。但是,您为每个事件和ItemStack方法调用新创建的openGUI(Player)并不是唯一的,并且可以代替库存只创建一次以节省资源(换句话说,您可以每次调用Inventory方法时都会创建一个新的openGUI(Player),但始终引用相同的ItemStack s。