Skip to content

Commit c78ddf2

Browse files
authored
v1.2.7
test
1 parent 1879e75 commit c78ddf2

20 files changed

Lines changed: 1752 additions & 2814 deletions

src/main/java/com/guild/GuildPlugin.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
import com.guild.services.GuildService;
1616
import com.guild.core.utils.ServerUtils;
1717
import com.guild.core.utils.TestUtils;
18+
import com.guild.utils.FoliaUtils;
19+
import com.guild.utils.TeleportManager;
20+
import org.bukkit.Location;
21+
import org.bukkit.entity.Player;
1822
import org.bukkit.plugin.java.JavaPlugin;
1923

2024
import java.util.logging.Logger;
@@ -31,6 +35,7 @@ public class GuildPlugin extends JavaPlugin {
3135
private PermissionManager permissionManager;
3236
private EconomyManager economyManager;
3337
private GuildService guildService;
38+
private boolean folia = false; // Folia 标识(在 onEnable 中初始化)
3439

3540
@Override
3641
public void onEnable() {
@@ -100,6 +105,12 @@ public void onEnable() {
100105
// 启动服务
101106
startServices();
102107

108+
// 检测是否为 Folia,并记录日志
109+
this.folia = FoliaUtils.isFolia();
110+
if (this.folia) {
111+
getLogger().warning("检测到 Folia 服务端,插件内的传送功能将被禁用以避免触发看门狗。");
112+
}
113+
103114
logger.info("工会插件启动成功!");
104115
logger.info("兼容模式: " + (ServerUtils.isFolia() ? "Folia" : "Spigot"));
105116

@@ -199,4 +210,17 @@ public EconomyManager getEconomyManager() {
199210
public GuildService getGuildService() {
200211
return guildService;
201212
}
213+
214+
public boolean isFolia() {
215+
return folia;
216+
}
217+
218+
/**
219+
* 插件内部使用的安全传送封装:
220+
* - 如果是 Folia,则阻止并返回 false(已经通知玩家)
221+
* - 否则在主线程执行传送并返回 true
222+
*/
223+
public boolean safeTeleport(Player player, Location target) {
224+
return TeleportManager.teleport(this, player, target);
225+
}
202226
}

src/main/java/com/guild/commands/GuildAdminCommand.java

Lines changed: 104 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package com.guild.commands;
22

3-
import com.guild.GuildPlugin;
4-
import com.guild.core.utils.ColorUtils;
5-
import com.guild.gui.AdminGuildGUI;
6-
import com.guild.gui.RelationManagementGUI;
7-
import com.guild.models.Guild;
8-
import com.guild.models.GuildRelation;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.UUID;
7+
import java.util.concurrent.CompletableFuture;
8+
99
import org.bukkit.Bukkit;
1010
import org.bukkit.command.Command;
1111
import org.bukkit.command.CommandExecutor;
1212
import org.bukkit.command.CommandSender;
1313
import org.bukkit.command.TabCompleter;
1414
import org.bukkit.entity.Player;
1515

16-
import java.util.ArrayList;
17-
import java.util.Arrays;
18-
import java.util.List;
19-
import java.util.UUID;
20-
import java.util.concurrent.CompletableFuture;
16+
import com.guild.GuildPlugin;
17+
import com.guild.core.utils.ColorUtils;
18+
import com.guild.gui.AdminGuildGUI;
19+
import com.guild.gui.ConfirmDeleteGUI;
20+
import com.guild.gui.GuildListManagementGUI;
21+
import com.guild.gui.RelationManagementGUI;
22+
import com.guild.models.Guild;
23+
import com.guild.models.GuildRelation;
24+
25+
import net.md_5.bungee.api.chat.ClickEvent;
26+
import net.md_5.bungee.api.chat.ComponentBuilder;
27+
import net.md_5.bungee.api.chat.HoverEvent;
28+
import net.md_5.bungee.api.chat.TextComponent;
2129

2230
/**
2331
* 工会管理员命令
@@ -56,7 +64,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
5664
handleInfo(sender, args);
5765
break;
5866
case "delete":
59-
handleDelete(sender, args);
67+
handleDeleteCommand(sender, args);
6068
break;
6169
case "freeze":
6270
handleFreeze(sender, args);
@@ -212,27 +220,87 @@ private void handleInfo(CommandSender sender, String[] args) {
212220
});
213221
}
214222

215-
private void handleDelete(CommandSender sender, String[] args) {
223+
private void handleDeleteCommand(CommandSender sender, String[] args) {
216224
if (args.length < 2) {
217-
sender.sendMessage(ColorUtils.colorize("&c用法: /guildadmin delete <工会名称>"));
225+
sender.sendMessage(ColorUtils.colorize("&c用法: /guildadmin delete <工会名称或ID> [confirm]"));
218226
return;
219227
}
220-
221-
String guildName = args[1];
222-
plugin.getGuildService().getGuildByNameAsync(guildName).thenAccept(guild -> {
223-
if (guild == null) {
224-
sender.sendMessage(ColorUtils.colorize("&c工会 " + guildName + " 不存在!"));
228+
229+
final String target = args[1];
230+
231+
plugin.getGuildService().getAllGuildsAsync().thenAccept(guilds -> {
232+
Guild found = null;
233+
UUID targetId = null;
234+
try {
235+
targetId = UUID.fromString(target);
236+
} catch (Exception ignored) {}
237+
238+
if (targetId != null) {
239+
for (Guild g : guilds) {
240+
if (String.valueOf(g.getId()).equals(String.valueOf(targetId))) {
241+
found = g;
242+
break;
243+
}
244+
}
245+
} else {
246+
for (Guild g : guilds) {
247+
if (g.getName().equalsIgnoreCase(target)) {
248+
found = g;
249+
break;
250+
}
251+
}
252+
}
253+
254+
if (found == null) {
255+
plugin.getServer().getScheduler().runTask(plugin, () ->
256+
sender.sendMessage(ColorUtils.colorize("&c未找到工会: " + target))
257+
);
225258
return;
226259
}
227-
228-
// 强制删除工会
229-
plugin.getGuildService().deleteGuildAsync(guild.getId(), UUID.randomUUID()).thenAccept(success -> {
230-
if (success) {
231-
sender.sendMessage(ColorUtils.colorize("&a工会 " + guildName + " 已被强制删除!"));
232-
} else {
233-
sender.sendMessage(ColorUtils.colorize("&c删除工会失败!"));
260+
261+
if (sender instanceof Player) {
262+
Player player = (Player) sender;
263+
if (!plugin.getPermissionManager().hasPermission(player, "guild.admin")) {
264+
player.sendMessage(ColorUtils.colorize("&c你没有执行该命令的权限(guild.admin)"));
265+
return;
234266
}
235-
});
267+
// 统一通过 GUI 进行确认删除
268+
plugin.getGuiManager().openGUI(player, new ConfirmDeleteGUI(plugin, player, found, true));
269+
player.sendMessage(ColorUtils.colorize("&e已打开删除确认 GUI,请在 GUI 中确认删除。"));
270+
} else {
271+
// 控制台:必须带 confirm
272+
boolean confirm = args.length >= 3 && args[2].equalsIgnoreCase("confirm");
273+
if (!confirm) {
274+
sender.sendMessage(ColorUtils.colorize("&c控制台无法打开GUI,请使用: /guildadmin delete " + target + " confirm"));
275+
return;
276+
}
277+
ConfirmDeleteGUI.confirmDelete(plugin, sender, found, true);
278+
}
279+
280+
return;
281+
});
282+
}
283+
284+
private void suggestUseGuiForDeletion(CommandSender sender, Guild guild, String reason) {
285+
plugin.getServer().getScheduler().runTask(plugin, () -> {
286+
sender.sendMessage(ColorUtils.colorize("&c删除工会失败: &f" + guild.getName()));
287+
if (reason != null && !reason.isEmpty()) {
288+
sender.sendMessage(ColorUtils.colorize("&7原因: &f" + reason));
289+
}
290+
sender.sendMessage(ColorUtils.colorize("&e建议:在管理GUI中手动删除工会(先删除关系/成员后重试),或查看控制台日志以获取详细错误信息。"));
291+
292+
if (sender instanceof Player player) {
293+
TextComponent tc = new TextComponent(ColorUtils.colorize("&a点击此处打开工会管理GUI以尝试手动删除"));
294+
tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/guildadmin"));
295+
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ColorUtils.colorize("&7在 GUI 中可删除关系、移除成员,然后再次删除该工会")).create()));
296+
player.spigot().sendMessage(tc);
297+
298+
// 打开工会列表管理界面,便于直接操作
299+
plugin.getGuiManager().openGUI(player, new GuildListManagementGUI(plugin, player));
300+
} else {
301+
// 控制台提示
302+
plugin.getLogger().info("管理员可在玩家端使用 /guildadmin 打开管理GUI后手动删除工会: " + guild.getName());
303+
}
236304
});
237305
}
238306

@@ -465,7 +533,8 @@ private void handleCreateRelation(CommandSender sender, String[] args) {
465533
sender.sendMessage(ColorUtils.colorize("&c工会 " + guild2Name + " 不存在!"));
466534
return;
467535
}
468-
if (guild1.getId() == guild2.getId()) {
536+
// 兼容不同类型的 id(UUID / int 等),使用字符串比较
537+
if (String.valueOf(guild1.getId()).equals(String.valueOf(guild2.getId()))) {
469538
sender.sendMessage(ColorUtils.colorize("&c不能与自己建立关系!"));
470539
return;
471540
}
@@ -514,9 +583,12 @@ private void handleDeleteRelation(CommandSender sender, String[] args) {
514583
// 查找并删除关系
515584
plugin.getGuildService().getGuildRelationsAsync(guild1.getId()).thenAccept(relations -> {
516585
for (GuildRelation relation : relations) {
517-
if ((relation.getGuild1Id() == guild1.getId() && relation.getGuild2Id() == guild2.getId()) ||
518-
(relation.getGuild1Id() == guild2.getId() && relation.getGuild2Id() == guild1.getId())) {
519-
586+
// 使用字符串比较以兼容 UUID / int / Integer 等实现
587+
String r1 = String.valueOf(relation.getGuild1Id());
588+
String r2 = String.valueOf(relation.getGuild2Id());
589+
String id1 = String.valueOf(guild1.getId());
590+
String id2 = String.valueOf(guild2.getId());
591+
if ((r1.equals(id1) && r2.equals(id2)) || (r1.equals(id2) && r2.equals(id1))) {
520592
plugin.getGuildService().deleteGuildRelationAsync(relation.getId()).thenAccept(success -> {
521593
if (success) {
522594
sender.sendMessage(ColorUtils.colorize("&a已删除关系: " + guild1Name + " ↔ " + guild2Name));
@@ -657,7 +729,7 @@ private void handleTest(CommandSender sender, String[] args) {
657729
sender.sendMessage(ColorUtils.colorize("&c工会 " + guild2NameTest + " 不存在!"));
658730
return;
659731
}
660-
if (guild1.getId() == guild2.getId()) {
732+
if (String.valueOf(guild1.getId()).equals(String.valueOf(guild2.getId()))) {
661733
sender.sendMessage(ColorUtils.colorize("&c不能与自己建立关系!"));
662734
return;
663735
}

0 commit comments

Comments
 (0)