跳到主要内容

开发者 API

要求

BetterStructures 需要:

  • Java 21 或更高版本
  • Spigot/Paper 服务器
  • WorldEdit 插件(硬依赖)

可选依赖:

  • WorldGuard - 用于区域保护功能(需要同时安装 EliteMobs 才能完全集成)
  • EliteMobs - 用于精英怪物生成集成
  • MythicMobs - 用于 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() 获取容器的快照物品栏,然后使用 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();
// 自定义逻辑
// event.setCancelled(true); // 阻止放置
}

@EventHandler
public void onChestFill(ChestFillEvent event) {
Container container = event.getContainer();
Inventory inv = container.getSnapshotInventory();
// 在此修改物品栏
inv.addItem(new ItemStack(Material.DIAMOND, 1));
}

@EventHandler
public void onWorldGenFinish(WorldGenerationFinishEvent event) {
ModularWorld world = event.getModularWorld();
// 访问生成的世界数据
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 被击杀时移除区域保护