Skip to content

Commit 2962656

Browse files
authored
Update creating-your-first-command.md
1 parent 0485895 commit 2962656

1 file changed

Lines changed: 47 additions & 7 deletions

File tree

docs/development/creating-your-first-command.md

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_label: Creating Your First Command
55
---
66
## Prerequisites
77

8-
This guide assumes that you have a [plugin environment](/docs/development/creating-first-plugin) setup.
8+
This guide assumes that you have a [plugin environment](creating-a-first-plugin.md) setup.
99
If you have not already, follow that guide first.
1010

1111
## Writing a ```Command``` type
@@ -82,6 +82,39 @@ public class CommandVelocity extends Command {
8282
}
8383
```
8484

85+
## World check and grab Plugin main class
86+
Due to GoMints plugin world restriction ideas, we should not allow the plugin to run in worlds it should not be active in. We use the [```@InjectPlugin```](https://janmm14.de/static/gomint/index.html?gomint.api/io/gomint/plugin/injection/InjectPlugin.html) annotation here to get our plugin instance into our command; this only works in annotation-defined commands. Commands by players are executed in the world thread of the player, so we do not need to care about this for now.
87+
88+
```java
89+
@Name("velocity")
90+
@Description("Give custom velocity to the player who runs it")
91+
public class CommandVelocity extends Command {
92+
93+
@InjectPlugin
94+
private TestPlugin plugin;
95+
96+
@Override
97+
public void execute(CommandSender commandSender, String alias, Map<String, Object> arguments, CommandOutput output) {
98+
99+
if (commandSender instanceof PlayerCommandSender) {
100+
EntityPlayer player = (EntityPlayer) commandSender;
101+
if (!plugin.activeInworld(player.world()) {
102+
output.fail("Plugin not active in your world");
103+
return;
104+
}
105+
106+
// Now that we have casted the CommandSender to an EntityPlayer, we can use those methods on the object.
107+
player.setVelocity(new Vector(0, 2, 0));
108+
} else if (commandSender instanceof ConsoleCommandSender) {
109+
// TODO: Let's add arguments in a moment!
110+
}
111+
112+
return output;
113+
}
114+
}
115+
```
116+
117+
85118
## Adding Permissions
86119
Adding permissions to a command is as simple as adding the permission annotation.
87120
Supposing that we want to only allow players to use the velocity command if they have the ```velocityplugin.command.velocity```, we can append the annotation to the class declaration:
@@ -120,7 +153,7 @@ The parameter annotation accepts the following fields:
120153
```
121154
122155
### Arguments Passed
123-
The arguments passed when a command is executed by the player/console are passed to ```execute``` in the Map object ```arguments```. For our velocity command example, we will allow a ```ConsoleCommandSender``` to specify a player's name to apply the velocity to.
156+
The arguments passed when a command is executed by the player/console are passed to ```execute``` in the Map object ```arguments```. For our velocity command example, we will allow a ```ConsoleCommandSender``` to specify a player's name to apply the velocity to. We also have to schedule our work to the target player's world here, as console commands are running in a different thread.
124157
125158
```java
126159
// Continued from above
@@ -130,12 +163,19 @@ The arguments passed when a command is executed by the player/console are passed
130163
Float velocity_y = (Float) arguments.getOrDefault("velocity_y", 2f);
131164
Float velocity_z = (Float) arguments.getOrDefault("velocity_z", 0f);
132165
133-
// If all the parameters were passed, the player will receive the specified velocity.
134-
// Otherwise, they will receive a velocity of (x: 0, y: 2, z: 0).
135-
player.setVelocity(new Vector(velocity_x, velocity_y, velocity_z));
166+
// As we are scheduling the command's world to another thread, command execution ends asynchroniously
167+
// We have to mark this and later call markFinished when our command execution is over.
168+
output.markAsync();
169+
170+
// Do not edit player's velocity asynchroniously - we schedule to its thread
171+
player.world().scheduler().execute(() -> {
172+
// If all the parameters were passed, the player will receive the specified velocity.
173+
// Otherwise, they will receive a velocity of (x: 0, y: 2, z: 0).
174+
player.setVelocity(new Vector(velocity_x, velocity_y, velocity_z));
136175

137-
// When the velocity was successfully applied to the given player, a messagae will be sent to the ConsoleCommandSender.
138-
output.success("Applied velocity to " + player.getNameTag());
176+
// When the velocity was successfully applied to the given player, a message will be sent to the ConsoleCommandSender.
177+
output.success("Applied velocity to " + player.getNameTag()).markFinished();
178+
});
139179
}
140180
```
141181

0 commit comments

Comments
 (0)