Skip to content

Commit b7d1f8f

Browse files
committed
Refactor code examples
1 parent fb8d1ce commit b7d1f8f

File tree

7 files changed

+65
-38
lines changed

7 files changed

+65
-38
lines changed

docs/paper/dev/api/command-api/arguments/entity-player.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static LiteralCommandNode<CommandSourceStack> playerProfilesArgument() {
162162
final Collection<PlayerProfile> foundProfiles = profilesResolver.resolve(ctx.getSource());
163163

164164
for (final PlayerProfile profile : foundProfiles) {
165-
ctx.getSource().getSender().sendMessage("Found " + profile.getName());
165+
ctx.getSource().getSender().sendPlainMessage("Found " + profile.getName());
166166
}
167167

168168
return Command.SINGLE_SUCCESS;

docs/paper/dev/api/command-api/arguments/enums.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static LiteralCommandNode<CommandSourceStack> gameModeArgument() {
5757
);
5858
}
5959
else {
60-
ctx.getSource().getSender().sendRichMessage("This command requires a player!");
60+
ctx.getSource().getSender().sendPlainMessage("This command requires a player!");
6161
}
6262

6363
return Command.SINGLE_SUCCESS;
@@ -108,7 +108,7 @@ public static LiteralCommandNode<CommandSourceStack> scoreboardDisplaySlotArgume
108108
.executes(ctx -> {
109109
final DisplaySlot slot = ctx.getArgument("slot", DisplaySlot.class);
110110

111-
ctx.getSource().getSender().sendMessage("You selected: " + slot.getId());
111+
ctx.getSource().getSender().sendPlainMessage("You selected: " + slot.getId());
112112

113113
return Command.SINGLE_SUCCESS;
114114
})
@@ -132,7 +132,7 @@ public static LiteralCommandNode<CommandSourceStack> templateMirrorArgument() {
132132
.executes(ctx -> {
133133
final Mirror mirror = ctx.getArgument("mirror", Mirror.class);
134134

135-
ctx.getSource().getSender().sendMessage("You selected: " + mirror.name());
135+
ctx.getSource().getSender().sendPlainMessage("You selected: " + mirror.name());
136136

137137
return Command.SINGLE_SUCCESS;
138138
})
@@ -156,7 +156,7 @@ public static LiteralCommandNode<CommandSourceStack> templateRotationArgument()
156156
.executes(ctx -> {
157157
final StructureRotation rotation = ctx.getArgument("rotation", StructureRotation.class);
158158

159-
ctx.getSource().getSender().sendMessage("You selected: " + rotation.name());
159+
ctx.getSource().getSender().sendPlainMessage("You selected: " + rotation.name());
160160

161161
return Command.SINGLE_SUCCESS;
162162
})

docs/paper/dev/api/command-api/arguments/location.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static LiteralCommandNode<CommandSourceStack> blockPositionArgument() {
2323
final BlockPositionResolver blockPositionResolver = ctx.getArgument("arg", BlockPositionResolver.class);
2424
final BlockPosition blockPosition = blockPositionResolver.resolve(ctx.getSource());
2525

26-
ctx.getSource().getSender().sendMessage("Put in " + blockPosition.x() + " " + blockPosition.y() + " " + blockPosition.z());
26+
ctx.getSource().getSender().sendPlainMessage("Put in " + blockPosition.x() + " " + blockPosition.y() + " " + blockPosition.z());
2727
return Command.SINGLE_SUCCESS;
2828
}))
2929
.build();

docs/paper/dev/api/command-api/arguments/paper.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import TimeMp4 from "./assets/vanilla-arguments/time.mp4";
1010
import UuidMp4 from "./assets/vanilla-arguments/uuid.mp4";
1111
import ObjectiveCriteriaMp4 from "./assets/vanilla-arguments/objectivecriteria.mp4";
1212

13-
# Arguments for Types Frequently Used in Paper API
13+
# Paper Arguments
14+
The arguments in this section return objects frequently used in Paper API.
1415

1516
## Block state argument
1617
The block state argument can be used for getting a block type and explicit, associated data.
@@ -23,7 +24,7 @@ public static LiteralCommandNode<CommandSourceStack> blockStateArgument() {
2324
.executes(ctx -> {
2425
final BlockState blockState = ctx.getArgument("arg", BlockState.class);
2526

26-
ctx.getSource().getSender().sendMessage("You specified a " + blockState.getType() + "!");
27+
ctx.getSource().getSender().sendPlainMessage("You specified a " + blockState.getType() + "!");
2728
return Command.SINGLE_SUCCESS;
2829
}))
2930
.build();
@@ -118,7 +119,7 @@ public static LiteralCommandNode<CommandSourceStack> timeArgument() {
118119
player.sendRichMessage("Moved time forward by " + timeInTicks + " ticks!");
119120
}
120121
else {
121-
ctx.getSource().getSender().sendMessage("This argument requires a player!");
122+
ctx.getSource().getSender().sendPlainMessage("This argument requires a player!");
122123
}
123124

