Skip to content

API for Developers

Kacper edited this page Sep 6, 2025 · 2 revisions

πŸ“‘ API for Developers

Welcome, developer! πŸ‘¨β€πŸ’»
The OpenSectors API provides a clean and flexible way to interact with the sector system.
You can use it to listen for events, synchronize data, and extend the project with your own features.


πŸ“¦ Repository (Maven)

<repository>
    <id>github</id>
    <url>https://maven.pkg.github.com/fajzu1/OpenSectors</url>
</repository>

πŸ”— Dependency (Maven)

<dependency>
  <groupId>net.lightcode</groupId>
  <artifactId>sectors-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>provided</scope>
</dependency>

πŸš€ Getting Started

The main instance of the plugin should only be fetched once, for example:

public class MyPlugin extends JavaPlugin {

    private BukkitSectorIntegration integration;

    @Override
    public void onEnable() {
        this.integration = BukkitSectorIntegration.create();
    }
}

πŸ‘€ User

Available methods to get a user:

  • BukkitSectorIntegration#findUserByName(String)
  • BukkitSectorIntegration#findUserByUUID(UUID)
 User user = this.integration.findUserByName("fajzu");
        
 System.out.println(user.name());

🏰 Sector

Available methods to get a sector:

  • BukkitSectorIntegration#findSectorByName(String)
  • BukkitSectorIntegration#findSectorByLocation(Location)
 Sector sector = this.integration.findSectorByName("s1");
        
 System.out.println(sector.id());

πŸ”„ Network

Available methods for publishing, subscribing class:

  • BukkitSectorIntegration#subscribe(String, PacketListener)
  • BukkitSectorIntegration#publish(String, Packet)
    this.integration.subscribe("test", new MyPacketListener());
    this.integration.publish("test", new MyPacket("Hello!"));

    public static class MyPacket extends Packet {

        private final String message;

        public MyPacket() {
            this(null); //required for JacksonCodec
        }

        public MyPacket(String message) {
            this.message = message;
        }

        public String message() {
            return this.message;
        }
    }

    public static class MyPacketListener extends PacketListener<MyPacket> {

        public MyPacketListener() {
            super(MyPacket.class);
        }

        @Override
        public void handle(MyPacket packet) {
            System.out.println(packet.message());
        }
    }

🏰 Listening for events

Available custom events to use:

  • PlayerLoadDataEvent
  • PlayerSaveDataEvent
  • PlayerSectorChangeEvent
public class PlayerSectorChangeListener implements Listener {

    @EventHandler
    public void onSectorChange(PlayerSectorChangeEvent event) {
        Player player = event.getPlayer();
        Sector currentSector = event.currentSector(); 
        Sector newSector = event.newSector();
        
        event.setCancelled(true); 

        player.sendMessage("Transfer cancelled " + newSector.id());
    }
}

Registration in onEnable():

@Override
public void onEnable() {
    this.getServer().getPluginManager().registerEvents(new PlayerSectorChangeListener(), this);
}