本文整理汇总了Java中org.spongepowered.api.item.inventory.ItemStack.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java ItemStack.Builder方法的具体用法?Java ItemStack.Builder怎么用?Java ItemStack.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.spongepowered.api.item.inventory.ItemStack
的用法示例。
在下文中一共展示了ItemStack.Builder方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deserialize
import org.spongepowered.api.item.inventory.ItemStack; //导入方法依赖的package包/类
@Override
public ItemStack 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;
}
示例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();
}