124125
return Command.SINGLE_SUCCESS;

docs/paper/dev/api/command-api/basics/executors.mdx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ Commands.literal("flyspeed")
5555
.then(Commands.argument("speed", FloatArgumentType.floatArg(0, 1.0f))
5656
.executes(ctx -> {
5757
float speed = ctx.getArgument("speed", float.class); // Retrieve the speed argument
58+
// highlight-next-line
5859
CommandSender sender = ctx.getSource().getSender(); // Retrieve the command sender
60+
// highlight-next-line
5961
Entity executor = ctx.getSource().getExecutor(); // Retrieve the command executor, which may or may not be the same as the sender
6062

6163
// Check whether the executor is a player, as you can only set a player's flight speed
6264
if (!(executor instanceof Player player)) {
6365
// If a non-player tried to set their own flight speed
64-
sender.sendMessage("Only players can fly!");
66+
sender.sendPlainMessage("Only players can fly!");
6567
return Command.SINGLE_SUCCESS;
6668
}
6769

@@ -70,12 +72,12 @@ Commands.literal("flyspeed")
7072

7173
if (sender == executor) {
7274
// If the player executed the command themselves
73-
player.sendMessage(Component.text("Successfully set your flight speed to " + speed));
75+
player.sendPlainMessage("Successfully set your flight speed to " + speed);
7476
}
7577
else {
7678
// If the speed was set by a different sender (Like using /execute)
7779
sender.sendRichMessage("Successfully set <playername>'s flight speed to " + speed, Placeholder.component("playername", player.name()));
78-
player.sendMessage(Component.text("Your flight speed has been set to " + speed));
80+
player.sendPlainMessage("Your flight speed has been set to " + speed);
7981
}
8082

8183
return Command.SINGLE_SUCCESS;
@@ -133,23 +135,28 @@ public class FlightSpeedCommand {
133135
}
134136

135137
private static int runFlySpeedLogic(CommandContext<CommandSourceStack> ctx) {
136-
float speed = ctx.getArgument("speed", float.class);
137-
CommandSender sender = ctx.getSource().getSender();
138-
Entity executor = ctx.getSource().getExecutor();
138+
float speed = ctx.getArgument("speed", float.class); // Retrieve the speed argument
139+
CommandSender sender = ctx.getSource().getSender(); // Retrieve the command sender
140+
Entity executor = ctx.getSource().getExecutor(); // Retrieve the command executor, which may or may not be the same as the sender
141+
142+
// Check whether the executor is a player, as you can only set a player's flight speed
143+
if (!(executor instanceof Player player)) {
144+
// If a non-player tried to set their own flight speed
145+
sender.sendPlainMessage("Only players can fly!");
146+
return Command.SINGLE_SUCCESS;
147+
}
139148

140-
if (executor instanceof Player player) {
141-
player.setFlySpeed(speed);
149+
// Set the player's speed
150+
player.setFlySpeed(speed);
142151

143-
if (sender == executor) {
144-
player.sendMessage("Successfully set your flight speed to " + speed);
145-
}
146-
else {
147-
sender.sendMessage("Successfully set " + player.getName() + "'s flight speed to " + speed);
148-
player.sendMessage("Your flight speed has been set to " + speed);
149-
}
152+
if (sender == executor) {
153+
// If the player executed the command themselves
154+
player.sendPlainMessage("Successfully set your flight speed to " + speed);
150155
}
151156
else {
152-
sender.sendMessage("Consoles can't fly!");
157+
// If the speed was set by a different sender (Like using /execute)
158+
sender.sendRichMessage("Successfully set <playername>'s flight speed to " + speed, Placeholder.component("playername", player.name()));
159+
player.sendPlainMessage("Your flight speed has been set to " + speed);
153160
}
154161

155162
return Command.SINGLE_SUCCESS;

docs/paper/dev/api/command-api/misc/basic-command.mdx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ package your.package.name;
2626

2727
import io.papermc.paper.command.brigadier.BasicCommand;
2828
import io.papermc.paper.command.brigadier.CommandSourceStack;
29+
import org.jspecify.annotations.NullMarked;
2930

31+
@NullMarked
3032
public class YourCommand implements BasicCommand {
3133

3234
@Override
@@ -62,13 +64,15 @@ With the permission method you can, similar to the `canUse` method, set the perm
6264
## Example: Broadcast command
6365
As an example, we can create a simple broadcast command. We start by declaring creating a class which implements `BasicCommand` and overrides `execute` and `permission`:
6466

65-
```java
67+
```java title="BroadcastCommand.java"
6668
package your.package.name;
6769

6870
import io.papermc.paper.command.brigadier.BasicCommand;
6971
import io.papermc.paper.command.brigadier.CommandSourceStack;
70-
import org.jetbrains.annotations.Nullable;
72+
import org.jspecify.annotations.NullMarked;
73+
import org.jspecify.annotations.Nullable;
7174

75+
@NullMarked
7276
public class BroadcastCommand implements BasicCommand {
7377

7478
@Override
@@ -134,8 +138,10 @@ import net.kyori.adventure.text.Component;
134138
import net.kyori.adventure.text.minimessage.MiniMessage;
135139
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
136140
import org.bukkit.Bukkit;
137-
import org.jetbrains.annotations.Nullable;
141+
import org.jspecify.annotations.NullMarked;
142+
import org.jspecify.annotations.Nullable;
138143

144+
@NullMarked
139145
public class BroadcastCommand implements BasicCommand {
140146

141147
@Override
@@ -237,10 +243,12 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
237243
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
238244
import org.bukkit.Bukkit;
239245
import org.bukkit.entity.Player;
240-
import org.jetbrains.annotations.Nullable;
246+
import org.jspecify.annotations.NullMarked;
247+
import org.jspecify.annotations.Nullable;
241248

242249
import java.util.Collection;
243250

251+
@NullMarked
244252
public class BroadcastCommand implements BasicCommand {
245253

246254
@Override

docs/paper/dev/api/command-api/misc/comparison-bukkit-brigadier.mdx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,42 @@ import PaperPartyCommandImage from "./assets/paperparty-command.png";
1414
In order to register Bukkit commands, you would define a class that extends `BukkitCommand`, and implements the `execute(...)` and `tabComplete(...)`
1515
methods. This might look like this:
1616
```java title="BukkitPartyCommand.java"
17+
package your.package.name;
18+
19+
import org.bukkit.Bukkit;
20+
import org.bukkit.command.CommandSender;
21+
import org.bukkit.command.defaults.BukkitCommand;
22+
import org.bukkit.entity.Player;
23+
import org.jspecify.annotations.NullMarked;
24+
25+
import java.util.List;
26+
27+
@NullMarked
1728
public class BukkitPartyCommand extends BukkitCommand {
18-
public BukkitPartyCommand(@NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases) {
29+
public BukkitPartyCommand(String name, String description, String usageMessage, List<String> aliases) {
1930
super(name, description, usageMessage, aliases);
2031
}
2132

2233
@Override
23-
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
34+
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
2435
if (args.length == 0) {
25-
sender.sendMessage("Please provide a player!");
36+
sender.sendPlainMessage("Please provide a player!");
2637
return true;
2738
}
2839

2940
final Player targetPlayer = Bukkit.getPlayer(args[0]);
3041
if (targetPlayer == null) {
31-
sender.sendMessage("Please provide a valid player!");
42+
sender.sendPlainMessage("Please provide a valid player!");
3243
return true;
3344
}
3445

35-
targetPlayer.sendMessage(sender.getName() + " started partying with you!");
36-
sender.sendMessage("You are now partying with " + targetPlayer.getName() + "!");
46+
targetPlayer.sendPlainMessage(sender.getName() + " started partying with you!");
47+
sender.sendPlainMessage("You are now partying with " + targetPlayer.getName() + "!");
3748
return false;
3849
}
3950

4051
@Override
41-
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
52+
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
4253
if (args.length == 1) {
4354
return Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
4455
}
@@ -76,8 +87,8 @@ public static LiteralCommandNode<CommandSourceStack> createCommand(final String
7687
final Player targetPlayer = playerSelector.resolve(ctx.getSource()).getFirst();
7788
final CommandSender sender = ctx.getSource().getSender();
7889

79-
targetPlayer.sendMessage(sender.getName() + " started partying with you!");
80-
sender.sendMessage("You are now partying with " + targetPlayer.getName() + "!");
90+
targetPlayer.sendPlainMessage(sender.getName() + " started partying with you!");
91+
sender.sendPlainMessage("You are now partying with " + targetPlayer.getName() + "!");
8192

8293
return Command.SINGLE_SUCCESS;
8394
}))

0 commit comments

Comments
 (0)