解决了ConcurrentHashMap并发错误问题

This commit is contained in:
zhangyuheng 2024-02-24 11:51:43 +08:00
parent 3ac9d5a1c5
commit 8b8fbfac62
2 changed files with 15 additions and 11 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.lunadeer</groupId> <groupId>cn.lunadeer</groupId>
<artifactId>Dominion</artifactId> <artifactId>Dominion</artifactId>
<version>1.10.0-beta</version> <version>1.10.4-beta</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Dominion</name> <name>Dominion</name>

View File

@ -4,6 +4,7 @@ import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.dtos.PlayerPrivilegeDTO; import cn.lunadeer.dominion.dtos.PlayerPrivilegeDTO;
import cn.lunadeer.dominion.utils.Notification; import cn.lunadeer.dominion.utils.Notification;
import cn.lunadeer.dominion.utils.XLogger; import cn.lunadeer.dominion.utils.XLogger;
import net.kyori.adventure.text.Component;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,7 +15,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class Cache { public class Cache {
public Cache() { public Cache() {
player_current_dominion = new ConcurrentHashMap<>(); player_current_dominion_id = new HashMap<>();
loadDominions(); loadDominions();
loadPlayerPrivileges(); loadPlayerPrivileges();
} }
@ -75,20 +76,23 @@ public class Cache {
* @return 玩家当前所在领地 * @return 玩家当前所在领地
*/ */
public DominionDTO getPlayerCurrentDominion(Player player) { public DominionDTO getPlayerCurrentDominion(Player player) {
Integer dominion_id = player_current_dominion.get(player.getUniqueId()); Integer dominion_id = player_current_dominion_id.get(player.getUniqueId());
DominionDTO dominion = id_dominions.get(dominion_id); DominionDTO dominion = null;
if (dominion_id != null) {
dominion = id_dominions.get(dominion_id);
}
if (dominion != null) { if (dominion != null) {
if (!isInDominion(dominion, player)) { if (!isInDominion(dominion, player)) {
// glow // glow
player.setGlowing(false); player.setGlowing(false);
if (dominion.getParentDomId() == -1) { if (dominion.getParentDomId() == -1) {
Notification.info(player, "您已离开领地:" + dominion.getName()); Notification.info(player, "您已离开领地:" + dominion.getName());
Notification.info(player, dominion.getLeaveMessage()); player.sendMessage(Component.text(dominion.getLeaveMessage()));
player_current_dominion.put(player.getUniqueId(), null); player_current_dominion_id.put(player.getUniqueId(), null);
dominion = null; dominion = null;
} else { } else {
Notification.info(player, "您已离开子领地:" + dominion.getName()); Notification.info(player, "您已离开子领地:" + dominion.getName());
Notification.info(player, dominion.getLeaveMessage()); player.sendMessage(Component.text(dominion.getLeaveMessage()));
dominion = id_dominions.get(dominion.getParentDomId()); dominion = id_dominions.get(dominion.getParentDomId());
update_player_current_dominion(player, dominion); update_player_current_dominion(player, dominion);
} }
@ -100,7 +104,7 @@ public class Cache {
if (isInDominion(child, player)) { if (isInDominion(child, player)) {
dominion = child; dominion = child;
Notification.info(player, "您正在进入子领地:" + dominion.getName()); Notification.info(player, "您正在进入子领地:" + dominion.getName());
Notification.info(player, dominion.getJoinMessage()); player.sendMessage(Component.text(dominion.getJoinMessage()));
update_player_current_dominion(player, dominion); update_player_current_dominion(player, dominion);
break; break;
} }
@ -122,14 +126,14 @@ public class Cache {
in_dominions.sort(Comparator.comparingInt(DominionDTO::getId)); in_dominions.sort(Comparator.comparingInt(DominionDTO::getId));
dominion = in_dominions.get(0); dominion = in_dominions.get(0);
Notification.info(player, "您正在进入领地:" + dominion.getName()); Notification.info(player, "您正在进入领地:" + dominion.getName());
Notification.info(player, dominion.getJoinMessage()); player.sendMessage(Component.text(dominion.getJoinMessage()));
update_player_current_dominion(player, dominion); update_player_current_dominion(player, dominion);
} }
return dominion; return dominion;
} }
private void update_player_current_dominion(Player player, DominionDTO dominion) { private void update_player_current_dominion(Player player, DominionDTO dominion) {
player_current_dominion.put(player.getUniqueId(), dominion.getId()); player_current_dominion_id.put(player.getUniqueId(), dominion.getId());
// glow // glow
PlayerPrivilegeDTO privilege = getPlayerPrivilege(player, dominion); PlayerPrivilegeDTO privilege = getPlayerPrivilege(player, dominion);
if (privilege != null) { if (privilege != null) {
@ -201,6 +205,6 @@ public class Cache {
private ConcurrentHashMap<Integer, DominionDTO> id_dominions; private ConcurrentHashMap<Integer, DominionDTO> id_dominions;
private ConcurrentHashMap<String, List<Integer>> world_dominions; // 所有领地 private ConcurrentHashMap<String, List<Integer>> world_dominions; // 所有领地
private ConcurrentHashMap<UUID, ConcurrentHashMap<Integer, PlayerPrivilegeDTO>> player_uuid_to_privilege; // 玩家所有的特权 private ConcurrentHashMap<UUID, ConcurrentHashMap<Integer, PlayerPrivilegeDTO>> player_uuid_to_privilege; // 玩家所有的特权
private ConcurrentHashMap<UUID, Integer> player_current_dominion; // 玩家当前所在领地 private final Map<UUID, Integer> player_current_dominion_id; // 玩家当前所在领地
private ConcurrentHashMap<Integer, List<Integer>> dominion_children; private ConcurrentHashMap<Integer, List<Integer>> dominion_children;
} }