Tworzenie ItemStack

Jeśli chcesz utworzyć własne przedmioty, musisz przejść przez kilka kroków. Przejdźmy więc przez podstawowy przykład i stwórz zaczarowany diamentowy miecz.

Aby utworzyć ItemStack, musimy najpierw uzyskać jego buildera (budowniczego) z ItemStack. Możemy go uzyskać używając metody ItemStack#builder(). W konstruktorze możemy określić rzeczy takie jak typ przedmiotu ItemType i jego ilość. W naszym przykładzie utworzymy diamentowy miecz, który zawiera enchanty, niestandardową nazwę i jest niezniszczalny. Jeśli chcesz stworzyć pusty miecz bez żadnych innych danych, to wszystko co potrzebujesz zrobić to:

import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;

public ItemStack generateSword() {
    ItemStack superMegaAwesomeSword = ItemStack.builder()
        .itemType(ItemTypes.DIAMOND_SWORD).build();
    return superMegaAwesomeSword;
}

Creating the basic item is done. Now this is a normal diamond sword that we created, but what if we wanted something more interesting? What about enchanting and naming our sword? We can use Keys#APPLIED_ENCHANTMENTS to give our sword some enchantments. The following example will give our sword every enchantment in the game, to level 1000.

import java.util.List;
import java.util.stream.Collectors;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.meta.ItemEnchantment
import org.spongepowered.api.item.Enchantment;

public void withThousandEnchantmentLevel(ItemStack superMegaAwesomeSword){
    List<Enchantment> enchantments = RegistryTypes
        .ENCHANTMENT_TYPE
        .get()
        .stream()
        .filter(type -> type.canBeAppliedToStack(superMegaAwesomeSword))
        .map(type -> Enchantment.of(type, 1000))
        .collect(Collectors.toList());

    superMegaAwesomeSword.offer(Keys.APPLIED_ENCHANTMENTS);
}

Now let’s say we wanted to give our overpowered sword a cool name to go with it. Here, we can directly offer a key to the ItemStack. Using this key, we can change the name of the ItemStack to „SUPER MEGA AWESOME Diamond Sword”

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.item.ItemTypes;

superMegaAwesomeSword.offer(Keys.DISPLAY_NAME, TextComponent.ofChildren(
    Component.text("SUPER ", NamedTextColor.BLUE),
    Component.text("MEGA ", NamedTextColor.GOLD),
    Component.text("AWESOME ", NamedTextColor.DARK_AQUA),
    ItemTypes.DIAMOND_SWORD.get().asComponent().color(NamedTextColor.AQUA));

Finally, to make the sword unbreakable, we can use keys again:

superMegaAwesomeSword.offer(Keys.UNBREAKABLE, true);

That’s it. You now have a fully enchanted, unbreakable, and beautifully named sword which you can give to players.

Pojawianie przedmiotu

Sure, we can simply put the sword into a player’s inventory, but what if we wanted to throw it out into the open world and spawn the item? This is where entity spawning comes into play. Since the in-game graphical representation of an ItemStack is Item, we can spawn it in similarly to a normal Entity. The EntityType will simply be EntityTypes#ITEM and we will need to specify that the Entity will represent our ItemStack. This can be done using the Keys#REPRESENTED_ITEM key. An example is shown below:

import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.event.CauseStackManager.StackFrame;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.server.ServerWorld;

import java.util.Optional;

public void spawnItem(ItemStack superMegaAwesomeSword, ServerLocation spawnLocation) {
    ServerWorld world = spawnLocation.world();
    Item item = world.createEntity(EntityTypes.ITEM, spawnLocation.getPosition());
    item.offer(Keys.REPRESENTED_ITEM, superMegaAwesomeSword.createSnapshot());

    try (StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
        frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
        word.spawnEntity(item);
    }
}

Creating an ItemStack From a Block

An ItemStack for a block can be created by using the method ItemStack.Builder#itemType(ItemType) similarly to normal items, but what if we wanted to create an ItemStack from a BlockState itself? To create an ItemStack from a BlockState, you would need to use the ItemStack.Builder#fromBlockState(BlockState) method on the ItemStack builder. An example of this is shown below:

import org.spongepowered.api.block.BlockState;

public ItemStack createStack(BlockState state) {
    return ItemStack.builder().fromBlockState(state).build();
}