Skip to content

Latest commit

 

History

History
109 lines (81 loc) · 3.84 KB

README.md

File metadata and controls

109 lines (81 loc) · 3.84 KB

BCommons

Provides reusable and maintainable Spigot components. Stable releases can be found below, hosted on Maven Central.

Maven Central License

Dependency

BCommons is hosted on maven's central repository, below are examples of how to include BCommons into your project:

Maven

<dependency>
  <groupId>me.bradleysteele</groupId>
  <artifactId>commons</artifactId>
  <version>VERSION</version>
</dependency>

Gradle

implementation 'me.bradleysteele:commons:VERSION'

BPlugin

The JavaPlugin is extended by the BPlugin class and handles pre and post processing of loading, enabling and disabling. An example of how to use the BPlugin class can be found below.

public class ExamplePlugin extends BPlugin {
    
    @Override
    public void enable() {
        this.register(
                WorkerExample.class, 
            
                CmdExample.class
        );
    }
}

Registrable

All Registrable classes must be enabled through BPlugin#register. Singletons must have a static get() or getInstance() method returning an instance of the class being registered, otherwise a new instance will be created and registered. Some examples of built-in registrables can be found below.

BCommand

The BCommand registrable provides simple methods for easily creating command executors. It is important to note when using the BCommand class, you do not have to register commands in your plugin.yml.

public class CmdExample extends BCommand {
    
    public CmdExample() {
        // Note: the "main" command executor must be in this list too.
        this.setAliases("example", "exmpl", "test", "tst");
        
        // The information when shown when typing '/minecraft:help /example'.
        this.setDescription("Some information about the command.");
        
        // Default: false, allows the command to be ran by the console.
        this.setAllowConsole(true);
        
        this.setPermission("example.permission.node");
        this.setPermissionDenyMessage("&cYou do not have permission.");
    }
    
    // The method executed when the command is ran. Note that this is not fired 
    // if cancelled by the PlayerCommandPreprocessEvent.
    @Override
    public void execute(CommandSender sender, String[] args) {
        sender.sendMessage("Hello!");
    }
}

BWorker

The BWorker registrable should be used for event handling and running repeating tasks.

public class WorkerExample extends BWorker {

    private boolean canFly;

    public WorkerExample() {
        // Initial delay
        this.setDelay(20);
        
        // Every second (20 ticks)
        this.setPeriod(20);
        
        // Asynchronous
        this.setSync(false); 
    }
    
    @Override
    public void run() {
        canFly = ThreadLocalRandom.current().nextBoolean();
    }
    
    @EventHandler
    public void onToggleFlight(PlayerToggleFlightEvent event) {
        event.setCancelled(event.isFlying() && !canFly);
    }
}