Adding Custom Objective

A guide on adding your own custom objectives into XLTournaments

Creating a custom objective is a fairly straight forward process. First of all, ensure you have the API dependency loaded into your project and set it as a depend in your plugin.yml file.

Create your objective class that extends XLObjective. In this example, I will create a class called PlaceObjective which will track block placement:

public class PlaceObjective extends XLObjective {
 
}

You will need to create a contructor matching the super class. Your IDE may prompt you to do so. Inside the super(...); you will add your objective identifier. In this case, it's called "BLOCK_PLACE" and you'd use this in the tournament configuration.

public class PlaceObjective extends XLObjective {

    public PlaceObjective() {
        super("BLOCK_PLACE");
    }
    
}

Next we'll need to add the loadTournament method, this will allow you to load any objective exclusive settings from each tournament configuration file:

public class PlaceObjective extends XLObjective {

    public PlaceObjective() {
        super("BLOCK_PLACE");
    }
    
    @Override
    public boolean loadTournament(FileConfiguration config) {
        // Load optional or required values from config
        
        // Return true if successful load, otherwise false and the tournament being loaded will be disabled
        return true;
    }
    
}

Finally, we add our event listener (and method contents) to track block placement:

You do not need to register the events in this class as this is done by XLT internally in the super class.

public class PlaceObjective extends XLObjective {

    public PlaceObjective() {
        super("BLOCK_PLACE");
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();
        
        for(Tournament tournament : getTournaments()) {
            if(canExecute(tournament, player)) {
                tournament.addScore(player.getUniqueId(), 1);
            }
        }
    }

}

We need to get all active tournaments which use the block place objective and using the canExecute method we if the user is a participant and has finished the challenge (if applicable) and if they are in a disabled world. We can then add the score directly to the tournament passing the UUID of the player and the amount of points to add. We add 1 point in this example as this is represents one block broken.

Register the objective

Simply register the objective as shown below:

// Accessing the API
XLTournamentsAPI api = (XLTournamentsAPI) Bukkit.getPluginManager().getPlugin("XLTournaments");

// Register (internal) objective
api.registerObjective(new PlaceObjective());

If your objective requires an external plugin (we will check if the plugin is enabled) then use:

// Register objective requiring an external dependency
api.registerObjective(new PlaceObjective(), "RequiredPlugin");

And that's it done, make sure to add XLTournaments as a depend or softdepend in your plugin.yml as well.

Last updated