跳至主要内容

開發者 API

要求

BetterStructures 需要:

  • Java 21 或更高版本
  • Spigot/Paper 伺服器
  • WorldEdit 外掛(硬依賴)

可選依賴:

  • WorldGuard - 用於區域保護功能(需要同時安裝 EliteMobs 以實現完整整合)
  • EliteMobs - 用於精英怪物生成整合
  • MythicMobs - 用於神話怪物整合
  • Terralith、Iris、Terra、TerraformGenerator - 用於自訂生態域相容性

公共儲存庫

Maven

<repositories>
<repository>
<id>magmaguy-repo-releases</id>
<name>MagmaGuy's Repository</name>
<url>https://repo.magmaguy.com/releases</url>
</repository>
</repositories>

<dependency>
<groupId>com.magmaguy</groupId>
<artifactId>BetterStructures</artifactId>
<version>2.3.0</version>
<scope>provided</scope>
</dependency>

注意:訪問 https://repo.magmaguy.com/releases/com/magmaguy/BetterStructures 查看可用版本。最新穩定版:2.3.0

Gradle

repositories {
maven {
name = "magmaguyRepoReleases"
url = uri("https://repo.magmaguy.com/releases")
}
}

dependencies {
implementation 'com.magmaguy:BetterStructures:2.3.0'
}

注意:訪問 https://repo.magmaguy.com/releases/com/magmaguy/BetterStructures 查看可用版本。最新穩定版:2.3.0

事件

注意:事件位於 com.magmaguy.betterstructures.api

套件匯入

import com.magmaguy.betterstructures.api.BuildPlaceEvent;
import com.magmaguy.betterstructures.api.ChestFillEvent;
import com.magmaguy.betterstructures.api.WorldGenerationFinishEvent;
import com.magmaguy.betterstructures.buildingfitter.FitAnything;
import com.magmaguy.betterstructures.modules.ModularWorld;
import com.magmaguy.betterstructures.thirdparty.WorldGuard;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

BuildPlaceEvent

當建築即將被放置時呼叫。透過 FitAnything 物件暴露關於哪個建築將被放置在哪裡等資料。

不要嘗試修改正在放置的建築! 你可以修改小的細節,但更改整個建築可能會導致適配不良。

此事件可以取消。

ChestFillEvent

當箱子被填充時呼叫。使用容器的快照庫存安全地儲存要應用的資料。

你可以使用透過 getContainer().getSnapshotInventory() 取得的容器快照庫存上的 Inventory 方法 addItem()removeItem() 來修改戰利品。

你也可以呼叫 getTreasureConfigFilename() 來識別用於填充此箱子的戰利品表設定。

此事件可以取消。

WorldGenerationFinishEvent

當世界生成完成時呼叫。提供對 ModularWorld 物件的存取,該物件包含生成世界的所有資料,包括生成點、箱子位置和出口點。

此事件不可取消。

你可以透過 getModularWorld() 方法存取世界資料。請參閱下方的 ModularWorld 章節了解所有可用方法。

事件監聽器範例

public class MyBetterStructuresListener implements Listener {

@EventHandler
public void onBuildPlace(BuildPlaceEvent event) {
FitAnything build = event.getFitAnything();
Location location = build.getLocation();
// Custom logic here
// event.setCancelled(true); // To prevent placement
}

@EventHandler
public void onChestFill(ChestFillEvent event) {
Container container = event.getContainer();
Inventory inv = container.getSnapshotInventory();
// Modify inventory here
inv.addItem(new ItemStack(Material.DIAMOND, 1));
}

@EventHandler
public void onWorldGenFinish(WorldGenerationFinishEvent event) {
ModularWorld world = event.getModularWorld();
// Access generated world data
List<Location> spawns = world.getSpawnLocations();
}
}

關鍵類別

FitAnything

FitAnything 類別是在建築被貼上時實例化的類別,處理貼上的每個方面,包括填充箱子和生成怪物。

可用的 getter 方法:

  • getSchematicContainer() - 返回結構圖容器
  • getSchematicClipboard() - 返回 WorldEdit 的 Clipboard 物件
  • getSchematicOffset() - 返回放置用的 Vector 偏移
  • getLocation() - 返回放置位置

這些方法在建立自訂區域保護或分析建築放置時很有用。

ModularWorld

ModularWorld 類別表示生成的模組化世界,透過 WorldGenerationFinishEvent 提供。包含所有位置資料和在生成世界中生成實體和容器的方法。

關鍵方法:

  • getWorld() - 返回 Bukkit World 實例
  • getWorldFolder() - 返回世界的 File 資料夾
  • getSpawnLocations() - 返回所有標記的生成位置(List<Location>
  • getChestLocations() - 返回所有箱子生成位置(List<Location>
  • getBarrelLocations() - 返回所有木桶生成位置(List<Location>
  • getExitLocations() - 返回所有出口點位置(List<ExitLocation>
  • spawnChests() - 在標記位置生成箱子,返回 List<Block>
  • spawnBarrels() - 在標記位置生成木桶,返回 List<Block>
  • spawnInaccessibleExitLocations() - 生成上層電梯結構圖,返回 List<Location>
  • spawnAccessibleExitLocations() - 生成下層電梯結構圖,返回 List<Location>
  • spawnOtherEntities() - 從生成池生成自訂實體
  • spawnInstancedEntities(MatchInstance) - 生成實例化的 Boss 實體,返回 List<InstancedBossEntity>

WorldGuard

WorldGuard 類別處理 WorldGuard 區域保護。實用方法 public static ProtectedRegion generateProtectedRegion(FitAnything fitAnything, String regionName) 供開發者使用,可以輕鬆在 BetterStructures 之上掛接自訂區域保護方案。

其他有用的方法:

  • checkArea(Location, Player) - 檢查位置是否在 BetterStructures 保護區域內(如果受保護則返回 true,向玩家傳送警告訊息)
  • Protect(...) - 以程式方式建立受保護區域
  • Unprotect(CustomBossEntity) - 當 Boss 被擊殺時移除區域保護