|
1 | 1 | package com.guild.commands; |
2 | 2 |
|
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 | + |
9 | 9 | import org.bukkit.Bukkit; |
10 | 10 | import org.bukkit.command.Command; |
11 | 11 | import org.bukkit.command.CommandExecutor; |
12 | 12 | import org.bukkit.command.CommandSender; |
13 | 13 | import org.bukkit.command.TabCompleter; |
14 | 14 | import org.bukkit.entity.Player; |
15 | 15 |
|
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; |
21 | 29 |
|
22 | 30 | /** |
23 | 31 | * 工会管理员命令 |
@@ -56,7 +64,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St |
56 | 64 | handleInfo(sender, args); |
57 | 65 | break; |
58 | 66 | case "delete": |
59 | | - handleDelete(sender, args); |
| 67 | + handleDeleteCommand(sender, args); |
60 | 68 | break; |
61 | 69 | case "freeze": |
62 | 70 | handleFreeze(sender, args); |
@@ -212,27 +220,87 @@ private void handleInfo(CommandSender sender, String[] args) { |
212 | 220 | }); |
213 | 221 | } |
214 | 222 |
|
215 | | - private void handleDelete(CommandSender sender, String[] args) { |
| 223 | + private void handleDeleteCommand(CommandSender sender, String[] args) { |
216 | 224 | if (args.length < 2) { |
217 | | - sender.sendMessage(ColorUtils.colorize("&c用法: /guildadmin delete <工会名称>")); |
| 225 | + sender.sendMessage(ColorUtils.colorize("&c用法: /guildadmin delete <工会名称或ID> [confirm]")); |
218 | 226 | return; |
219 | 227 | } |
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 | + ); |
225 | 258 | return; |
226 | 259 | } |
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; |
234 | 266 | } |
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 | + } |
236 | 304 | }); |
237 | 305 | } |
238 | 306 |
|
@@ -465,7 +533,8 @@ private void handleCreateRelation(CommandSender sender, String[] args) { |
465 | 533 | sender.sendMessage(ColorUtils.colorize("&c工会 " + guild2Name + " 不存在!")); |
466 | 534 | return; |
467 | 535 | } |
468 | | - if (guild1.getId() == guild2.getId()) { |
| 536 | + // 兼容不同类型的 id(UUID / int 等),使用字符串比较 |
| 537 | + if (String.valueOf(guild1.getId()).equals(String.valueOf(guild2.getId()))) { |
469 | 538 | sender.sendMessage(ColorUtils.colorize("&c不能与自己建立关系!")); |
470 | 539 | return; |
471 | 540 | } |
@@ -514,9 +583,12 @@ private void handleDeleteRelation(CommandSender sender, String[] args) { |
514 | 583 | // 查找并删除关系 |
515 | 584 | plugin.getGuildService().getGuildRelationsAsync(guild1.getId()).thenAccept(relations -> { |
516 | 585 | 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))) { |
520 | 592 | plugin.getGuildService().deleteGuildRelationAsync(relation.getId()).thenAccept(success -> { |
521 | 593 | if (success) { |
522 | 594 | sender.sendMessage(ColorUtils.colorize("&a已删除关系: " + guild1Name + " ↔ " + guild2Name)); |
@@ -657,7 +729,7 @@ private void handleTest(CommandSender sender, String[] args) { |
657 | 729 | sender.sendMessage(ColorUtils.colorize("&c工会 " + guild2NameTest + " 不存在!")); |
658 | 730 | return; |
659 | 731 | } |
660 | | - if (guild1.getId() == guild2.getId()) { |
| 732 | + if (String.valueOf(guild1.getId()).equals(String.valueOf(guild2.getId()))) { |
661 | 733 | sender.sendMessage(ColorUtils.colorize("&c不能与自己建立关系!")); |
662 | 734 | return; |
663 | 735 | } |
|
0 commit comments