Skip to content

Commit 47652fb

Browse files
authored
Updated items (#956)
* Updated items * Updated code
1 parent 75bd738 commit 47652fb

File tree

3 files changed

+32
-53
lines changed

3 files changed

+32
-53
lines changed

source/plugin/items/creating.rst

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
Creating an ItemStack
33
=====================
44

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-
95
.. javadoc-import::
106
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
138
org.spongepowered.api.entity.Entity
149
org.spongepowered.api.entity.EntityType
1510
org.spongepowered.api.entity.EntityTypes
@@ -38,30 +33,29 @@ and is unbreakable. If you want a plain sword without any other data, then this
3833
}
3934
4035
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.
4438

4539
.. code-block:: java
4640
4741
import java.util.List;
4842
import java.util.stream.Collectors;
4943
5044
import org.spongepowered.api.Sponge;
51-
import org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData;
5245
import org.spongepowered.api.data.meta.ItemEnchantment
5346
import org.spongepowered.api.item.Enchantment;
5447
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());
5956
60-
for (EnchantmentType enchantment : enchantments) {
61-
enchantmentData.set(enchantmentData.enchantments()
62-
.add(Enchantment.of(enchantment, 1000)));
57+
superMegaAwesomeSword.offer(Keys.APPLIED_ENCHANTMENTS);
6358
}
64-
superMegaAwesomeSword.offer(enchantmentData);
6559
6660
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
6761
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
7165
import net.kyori.adventure.text.Component;
7266
import net.kyori.adventure.text.TextComponent;
7367
import net.kyori.adventure.text.format.NamedTextColor;
74-
import org.spongepowered.api.data.key.Keys;
68+
import org.spongepowered.api.data.Keys;
7569
import org.spongepowered.api.item.ItemTypes;
7670
7771
superMegaAwesomeSword.offer(Keys.DISPLAY_NAME, TextComponent.ofChildren(
7872
Component.text("SUPER ", NamedTextColor.BLUE),
7973
Component.text("MEGA ", NamedTextColor.GOLD),
8074
Component.text("AWESOME ", NamedTextColor.DARK_AQUA),
81-
ItemTypes.DIAMOND_SWORD.asComponent().color(NamedTextColor.AQUA));
75+
ItemTypes.DIAMOND_SWORD.get().asComponent().color(NamedTextColor.AQUA));
8276
8377
Finally, to make the sword unbreakable, we can use keys again:
8478
@@ -105,18 +99,18 @@ An example is shown below:
10599
import org.spongepowered.api.event.CauseStackManager.StackFrame;
106100
import org.spongepowered.api.world.Location;
107101
import org.spongepowered.api.world.World;
108-
import org.spongepowered.api.world.extent.Extent;
102+
import org.spongepowered.api.world.server.ServerWorld;
109103
110104
import java.util.Optional;
111105
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());
115109
item.offer(Keys.REPRESENTED_ITEM, superMegaAwesomeSword.createSnapshot());
116110
117-
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
111+
try (StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
118112
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
119-
extent.spawnEntity(item);
113+
word.spawnEntity(item);
120114
}
121115
}
122116

source/plugin/items/index.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
Items
33
=====
44

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-
95
Items are a fundamental feature of Minecraft and plugins. This section shows some basic usage examples and how to
106
create your own items.
117

source/plugin/items/usage.rst

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
Basic Item Usage
33
================
44

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-
95
.. javadoc-import::
106
net.kyori.adventure.text.Component
117
org.spongepowered.api.data.key.Keys
@@ -22,7 +18,7 @@ actual ``ItemStack`` and thus, you will need to set it back into an inventory if
2218
Checking an Item's Type
2319
~~~~~~~~~~~~~~~~~~~~~~~
2420

25-
Checking the type of the item is very simple. You just need to call the :javadoc:`ItemStack#getType()` method.
21+
Checking the type of the item is very simple. You just need to call the :javadoc:`ItemStack#type()` method.
2622

2723
.. code-block:: java
2824
@@ -31,13 +27,13 @@ Checking the type of the item is very simple. You just need to call the :javadoc
3127
import org.spongepowered.api.item.inventory.ItemStack;
3228
3329
public boolean isStick(ItemStack stack) {
34-
ItemType type = stack.getType();
35-
return type.equals(ItemTypes.STICK);
30+
ItemType type = stack.type();
31+
return type.equals(ItemTypes.STICK.get());
3632
}
3733
3834
See how simple that is? Because sticks can stack, we can also find out how many are present.
3935

40-
Getting the number of items in an ``ItemStack`` is relatively easy. The :javadoc:`ItemStack#getQuantity()` method will
36+
Getting the number of items in an ``ItemStack`` is relatively easy. The :javadoc:`ItemStack#quantity()` method will
4137
handle this for us.
4238

4339
Modifying ItemStack Data
@@ -86,29 +82,22 @@ Item Properties
8682
Certain items may hold specific properties. For example, certain items can mine specific blocks, such as a diamond
8783
pickaxe to obsidian. Properties are used for determining if an item can cause an action without actually checking up
8884
the type of the item. We can check if an item can mine obsidian by using the
89-
:javadoc:`HarvestingProperty` of that item.
85+
:javadoc:`Keys#CAN_HARVEST` of that item.
9086

9187
.. code-block:: java
9288
9389
import org.spongepowered.api.block.BlockTypes;
94-
import org.spongepowered.api.data.property.item.HarvestingProperty;
95-
96-
import java.util.Optional;
9790
9891
public boolean canMineObsidian(ItemStack stack) {
99-
Optional<HarvestingProperty> optional =
100-
stack.getProperty(HarvestingProperty.class);
101-
102-
if (optional.isPresent()) {
103-
HarvestingProperty property = optional.get();
104-
return property.getValue().contains(BlockTypes.OBSIDIAN);
105-
}
106-
return false;
92+
List<BlockType> canHarvest =
93+
stack.get(Keys.CAN_HARVEST).orElse(Collections.emptyList());
94+
return canHarvest.contains(BlockTypes.OBSIDIAN.get());
10795
}
10896
109-
This code will check to see if the item has a ``HarvestingProperty``, such as a pickaxe. If present, it will then
110-
return if this item can harvest obsidian without the need to check the type of the item. This is useful in the event
111-
that a mod or a Minecraft update adds a new tool with the capabilities of mining obsidian.
97+
This code will check to see if the item has a assigned key of ``CAN_HARVEST``, such as a pickaxe,
98+
if it doesn't then it uses an empty array. It will then return if obsidian is contained within the list of blocks the
99+
item can harvest. This is useful in the event that a mod or a Minecraft update adds a new tool with the capabilities of
100+
mining obsidian.
112101

113102
Comparing ItemStacks
114103
~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)