ItemStack oluşturma

Uyarı

These docs were written for SpongeAPI 7 and are likely out of date. If you feel like you can help update them, please submit a PR!

Kendi eşyalarınızı yaratmak istiyorsanız, birkaç adım geçmeniz gerekiyor. Şimdi temel bir örnek verelim ve büyülü bir elmas kılıcı oluşturalım.

Bir ItemStack oluşturmak için öncelikle yapıcıyı ItemStack’den almamız gerekiyor. Bu, ItemStack#builder() yöntemi ile yapılır. Oluşturucuda, ItemType veya öğe miktarı gibi şeyleri belirtebiliriz. Örneğimizde, büyü özel adını içeren ve kırılmaz olan bir elmas kılıcı oluşturacağız. Başka bir veri olmadan basit bir kılıç istiyorsanız, o zaman yapmanız gereken tek şey:

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

Temel eşyayı yaratma işlemi tamam. Şimdi bu, yarattığımız normal bir elmas kılıcımızdı, ama biz daha ilginç bir şey istediysek ne olacaktı? Kılıcımızı büyülemek ve adlandırmak nasıl olacak? Kılıcımızı büyülemek için EnchantmentData komutunu kullanabilirsiniz. Aşağıdaki örnek, oyunda bulunan her büyüyü kılıcımıza 1000 düzeyine vereceğiz. Bunların tümünü superMegaAwesomeSword;` dönmeden önce eklediğinizden emin olun.

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<EnchantmentType> enchantments = Sponge.getRegistry()
    .getAllOf(EnchantmentType.class).stream().collect(Collectors.toList());

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

Şimdi de, aşırı güçlü kılıcına onunla birlikte gelişmesi için güzel bir isim vermek istedik diyelim. Burada, doğrudan ItemStack için bir anahtar sunabiliriz. Bu tuşu kullanarak, ItemStack ismini “SUPER MEGA AWESOME Diamond Sword” olarak değiştirebiliriz

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.api.data.key.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.asComponent().color(NamedTextColor.AQUA));

Son olarak, kılıcı kırılmaz yapmak için, tuşları tekrar kullanabiliriz:

superMegaAwesomeSword.offer(Keys.UNBREAKABLE, true);

Bu kadar. Artık oyuncularınıza verebileceğiniz tamamen büyüleyici, kırılmaz ve güzel bir şekile sahip kılıç var.

Öğe’yi yumurtlamak

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.extent.Extent;

import java.util.Optional;

public void spawnItem(ItemStack superMegaAwesomeSword, Location<World> spawnLocation) {
    Extent extent = spawnLocation.getExtent();
    Entity item = extent.createEntity(EntityTypes.ITEM, spawnLocation.getPosition());
    item.offer(Keys.REPRESENTED_ITEM, superMegaAwesomeSword.createSnapshot());

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

Bir Blok Üzerinden ItemStack Oluşturma

Bir blok için ItemStack ItemStack.Builder#itemType (ItemType) yöntemi kullanılarak oluşturulabilir. normal öğelere benzer şekilde, ancak a’dan bir ItemStack yaratmak istediysek ne olur? javadoc:BlockState’in kendisi mi? Bir BlockState öğesinden bir ItemStack yaratmak için, ItemStack yapıcı üzerinde ItemStack.Builder#fromBlockState(BlockState) yöntemi kullanılmalıdır. Buna bir örnek aşağıda gösterilmiştir:

import org.spongepowered.api.block.BlockState;

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