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