當前位置: 首頁>>代碼示例>>Java>>正文


Java ItemStack.createSnapshot方法代碼示例

本文整理匯總了Java中org.spongepowered.api.item.inventory.ItemStack.createSnapshot方法的典型用法代碼示例。如果您正苦於以下問題:Java ItemStack.createSnapshot方法的具體用法?Java ItemStack.createSnapshot怎麽用?Java ItemStack.createSnapshot使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.spongepowered.api.item.inventory.ItemStack的用法示例。


在下文中一共展示了ItemStack.createSnapshot方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createItem

import org.spongepowered.api.item.inventory.ItemStack; //導入方法依賴的package包/類
public ShopTransactionResult createItem(Player player, ItemStack itemStack) {

        //If the player is not the owner of the shop return a message to the player
        if (!hasRenterPermissions(player)) {
            return new ShopTransactionResult(Messages.YOU_ARE_NOT_THE_OWNER_OF_THIS_SHOP);
        }

        //If the item already exists return a message to the player
        for (ShopItem item : items.values()) {
            if (InventoryUtils.itemStackEqualsIgnoreSize(item.getItemStack(), itemStack)) {
                return new ShopTransactionResult(Messages.THE_SPECIFIED_ITEM_IS_ALREADY_IN_THIS_SHOP);
            }
        }

        //The item is not already in the shop, we need to add it
        ItemStack itemToAdd = itemStack.copy();
        itemToAdd.setQuantity(1);
        ShopItem newShopItem = new ShopItem(itemToAdd.createSnapshot(), 0, -1, -1, -1);
        items.put(newShopItem.getShopItemUUID(), newShopItem);
        return ShopTransactionResult.SUCCESS;
    }
 
開發者ID:Zerthick,項目名稱:PlayerShopsRPG,代碼行數:22,代碼來源:Shop.java

示例2: deserialize

import org.spongepowered.api.item.inventory.ItemStack; //導入方法依賴的package包/類
@Override
public ItemStackSnapshot deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonNode root = p.readValueAsTree();
    if (root.path("type").path("id").isMissingNode())
        throw new IOException("Missing item type");

    String id = root.path("type").path("id").asText();
    Optional<ItemType> optType = Sponge.getRegistry().getType(ItemType.class, id);
    if (!optType.isPresent())
        throw new IOException("Invalid item type " + id);

    Integer amount = root.path("quantity").isMissingNode() ? 1 : root.path("quantity").asInt();

    ItemType type = optType.get();

    ItemStack.Builder builder = ItemStack.builder().itemType(type).quantity(amount);
    ItemStack item = builder.build();

    if (!root.path("data").isMissingNode()) {
        Iterator<Map.Entry<String, JsonNode>> it = root.path("data").fields();
        while (it.hasNext()) {
            Map.Entry<String, JsonNode> entry = it.next();
            Class<? extends DataManipulator> c = WebAPI.getSerializeService().getSupportedData().get(entry.getKey());
            if (c == null) continue;
            Optional<? extends DataManipulator> optData = item.getOrCreate(c);
            if (!optData.isPresent())
                throw new IOException("Invalid item data: " + entry.getKey());
            DataManipulator data = optData.get();
            item.offer(data);
        }
    }

    return item.createSnapshot();
}
 
開發者ID:Valandur,項目名稱:Web-API,代碼行數:35,代碼來源:ItemStackSnapshotDeserializer.java


注:本文中的org.spongepowered.api.item.inventory.ItemStack.createSnapshot方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。