Coder Social home page Coder Social logo

jumanji144 / picocli-minecraft Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 110 KB

A Small and simple wrapper around picocli as a minecraft command system

License: MIT License

Java 100.00%
bukkit bukkit-plugin command-line gradle java minecraft minecraft-plugin picocli

picocli-minecraft's Introduction

Picocli Minecraft

This project is small wrapper around picocli in the use of a command system.

Usage

The command system is highly oriented around the already existing picocli syntax with a few extra added things.
To register commands you must create a class that extends the respective platform base. After that picocli syntax can follow, with the exception that there is an added annotation @Permission that can be added to @Command annotated classes or methods that will check for the given permission before execution.
For execution the Base class requires you to implement the int execute() method. For context related values the respetive Example for bukkit:

@Command(
        name = "teleport",
        description = "Teleport players to a location",
        mixinStandardHelpOptions = true
)
@Permission(permission = "teleport.use")
public class TeleportCommand extends BukkitBase {

    @Option(names = {"-p"}, description="Player to teleport to", required=true)
    private Player p;
    
    public int execute() {
        if(getSender() instanceof Player) {
            ((Player)getSender()).teleport(p);
        }
    }

}

To then register the commands a CommandManager must be created and given a platform respective ICommandRegistrar.

Bukkit

public final class Test extends JavaPlugin {

    @Override
    public void onEnable() {
        CommandManager manager = new CommandManager(new BukkitCommandRegistrar(this));
        manager.register(new TeleportCommand());
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}

BungeeCord

public final class Test extends Plugin {

    @Override
    public void onEnable() {
        CommandManager manager = new CommandManager(new BungeeCommandRegistrar(this));
        manager.register(new TeleportCommand());
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}

CommandManager also allows you to define the help color scheme via setColorScheme

Examples

SpawnCommand

@Command(
        name = "spawn",
        description = "Spawn entities or blocks"
)
public class SpawnCommand extends BukkitBase {

    @Command(
            name = "entity",
            description = "Spawn an entity"
    )
    public int spawnEntity(@Parameters(paramLabel = "type", description = "The entity type to spawn") EntityType type) {
        if(getSender() instanceof Player) {
            Player player = (Player) getSender();
            player.getWorld().spawnEntity(player.getLocation(), type);
            return 0;
        } else {
            getSender().sendMessage("You must be a player to use this command");
            return 1;
        }
    }

    @Command(
            name = "block",
            description = "Spawn a block"
    )
    public int spawnBlock(@Parameters(paramLabel = "type", description = "The block type to spawn") Material material) {
        if(getSender() instanceof Player) {
            Player player = (Player) getSender();
            player.getWorld().getBlockAt(player.getLocation()).setType(material);
            return 0;
        } else {
            getSender().sendMessage("You must be a player to use this command");
            return 1;
        }
    }

    @Override
    public int execute() {
        // make it show the help message
        throw new ParameterException(getCommandLine(), "You must specify a subcommand");
    }
}

TeleportCommand

@Command(
        name = "teleport",
        description = "Teleport players to a location",
        mixinStandardHelpOptions = true
)
public class TeleportCommand extends BukkitBase {

    @Parameters(description = "The x coordinate to teleport to", arity = "0..1", defaultValue = "_NULL_")
    Optional<Double> x;

    @Parameters(description = "The y coordinate to teleport to", arity = "0..1", defaultValue = "_NULL_")
    Optional<Double> y;

    @Parameters(description = "The z coordinate to teleport to", arity = "0..1", defaultValue = "_NULL_")
    Optional<Double> z;

    @Option(names = {"-p", "--player"}, description = "The player to teleport", arity = "0..1", defaultValue = "_NULL_")
    Optional<Player> player;

    @Option(names = {"-w", "--world"}, description = "The world to teleport to", arity = "0..1", defaultValue = "_NULL_")
    Optional<World> world;


    @Override
    public int execute() {
        Player executor = null;
        if(getSender() instanceof Player) {
            executor = (Player) getSender();
        }
        Player target = player.orElse(executor);
        if(target == null) {
            getSender().sendMessage(ChatColor.RED + "You must be a player to use this command");
            return 1;
        }
        World world = this.world.orElseGet(target::getWorld);
        if(x.isPresent() && y.isPresent() && z.isPresent()) {
            target.teleport(new Location(world, x.get(), y.get(), z.get()));
        } else {
            if(target != executor) {
                target.teleport(executor);
            } else {
                if(player.isPresent()) {
                    getSender().sendMessage(ChatColor.RED + "You cannot teleport to yourself!");
                } else {
                    throw new ParameterException(getCommandLine(), "You must specify a location to teleport to");
                }
            }
        }
        return 0;
    }
}

Installation

To use this library in your own project you must add the following based on your platform:

Maven

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.github.Nowilltolife</groupId>
        <artifactId>picocli-minecraft</artifactId>
        <version>1.1.1-beta</version>
    </dependency>
</dependencies>

Gradle

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    implementation 'com.github.Nowilltolife:picocli-minecraft:1.1.1-beta'
}

Credits

picocli-minecraft's People

Contributors

jumanji144 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.