Skip to content

Commit

Permalink
add events support to api, now we can let dominion change event to be…
Browse files Browse the repository at this point in the history
… announced
  • Loading branch information
ColdeZhang committed Oct 19, 2024
1 parent 3daa40a commit 4282f62
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var libraries = listOf<String>()
libraries = libraries + "cn.lunadeer:MinecraftPluginUtils:2.0.7"

group = "cn.lunadeer"
version = "2.14.4-beta"
version = "2.14.5-beta"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Expand Down
98 changes: 68 additions & 30 deletions core/src/main/java/cn/lunadeer/dominion/Cache.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package cn.lunadeer.dominion;

import cn.lunadeer.dominion.api.DominionAPI;
import cn.lunadeer.dominion.dtos.*;
import cn.lunadeer.dominion.events.PlayerCrossDominionBorderEvent;
import cn.lunadeer.dominion.events.PlayerMoveInDominionEvent;
import cn.lunadeer.dominion.events.PlayerMoveOutDominionEvent;
import cn.lunadeer.dominion.utils.MessageDisplay;
import cn.lunadeer.dominion.utils.Particle;
import cn.lunadeer.dominion.utils.ResMigration;
import cn.lunadeer.dominion.utils.map.MapRender;
import cn.lunadeer.minecraftpluginutils.AutoTimer;
import cn.lunadeer.minecraftpluginutils.Scheduler;
import cn.lunadeer.minecraftpluginutils.XLogger;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -26,13 +31,15 @@
import static cn.lunadeer.dominion.DominionNode.getLocInDominionNode;
import static cn.lunadeer.dominion.DominionNode.isInDominion;

