当前位置: 首页 > 知识库问答 >
问题:

Bukkit/Spigot:插件-自定义GUI商店错误

姬奇思
2023-03-14

我目前正在为我的服务器网络编写自己的GUI商店,我面临着两个奇怪的问题。

第一个问题是,每当我想获得两个带有项目ID 161和两个不同元ID(0和1)的树叶时,我只得到一个元ID 2。

这是我在调试中尝试获取项目数据时得到的内容:问题 1

这就是我在库存中得到的:问题 1

但它应该是这个(例如):161/0 或 161/1

我从Spigot/Bukkit自己那里得到了这个物品Meta,所以我无论如何都不会自己得到它。

我的下一个问题是,每当我想买羊毛时,它只能完美地工作到第9个字节,然后它上面的所有内容每次都返回我橙色羊毛、粘土或玻璃。

这就是我显示项目的方式:

if(t == ShopType.SHOP_DECORATION){
        inv = Bukkit.createInventory(null, 54, "Shop: Decoration");
        createDisplay(Material.WOOL, (byte)0, inv, 0, null, getShopLore("wool"));
        createDisplay(Material.WOOL, (byte)1, inv, 1, null, getShopLore("wool2"));
        createDisplay(Material.WOOL, (byte)2, inv, 2, null, getShopLore("wool3"));
        createDisplay(Material.WOOL, (byte)3, inv, 3, null, getShopLore("wool4"));
        createDisplay(Material.WOOL, (byte)4, inv, 4, null, getShopLore("wool5"));
        createDisplay(Material.WOOL, (byte)5, inv, 5, null, getShopLore("wool6"));
        createDisplay(Material.WOOL, (byte)6, inv, 6, null, getShopLore("wool7"));
        createDisplay(Material.WOOL, (byte)7, inv, 7, null, getShopLore("wool8"));
        createDisplay(Material.WOOL, (byte)8, inv, 8, null, getShopLore("wool9"));
        createDisplay(Material.WOOL, (byte)9, inv, 9, null, getShopLore("woola"));
        createDisplay(Material.WOOL, (byte)10, inv, 10, null, getShopLore("woolb"));
        createDisplay(Material.WOOL, (byte)11, inv, 11, null, getShopLore("woolc"));
        createDisplay(Material.WOOL, (byte)12, inv, 12, null, getShopLore("woold"));
        createDisplay(Material.WOOL, (byte)13, inv, 13, null, getShopLore("woole"));
        createDisplay(Material.WOOL, (byte)14, inv, 14, null, getShopLore("woolf"));
        createDisplay(Material.WOOL, (byte)15, inv, 15, null, getShopLore("woolg"));
        createDisplay(Material.STAINED_GLASS, (byte)0, inv, 16, null, getShopLore("glass"));
        createDisplay(Material.STAINED_GLASS, (byte)1, inv, 17, null, getShopLore("glass2"));
        createDisplay(Material.STAINED_GLASS, (byte)2, inv, 18, null, getShopLore("glass3"));
        createDisplay(Material.STAINED_GLASS, (byte)3, inv, 19, null, getShopLore("glass4"));
        createDisplay(Material.STAINED_GLASS, (byte)4, inv, 20, null, getShopLore("glass5"));
        createDisplay(Material.STAINED_GLASS, (byte)5, inv, 21, null, getShopLore("glass6"));
        createDisplay(Material.STAINED_GLASS, (byte)6, inv, 22, null, getShopLore("glass7"));
        createDisplay(Material.STAINED_GLASS, (byte)7, inv, 23, null, getShopLore("glass8"));
        createDisplay(Material.STAINED_GLASS, (byte)8, inv, 24, null, getShopLore("glass9"));
        createDisplay(Material.STAINED_GLASS, (byte)9, inv, 25, null, getShopLore("glassa"));
        createDisplay(Material.STAINED_GLASS, (byte)10, inv, 26, null, getShopLore("glassb"));
        createDisplay(Material.STAINED_GLASS, (byte)11, inv, 27, null, getShopLore("glassc"));
        createDisplay(Material.STAINED_GLASS, (byte)12, inv, 28, null, getShopLore("glassd"));
        createDisplay(Material.STAINED_GLASS, (byte)13, inv, 29, null, getShopLore("glasse"));
        createDisplay(Material.STAINED_GLASS, (byte)14, inv, 30, null, getShopLore("glassf"));
        createDisplay(Material.STAINED_GLASS, (byte)15, inv, 31, null, getShopLore("glassg"));
        createDisplay(Material.STAINED_CLAY, (byte)0, inv, 32, null, getShopLore("clay"));
        createDisplay(Material.STAINED_CLAY, (byte)1, inv, 33, null, getShopLore("clay2"));
        createDisplay(Material.STAINED_CLAY, (byte)2, inv, 34, null, getShopLore("clay3"));
        createDisplay(Material.STAINED_CLAY, (byte)3, inv, 35, null, getShopLore("clay4"));
        createDisplay(Material.STAINED_CLAY, (byte)4, inv, 36, null, getShopLore("clay5"));
        createDisplay(Material.STAINED_CLAY, (byte)5, inv, 37, null, getShopLore("clay6"));
        createDisplay(Material.STAINED_CLAY, (byte)6, inv, 38, null, getShopLore("clay7"));
        createDisplay(Material.STAINED_CLAY, (byte)7, inv, 39, null, getShopLore("clay8"));
        createDisplay(Material.STAINED_CLAY, (byte)8, inv, 40, null, getShopLore("clay9"));
        createDisplay(Material.STAINED_CLAY, (byte)9, inv, 41, null, getShopLore("claya"));
        createDisplay(Material.STAINED_CLAY, (byte)10, inv, 42, null, getShopLore("clayb"));
        createDisplay(Material.STAINED_CLAY, (byte)11, inv, 43, null, getShopLore("clayc"));
        createDisplay(Material.STAINED_CLAY, (byte)12, inv, 44, null, getShopLore("clayd"));
        createDisplay(Material.STAINED_CLAY, (byte)13, inv, 45, null, getShopLore("claye"));
        createDisplay(Material.STAINED_CLAY, (byte)14, inv, 46, null, getShopLore("clayf"));
        createDisplay(Material.STAINED_CLAY, (byte)15, inv, 47, null, getShopLore("clayg"));
        createButtons(inv);
        p.openInventory(inv);
    }

