# Developers (API)

GWorld v2 offers a modern API for managing and creating worlds and changing settings (flags) programmatically.

Version v2.0.1

# 1. Setup & Integration

To use GWorld v2 in your plugin, you must add it as a dependency.

## 1. Maven Dependency

<span style="white-space: pre-wrap;">Add the </span>`<span class="editor-theme-code">core</span>`<span style="white-space: pre-wrap;"> module from GWorld to your </span>`<span class="editor-theme-code">pom.xml</span>`<span style="white-space: pre-wrap;">. Since the API is provided by the server at runtime, we use the </span>`<span class="editor-theme-code">provided</span>`<span style="white-space: pre-wrap;"> scope.</span>

```XML
<dependency>
  <groupId>de.gilljan</groupId>
  <artifactId>gworld-core</artifactId>
  <version>2.0.1-SNAPSHOT</version>
  <scope>provided</scope>
</dependency>
```

We use the GitHub Maven Package Registry. To use it, please refer to the GitHub documentation:  
[https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#installing-a-package](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#installing-a-package)

## 2. plugin.yml

<span style="white-space: pre-wrap;">To ensure that your plugin can access GWorld, it must be loaded </span>**after**<span style="white-space: pre-wrap;"> GWorld. To do this, add the entry to your </span>`<span class="editor-theme-code">plugin.yml</span>`:

```YAML
depend: [GWorld]
# Alternatively, if GWorld is optional:
# softdepend: [GWorld]
```

## 3. Access to the API

<span style="white-space: pre-wrap;">The main entry point is the </span>`<span class="editor-theme-code">GWorldAPI</span>`<span style="white-space: pre-wrap;"> interface. You can obtain the instance using the static method of the main class.</span>

```JAVA
import de.gilljan.gworld.GWorld;
import de.gilljan.gworld.api.GWorldAPI;
import de.gilljan.gworld.api.IWorldManager;

public class MyPlugin extends JavaPlugin {
    
    private IWorldManager worldManager;

    @Override
    public void onEnable() {
        if (Bukkit.getPluginManager().isPluginEnabled("GWorld")) {
            // Retrieve API instance
            GWorldAPI api = GWorld.getInstance();
            
            // Load the manager for worlds
            this.worldManager = api.getWorldManager();
        }
    }
}
```

# 2. Create worlds (Builder)

<span style="white-space: pre-wrap;">In GWorld v2, new worlds are created using the </span>**builder pattern**<span style="white-space: pre-wrap;">. This separates the </span>**configuration**<span style="white-space: pre-wrap;"> (registration) from the </span>**generation**<span style="white-space: pre-wrap;"> (load).</span>

# Step 1: The Builder (Registration)

<span style="white-space: pre-wrap;">With the </span>`<span class="editor-theme-code">WorldCreationBuilder </span>`<span style="white-space: pre-wrap;">you define all the properties of the world. Calling </span>`<span class="editor-theme-code">.build()</span>`<span style="white-space: pre-wrap;"> registers the world in the database, but does not yet physically create it.</span>

```java
import de.gilljan.gworld.api.IManageableWorld;
import de.gilljan.gworld.enums.WorldTypeMapping;

// Start the builder
IManageableWorld newWorld = worldManager.createBuilder("MeineEventWelt")
    .worldType(WorldTypeMapping.NORMAL) // Environent: Normal, Nether, End, Large Biomes, Amplified, Flat
    .generator("PlotSquared")           // Optional: Custom Generator Name
    .seed(987654321L)                   // Optional: Set seed
    .build();                           // -> Saves the world to Config/DB
```

<p class="callout info">**Note:**<span style="white-space: pre-wrap;"> The </span>`<span class="editor-theme-code">.build()</span>`<span style="white-space: pre-wrap;"> method internally calls </span>`<span class="editor-theme-code">addWorldFromBuilder</span>`. The world is then known (`<span class="editor-theme-code">registered</span>`<span style="white-space: pre-wrap;">), but still </span>`<span class="editor-theme-code">unloaded</span>`.</p>

## Step 2: Load (generation)

```java
// Creates the bukkit world and loads chunks
boolean success = newWorld.createMap(); 

if (success) {
  getLogger().info("World is ready!");
}
```

<p class="callout success"><span style="white-space: pre-wrap;">The same applies to imports. Use the folder name of the world and use the </span>`<span class="editor-theme-code">importMap()</span>`<span style="white-space: pre-wrap;"> method instead of </span>`<span class="editor-theme-code">createMap()</span>`.</p>

## Why two steps?

1. **Performance:**<span style="white-space: pre-wrap;"> You can register worlds when the server starts, but only load them when a player starts a minigame.</span>
2. **Security:**<span style="white-space: pre-wrap;"> You can set flags (e.g., PvP off) before the first player enters the world.</span>

# 3. Management & Control

<span style="white-space: pre-wrap;">The </span>`<span class="editor-theme-code">IWorldManager</span>`<span style="white-space: pre-wrap;"> manages all worlds, while the </span>`<span class="editor-theme-code">IManageableWorld</span>`<span style="white-space: pre-wrap;"> object allows control over a specific world.</span>

# Retrieve worlds

```java
// Retrive a single world
Optional<IManageableWorld> worldOpt = worldManager.getWorld("Lobby");

// Retrive all worlds
List<IManageableWorld> allWorlds = worldManager.getWorlds();
```

## World actions

<span style="white-space: pre-wrap;">Each </span>`<span class="editor-theme-code">IManageableWorld</span>`<span style="white-space: pre-wrap;"> object provides methods for control:</span>

<table id="bkmrk-methodebeschreibungl" style="margin-bottom: 32px;"><colgroup><col></col><col></col></colgroup><tbody><tr><td style="border: 1px solid;">**Method**

</td><td style="border: 1px solid;">**Description**

</td></tr><tr><td style="border: 1px solid;">`<span class="editor-theme-code">loadMap()</span>`

</td><td style="border: 1px solid;">Loads the world from the hard drive (Bukkit World Init).

</td></tr><tr><td style="border: 1px solid;">`<span class="editor-theme-code">unloadMap()</span>`

</td><td style="border: 1px solid;">Unloads the world and teleports players to the main spawn.

</td></tr><tr><td style="border: 1px solid;">`<span class="editor-theme-code">deleteMap()</span>`

</td><td style="border: 1px solid;">Irrevocably deletes the world (files &amp; database entry).

</td></tr><tr><td style="border: 1px solid;">`<span class="editor-theme-code">reCreate(boolean save)</span>`

</td><td style="border: 1px solid;">Deletes the world and regenerates it (reset). Optionally with backup (true) or without (false).

</td></tr><tr><td style="border: 1px solid;">`<span class="editor-theme-code">clone(String name)</span>`

</td><td style="border: 1px solid;">Creates a copy of the world under a new name.

</td></tr></tbody></table>

### Example: Performing a world reset

```java
worldManager.getWorld(“Farmworld”).ifPresent(world -> {
    // Reset world and keep copy of old world
    world.reCreate(true); 
});
```

### Example: Remove world (from the system)

If you want to remove a world from GWorld (including deleting the files):

```java
// Option A: Directly via the object (recommended)
world.deleteMap();

// Option B: Over the manager
worldManager.removeWorld(world);
```

# 4. Flags & Properties

GWorld allows you to change world settings (flags) programmatically. These settings are stored persistently.

# Set flags

<span style="white-space: pre-wrap;">You can change flags directly on the </span>`<span class="editor-theme-code">IManageableWorld</span>`<span style="white-space: pre-wrap;"> object or set them during creation in the builder.</span>

```java
// Example: Disable PVP and set difficulty to hard
world.setAllowPvP(false);
world.setDifficulty(Difficulty.HARD);

// IMPORTANT: Save changes to disk. Ensures data integrity after a restart.
world.saveProperties();
```

## Available settings

<span style="white-space: pre-wrap;">Here is an overview of the most important methods in </span>`<span class="editor-theme-code">IManageableWorld</span>`<span style="white-space: pre-wrap;"> interface:</span>

<table id="bkmrk-categorymethods-%28get" style="margin-bottom: 32px;"><colgroup><col></col><col></col><col></col></colgroup><tbody><tr><td style="border: 1px solid;">**Category**

</td><td style="border: 1px solid;">**Methods (Getter / Setter)**

</td><td style="border: 1px solid;">**Description**

</td></tr><tr><td style="border: 1px solid;">**PVP**

</td><td style="border: 1px solid;">`<span class="editor-theme-code">isAllowPvP</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setAllowPvP</span>`

</td><td style="border: 1px solid;">Global PvP on/off.

</td></tr><tr><td style="border: 1px solid;">**Spawning**

</td><td style="border: 1px solid;">`<span class="editor-theme-code">isMonsterSpawning</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setMonsterSpawning</span>`

</td><td style="border: 1px solid;">Spawning monsters.

</td></tr><tr><td style="border: 1px solid;"></td><td style="border: 1px solid;">`<span class="editor-theme-code">isAnimalSpawning</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setAnimalSpawning</span>`

</td><td style="border: 1px solid;">Spawning animals.

</td></tr><tr><td style="border: 1px solid;">**Environment**

</td><td style="border: 1px solid;">`<span class="editor-theme-code">isWeatherCycle</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setWeatherCycle</span>`

</td><td style="border: 1px solid;">Whether the weather changes.

</td></tr><tr><td style="border: 1px solid;"></td><td style="border: 1px solid;">`<span class="editor-theme-code">isTimeCycle</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setTimeCycle</span>`

</td><td style="border: 1px solid;">Whether the time of day is advancing.

</td></tr><tr><td></td><td>`<span class="editor-theme-code">getTime</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setTime</span>`

</td><td>Current time in ticks.

</td></tr><tr><td style="border: 1px solid;">**Player**

</td><td style="border: 1px solid;">`<span class="editor-theme-code">getGameMode</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setGameMode</span>`

</td><td style="border: 1px solid;">Default game mode of the world.

</td></tr><tr><td style="border: 1px solid;">**System**

</td><td style="border: 1px solid;">`<span class="editor-theme-code">isLoadOnStartup</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setLoadOnStartup</span>`

</td><td style="border: 1px solid;">Should the world be loaded when the server starts?

</td></tr><tr><td style="border: 1px solid;"></td><td style="border: 1px solid;">`<span class="editor-theme-code">isAllowPvP</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">setAllowPvP</span>`

</td><td style="border: 1px solid;">Keep spawn chunks in RAM.

</td></tr></tbody></table>

## Use in the Builder

<span style="white-space: pre-wrap;">When creating a world, you can also set flags generically via </span>`<span class="editor-theme-code">WorldProperty</span>`:

```java
import de.gilljan.gworld.data.properties.WorldProperty;

manager.createBuilder("Lobby")
    .property(WorldProperty.PVP, false)
    .property(WorldProperty.ANIMALS, false) 
    .build();
```