public class Cache {
public class Cache implements Listener {

public Cache() {
instance = this;
player_current_dominion_id = new HashMap<>();
loadDominions();
loadMembers();
loadGroups();
Bukkit.getPluginManager().registerEvents(this, Dominion.instance);
}

/**
Expand Down Expand Up @@ -213,50 +220,42 @@ public DominionDTO getPlayerCurrentDominion(@NotNull Player player) {
if (last_in_dom_id != null) {
last_dominion = id_dominions.get(last_in_dom_id);
}
if (isInDominion(last_dominion, player.getLocation())) {
if (dominion_children.get(last_in_dom_id) == null || dominion_children.get(last_in_dom_id).isEmpty()) {
// 如果玩家仍在领地内,且领地没有子领地,则直接返回
if (recheckPlayerState) {
lightOrNot(player, last_dominion);
flyOrNot(player, last_dominion);
recheckPlayerState = false;
}
return last_dominion;
// 如果玩家仍在上次一记录的领地内,且领地没有子领地,则直接返回
if (isInDominion(last_dominion, player.getLocation())
&& (dominion_children.get(last_in_dom_id) == null || dominion_children.get(last_in_dom_id).isEmpty())) {
// 如果缓存更新则需要重新检查玩家状态
if (recheckPlayerState) {
checkPlayerStates(player, last_dominion);
recheckPlayerState = false;
}
return last_dominion;

}
// 否则获取玩家当前所在领地,然后对比上次记录的领地
DominionDTO current_dominion = dominion_trees.getLocInDominionDTO(player.getLocation());
new PlayerCrossDominionBorderEvent(player, last_dominion, current_dominion).callEvent();

int last_dom_id = last_dominion == null ? -1 : last_dominion.getId();
int current_dom_id = current_dominion == null ? -1 : current_dominion.getId();
if (last_dom_id == current_dom_id) {
return last_dominion;
}
// 如果上次记录的领地不为空,则触发玩家离开领地事件
if (last_dom_id != -1) {
MessageDisplay.show(player, Dominion.config.getMessageDisplayJoinLeave(),
last_dominion.getLeaveMessage()
.replace("{DOM}", last_dominion.getName())
.replace("{OWNER}", getPlayerName(last_dominion.getOwner()))
);
new PlayerMoveOutDominionEvent(player, last_dominion).callEvent();
}
// 如果当前领地不为空,则触发玩家进入领地事件
if (current_dom_id != -1) {
MessageDisplay.show(player, Dominion.config.getMessageDisplayJoinLeave(),
current_dominion.getJoinMessage()
.replace("{DOM}", current_dominion.getName())
.replace("{OWNER}", getPlayerName(current_dominion.getOwner()))
);
new PlayerMoveInDominionEvent(player, current_dominion).callEvent();
}

lightOrNot(player, current_dominion); // 发光检查
flyOrNot(player, current_dominion); // 飞行检查
// 更新玩家当前所在领地缓存
if (current_dominion == null) {
player_current_dominion_id.put(player.getUniqueId(), null);
return null;
} else {
player_current_dominion_id.put(player.getUniqueId(), current_dominion.getId());
return current_dominion;
}
player_current_dominion_id.put(player.getUniqueId(), current_dominion.getId());
// show border
if (current_dominion.getFlagValue(Flag.SHOW_BORDER)) {
Particle.showBorder(player, current_dominion);
}
return current_dominion;
}
}

Expand Down Expand Up @@ -607,4 +606,43 @@ private void init(List<DominionDTO> dominions) {
public void updatePlayerUsingGroupTitle(UUID uuid, Integer groupId) {
map_player_using_group_title_id.put(uuid, groupId);
}

private void checkPlayerStates(Player player, DominionDTO dominion) {
flyOrNot(player, dominion);
lightOrNot(player, dominion);
}

@EventHandler
public void onPlayerMoveInDominion(PlayerMoveInDominionEvent event) {
XLogger.debug("PlayerMoveInDominionEvent called.");
MessageDisplay.show(event.getPlayer(), Dominion.config.getMessageDisplayJoinLeave(),
event.getDominion().getJoinMessage()
.replace("{DOM}", event.getDominion().getName())
.replace("{OWNER}", getPlayerName(event.getDominion().getOwner()))
);
// show border
if (event.getDominion().getEnvironmentFlagValue().get(Flag.SHOW_BORDER)) {
Particle.showBorder(event.getPlayer(), (DominionDTO) event.getDominion());
}
}

@EventHandler
public void onPlayerMoveOutDominion(PlayerMoveOutDominionEvent event) {
XLogger.debug("PlayerMoveOutDominionEvent called.");
MessageDisplay.show(event.getPlayer(), Dominion.config.getMessageDisplayJoinLeave(),
event.getDominion().getLeaveMessage()
.replace("{DOM}", event.getDominion().getName())
.replace("{OWNER}", getPlayerName(event.getDominion().getOwner()))
);
// show border
if (event.getDominion().getEnvironmentFlagValue().get(Flag.SHOW_BORDER)) {
Particle.showBorder(event.getPlayer(), (DominionDTO) event.getDominion());
}
}

@EventHandler
public void onPlayerCrossDominionBorderEvent(PlayerCrossDominionBorderEvent event) {
XLogger.debug("PlayerCrossDominionBorderEvent called.");
checkPlayerStates(event.getPlayer(), (DominionDTO) event.getTo());
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/cn/lunadeer/dominion/Dominion.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public void onEnable() {
config.getDbPass());
DatabaseTables.migrate();
new Scheduler(this);
AutoClean.run();
Cache.instance = new Cache();
DominionInterface.instance = new DominionInterface();
new Cache();
new DominionInterface();

if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new PlaceHolderApi(this);
Expand Down Expand Up @@ -81,6 +80,7 @@ public void onEnable() {

if (config.getDynmap()) new DynmapConnect(); // 注册 Dynmap API
Scheduler.runTaskLaterAsync(MapRender::render, 40 * 20);
AutoClean.run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class DominionInterface implements DominionAPI {

public static DominionInterface instance;

public DominionInterface() {
instance = this;
}

@Override
public DominionDTO getPlayerCurrentDominion(@NotNull Player player) {
return Cache.instance.getPlayerCurrentDominion(player);
Expand Down

0 comments on commit 4282f62

Please sign in to comment.