Skip to content

Commit d7c09bd

Browse files
committed
chore: update code snippets
1 parent 2949769 commit d7c09bd

12 files changed

+71
-67
lines changed
Loading

docs/paper/dev/api/custom-inventory-holder.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class MyInventory implements InventoryHolder {
104104
this.inventory = plugin.getServer().createInventory(this, 9);
105105

106106
// Set the stone that we're going to be clicking.
107-
this.inventory.setItem(0, new ItemStack(Material.STONE));
107+
this.inventory.setItem(0, ItemStack.of(Material.STONE));
108108
}
109109

110110
// A method we will call in the listener whenever the player clicks the stone.
@@ -115,7 +115,7 @@ public class MyInventory implements InventoryHolder {
115115

116116
// A method that will update the counter item.
117117
private void updateCounter() {
118-
this.inventory.setItem(8, new ItemStack(Material.BEDROCK, this.clicks));
118+
this.inventory.setItem(8, ItemStack.of(Material.BEDROCK, this.clicks));
119119
}
120120

121121
@Override

docs/paper/dev/api/data-component-api.mdx

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ stack.resetData(DataComponentTypes.MAX_STACK_SIZE);
147147
Some components are only flags and don't carry any sort of value:
148148

149149
```java
150-
// Make the item unbreakable
151-
stack.setData(DataComponentTypes.UNBREAKABLE);
150+
// Make the item a glider to be used like elytra (combined with the equippable component)
151+
stack.setData(DataComponentTypes.GLIDER);
152152

153-
// Remove the unbreakable flag
154-
stack.unsetData(DataComponentTypes.UNBREAKABLE);
153+
// Remove the glider flag
154+
stack.unsetData(DataComponentTypes.GLIDER);
155155
```
156156

157157
## Advanced usage with builders
@@ -161,57 +161,57 @@ Many data components have complex structures that require builders.
161161
### Modifying prototype component values
162162

163163
```java
164-
ItemStack itemStack = ItemStack.of(Material.DIAMOND_HELMET);
164+
ItemStack helmet = ItemStack.of(Material.DIAMOND_HELMET);
165165
// Get the equippable component for this item, and make it a builder.
166166
// Note: Not all types have .toBuilder() methods
167167
// This is the prototype value of the diamond helmet.
168-
Equippable.Builder builder = itemStack.getData(DataComponentTypes.EQUIPPABLE).toBuilder();
168+
Equippable.Builder builder = helmet.getData(DataComponentTypes.EQUIPPABLE).toBuilder();
169169

170170
// Make the helmet look like netherite
171171
// We get the prototype equippable value from NETHERITE_HELMET
172172
builder.assetId(Material.NETHERITE_HELMET.getDefaultData(DataComponentTypes.EQUIPPABLE).assetId());
173173
// And give it a spooky sound when putting it on
174-
builder.equipSound(Registry.SOUNDS.getKeyOrThrow(Sound.ENTITY_GHAST_HURT));
174+
builder.equipSound(SoundEventKeys.ENTITY_GHAST_HURT);
175175

176176
// Set our new item
177-
itemStack.setData(DataComponentTypes.EQUIPPABLE, builder);
177+
helmet.setData(DataComponentTypes.EQUIPPABLE, builder);
178178
```
179179
This will create a diamond helmet that looks like a netherite helmet and plays a spooky ghast sound when equipped.
180180

181181
### Example: Written book
182182

183183
```java
184-
ItemStack writtenBook = ItemStack.of(Material.WRITTEN_BOOK);
185-
WrittenBookContent.Builder bookBuilder = WrittenBookContent.writtenBookContent("My Book", "AuthorName");
184+
ItemStack book = ItemStack.of(Material.WRITTEN_BOOK);
185+
WrittenBookContent.Builder builder = WrittenBookContent.writtenBookContent("My Book", "AuthorName");
186186

187187
// Add a page
188-
bookBuilder.addPage(Component.text("This is a new page!"));
188+
builder.addPage(Component.text("This is a new page!"));
189189

190190
// Add a page that shows differently for people who have swear filtering on
191191
// Players who have disabled filtering, will see "I hate Paper!", while those with filtering on will see the "I love Paper!".
192-
bookBuilder.addFilteredPage(
192+
builder.addFilteredPage(
193193
Filtered.of(Component.text("I hate Paper!"), Component.text("I love Paper!"))
194194
);
195195

196196
// Change generation
197-
bookBuilder.generation(1);
197+
builder.generation(1);
198198

199199
// Apply changes
200-
writtenBook.setData(DataComponentTypes.WRITTEN_BOOK_CONTENT, bookBuilder.build());
200+
book.setData(DataComponentTypes.WRITTEN_BOOK_CONTENT, builder.build());
201201
```
202202

203203
### Example: Cool sword
204204

205205
```java
206-
ItemStack itemStack = ItemStack.of(Material.DIAMOND_SWORD);
207-
itemStack.setData(DataComponentTypes.LORE, ItemLore.lore().addLine(Component.text("Cool sword!")).build());
208-
itemStack.setData(DataComponentTypes.ENCHANTMENTS, ItemEnchantments.itemEnchantments().add(Enchantment.SHARPNESS, 10).build());
209-
itemStack.setData(DataComponentTypes.RARITY, ItemRarity.RARE);
206+
ItemStack sword = ItemStack.of(Material.DIAMOND_SWORD);
207+
sword.setData(DataComponentTypes.LORE, ItemLore.lore().addLine(Component.text("Cool sword!")).build());
208+
sword.setData(DataComponentTypes.ENCHANTMENTS, ItemEnchantments.itemEnchantments().add(Enchantment.SHARPNESS, 10).build());
209+
sword.setData(DataComponentTypes.RARITY, ItemRarity.RARE);
210210

211-
itemStack.unsetData(DataComponentTypes.TOOL); // Remove the tool component
211+
sword.unsetData(DataComponentTypes.TOOL); // Remove the tool component
212212

213-
itemStack.setData(DataComponentTypes.MAX_DAMAGE, 10);
214-
itemStack.setData(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true); // Make it glow!
213+
sword.setData(DataComponentTypes.MAX_DAMAGE, 10);
214+
sword.setData(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true); // Make it glow!
215215
```
216216

217217
## Matching items without certain data components
@@ -223,10 +223,10 @@ method.
223223
For example, here we compare two diamond swords whilst ignoring their durability:
224224

225225
```java
226-
ItemStack originalSword = new ItemStack(Material.DIAMOND_SWORD);
227-
ItemStack damagedSword = new ItemStack(Material.DIAMOND_SWORD);
226+
ItemStack originalSword = ItemStack.of(Material.DIAMOND_SWORD);
227+
ItemStack damagedSword = ItemStack.of(Material.DIAMOND_SWORD);
228228
damagedSword.setData(DataComponentTypes.DAMAGE, 100);
229229

230230
boolean match = damagedSword.matchesWithoutData(originalSword, Set.of(DataComponentTypes.DAMAGE), false);
231-
logger.info("Do the sword match? " + match); // true
231+
logger.info("Do the sword match? " + match); // -> true
232232
```

docs/paper/dev/api/entity-api/display-entities.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ its position in the corner of the block (this can be seen with the hitbox debug
5353
```java
5454
ItemDisplay display = world.spawn(location, ItemDisplay.class, entity -> {
5555
// customize the entity!
56-
entity.setItemStack(new ItemStack(Material.SKELETON_SKULL));
56+
entity.setItemStack(ItemStack.of(Material.SKELETON_SKULL));
5757
});
5858
```
5959

@@ -181,7 +181,7 @@ making the display spin indefinitely:
181181

182182
```java
183183
ItemDisplay display = location.getWorld().spawn(location, ItemDisplay.class, entity -> {
184-
entity.setItemStack(new ItemStack(Material.GOLDEN_SWORD));
184+
entity.setItemStack(ItemStack.of(Material.GOLDEN_SWORD));
185185
});
186186

187187
int duration = 5 * 20; // duration of half a revolution (5 * 20 ticks = 5 seconds)

docs/paper/dev/api/event-api/custom-events.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: A guide to show you how to add custom events to your plugin.
55

66
# Custom Events
77

8-
Creating custom events is a great way to add functionality to your plugin.
8+
Creating custom events is a great way to add functionality to your plugin.
99
This will allow other plugins to listen to your custom events and add functionality to your plugin.
1010

1111
## Creating a custom event
@@ -85,7 +85,7 @@ public class ExamplePlugin extends JavaPlugin {
8585
PaperIsCoolEvent coolEvent = new PaperIsCoolEvent(Component.text("Paper is cool!"));
8686
coolEvent.callEvent();
8787
// Plugins could have changed the message from inside their listeners here. So we need to get the message again.
88-
// This event structure allows for other plugins to change the message to their taste.
88+
// This event structure allows for other plugins to change the message to their taste.
8989
// Like, for example, a plugin that adds a prefix to all messages.
9090
Bukkit.broadcast(coolEvent.getMessage());
9191
}

docs/paper/dev/api/event-api/handler-lists.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class ExampleListener implements Listener {
4545
handlerList.unregister(this);
4646
// ...
4747
}
48-
48+
4949
// Or:
50-
50+
5151
public ExampleListener() {
5252
// Access the handler list through the static getter
5353
HandlerList handlerList = PlayerJoinEvent.getHandlerList();

docs/paper/dev/api/folia-support.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: How to support both Folia and Paper within your plugin.
77

88
![](/img/folia.png)
99

10-
[Folia](https://github.com/PaperMC/Folia) is a fork of Paper, which is currently maintained by the PaperMC team.
10+
[Folia](https://github.com/PaperMC/Folia) is a fork of Paper, which is currently maintained by the PaperMC team.
1111
It adds the ability to split the world into regions as outlined [here](/folia/reference/overview) in more depth.
1212

1313
# Checking for Folia
@@ -28,7 +28,7 @@ private static boolean isFolia() {
2828

2929
## Schedulers
3030

31-
In order to support Paper and Folia, you must use the correct scheduler. Folia has different types of schedulers
31+
In order to support Paper and Folia, you must use the correct scheduler. Folia has different types of schedulers
3232
that can be used for different things. They are:
3333

3434
- [Global](#global-scheduler)
@@ -47,7 +47,7 @@ GlobalRegionScheduler globalScheduler = server.getGlobalRegionScheduler();
4747
```
4848

4949
### Region scheduler
50-
The region scheduler will be in charge of running tasks for the region that owns a certain location. Do not use this scheduler for
50+
The region scheduler will be in charge of running tasks for the region that owns a certain location. Do not use this scheduler for
5151
operations on entities, as this scheduler is tied to the region. Each entity has its [own scheduler](#entity-scheduler)
5252
which will follow it across regions. As an example, let's say I want to set a block to a beehive:
5353
```java

docs/paper/dev/api/pdc.mdx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ which is the object you want to store the data on. The third is the data itself.
3434
// Create a NamespacedKey
3535
NamespacedKey key = new NamespacedKey(pluginInstance, "example-key");
3636

37-
ItemStack item = new ItemStack(Material.DIAMOND);
37+
ItemStack item = ItemStack.of(Material.DIAMOND);
3838
// ItemMeta implements PersistentDataHolder, so we can get the PDC from it
39-
ItemMeta meta = item.getItemMeta();
40-
meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, "I love Tacos!");
41-
item.setItemMeta(meta);
39+
item.editMeta(meta -> {
40+
meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, "I love Tacos!");
41+
});
4242
```
4343

4444
:::info
@@ -92,9 +92,9 @@ The PDC supports a wide range of data types, such as:
9292
// Storing a list of strings in a container by verbosely creating
9393
// a list data type wrapping the string data type.
9494
container.set(
95-
key,
96-
PersistentDataType.LIST.listTypeFrom(PersistentDataType.STRING),
97-
List.of("a", "list", "of", "strings")
95+
key,
96+
PersistentDataType.LIST.listTypeFrom(PersistentDataType.STRING),
97+
List.of("a", "list", "of", "strings")
9898
);
9999

100100
// Storing a list of strings in a container by using the api
@@ -134,7 +134,7 @@ public class UUIDDataType implements PersistentDataType<byte[], UUID> {
134134

135135
@Override
136136
public byte[] toPrimitive(UUID complex, PersistentDataAdapterContext context) {
137-
ByteBuffer bb = ByteBuffer.wrap(new byte[Long.BYTES * 2]);
137+
ByteBuffer bb = ByteBuffer.allocate(Long.BYTES * 2);
138138
bb.putLong(complex.getMostSignificantBits());
139139
bb.putLong(complex.getLeastSignificantBits());
140140
return bb.array();
@@ -183,13 +183,13 @@ and their PDC can be fetched with <Javadoc name={"org.bukkit.persistence.Persist
183183
- `Entity#getPersistentDataContainer()`
184184
- ##### <Javadoc name={"org.bukkit.block.TileState"}>`TileState`</Javadoc>
185185
- This is slightly more complicated, as you need to cast the block's state to something that extends `TileState`.
186-
This does not work for all blocks, only those that have a tile entity.
186+
This does not work for all blocks, only those that have a block entity.
187187
```java
188-
Block block = ...;
189-
if (block.getState() instanceof Chest chest) {
190-
chest.getPersistentDataContainer().set(key, PersistentDataType.STRING, "I love Tacos!");
191-
chest.update();
192-
}
188+
Block block = ...;
189+
if (block.getState() instanceof Chest chest) {
190+
chest.getPersistentDataContainer().set(key, PersistentDataType.STRING, "I love Tacos!");
191+
chest.update();
192+
}
193193
```
194194
- ##### <Javadoc name={"org.bukkit.structure.Structure"}>`Structure`</Javadoc>
195195
- `Structure#getPersistentDataContainer()`

docs/paper/dev/api/recipes.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class TestPlugin extends JavaPlugin {
2020
@Override
2121
public void onEnable() {
2222
NamespacedKey key = new NamespacedKey(this, "WarriorSword");
23-
ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
23+
ItemStack item = ItemStack.of(Material.DIAMOND_SWORD);
2424

2525
ShapedRecipe recipe = new ShapedRecipe(key, item);
2626
recipe.shape(" A ", "AAA", " B ");
@@ -70,7 +70,7 @@ public class TestPlugin extends JavaPlugin {
7070
@Override
7171
public void onEnable() {
7272
NamespacedKey key = new NamespacedKey(this, "WarriorSword");
73-
ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
73+
ItemStack item = ItemStack.of(Material.DIAMOND_SWORD);
7474

7575
ShapelessRecipe recipe = new ShapelessRecipe(key, item);
7676
recipe.addIngredient(3, Material.DIAMOND);

docs/paper/dev/api/registries.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ final Enchantment enchantment = enchantmentRegistry.getOrThrow(TypedKey.create(
5151
RegistryKey.ENCHANTMENT, Key.key("minecraft:sharpness"))
5252
);
5353

54+
// Same as above, but using the instance's method
55+
final Enchantment enchantment = enchantmentRegistry.getOrThrow(
56+
RegistryKey.ENCHANTMENT.typedKey(Key.key("minecraft:sharpness"))
57+
);
58+
5459
// Same as above, but using generated create method
5560
// available for data-driven registries or "writable" ones
5661
// (those bound to a lifecycle event in RegistryEvents).

0 commit comments

Comments
 (0)