这些显示器在GUI中显示正确,但是当我想购买它们时,我总是在青色羊毛/粘土/玻璃之后得到橙色羊毛或粘土或玻璃。

我是这样给他们的:

    public static boolean withdrawPlayer(Player player, double amount, int am){
    double money = econ.getBalance(player);
    if(money < amount){
        return false;
    }
    econ.withdrawPlayer(player, amount);
    JSONObject json = Shop.getItemData(player);
    int itemID = json.getInt("item_id");
    byte itemMeta = (byte)json.getInt("item_data");
    ItemStack item = new ItemStack(itemID, 1, itemMeta);

    item.setAmount(am);
    player.sendMessage("Debug: " + item.getAmount() + " " + item.getType() + " -- Data: " + Shop.getData(item.getData()) + " ||| " + item.getData());
    player.getInventory().addItem(item);
    player.updateInventory();
    return true;
}

这就是我添加 JSONObject 的方式:

private static void addItem(String item, int price, ShopType type){
    priceList.put(item, price);
    slotList.put(item, type);
    JSONObject temp = new JSONObject();
    switch(type){
    case SHOP_REDSTONE:
        temp.put("slot", a);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        a++;
        break;
    case SHOP_ORES:
        temp.put("slot", b);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        b++;
        break;
    case SHOP_SEEDS:
        temp.put("slot", c);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        c++++;
        break;
    case SHOP_FOOD:
        temp.put("slot", d);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        d++;
        break;
    case SHOP_DROPS:
        temp.put("slot", e);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        e++;
        break;
    case SHOP_MISC:
        temp.put("slot", f);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        f++;
        break;
    case SHOP_DECORATION:
        temp.put("slot", g);
        temp.put("type", type.toString());
        temp.put("name", item);
        temp.put("price", price);
        g++;
        break;
    }
    System.out.println("A: " +a);
    System.out.println("B: " +b);
    System.out.println("C: " +c);
    System.out.println("D: " +d);
    System.out.println("E: " +e);
    System.out.println("F: " +f);
    System.out.println("G: " +g);
    debug.add(temp);
}

这是当有人单击项目时运行的代码:

