-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add acf-command example incl. tests
- Loading branch information
Showing
7 changed files
with
147 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.silthus.template; | ||
|
||
public final class Constants { | ||
|
||
public static final String ACF_BASE_KEY = "commands"; | ||
public static final String INFO_CMD_PERMISSION = "stemplate.admin.info"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/net/silthus/template/commands/TemplateCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.silthus.template.commands; | ||
|
||
import co.aikar.commands.BaseCommand; | ||
import co.aikar.commands.CommandHelp; | ||
import co.aikar.commands.MessageType; | ||
import co.aikar.commands.annotation.*; | ||
import co.aikar.locales.MessageKey; | ||
import org.bukkit.Statistic; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import static net.silthus.template.Constants.ACF_BASE_KEY; | ||
import static net.silthus.template.Constants.INFO_CMD_PERMISSION; | ||
|
||
@CommandAlias("stemplate") | ||
public class TemplateCommands extends BaseCommand { | ||
|
||
// see https://github.com/aikar/commands/wiki/Locales | ||
static MessageKey key(String key) { | ||
return MessageKey.of(ACF_BASE_KEY + "." + key); | ||
} | ||
|
||
// see https://github.com/aikar/commands/wiki/Command-Help | ||
@HelpCommand | ||
@Subcommand("help") | ||
public void showHelp(CommandSender sender, CommandHelp help) { | ||
help.showHelp(); | ||
} | ||
|
||
@Subcommand("info|i") | ||
@CommandAlias("info") | ||
@Description("{@@commands.descriptions.info}") | ||
@CommandCompletion("@players") | ||
@CommandPermission(INFO_CMD_PERMISSION) | ||
public void info(@Flags("self") Player player) { | ||
success("info", | ||
"{player}", player.getName(), | ||
"{play_time}", player.getStatistic(Statistic.PLAY_ONE_MINUTE) + " Minutes" | ||
); | ||
} | ||
|
||
private void success(String key, String... replacements) { | ||
getCurrentCommandIssuer().sendMessage(MessageType.INFO, key(key), replacements); | ||
} | ||
|
||
private void error(String key, String... replacements) { | ||
getCurrentCommandIssuer().sendMessage(MessageType.ERROR, key(key), replacements); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# See https://github.com/aikar/commands/wiki/Locales | ||
commands: | ||
descriptions: | ||
info: 'Prints out the players name and play time.' | ||
info: 'Your name is: {player}. Playtime: {play_time}.' |
32 changes: 32 additions & 0 deletions
32
src/test/java/net/silthus/template/commands/TemplateCommandsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.silthus.template.commands; | ||
|
||
import be.seeseemelk.mockbukkit.entity.PlayerMock; | ||
import net.silthus.template.Constants; | ||
import net.silthus.template.TestBase; | ||
import org.bukkit.Statistic; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class TemplateCommandsTests extends TestBase { | ||
|
||
private PlayerMock player; | ||
|
||
@Override | ||
@BeforeEach | ||
public void setUp() { | ||
super.setUp(); | ||
|
||
player = server.addPlayer(); | ||
player.addAttachment(plugin, Constants.INFO_CMD_PERMISSION, true); | ||
} | ||
|
||
@Test | ||
void info_forSelf_printsOwnPlayerName() { | ||
player.performCommand("stemplate info"); | ||
|
||
int minutesPlayed = player.getStatistic(Statistic.PLAY_ONE_MINUTE); | ||
assertThat(player.nextMessage()).contains("Your name is: Player0. Playtime: " + minutesPlayed); | ||
} | ||
} |