mirror of
https://github.com/aikar/commands.git
synced 2026-06-01 23:02:18 +00:00
Add an example plugin
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package co.aikar.commands.example;
|
||||
|
||||
import co.aikar.commands.ACF;
|
||||
import co.aikar.commands.CommandManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class ACFExample extends JavaPlugin {
|
||||
|
||||
private static ACFExample plugin;
|
||||
private static CommandManager commandManager;
|
||||
@Override
|
||||
public void onEnable() {
|
||||
plugin = this;
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
commandManager = ACF.createManager(this);
|
||||
commandManager.getCommandContexts().registerContext(SomeObject.class, SomeObject.getContextResolver());
|
||||
commandManager.registerCommand(new SomeCommand());
|
||||
}
|
||||
|
||||
public static ACFExample getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public static CommandManager getCommandManager() {
|
||||
return commandManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package co.aikar.commands.example;
|
||||
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Optional;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CommandAlias("acf|somecommand|sc|somcom")
|
||||
public class SomeCommand extends BaseCommand {
|
||||
|
||||
@Subcommand("test")
|
||||
@CommandAlias("acftest|acft")
|
||||
public void onCommand(CommandSender sender, SomeObject someObject) {
|
||||
sender.sendMessage("You got an object of type: " + someObject.getClass().getName() + " with a value of: " + someObject.getValue());
|
||||
}
|
||||
|
||||
@Subcommand("admin")
|
||||
@CommandPermission("some.perm")
|
||||
@CommandAlias("acfadmin|acfa")
|
||||
public void onAdminCommand(Player player) {
|
||||
player.sendMessage("You got permission!");
|
||||
}
|
||||
|
||||
@Subcommand("optional")
|
||||
@CommandAlias("acfoptional|acfo")
|
||||
public void onOptional(CommandSender sender, @Optional String opt) {
|
||||
if (opt == null) {
|
||||
sender.sendMessage("You did not supply an option.");
|
||||
} else {
|
||||
sender.sendMessage("You supplied: " + opt);
|
||||
}
|
||||
}
|
||||
@Subcommand("default")
|
||||
@CommandAlias("acfdefault|acfd")
|
||||
public void onTestDefault(CommandSender sender, @Default("Unknown User") String name) {
|
||||
sender.sendMessage("Hello, " + name);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package co.aikar.commands.example;
|
||||
|
||||
import co.aikar.commands.CommandContexts;
|
||||
import co.aikar.commands.InvalidCommandArgument;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.contexts.ContextResolver;
|
||||
|
||||
public abstract class SomeObject {
|
||||
private final Integer thisValue;
|
||||
|
||||
SomeObject(Integer thisValue) {
|
||||
this.thisValue = thisValue;
|
||||
}
|
||||
public Integer getValue() {
|
||||
return this.thisValue;
|
||||
}
|
||||
|
||||
public static ContextResolver<SomeObject> getContextResolver() {
|
||||
return (c) -> {
|
||||
String first = c.popFirstArg();
|
||||
if (first == null) {
|
||||
throw new InvalidCommandArgument("Must supply a number");
|
||||
}
|
||||
if ("1".equals(first)) {
|
||||
return new Test1();
|
||||
} else if ("2".equals(first)) {
|
||||
return new Test2();
|
||||
} else {
|
||||
try {
|
||||
return new TestOther(Integer.parseInt(first));
|
||||
} catch (NumberFormatException ignored) {
|
||||
throw new InvalidCommandArgument("Must be a valid number");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static class Test1 extends SomeObject {
|
||||
Test1() {
|
||||
super(1);
|
||||
}
|
||||
}
|
||||
public static class Test2 extends SomeObject {
|
||||
Test2() {
|
||||
super(2);
|
||||
}
|
||||
}
|
||||
public static class TestOther extends SomeObject {
|
||||
TestOther(Integer other) {
|
||||
super(other);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user