使用
482 字约 2 分钟
2025-05-13
当你想要直接创建或者是修改某个领地的内容时,你可以直接主动发起一个 Dominion 事件。 当事件被发起后,Dominion 核心会监听到你发起的此事件,并检查其操作是否合法。
因此无需担心你的附属操作会弄乱 Dominion 的数据。
也不用担心会影响到其他的附属插件,因为其他附属插件理论上应当同样遵循事件系统机制。
以 DominionCreateEvent()
为例
事件定义
public class DominionCreateEvent extends ResultEvent {
/**
* Constructs a new DominionCreateEvent.
*
* @param operator the command sender who initiated the event
* @param name the name of the dominion
* @param owner the owner of the dominion
* @param world the world where the dominion is located
* @param cuboid the cuboid representing the dominion's boundaries
* @param parent the parent dominion, or null if there is no parent
*/
public DominionCreateEvent(@NotNull CommandSender operator,
@NotNull String name, @NotNull UUID owner,
@NotNull World world, @NotNull CuboidDTO cuboid,
@Nullable DominionDTO parent);
}
几乎所有 Dominion 事件都有一个 CommandSender operator
参数,其表示发起事件的主体,通常应该填写对应的玩家对象。 Dominion 核心在检查事件是否合法的时候就是据此判断的,例如当一个玩家已经达到了他的领地数量上限时,此事件发起后会被核心拦截并取消。
如果想要突破对玩家的限制,那么可以给 operator
传入 Bukkit.getConsoleSender()
以控制台的身份发起事件。 这样 Dominion 核心会忽略掉所有的玩家限制,仅检查领地规则(避免重叠等情况)是否合法。
实践操作
public class example {
public void createDominion(Player player, String dominionName, World world, Location loc1, Location loc2) {
try {
boolean success = new DominionCreateEvent(
player, // 事件主体
dominionName, // 领地名称
player.getUniqueId(), // 领地所属玩家UUID
world, // 领地所属世界
new CuboidDTO(loc1, loc2), // 领地范围
null // 父领地
).call();
if (success) {
player.sendMessage("领地创建成功");
} else {
player.sendMessage("领地创建失败");
}
} catch (Exception e) {
Notification.error(sender, e);
}
}
}