@EventHandler
public void onInventoryClick(InventoryClickEvent e){
    Player p = (Player) e.getWhoClicked();
    int slot = e.getSlot();

    if (e.getSlotType().equals(SlotType.OUTSIDE)) {
        return;
    }


    if(e.getClickedInventory().getName().equalsIgnoreCase("Menu")){

        if (e.getSlotType().equals(SlotType.OUTSIDE)) {
            return;
        }

        for(ClickType type : ClickType.values()){
            if(e.getClick() == type){
                e.setCancelled(true);
            }
        }

        for(InventoryAction action : InventoryAction.values()){
            if(e.getAction() == action){
                e.setCancelled(true);
            }
        }
        if(e.isRightClick() || e.isLeftClick()){
            e.setCancelled(true);
            switch(slot){
            case 0:
                p.closeInventory();
                new Shop(ShopType.SHOP_REDSTONE, p);
                break;
            case 1:
                p.closeInventory();
                new Shop(ShopType.SHOP_ORES, p);
                break;
            case 2:
                p.closeInventory();
                new Shop(ShopType.SHOP_SEEDS, p);
                break;
            case 3:
                p.closeInventory();
                new Shop(ShopType.SHOP_FOOD, p);
                break;
            case 4:
                p.closeInventory();
                new Shop(ShopType.SHOP_DROPS, p);
                break;
            case 5:
                p.closeInventory();
                new Shop(ShopType.SHOP_MISC, p);
                break;
            case 6:
                p.closeInventory();
                new Shop(ShopType.SHOP_DECORATION, p);
                break;
            }   
        }
    }

    if(e.getClickedInventory().getName().contains("Shop: ")){
        if (e.getSlotType().equals(SlotType.OUTSIDE)) {
            return;
        }

        for(ClickType type : ClickType.values()){
            if(e.getClick() == type){
                e.setCancelled(true);
            }
        }

        for(InventoryAction action : InventoryAction.values()){
            if(e.getAction() == action){
                e.setCancelled(true);
            }
        }

        if(e.isRightClick() || e.isLeftClick()){
            e.setCancelled(true);
            ItemStack it = e.getCurrentItem();
            if(it == null){
                return;
            }
            int paperID = 339;
            if(it.getTypeId() == paperID){
                Shop.goBack(p);
            }
        }
    }



    switch(e.getClickedInventory().getName()){
    case "Shop: Redstone":
        e.setCancelled(true);
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_REDSTONE, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }

        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_REDSTONE, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    case "Shop: Ores":
        e.setCancelled(true);
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_ORES, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }

        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_ORES, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    case "Shop: Seeds":
        e.setCancelled(true);
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_SEEDS, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }

        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_SEEDS, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    case "Shop: Food":
        e.setCancelled(true);
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_FOOD, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }


        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_FOOD, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    case "Shop: Drops":
        e.setCancelled(true);
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_DROPS, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }

        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_DROPS, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    case "Shop: Misc.":
        e.setCancelled(true);
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_MISC, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }

        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_MISC, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    case "Shop: Decoration":
        e.setCancelled(true);
        if(e.getClick() == ClickType.DROP){
            return;
        }
        if(e.isLeftClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareBuy(p, slot, ShopType.SHOP_DECORATION, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }

        if(e.isRightClick() && e.getCurrentItem().getTypeId() != 0){
            ItemStack item = e.getCurrentItem();
            Material mat = item.getType();
            Shop.prepareSell(p, slot, ShopType.SHOP_DECORATION, mat.name());
            Shop.addItemData(p, e.getCurrentItem().getTypeId(), Shop.getData(e.getCurrentItem().getData()));
        }
        break;
    }
}

所以拜托,我是错过了什么还是做错了什么?

共有1个答案

子车鸿运
2023-03-14

对于第一个问题:new ItemStack(材料。LEAVES_2,1,(short)0);新项目堆叠(材料。LEAVES_2,1,(短)1);你的叶子的构造函数应该是这样的。

第二个问题:购物是什么。getData是否准确返回?如果你想给他一件与商店物品相似的物品,你可以做以下操作:

p.getInventory().addItem(new ItemStack(e.getCurrentItem().getType(),1,e.getCurrentItem().getDurability()));

“:”后面的值(idk在英语中称为idk;我是德语)称为项目的耐久性,称为块的数据。同样,对于耐久性,Bukkit使用简称,但对于块数据,他们使用字节。

 类似资料:
  • 在另一个线程中,我正在从GoogleDeveloper的StoreLocator插件中寻找面板的一些定制帮助 如何从谷歌开发者定制商店定位器插件的侧边框 我需要进一步的定制,如:1.我想为要订购的商店列表制作无序列表。2.想把更多的信息和细节的列表。3.想要获得距离每个商店从搜索位置。 你能建议我如何前进吗?

  • 我想制作一个插件,允许玩家自定义他们的盔甲模型和纹理。我希望服务器上的其他玩家能够看到这些自定义设置。 由于我从未在插件中做过纹理覆盖,我不知道这是否可行,我也无法在网上找到任何相关的东西。我知道你可以让积木看起来和其他积木一样,但是盔甲不行。 我必须给在线玩家发送一个数据包,还是有其他方法可以做到这一点?如果这是可能的,是否有一个地方记录了如何做到这一点?

  • 我试图创建一个类似于权限插件的东西,但我的问题是我不知道如何给一个玩家权限,例如< code>aplugin.chat和< code>aplugin.color,当我只给他权限< code>aplugin时。*。当然,如果许可是注册的,这没有问题,我可以从< code>Bukkit.getPluginManager()检查每个许可。getPermissions()。没有注册怎么解决?在Bungee

  • 我使用谷歌商店定位器(http://storelocator.googlecode.com/git/examples/custom.html)显示体育赛事。 现在我想自定义来自谷歌的消息。我想隐藏信息“这个地区没有商店。然而,离你最近的商店列在下面。"如果缩放区域中没有事件。我想在面板中的孔线上放一个链接,以重定向到特定的网址。 是否有任何选项可以根据我的愿望定制商店定位器? 谢谢

  • 我正在尝试创建一个菜单,我想确保所有玩家都有加入的指南针。 我的代码不起作用——如果你的库存中有一个没有命名的指南针,它就不能给你指南针。如果我没有检查这些,我的代码会给出一个带有上述条件的。 Eclipse不显示错误。我做错了什么?