Developer API
Read and change minions from your own plugin, listen to minion events, and add new behaviours, block sources and access providers.
MinionCore has a small public API for other plugins. You can look up minions, change their level, upgrades, fuel and storage, react to what they do through Bukkit events, and plug in your own minion behaviours, custom blocks and island or guild membership.
Everything public lives in cc.arrowtan.minioncore.api and cc.arrowtan.minioncore.api.event. Anything outside those two packages is internal and can change between builds.
Setup
Compile against the MinionCore jar as a provided dependency. Do not shade it into your plugin.
dependencies {
compileOnly(files("libs/MinionCore.jar"))
}<dependency>
<groupId>cc.arrowtan</groupId>
<artifactId>MinionCore</artifactId>
<version>VERSION</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/MinionCore.jar</systemPath>
</dependency>Tell the server to load MinionCore first. Use depend when your plugin cannot work without it, or softdepend when it is optional and you check for it yourself.
depend: [MinionCore]Getting the API
There are two ways in. Both return the same object.
import cc.arrowtan.minioncore.api.MinionCoreAPI;
MinionCoreAPI api = MinionCoreAPI.get();MinionCoreAPI.get() throws IllegalStateException when MinionCore is not enabled, so with a softdepend check first:
if (Bukkit.getPluginManager().isPluginEnabled("MinionCore")) {
MinionCoreAPI api = MinionCoreAPI.get();
}The API is also registered with the Bukkit services manager:
RegisteredServiceProvider<MinionCoreAPI> provider =
Bukkit.getServicesManager().getRegistration(MinionCoreAPI.class);
if (provider != null) {
MinionCoreAPI api = provider.getProvider();
}Call the API from the main server thread. Placed minions are read from the database in the background right after MinionCore enables, so lookups made during your own onEnable can come back empty. Look minions up when you need them, not once at startup.
MinionCoreAPI methods
Lookups only see placed minions. A minion that has been picked up and is an item is not found.
minion
Optional<Minion> minion(UUID id)api.minion(minionId).ifPresent(minion ->
getLogger().info(minion.typeId() + " is level " + minion.level()));minionsOf
Collection<Minion> minionsOf(UUID owner)Every placed minion the player owns, online or not.
int miners = (int) api.minionsOf(player.getUniqueId()).stream()
.filter(minion -> minion.typeId().equals("miner"))
.count();minionAt
Optional<Minion> minionAt(Location location)The minion standing on that exact block position.
api.minionAt(block.getLocation()).ifPresent(minion -> event.setCancelled(true));minionsIn
Collection<Minion> minionsIn(Chunk chunk)for (Minion minion : api.minionsIn(player.getLocation().getChunk())) {
player.sendMessage(minion.typeId() + " owned by " + minion.owner());
}setLevel
boolean setLevel(UUID minion, int level)Sets the level, kept between 1 and the highest level in the type file. Returns false when no placed minion has that id.
api.setLevel(minionId, 5);setUpgrades
boolean setUpgrades(UUID minion, Map<String, Integer> upgrades)Replaces the minion's applied upgrades. The keys are upgrade ids from upgrades.yml, the values how many of each are applied.
api.setUpgrades(minionId, Map.of("storage-1", 1, "speed", 2));addFuel
long addFuel(UUID minion, long actions)Adds stored actions of fuel, capped at the type's max-stored-actions. A negative number takes fuel away, never below zero. Returns the new amount, or -1 when no placed minion has that id.
long now = api.addFuel(minionId, 500);storage
List<ItemStack> storage(UUID minion)A copy of the items in the minion's storage. Changing the list changes nothing. Empty when no placed minion has that id.
int diamonds = api.storage(minionId).stream()
.filter(item -> item.getType() == Material.DIAMOND)
.mapToInt(ItemStack::getAmount)
.sum();setStorage
boolean setStorage(UUID minion, List<ItemStack> items)Replaces the storage contents. Air and empty stacks are dropped. Returns false when no placed minion has that id, or when the minion is still loading its storage or is being picked up.
List<ItemStack> kept = new ArrayList<>(api.storage(minionId));
kept.removeIf(item -> item.getType() == Material.COBBLESTONE);
api.setStorage(minionId, kept);After setLevel, setUpgrades, addFuel and setStorage the minion is woken up, open menus refresh, and the change is written on the next save.
isHologramHidden and setHologramHidden
boolean isHologramHidden(UUID minion)
boolean setHologramHidden(UUID minion, boolean hidden)Reads or changes whether a minion's own hologram is hidden, the same setting as the Hologram button. setHologramHidden returns false when no placed minion has that id. The server and type switches still win, so showing a hologram on a type with hologram.enabled: false changes the setting but shows nothing.
repair
boolean repair(UUID minion)Repairs a broken minion for free. A MinionRepairEvent with no player fires first, and cancelling it keeps the minion broken. Returns true only when the minion was broken and is now repaired, and false when no placed minion has that id, it is not broken, or the event was cancelled. The repair particles show at the minion, but no sound plays and no message is sent. See Breakdowns and repairs.
for (Minion minion : api.minionsOf(player.getUniqueId())) {
if (minion.status() == MinionStatus.BROKEN) {
api.repair(minion.id());
}
}typeSettings
Map<String, Object> typeSettings(String typeId)The settings section of a minion type file, as a read only map. Nested sections come back as maps and lists stay lists, and the values are the plain yml values such as String, Integer, Double and Boolean. The type id is not case sensitive. Returns an empty map when no type has that id, or when the type file has no settings section.
Map<String, Object> settings = api.typeSettings("generator");
Object interval = settings.getOrDefault("interval-seconds", 30);The map is a copy made when you call the method. Call it again after /minion reload to see changed values.
collectorOf and pairedWith
Optional<UUID> collectorOf(UUID minion)
Collection<UUID> pairedWith(UUID collector)collectorOf gives the id of the chunk collector a minion is paired with, and pairedWith gives the ids of every minion paired with a collector. Both read the pairs MinionCore has loaded, which are those of placed minions and collectors. A pair whose collector has been picked up is still reported, since it comes back when the collector is placed again. See Collector pairing.
api.collectorOf(minionId).flatMap(api::minion).ifPresent(collector ->
player.sendMessage("Paired with the collector at " + collector.location()));pair
boolean pair(UUID collector, UUID minion)Pairs a placed minion with a placed chunk collector. Returns false when:
- Either id is not a placed minion, or the first one is not a chunk collector.
- The minion is a chunk collector or a chunk loader.
- Either one is still loading or is being picked up.
- The minion is already paired with this collector, or with another collector that is placed.
- The collector has no free connection.
Ownership and access are not checked, and neither is range, so a pair made here can belong to two different players and can be out of range. A pair with a collector that is not placed is replaced.
unpair
boolean unpair(UUID minion)Removes the pair of a minion, whether or not its collector is placed. Returns false when the minion has no pair, or when the minion or its collector is still loading its pairs.
registerBehaviour
boolean registerBehaviour(MinionBehaviour behaviour)Adds a new minion behaviour. Returns false when the id is already taken, including the built in ids FARMER, MINER, HUNTER, LUMBERJACK, FISHERMAN, CHUNK_LOADER and CHUNK_COLLECTOR. See Custom behaviours.
api.registerBehaviour(new GeneratorBehaviour());registerBlockSource
boolean registerBlockSource(BlockSource source)Adds a source of custom blocks for miners and lumberjacks. Returns false when the id is taken. See Block sources.
api.registerBlockSource(new RubyBlocks());registerAccessProvider and unregisterAccessProvider
void registerAccessProvider(AccessProvider provider)
void unregisterAccessProvider(String id)Lets members of an island, guild or claim use each other's minions. Registering a second provider with the same id replaces the first. See Access providers.
api.registerAccessProvider(new IslandAccess());
api.unregisterAccessProvider("islands");registerAppearance and unregisterAppearance
boolean registerAppearance(MinionAppearance appearance)
void unregisterAppearance(String id)Adds a minion look from your plugin. A type file uses it with appearance.type: PROVIDER and appearance.provider: <id>. Placed minions of those types respawn with the new look straight away. Returns false when the id is taken. See Custom looks.
api.registerAppearance(new StatueLook());The Minion interface
Every lookup returns Minion objects. They are live views of a placed minion.
| Method | Returns |
|---|---|
UUID id() | The minion's id, which stays the same through pick up and placing again |
String typeId() | The type file name, such as miner |
String behaviourId() | The behaviour in upper case, such as MINER |
UUID owner() | The owner's id |
Location location() | Where the minion stands |
int level() | Its level |
long fuel() | Stored fuel, in actions |
boolean paused() | Whether the owner paused it |
MinionStatus status() | WORKING, NO_FUEL, STORAGE_FULL, NO_TARGET, PAUSED, CHUNK_UNLOADED, OWNER_OFFLINE or BROKEN |
Map<String, Integer> upgrades() | Applied upgrade ids and how many of each |
int range() | Work range in blocks, after upgrades and capped at max-range |
int height() | How far up and down the work area reaches |
int delaySeconds() | Seconds between actions, after speed upgrades |
int storageSlots() | Storage size, after upgrades and capped at max-storage |
int fuelEfficiency() | How much less fuel each action uses, in percent from 0 to 100 |
List<ItemStack> storage() | The storage contents, read only. Use setStorage to change them |
Optional<Entity> entity() | The entity that shows the minion while it is spawned, empty otherwise. With a ModelEngine look this is the base entity the model sits on. Call it on the main thread |
BROKEN means the minion has broken down and waits for a repair. Like NO_FUEL, STORAGE_FULL, PAUSED and OWNER_OFFLINE, it takes the minion out of the work queue until something changes.
Events
All nine events are normal Bukkit events and fire on the main server thread. Listen to them like any other event.
| Event | Fires when | Cancellable |
|---|---|---|
MinionPlaceEvent | A player right clicks a block with a minion item | Yes |
MinionActionEvent | A minion is about to do one action | Yes |
MinionStorageFullEvent | A minion stops because its next drops have nowhere to go | No |
MinionFuelEvent | A player adds fuel | Yes |
MinionUpgradeEvent | A player applies an upgrade item or buys a level | Yes |
MinionBreakEvent | A minion is about to break down after an action | Yes |
MinionRepairEvent | A broken minion is about to be repaired, by a player or through the API | Yes |
MinionRemoveEvent | A minion is picked up, removed with /minion remove, or removed by an admin | Yes |
MinionStatusChangeEvent | A minion's status changed | No |
Every event except MinionPlaceEvent extends MinionEvent, which has getMinion().
MinionPlaceEvent
Fires after the world, the spot, the distance to other minions and protection plugins have been checked, and before the placement limits are checked. There is no Minion yet, so this event does not extend MinionEvent.
| Getter | Returns |
|---|---|
getPlayer() | The player placing it |
getMinionId() | The id the minion will have |
getTypeId() | The type id |
getLocation() | Where it will stand |
Cancelling it leaves the item in the player's hand.
@EventHandler
public void onPlace(MinionPlaceEvent event) {
if (event.getLocation().getWorld().getName().equals("spawn")) {
event.setCancelled(true);
event.getPlayer().sendMessage("No minions at spawn.");
}
}MinionActionEvent
Fires every time a minion has found work and is about to do it.
| Getter | Returns |
|---|---|
getMinion() | The minion |
getPlan() | The ActionPlan: blocks(), entities(), drops(), experience() and producedAmount() |
getOwner() | The owner's id |
getTypeId() | The type id |
Cancelling it skips this action. No block is changed, no drops are stored and no fuel is used, and the minion tries again after its normal delay. The list from getPlan().drops() can be changed in a listener before filters and storage see it.
@EventHandler
public void onAction(MinionActionEvent event) {
event.getPlan().drops().removeIf(item -> item.getType() == Material.GRAVEL);
}MinionStorageFullEvent
Fires once when a minion's status turns to storage full, not on every tick it stays full. It cannot be cancelled.
| Getter | Returns |
|---|---|
getMinion() | The minion |
@EventHandler
public void onFull(MinionStorageFullEvent event) {
Player owner = Bukkit.getPlayer(event.getMinion().owner());
if (owner != null) {
owner.playSound(owner.getLocation(), "block.note_block.pling", 1f, 1f);
}
}MinionFuelEvent
Fires after the fuel has been checked as valid for the minion, before it is added.
| Getter | Returns |
|---|---|
getMinion() | The minion |
getPlayer() | The player adding fuel |
getFuelId() | The fuel id from fuel.yml |
getActions() | How many actions of fuel are being added |
Cancelling it adds nothing and the player keeps the fuel item.
MinionUpgradeEvent
Fires when an upgrade item passes its checks, or when a player can afford a level, before anything is applied or paid.
| Getter | Returns |
|---|---|
getMinion() | The minion |
getPlayer() | The player |
getUpgradeId() | The upgrade id from upgrades.yml, or level for a level bought from the menu |
getKind() | The kind as a String: STORAGE, SPEED, RANGE, FUEL_EFFICIENCY, LEVEL, FIRE_ASPECT, DAMAGE, LOOTING, SILK_TOUCH, RELIABILITY, CONNECTIONS or REPLANT |
Cancelling it applies nothing, takes no payment and keeps the upgrade item.
@EventHandler
public void onUpgrade(MinionUpgradeEvent event) {
if (event.getKind().equals("LEVEL") && event.getMinion().level() >= 10
&& !event.getPlayer().hasPermission("myserver.vip")) {
event.setCancelled(true);
}
}MinionBreakEvent
Fires when the breakdown roll after a finished action says the minion breaks. The action itself has already happened and its fuel is used. See Breakdowns and repairs.
| Getter | Returns |
|---|---|
getMinion() | The minion |
getChance() | The breakdown chance in percent that was rolled for this action, such as 0.1 |
Cancelling it keeps the minion working, as if the roll had missed.
@EventHandler
public void onBreak(MinionBreakEvent event) {
Player owner = Bukkit.getPlayer(event.getMinion().owner());
if (owner != null && owner.hasPermission("myserver.vip")) {
event.setCancelled(true);
}
}MinionRepairEvent
Fires before a broken minion is repaired. For a player, it fires after the price has been checked as affordable and before it is paid.
| Getter | Returns |
|---|---|
getMinion() | The minion |
getPlayer() | The player repairing it, or null when the repair came from repair in the API |
Cancelling it keeps the minion broken and takes no payment.
MinionRemoveEvent
Fires before a minion is taken out of the world.
| Getter | Returns |
|---|---|
getMinion() | The minion |
getPlayer() | The player doing it. For an admin removal from the console this is null |
getCause() | PICKUP for shift and right click or the pick up button in the minion menu, COMMAND for /minion remove, ADMIN for admin removal |
Cancelling it keeps the minion where it is.
MinionStatusChangeEvent
Fires after a minion's status changes, such as from WORKING to NO_FUEL. It does not fire when a status is set again to the same value.
| Getter | Returns |
|---|---|
getMinion() | The minion, already showing the new status |
getPreviousStatus() | The status before the change |
getStatus() | The new status |
@EventHandler
public void onStatus(MinionStatusChangeEvent event) {
if (event.getStatus() == MinionStatus.NO_FUEL) {
Player owner = Bukkit.getPlayer(event.getMinion().owner());
if (owner != null) {
owner.sendMessage("Your minion ran out of fuel.");
}
}
}Custom behaviours
A behaviour is what a minion does on each action. Register your own and any type file can use it.
public interface MinionBehaviour {
String id();
ActionPlan plan(Minion minion);
boolean execute(Minion minion, ActionPlan plan);
default boolean usesTargetBlocks() { return false; }
default boolean isTarget(Minion minion, Block block) { return false; }
default boolean usesFuel() { return true; }
default boolean dropsArriveLater() { return false; }
default void stop(Minion minion) { }
}| Method | What it is for |
|---|---|
id() | The name type files use, matched without caring about case |
plan(minion) | Called when it is the minion's turn. Return what it will do as an ActionPlan, or null when there is nothing to do. null shows the Nothing to do status and tries again after performance.no-target-retry seconds |
execute(minion, plan) | Do the work in the world. Return true when it happened, then MinionCore stores the plan's drops, uses fuel and records the stats. Return false and nothing is stored or used, and the minion tries again after its delay |
usesFuel() | Return false for a behaviour that never needs fuel, whatever the type file says |
usesTargetBlocks() and isTarget(minion, block) | Return true and answer isTarget for each block, and MinionCore checks the minion's area with it. A minion showing Nothing to do is woken as soon as a matching block appears nearby, and the area is checked again every performance.rescan-interval seconds |
dropsArriveLater() | Return true when the drops only exist after execute, like a mob that dies a moment later. The minion is then stopped as full before planning whenever its storage has no free slot, unless output.overflow is VOID or the minion's paired chunk collector can take items |
stop(minion) | Called when the minion stops: paused, out of fuel, broken down, stopped by the offline rules, or removed from the world. Clean up anything you started |
plan and execute run on the main thread, inside the shared per tick budget set by performance.actions-per-tick, so keep them quick.
A small example, a generator that makes one cobblestone per action without touching the world:
public final class GeneratorBehaviour implements MinionBehaviour {
@Override
public String id() {
return "GENERATOR";
}
@Override
public ActionPlan plan(Minion minion) {
if (minion.location() == null) {
return null;
}
return new ActionPlan().drop(new ItemStack(Material.COBBLESTONE));
}
@Override
public boolean execute(Minion minion, ActionPlan plan) {
return true;
}
}A behaviour that changes blocks adds them with plan.block(block) in plan, then checks they are still there in execute before changing them.
Register it in onEnable:
MinionCoreAPI.get().registerBehaviour(new GeneratorBehaviour());Then make a type file for it, for example by copying minions/miner.yml to minions/generator.yml and setting:
behaviour: GENERATORA type file that names an unknown behaviour is skipped at startup with a console warning. When your plugin registers the behaviour, MinionCore reads the type files again and loads the placed minions that use it, so load order between the two plugins does not lose any minions. Your behaviour can keep its own options in the settings section of its type files and read them with typeSettings.
Custom looks
A look decides which entity shows a minion. Register one and any type file can use it with appearance.type: PROVIDER.
public interface MinionAppearance {
String id();
Entity spawn(Minion minion, Location location);
default void remove(Minion minion, Entity entity) { }
default void action(Minion minion, Entity entity, ActionPlan plan) { }
default void status(Minion minion, Entity entity, MinionStatus previous, MinionStatus status) { }
default void event(Minion minion, Entity entity, String event) { }
}| Method | What it is for |
|---|---|
id() | The name type files put in appearance.provider, matched without caring about case |
spawn | Spawn and return the entity. location is the centre of the minion's block, facing its direction. MinionCore tags the entity as the minion's own, protects it and removes it later. Return null to use the vanilla look |
remove | Called right before MinionCore removes the entity, to clean up anything you attached to it |
action | Called on each action. The plan's variant() names the kind of action |
status | Called when the minion's status changes |
event | Called on place, pickup, fuel, upgrade, repair and break |
The built in strike animation does not play on a provided look, since your entity may have no arms. The turn and step toward the target still play.
appearance:
type: PROVIDER
provider: statuesA behaviour can name its kind of action with ActionPlan#variant, such as new ActionPlan().variant("smelt"). ModelEngine looks then play action-animations.smelt, and custom looks see it in action.
Block sources
A block source teaches miners and lumberjacks about custom blocks from another plugin. MinionCore ships one for ItemsAdder with the id itemsadder.
public interface BlockSource {
String id();
boolean available();
boolean matches(Block block, String blockId);
Collection<ItemStack> drops(Block block, ItemStack tool);
boolean breakBlock(Block block);
}| Method | What it is for |
|---|---|
id() | The first part of the block entry in a type file |
available() | Return false while your plugin cannot answer yet. Those entries are skipped with one warning |
matches(block, blockId) | Whether this block is the custom block named blockId |
drops(block, tool) | What breaking it gives |
breakBlock(block) | Break it in the world. Return true when it broke |
Entries are written as <source>:<namespace>:<id> in settings.blocks of a miner type or settings.logs of a lumberjack type. The part after the source id is passed as blockId, so the entry ruby:gems:ruby_ore calls the source with id ruby and blockId set to gems:ruby_ore.
settings:
blocks:
- STONE
- ruby:gems:ruby_oreAccess providers
An access provider lets island, guild or claim members use each other's minions without trusting every player by hand.
public interface AccessProvider {
String id();
boolean isMember(UUID player, Minion minion);
}public final class IslandAccess implements AccessProvider {
@Override
public String id() {
return "islands";
}
@Override
public boolean isMember(UUID player, Minion minion) {
return islands.sameIsland(player, minion.owner());
}
}A provider grants nothing on its own. The server owner lists what members may do under the provider's id in config.yml:
access:
providers:
islands:
- open
- take-storage
- fuelThe actions are open, take-storage, fuel, upgrade, filter, pickup, pair, repair and saplings. Pausing, managing the trust list, turning the minion and switching a collector's mode always stay with the owner. If isMember throws, MinionCore logs a warning and treats the player as not a member. See Trust and access.