初步实现了权限组称号功能(还缺少前后缀)

This commit is contained in:
zhangyuheng 2024-08-11 01:59:34 +08:00
parent 99125fa924
commit 8da3c1e871
13 changed files with 284 additions and 38 deletions

View File

@ -4,7 +4,7 @@ plugins {
}
group = "cn.lunadeer"
version = "2.0.2-beta"
version = "2.1.0-beta"
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
@ -27,11 +27,13 @@ allprojects {
maven("https://jitpack.io")
maven("https://repo.mikeprimm.com/")
maven("https://ssl.lunadeer.cn:14454/repository/maven-snapshots/")
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
}
dependencies {
compileOnly("com.github.BlueMap-Minecraft:BlueMapAPI:v2.6.2")
compileOnly("us.dynmap:DynmapCoreAPI:3.4")
compileOnly("me.clip:placeholderapi:2.11.6")
implementation("cn.lunadeer:MinecraftPluginUtils:1.3.4-SNAPSHOT")
implementation("org.yaml:snakeyaml:2.0")

View File

@ -1,9 +1,6 @@
package cn.lunadeer.dominion;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.dtos.Flag;
import cn.lunadeer.dominion.dtos.GroupDTO;
import cn.lunadeer.dominion.dtos.MemberDTO;
import cn.lunadeer.dominion.dtos.*;
import cn.lunadeer.dominion.utils.MapRender;
import cn.lunadeer.dominion.utils.Particle;
import cn.lunadeer.dominion.utils.ResMigration;
@ -14,6 +11,7 @@ import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.LocalDateTime;
import java.util.*;
@ -184,9 +182,13 @@ public class Cache {
if (groupId == null) {
id_groups = new ConcurrentHashMap<>();
List<GroupDTO> groups = GroupDTO.selectAll();
List<PlayerDTO> players = PlayerDTO.all();
for (GroupDTO group : groups) {
id_groups.put(group.getId(), group);
}
for (PlayerDTO player : players) {
map_player_using_group_title_id.put(player.getUuid(), player.getUsingGroupTitleID());
}
} else {
GroupDTO group = GroupDTO.select(groupId);
if (group == null && id_groups.containsKey(groupId)) {
@ -363,6 +365,20 @@ public class Cache {
return player_uuid_to_member.get(player_uuid).get(dominion.getId());
}
public List<GroupDTO> getBelongGroupsOf(UUID plauer_uuid) {
List<GroupDTO> groups = new ArrayList<>();
if (!player_uuid_to_member.containsKey(plauer_uuid)) return groups;
for (MemberDTO member : player_uuid_to_member.get(plauer_uuid).values()) {
if (member.getGroupId() != -1) {
GroupDTO group = getGroup(member.getGroupId());
if (group != null) {
groups.add(group);
}
}
}
return groups;
}
public DominionDTO getDominion(Integer id) {
return id_dominions.get(id);
}
@ -435,6 +451,8 @@ public class Cache {
private Map<UUID, List<ResMigration.ResidenceNode>> residence_data = null;
private final Map<UUID, Integer> map_player_using_group_title_id = new HashMap<>();
private static class WorldDominionTreeSectored {
/*
D | C
@ -546,4 +564,15 @@ public class Cache {
}
}
}
public @Nullable GroupDTO getPlayerUsingGroupTitle(UUID uuid) {
if (map_player_using_group_title_id.containsKey(uuid)) {
return getGroup(map_player_using_group_title_id.get(uuid));
}
return null;
}
public void updatePlayerUsingGroupTitle(UUID uuid, Integer groupId) {
map_player_using_group_title_id.put(uuid, groupId);
}
}

View File

@ -4,10 +4,7 @@ import cn.lunadeer.dominion.commands.*;
import cn.lunadeer.dominion.controllers.PlayerController;
import cn.lunadeer.dominion.cuis.*;
import cn.lunadeer.dominion.dtos.PlayerDTO;
import cn.lunadeer.dominion.tuis.AllDominion;
import cn.lunadeer.dominion.tuis.Menu;
import cn.lunadeer.dominion.tuis.MigrateList;
import cn.lunadeer.dominion.tuis.SysConfig;
import cn.lunadeer.dominion.tuis.*;
import cn.lunadeer.dominion.tuis.dominion.DominionList;
import cn.lunadeer.dominion.tuis.dominion.DominionManage;
import cn.lunadeer.dominion.tuis.dominion.manage.EnvSetting;
@ -137,6 +134,12 @@ public class Commands implements TabExecutor {
case "env_setting":
EnvSetting.show(sender, args);
break;
case "use_title":
Title.use_title(sender, args);
break;
case "title_list":
TitleList.show(sender, args);
break;
// ---=== Sub Command ===---
case "member":
Member.handle(sender, args);

View File

@ -3,6 +3,7 @@ package cn.lunadeer.dominion;
import cn.lunadeer.dominion.events.RegisterEvents;
import cn.lunadeer.dominion.managers.ConfigManager;
import cn.lunadeer.dominion.managers.DatabaseTables;
import cn.lunadeer.dominion.managers.PlaceHolderApi;
import cn.lunadeer.dominion.utils.DynmapConnect;
import cn.lunadeer.dominion.utils.MapRender;
import cn.lunadeer.minecraftpluginutils.*;
@ -39,6 +40,10 @@ public final class Dominion extends JavaPlugin {
AutoClean.run();
Cache.instance = new Cache();
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new PlaceHolderApi(this);
}
new RegisterEvents(this);
Objects.requireNonNull(Bukkit.getPluginCommand("dominion")).setExecutor(new Commands());

View File

@ -0,0 +1,61 @@
package cn.lunadeer.dominion.commands;
import cn.lunadeer.dominion.Cache;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.dtos.GroupDTO;
import cn.lunadeer.dominion.dtos.MemberDTO;
import cn.lunadeer.dominion.dtos.PlayerDTO;
import cn.lunadeer.dominion.tuis.TitleList;
import cn.lunadeer.minecraftpluginutils.Notification;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Objects;
import static cn.lunadeer.dominion.commands.Apis.hasPermission;
import static cn.lunadeer.dominion.commands.Apis.playerOnly;
import static cn.lunadeer.dominion.tuis.Apis.getPage;
public class Title {
public static void use_title(CommandSender sender, String[] args) {
if (!hasPermission(sender, "dominion.default")) {
return;
}
Player bukkit_player = playerOnly(sender);
if (bukkit_player == null) return;
if (args.length < 2) {
Notification.error(sender, "用法: /dominion use_title <权限组ID>");
return;
}
try {
PlayerDTO player = PlayerDTO.get(bukkit_player);
GroupDTO group = Cache.instance.getGroup(Integer.parseInt(args[1]));
if (group == null) {
Notification.error(sender, "权限组不存在");
return;
}
DominionDTO dominion = Cache.instance.getDominion(group.getDomID());
if (dominion == null) {
Notification.error(sender, "权限组 %s 所属领地不存在", group.getName());
return;
}
MemberDTO member = Cache.instance.getMember(bukkit_player, dominion);
if (member == null) {
Notification.error(sender, "你不是 %s 的成员,无法使用其称号", dominion.getName());
return;
}
if (!Objects.equals(member.getGroupId(), group.getId())) {
Notification.error(sender, "你不属于权限组 %s无法使用其称号", group.getName());
return;
}
player.setUsingGroupTitleID(group.getId());
Notification.info(sender, "成功使用权限组 %s 称号", group.getName());
int page = getPage(args, 2);
TitleList.show(sender, page);
} catch (Exception e) {
Notification.error(sender, "使用称号失败: " + e.getMessage());
}
}
}

View File

@ -1,11 +1,13 @@
package cn.lunadeer.dominion.dtos;
import cn.lunadeer.dominion.Cache;
import cn.lunadeer.minecraftpluginutils.ColorParser;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
import cn.lunadeer.minecraftpluginutils.databse.Field;
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
import cn.lunadeer.minecraftpluginutils.databse.syntax.InsertRow;
import cn.lunadeer.minecraftpluginutils.databse.syntax.UpdateRow;
import net.kyori.adventure.text.Component;
import java.sql.ResultSet;
import java.util.ArrayList;
@ -19,6 +21,8 @@ public class GroupDTO {
Field domID = new Field("dom_id", FieldType.INT);
Field name = new Field("name", FieldType.STRING);
Field admin = new Field("admin", FieldType.BOOLEAN);
Field name_colored = new Field("name_colored", FieldType.STRING);
private final Map<Flag, Boolean> flags = new HashMap<>();
public Integer getId() {
@ -33,6 +37,14 @@ public class GroupDTO {
return (String) name.value;
}
public Component getNameColoredComponent() {
return ColorParser.getComponentType((String) name_colored.value);
}
public String getNameColoredBukkit() {
return ColorParser.getBukkitType((String) name_colored.value);
}
public Boolean getAdmin() {
return (Boolean) admin.value;
}
@ -43,8 +55,9 @@ public class GroupDTO {
}
public GroupDTO setName(String name) {
this.name.value = name;
UpdateRow updateRow = new UpdateRow().field(this.name);
this.name_colored.value = name;
this.name.value = ColorParser.getPlainText(name);
UpdateRow updateRow = new UpdateRow().field(this.name).field(this.name_colored);
return doUpdate(updateRow);
}
@ -67,15 +80,16 @@ public class GroupDTO {
insertRow.table("dominion_group")
.field(group.domID)
.field(group.name)
.field(group.admin);
.field(group.admin)
.field(group.name_colored);
for (Flag f : Flag.getPrivilegeFlagsEnabled()) {
insertRow.field(new Field(f.getFlagName(), dominionDTO.getFlagValue(f)));
}
try (ResultSet rs = insertRow.execute()) {
List<GroupDTO> groups = getDTOFromRS(rs);
if (groups.size() == 0) return null;
Cache.instance.loadGroups(groups.get(0).getId());
return groups.get(0);
if (groups.isEmpty()) return null;
Cache.instance.loadGroups(groups.getFirst().getId());
return groups.getFirst();
} catch (Exception e) {
DatabaseManager.handleDatabaseError("创建权限组失败: ", e, "");
return null;
@ -99,15 +113,15 @@ public class GroupDTO {
public static GroupDTO select(Integer id) {
String sql = "SELECT * FROM dominion_group WHERE id = ?;";
List<GroupDTO> groups = getDTOFromRS(DatabaseManager.instance.query(sql, id));
if (groups.size() == 0) return null;
return groups.get(0);
if (groups.isEmpty()) return null;
return groups.getFirst();
}
public static GroupDTO select(Integer domID, String name) {
String sql = "SELECT * FROM dominion_group WHERE dom_id = ? AND name = ?;";
List<GroupDTO> groups = getDTOFromRS(DatabaseManager.instance.query(sql, domID, name));
if (groups.size() == 0) return null;
return groups.get(0);
if (groups.isEmpty()) return null;
return groups.getFirst();
}
public static List<GroupDTO> selectAll() {
@ -122,19 +136,21 @@ public class GroupDTO {
private GroupDTO(String name, Integer domID) {
this.domID.value = domID;
this.name.value = name;
this.name.value = ColorParser.getPlainText(name);
this.name_colored.value = name;
this.admin.value = false;
for (Flag f : Flag.getPrivilegeFlagsEnabled()) {
flags.put(f, f.getDefaultValue());
}
}
private GroupDTO(Integer id, Integer domID, String name, Boolean admin, Map<Flag, Boolean> flags) {
private GroupDTO(Integer id, Integer domID, String name, Boolean admin, Map<Flag, Boolean> flags, String nameColored) {
this.id.value = id;
this.domID.value = domID;
this.name.value = name;
this.admin.value = admin;
this.flags.putAll(flags);
this.name_colored.value = nameColored;
}
private static List<GroupDTO> getDTOFromRS(ResultSet rs) {
@ -151,7 +167,8 @@ public class GroupDTO {
rs.getInt("dom_id"),
rs.getString("name"),
rs.getBoolean("admin"),
flags
flags,
rs.getString("name_colored")
);
list.add(group);
}
@ -167,9 +184,9 @@ public class GroupDTO {
.where("id = ?", id.value);
try (ResultSet rs = updateRow.execute()) {
List<GroupDTO> groups = getDTOFromRS(rs);
if (groups.size() == 0) return null;
if (groups.isEmpty()) return null;
Cache.instance.loadGroups((Integer) id.value);
return groups.get(0);
return groups.getFirst();
} catch (Exception e) {
DatabaseManager.handleDatabaseError("更新权限组失败: ", e, "");
return null;

View File

@ -1,5 +1,6 @@
package cn.lunadeer.dominion.dtos;
import cn.lunadeer.dominion.Cache;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
import cn.lunadeer.minecraftpluginutils.databse.Field;
import cn.lunadeer.minecraftpluginutils.databse.syntax.InsertRow;
@ -65,7 +66,8 @@ public class PlayerDTO {
UUID uuid = UUID.fromString(rs.getString("uuid"));
String lastKnownName = rs.getString("last_known_name");
Long lastJoinAt = rs.getTimestamp("last_join_at").getTime();
PlayerDTO player = new PlayerDTO(id, uuid, lastKnownName, lastJoinAt);
Integer usingGroupTitleID = rs.getInt("using_group_title_id");
PlayerDTO player = new PlayerDTO(id, uuid, lastKnownName, lastJoinAt, usingGroupTitleID);
players.add(player);
}
return players;
@ -74,15 +76,15 @@ public class PlayerDTO {
public static PlayerDTO select(UUID uuid) {
String sql = "SELECT * FROM player_name WHERE uuid = ?;";
List<PlayerDTO> players = query(sql, uuid.toString());
if (players.size() == 0) return null;
return players.get(0);
if (players.isEmpty()) return null;
return players.getFirst();
}
public static PlayerDTO select(String name) {
String sql = "SELECT * FROM player_name WHERE last_known_name = ?;";
List<PlayerDTO> players = query(sql, name);
if (players.size() == 0) return null;
return players.get(0);
if (players.isEmpty()) return null;
return players.getFirst();
}
public static List<PlayerDTO> search(String name) {
@ -100,17 +102,19 @@ public class PlayerDTO {
Field uuid = new Field("uuid", player.getUuid().toString());
Field lastKnownName = new Field("last_known_name", player.getLastKnownName());
Field lastJoinAt = new Field("last_join_at", Timestamp.valueOf(LocalDateTime.now()));
Field usingGroupTitleID = new Field("using_group_title_id", player.getUsingGroupTitleID());
InsertRow insertRow = new InsertRow()
.table("player_name")
.field(uuid)
.field(lastKnownName)
.field(lastJoinAt)
.field(usingGroupTitleID)
.returningAll()
.onConflictOverwrite(new Field("id", null));
try (ResultSet rs = insertRow.execute()) {
List<PlayerDTO> players = getDTOFromRS(rs);
if (players.size() == 0) return null;
return players.get(0);
if (players.isEmpty()) return null;
return players.getFirst();
} catch (SQLException e) {
DatabaseManager.handleDatabaseError("插入玩家信息失败: ", e, insertRow.toString());
return null;
@ -121,31 +125,34 @@ public class PlayerDTO {
Field lastKnownName = new Field("last_known_name", player.getLastKnownName());
Field uuid = new Field("uuid", player.getUuid().toString());
Field lastJoinAt = new Field("last_join_at", Timestamp.valueOf(LocalDateTime.now()));
Field usingGroupTitleID = new Field("using_group_title_id", player.getUsingGroupTitleID());
UpdateRow updateRow = new UpdateRow()
.table("player_name")
.field(lastKnownName)
.field(lastJoinAt)
.field(usingGroupTitleID)
.where("uuid = ?", uuid.value)
.returningAll(uuid);
try (ResultSet rs = updateRow.execute()) {
List<PlayerDTO> players = getDTOFromRS(rs);
if (players.size() == 0) return null;
return players.get(0);
if (players.isEmpty()) return null;
return players.getFirst();
} catch (SQLException e) {
DatabaseManager.handleDatabaseError("更新玩家信息失败: ", e, updateRow.toString());
return null;
}
}
private PlayerDTO(Integer id, UUID uuid, String lastKnownName, Long lastJoinAt) {
private PlayerDTO(Integer id, UUID uuid, String lastKnownName, Long lastJoinAt, Integer using_group_title_id) {
this.id = id;
this.uuid = uuid;
this.lastKnownName = lastKnownName;
this.lastJoinAt = lastJoinAt;
this.using_group_title_id = using_group_title_id;
}
private PlayerDTO(UUID uuid, String lastKnownName, Long lastJoinAt) {
this(null, uuid, lastKnownName, lastJoinAt);
this(null, uuid, lastKnownName, lastJoinAt, -1);
}
public Integer getId() {
@ -180,8 +187,19 @@ public class PlayerDTO {
this.lastJoinAt = lastJoinAt;
}
public Integer getUsingGroupTitleID() {
return using_group_title_id;
}
public void setUsingGroupTitleID(Integer usingGroupTitleID) {
this.using_group_title_id = usingGroupTitleID;
update(this);
Cache.instance.updatePlayerUsingGroupTitle(uuid, usingGroupTitleID);
}
private Integer id;
private UUID uuid;
private String lastKnownName;
private Long lastJoinAt;
private Integer using_group_title_id;
}

View File

@ -231,5 +231,16 @@ public class DatabaseTables {
DatabaseManager.handleDatabaseError("迁移 player_privilege 到 dominion_member 失败", e, sql);
}
}
// 2.1.0-beta add group name colored
if (!Common.IsFieldExist("dominion_group", "name_colored")) {
TableColumn dominion_group_name_colored = new TableColumn("name_colored", FieldType.STRING, false, false, true, false, "'未命名'");
new AddColumn(dominion_group_name_colored).table("dominion_group").ifNotExists().execute();
String copy_sql = "UPDATE dominion_group SET name_colored = name;";
DatabaseManager.instance.query(copy_sql);
TableColumn player_name_using_group_title_id = new TableColumn("using_group_title_id", FieldType.INT, false, false, true, false, -1);
new AddColumn(player_name_using_group_title_id).table("player_name").ifNotExists().execute();
}
}
}

View File

@ -0,0 +1,52 @@
package cn.lunadeer.dominion.managers;
import cn.lunadeer.dominion.Cache;
import cn.lunadeer.dominion.dtos.GroupDTO;
import cn.lunadeer.minecraftpluginutils.XLogger;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public class PlaceHolderApi extends PlaceholderExpansion {
private final JavaPlugin plugin;
public static PlaceHolderApi instance;
public PlaceHolderApi(JavaPlugin plugin) {
this.plugin = plugin;
this.register();
instance = this;
XLogger.info("成功注册 PlaceholderAPI 扩展");
}
@Override
public String onPlaceholderRequest(Player bukkitPlayer, @NotNull String params) {
if (params.equalsIgnoreCase("group_title")) {
GroupDTO group = Cache.instance.getPlayerUsingGroupTitle(bukkitPlayer.getUniqueId());
if (group == null) {
return "";
}
return group.getNameColoredBukkit();
}
return null; //
}
@Override
public @NotNull String getIdentifier() {
return "dominion";
}
@Override
public @NotNull String getAuthor() {
return "zhangyuheng";
}
@Override
public @NotNull String getVersion() {
return plugin.getPluginMeta().getVersion();
}
}

View File

@ -0,0 +1,48 @@
package cn.lunadeer.dominion.tuis;
import cn.lunadeer.dominion.Cache;
import cn.lunadeer.dominion.dtos.GroupDTO;
import cn.lunadeer.minecraftpluginutils.stui.ListView;
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
import static cn.lunadeer.dominion.commands.Apis.playerOnly;
import static cn.lunadeer.dominion.tuis.Apis.getPage;
public class TitleList {
public static void show(CommandSender sender, int page) {
show(sender, new String[]{String.valueOf(page)});
}
public static void show(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
int page = getPage(args, 1);
ListView view = ListView.create(10, "/dominion title_list");
view.title("我可使用的权限组称号");
view.navigator(Line.create().append(Button.create("主菜单").setExecuteCommand("/dominion menu").build()).append("称号"));
List<GroupDTO> groups = Cache.instance.getBelongGroupsOf(player.getUniqueId());
GroupDTO using = Cache.instance.getPlayerUsingGroupTitle(player.getUniqueId());
for (GroupDTO group : groups) {
Line line = Line.create();
if (using != null && using.getId().equals(group.getId())) {
line.append(Button.createRed("卸下").setExecuteCommand("/dominion use_title -1").build());
} else {
line.append(Button.createGreen("使用").setExecuteCommand("/dominion use_title " + group.getId()).build());
}
line.append(group.getNameColoredComponent());
view.add(line);
}
view.showOn(player, page);
}
}

View File

@ -70,7 +70,7 @@ public class GroupList {
Button add = Button.createGreen("+")
.setHoverText("添加成员到权限组 " + group.getName())
.setExecuteCommand("/dominion group select_member " + dominion.getName() + " " + group.getName() + " " + page);
line.append(del.build()).append(edit.build()).append(group.getName()).append(add.build());
line.append(del.build()).append(edit.build()).append(group.getNameColoredComponent()).append(add.build());
view.add(line);
List<MemberDTO> players = MemberDTO.selectByGroupId(group.getId());
XLogger.debug("players: " + players.size());

View File

@ -45,7 +45,7 @@ public class GroupSetting {
}
ListView view = ListView.create(10, "/dominion group setting " + dominion.getName() + " " + group.getName());
view.title("权限组 " + group.getName() + " 管理");
view.title("权限组 " + group.getNameColoredBukkit() + " 管理");
view.navigator(
Line.create()
.append(Button.create("主菜单").setExecuteCommand("/dominion menu").build())

View File

@ -2,11 +2,11 @@ name: Dominion
version: @version@
main: cn.lunadeer.dominion.Dominion
api-version: '1.20'
load: STARTUP
folia-supported: true
softdepend:
- Vault
- dynmap
- PlaceholderAPI
commands:
Dominion:
description: 领地插件命令