Skip to content

Commit

Permalink
Add example for CommandAPI setup on Velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
DerEchtePilz committed Feb 27, 2024
1 parent 34bae23 commit c191687
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
package dev.jorel.commandapi.examples.java;

import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.proxy.ProxyServer;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.CommandAPIVelocityConfig;
import dev.jorel.commandapi.arguments.IntegerArgument;
import net.kyori.adventure.text.Component;
import org.slf4j.Logger;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@SuppressWarnings("InnerClassMayBeStatic")
public class Examples {

void velocityIntro() {
class velocityIntro {
public class ExamplePlugin {

private final ProxyServer server;
private final Logger logger;

/* ANCHOR: velocityIntro1 */
@Inject
public ExamplePlugin(ProxyServer server, Logger logger) {
this.server = server;
this.logger = logger;

CommandAPI.onLoad(new CommandAPIVelocityConfig(server));
}
/* ANCHOR_END: velocityIntro1 */

/* ANCHOR: velocityIntro2 */
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
// Any CommandAPI command registrations...
CommandAPI.onEnable();
}
/* ANCHOR_END: velocityIntro2 */
}

void velocityIntro() {
/* ANCHOR: velocityIntro3 */
new CommandAPICommand("randomnumber")
.withArguments(new IntegerArgument("min"))
.withArguments(new IntegerArgument("max"))
Expand All @@ -22,7 +55,7 @@ void velocityIntro() {
player.sendMessage(Component.text().content("Your random number is: " + randomNumber));
})
.register();
/* ANCHOR_END: velocityIntro1 */
/* ANCHOR_END: velocityIntro3 */
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package dev.jorel.commandapi.examples.kotlin

import com.google.inject.Inject
import com.velocitypowered.api.event.Subscribe
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent
import com.velocitypowered.api.proxy.ProxyServer
import dev.jorel.commandapi.CommandAPI
import dev.jorel.commandapi.CommandAPICommand
import dev.jorel.commandapi.CommandAPIVelocity
import dev.jorel.commandapi.CommandAPIVelocityConfig
import dev.jorel.commandapi.arguments.IntegerArgument
import dev.jorel.commandapi.executors.PlayerCommandExecutor
import net.kyori.adventure.text.Component
import org.slf4j.Logger
import java.util.concurrent.ThreadLocalRandom
import kotlin.random.Random

Expand Down
27 changes: 26 additions & 1 deletion docssrc/src/velocity_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
>
> The CommandAPI hasn't been released for Velocity yet.
> We do, however, offer snapshot builds. This small section on Velocity will outline how to get the snapshot builds and what limitations the CommandAPI currently has on Velocity.
>
> This page focuses on outlining how to set up the CommandAPI for Velocity. It expects that you are already familiar with how to set up a Velocity plugin.
## Adding the snapshot repository with Maven or Gradle

Expand Down Expand Up @@ -68,6 +70,29 @@ dependencies {

</div>

## Setting up the CommandAPI

The CommandAPI requires two steps: loading and enabling. We will perform these steps in Velocity's loading stages, construction and initialization. These two stages are explained in [their documentation](https://docs.papermc.io/velocity/dev/api-basics#a-word-of-caution).
We will perform the CommandAPI's loading step in the construction phase first:

<div class="multi-pre">

```java,Java
{{#include ../../commandapi-documentation-velocity-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:velocityIntro1}}
```

</div>

Next, we want to utilize Velocity's `ProxyInitializeEvent` to perform the CommandAPI's enabling step:

<div class="multi-pre">

```java,Java
{{#include ../../commandapi-documentation-velocity-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:velocityIntro2}}
```

</div>

## Current limitations

The CommandAPI currently only offers support for a very limited amount of arguments on Velocity. This is because arguments are primarily implemented on the backend servers.
Expand Down Expand Up @@ -103,7 +128,7 @@ To accomplish that, we register the command like this:
<div class="multi-pre">

```java,Java
{{#include ../../commandapi-documentation-velocity-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:velocityIntro1}}
{{#include ../../commandapi-documentation-velocity-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:velocityIntro3}}
```

```kotlin,Kotlin
Expand Down

0 comments on commit c191687

Please sign in to comment.