本文整理汇总了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;
}
示例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();
}