2
2
Creating an ItemStack
3
3
=====================
4
4
5
- .. warning ::
6
- These docs were written for SpongeAPI 7 and are likely out of date.
7
- `If you feel like you can help update them, please submit a PR! <https://github.com/SpongePowered/SpongeDocs >`__
8
-
9
5
.. javadoc-import ::
10
6
org.spongepowered.api.block.BlockState
11
- org.spongepowered.api.data.key.Keys
12
- org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData
7
+ org.spongepowered.api.data.Keys
13
8
org.spongepowered.api.entity.Entity
14
9
org.spongepowered.api.entity.EntityType
15
10
org.spongepowered.api.entity.EntityTypes
@@ -38,30 +33,29 @@ and is unbreakable. If you want a plain sword without any other data, then this
38
33
}
39
34
40
35
Creating the basic item is done. Now this is a normal diamond sword that we created, but what if we wanted something
41
- more interesting? What about enchanting and naming our sword? We can use :javadoc: `EnchantmentData ` to give our sword
42
- some enchantments. The following example will give our sword every enchantment in the game, to level 1000. Make sure to
43
- include this all before ``return superMegaAwesomeSword; ``.
36
+ more interesting? What about enchanting and naming our sword? We can use :javadoc: `Keys#APPLIED_ENCHANTMENTS ` to give
37
+ our sword some enchantments. The following example will give our sword every enchantment in the game, to level 1000.
44
38
45
39
.. code-block :: java
46
40
47
41
import java.util.List ;
48
42
import java.util.stream.Collectors ;
49
43
50
44
import org.spongepowered.api.Sponge ;
51
- import org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData ;
52
45
import org.spongepowered.api.data.meta.ItemEnchantment
53
46
import org.spongepowered.api.item.Enchantment ;
54
47
55
- EnchantmentData enchantmentData = superMegaAwesomeSword
56
- .getOrCreate(EnchantmentData . class). get();
57
- final List<EnchantmentType > enchantments = Sponge . getRegistry()
58
- .getAllOf(EnchantmentType . class). stream(). collect(Collectors . toList());
48
+ public void withThousandEnchantmentLevel(ItemStack superMegaAwesomeSword){
49
+ List<Enchantment > enchantments = RegistryTypes
50
+ . ENCHANTMENT_TYPE
51
+ .get()
52
+ .stream()
53
+ .filter(type - > type. canBeAppliedToStack(superMegaAwesomeSword))
54
+ .map(type - > Enchantment . of(type, 1000 ))
55
+ .collect(Collectors . toList());
59
56
60
- for (EnchantmentType enchantment : enchantments) {
61
- enchantmentData. set(enchantmentData. enchantments()
62
- .add(Enchantment . of(enchantment, 1000 )));
57
+ superMegaAwesomeSword. offer(Keys . APPLIED_ENCHANTMENTS );
63
58
}
64
- superMegaAwesomeSword. offer(enchantmentData);
65
59
66
60
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
67
61
the ``ItemStack ``. Using this key, we can change the name of the ``ItemStack `` to "SUPER MEGA AWESOME Diamond Sword"
@@ -71,14 +65,14 @@ the ``ItemStack``. Using this key, we can change the name of the ``ItemStack`` t
71
65
import net.kyori.adventure.text.Component ;
72
66
import net.kyori.adventure.text.TextComponent ;
73
67
import net.kyori.adventure.text.format.NamedTextColor ;
74
- import org.spongepowered.api.data.key. Keys ;
68
+ import org.spongepowered.api.data.Keys ;
75
69
import org.spongepowered.api.item.ItemTypes ;
76
70
77
71
superMegaAwesomeSword. offer(Keys . DISPLAY_NAME , TextComponent . ofChildren(
78
72
Component . text(" SUPER " , NamedTextColor . BLUE ),
79
73
Component . text(" MEGA " , NamedTextColor . GOLD ),
80
74
Component . text(" AWESOME " , NamedTextColor . DARK_AQUA ),
81
- ItemTypes . DIAMOND_SWORD. asComponent(). color(NamedTextColor . AQUA ));
75
+ ItemTypes . DIAMOND_SWORD. get() . asComponent(). color(NamedTextColor . AQUA ));
82
76
83
77
Finally , to make the sword unbreakable, we can use keys again:
84
78
@@ -105,18 +99,18 @@ An example is shown below:
105
99
import org.spongepowered.api.event. CauseStackManager . StackFrame ;
106
100
import org.spongepowered.api.world. Location ;
107
101
import org.spongepowered.api.world. World ;
108
- import org.spongepowered.api.world.extent . Extent ;
102
+ import org.spongepowered.api.world.server . ServerWorld ;
109
103
110
104
import java.util. Optional ;
111
105
112
- public void spawnItem(ItemStack superMegaAwesomeSword, Location< World > spawnLocation) {
113
- Extent extent = spawnLocation. getExtent ();
114
- Entity item = extent . createEntity(EntityTypes . ITEM , spawnLocation. getPosition());
106
+ public void spawnItem(ItemStack superMegaAwesomeSword, ServerLocation spawnLocation) {
107
+ ServerWorld world = spawnLocation. world ();
108
+ Item item = world . createEntity(EntityTypes . ITEM , spawnLocation. getPosition());
115
109
item. offer(Keys . REPRESENTED_ITEM , superMegaAwesomeSword. createSnapshot());
116
110
117
- try (StackFrame frame = Sponge . getCauseStackManager (). pushCauseFrame()) {
111
+ try (StackFrame frame = Sponge . server() . causeStackManager (). pushCauseFrame()) {
118
112
frame. addContext(EventContextKeys . SPAWN_TYPE , SpawnTypes . PLACEMENT );
119
- extent . spawnEntity(item);
113
+ word . spawnEntity(item);
120
114
}
121
115
}
122
116
0 commit comments