本文整理匯總了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();
}