Skip to content

Commit

Permalink
Implement argument parsers for Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Sep 24, 2017
1 parent fcbf72c commit b3c49c0
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 59 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ You can either integrate messenger into your own existing messaging system (usin
### [`Commands`](https://github.com/lucko/helper/blob/master/helper/src/main/java/me/lucko/helper/Commands.java)
helper provides a very simple command abstraction, designed to reduce some of the boilerplate needed when writing simple commands.

It doesn't have support for automatic argument parsing, sub commands, or anything like that. It's only purpose is removing the bloat from writing simple commands.

Specifically:
* Checking if the sender is a player/console sender, and then automatically casting.
* Checking for permission status
Expand All @@ -304,37 +302,37 @@ Commands.create()
.assertPermission("message.send")
.assertPlayer()
.assertUsage("<player> <message>")
.assertArgument(0, s -> Bukkit.getPlayerExact(s) != null, "&e{arg} is not online!")
.handler(c -> {
Player other = Bukkit.getPlayerExact(c.getArg(0));
Player sender = c.getSender();
String message = c.getArgs().subList(1, c.getArgs().size()).stream().collect(Collectors.joining(" "));
Player other = c.arg(0).parseOrFail(Player.class);
Player sender = c.sender();
String message = c.args().subList(1, c.args().size()).stream().collect(Collectors.joining(" "));

other.sendMessage("[" + sender.getName() + " --> you] " + message);
sender.sendMessage("[you --> " + sender.getName() + "] " + message);

})
.register(this, "msg");
```

All invalid usage/permission/argument messages can be altered when the command is built.
```java
Commands.create()
.assertConsole("&cNice try ;)")
.assertConsole("&cUse the console to shutdown the server!")
.assertUsage("[countdown]")
.handler(c -> {
ConsoleCommandSender sender = c.getSender();
ConsoleCommandSender sender = c.sender();
int delay = c.arg(0).parse(Integer.class).orElse(5);

sender.sendMessage("Performing graceful shutdown!");

Scheduler.runTaskRepeatingSync(task -> {
int countdown = 5 - task.getTimesRan();
int countdown = delay - task.getTimesRan();

if (countdown <= 0) {
Bukkit.shutdown();
return;
}

Players.forEach(p -> p.sendMessage("Server restarting in " + countdown + " seconds!"));

}, 20L, 20L);
})
.register(this, "shutdown");
Expand Down Expand Up @@ -762,7 +760,7 @@ Then, you can add dependencies for each helper module.
<dependency>
<groupId>me.lucko</groupId>
<artifactId>helper</artifactId>
<version>2.1.5</version>
<version>2.1.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand All @@ -771,7 +769,7 @@ Then, you can add dependencies for each helper module.
#### Gradle
```gradle
dependencies {
compile ("me.lucko:helper:2.1.5")
compile ("me.lucko:helper:2.1.6")
}
```

Expand Down
20 changes: 9 additions & 11 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ You can either integrate messenger into your own existing messaging system (usin
### Commands
helper provides a very simple command abstraction, designed to reduce some of the boilerplate needed when writing simple commands.

It doesn't have support for automatic argument parsing, sub commands, or anything like that. It's only purpose is removing the bloat from writing simple commands.

Specifically:
* Checking if the sender is a player/console sender, and then automatically casting.
* Checking for permission status
Expand All @@ -302,37 +300,37 @@ Commands.create()
.assertPermission("message.send")
.assertPlayer()
.assertUsage("<player> <message>")
.assertArgument(0, s -> Bukkit.getPlayerExact(s) != null, "&e{arg} is not online!")
.handler(c -> {
Player other = Bukkit.getPlayerExact(c.getArg(0));
Player sender = c.getSender();
String message = c.getArgs().subList(1, c.getArgs().size()).stream().collect(Collectors.joining(" "));
Player other = c.arg(0).parseOrFail(Player.class);
Player sender = c.sender();
String message = c.args().subList(1, c.args().size()).stream().collect(Collectors.joining(" "));

other.sendMessage("[" + sender.getName() + " --> you] " + message);
sender.sendMessage("[you --> " + sender.getName() + "] " + message);

})
.register(this, "msg");
```

All invalid usage/permission/argument messages can be altered when the command is built.
```java
Commands.create()
.assertConsole("&cNice try ;)")
.assertConsole("&cUse the console to shutdown the server!")
.assertUsage("[countdown]")
.handler(c -> {
ConsoleCommandSender sender = c.getSender();
ConsoleCommandSender sender = c.sender();
int delay = c.arg(0).parse(Integer.class).orElse(5);

sender.sendMessage("Performing graceful shutdown!");

Scheduler.runTaskRepeatingSync(task -> {
int countdown = 5 - task.getTimesRan();
int countdown = delay - task.getTimesRan();

if (countdown <= 0) {
Bukkit.shutdown();
return;
}

Players.forEach(p -> p.sendMessage("Server restarting in " + countdown + " seconds!"));

}, 20L, 20L);
})
.register(this, "shutdown");
Expand Down
2 changes: 1 addition & 1 deletion helper-mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
</executions>
<configuration>
<links>
<link>https://lucko.me/helper/</link>
<link>https://lucko.me/helper/javadoc/helper/</link>
</links>
</configuration>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion helper-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
</executions>
<configuration>
<links>
<link>https://lucko.me/helper/</link>
<link>https://lucko.me/helper/javadoc/helper/</link>
</links>
</configuration>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion helper-sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
</executions>
<configuration>
<links>
<link>https://lucko.me/helper/</link>
<link>https://lucko.me/helper/javadoc/helper/</link>
</links>
</configuration>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion helper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<artifactId>helper</artifactId>
<packaging>jar</packaging>
<version>2.1.5</version>
<version>2.1.6</version>

<name>helper</name>
<description>A utility to reduce boilerplate code in Bukkit plugins.</description>
Expand Down
Loading

0 comments on commit b3c49c0

Please sign in to comment.