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 EnchantmentData to give our sword some enchantments. The following example will give our sword every enchantment in the game, to level 1000. Make sure to include this all before return superMegaAwesomeSword;.

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

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

EnchantmentData enchantmentData = superMegaAwesomeSword
    .getOrCreate(EnchantmentData.class).get();
final List<Enchantment> enchantments = Sponge.getRegistry()
    .getAllOf(Enchantment.class).stream().collect(Collectors.toList());

for (Enchantment enchantment : enchantments) {
    enchantmentData.set(enchantmentData.enchantments()
        .add(new ItemEnchantment(enchantment, 1000)));
}
superMegaAwesomeSword.offer(enchantmentData);

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 org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

superMegaAwesomeSword.offer(Keys.DISPLAY_NAME, Text.of(
    TextColors.BLUE, "SUPER ",
    TextColors.GOLD, "MEGA ",
    TextColors.DARK_AQUA, "AWESOME ",
    TextColors.AQUA, "Diamond Sword"));

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.cause.Cause;
import org.spongepowered.api.event.cause.entity.spawn.EntitySpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.extent.Extent;

import java.util.Optional;

public void spawnItem(ItemStack superMegaAwesomeSword, Location<World> spawnLocation) {
    Extent extent = spawnLocation.getExtent();
    Optional<Entity> optional = extent
        .createEntity(EntityTypes.ITEM, spawnLocation.getPosition());
    if (optional.isPresent()) {
        Entity item = optional.get();
        item.offer(Keys.REPRESENTED_ITEM, superMegaAwesomeSword.createSnapshot());
        extent.spawnEntity(item, Cause.source(EntitySpawnCause.builder()
            .entity(item).type(SpawnTypes.PLUGIN).build()).build());
    }
}

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();
}