Java Code Examples for org.spongepowered.api.item.inventory.ItemStack#getOrCreate()

The following examples show how to use org.spongepowered.api.item.inventory.ItemStack#getOrCreate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemStackDeserializer.java    From Web-API with MIT License 5 votes vote down vote up
@Override
public ItemStack deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonNode root = p.readValueAsTree();
    if (root.path("type").isMissingNode())
        throw new IOException("Missing item type");

    String id = root.path("type").isTextual()
            ? root.path("type").asText()
            : 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;
}
 
Example 2
Source File: ItemStackSnapshotDeserializer.java    From Web-API with MIT License 5 votes vote down vote up
@Override
public ItemStackSnapshot deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonNode root = p.readValueAsTree();
    if (root.path("type").isMissingNode())
        throw new IOException("Missing item type");

    String id = root.path("type").isTextual()
            ? root.path("type").asText()
            : 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();
}
 
Example 3
Source File: MobSpawnerExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	EntityType entityType = ctx.<EntityType> getOne("mob").get();

	if (src instanceof Player)
	{
		Player player = (Player) src;

		ItemStack mobSpawnerStack = ItemStack.builder().itemType(ItemTypes.MOB_SPAWNER).quantity(1).build();
		Optional<MobSpawnerData> mobSpawnerData = mobSpawnerStack.getOrCreate(MobSpawnerData.class);

		if (mobSpawnerData.isPresent())
		{
			MobSpawnerData data = mobSpawnerData.get();
			data.nextEntityToSpawn().set(entityType, null);

			if (mobSpawnerStack.offer(data).isSuccessful())
			{
				player.setItemInHand(HandTypes.MAIN_HAND, mobSpawnerStack);
				player.sendMessage(Text.of(TextColors.GREEN, "Success! ", TextColors.YELLOW, "Created mob spawner."));
			}
			else
			{
				player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Failed to set mob spawner entity."));
			}
		}
		else
		{
			player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Failed to create mob spawner."));
		}
	}
	else
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /mobspawner!"));
	}

	return CommandResult.success();
}