-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can now register commands to be ran by Bukkit
- Loading branch information
Showing
5 changed files
with
82 additions
and
8 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
15 changes: 15 additions & 0 deletions
15
src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.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
26 changes: 26 additions & 0 deletions
26
src/main/java/fr/zcraft/quartzlib/components/commands/QuartzCommandExecutor.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,26 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class QuartzCommandExecutor implements CommandExecutor { | ||
private final CommandGroup group; | ||
|
||
public QuartzCommandExecutor(CommandGroup group) { | ||
this.group = group; | ||
} | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, | ||
@NotNull String[] args) { | ||
try { | ||
group.run(sender, args); | ||
} catch (CommandException e) { | ||
throw new RuntimeException(e); // TODO | ||
} | ||
return true; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/test/java/fr/zcraft/quartzlib/components/commands/CommandRegistrationTests.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,33 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import fr.zcraft.quartzlib.MockedToasterTest; | ||
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CommandRegistrationTests extends MockedToasterTest { | ||
private CommandManager commands; | ||
|
||
@Before | ||
public void beforeEach() { | ||
commands = new CommandManager(); | ||
} | ||
|
||
@Test | ||
public void canRegisterAndRunCommand() throws CommandException { | ||
|
||
final boolean[] ran = {false}; | ||
|
||
class FooCommand { | ||
public void get() { | ||
ran[0] = true; | ||
} | ||
} | ||
|
||
commands.registerCommand("toaster", FooCommand.class, () -> new FooCommand()); | ||
boolean success = server.dispatchCommand(server.addPlayer(), "toaster get"); | ||
Assert.assertTrue(success); | ||
Assert.assertArrayEquals(new boolean[] {true}, ran); | ||
} | ||
} |