|
15 | 15 | import pro.cloudnode.smp.smpcore.SMPCore; |
16 | 16 |
|
17 | 17 | import java.time.Instant; |
| 18 | +import java.util.ArrayList; |
18 | 19 | import java.util.Arrays; |
| 20 | +import java.util.Collections; |
19 | 21 | import java.util.Date; |
20 | 22 | import java.util.List; |
| 23 | +import java.util.Objects; |
21 | 24 | import java.util.Optional; |
| 25 | +import java.util.stream.Stream; |
22 | 26 |
|
23 | 27 | public final class NationCommand extends Command { |
24 | 28 |
|
@@ -65,9 +69,145 @@ public boolean run( |
65 | 69 | public @Nullable List<@NotNull String> tab( |
66 | 70 | final @NotNull CommandSender sender, |
67 | 71 | final @NotNull String label, |
68 | | - final @NotNull String @NotNull [] args |
| 72 | + @NotNull String @NotNull [] args |
| 73 | + ) { |
| 74 | + return tabComplete(sender, label, args); |
| 75 | + } |
| 76 | + |
| 77 | + public static @Nullable List<@NotNull String> tabComplete( |
| 78 | + final @NotNull CommandSender sender, |
| 79 | + final @NotNull String label, |
| 80 | + @NotNull String @NotNull [] args |
69 | 81 | ) { |
70 | | - return List.of(); |
| 82 | + final var list = new ArrayList<@NotNull String>(); |
| 83 | + |
| 84 | + if (!sender.hasPermission(Permission.NATION)) |
| 85 | + return list; |
| 86 | + |
| 87 | + final @NotNull Optional<@NotNull Member> member = sender instanceof final @NotNull Player player ? Member.get( |
| 88 | + player) : Optional.empty(); |
| 89 | + |
| 90 | + final @NotNull Optional<@NotNull Nation> nation; |
| 91 | + if (args.length > 0 && args[0].startsWith("id:")) { |
| 92 | + if (args.length == 1) { |
| 93 | + list.addAll(Nation.get().stream().map(n -> "id:" + n.id).sorted().toList()); |
| 94 | + return list; |
| 95 | + } |
| 96 | + final @NotNull String id = args[0].substring(3); |
| 97 | + nation = Nation.get(id); |
| 98 | + args = Arrays.copyOfRange(args, 1, args.length); |
| 99 | + if (nation.isEmpty()) |
| 100 | + return list; |
| 101 | + } |
| 102 | + else |
| 103 | + nation = member.flatMap(Member::nation); |
| 104 | + |
| 105 | + final boolean other = member.isEmpty() || nation.isEmpty() || !nation.get().id.equals(member.get().nationID); |
| 106 | + |
| 107 | + if (args.length == 1) { |
| 108 | + if (nation.isPresent()) { |
| 109 | + if ( |
| 110 | + (!other && hasAnyPermission(sender, Permission.NATION_CITIZENS_LIST, Permission.NATION_CITIZENS_KICK)) |
| 111 | + || hasAnyPermission(sender, Permission.NATION_CITIZENS_LIST_OTHER, Permission.NATION_CITIZENS_KICK_OTHER) |
| 112 | + ) |
| 113 | + Collections.addAll(list, "citizens", "members", "subjects", "nationals", "people", "residents", "population", "cits", "pop"); |
| 114 | + if (sender.hasPermission(Permission.NATION_LEAVE) && member.isPresent() && !other && !nation.get().leaderUUID.equals(member.get().uuid)) |
| 115 | + Collections.addAll(list, "leave", "abandon", "renounce"); |
| 116 | + } |
| 117 | + |
| 118 | + else if (hasAnyPermission(sender, Permission.NATION_JOIN_REQUEST) && ( |
| 119 | + member.isEmpty() || member.get().nationID == null || sender.hasPermission(Permission.NATION_JOIN_REQUEST_SWITCH) |
| 120 | + )) |
| 121 | + Collections.addAll(list, "join", "request", "req", "cancel", "reject", "decline", "withdraw", "refuse", "deny"); |
| 122 | + } |
| 123 | + else switch (args[0]) { |
| 124 | + case "citizens", "members", "subjects", "nationals", "people", "residents", "population", "cits", "pop" -> { |
| 125 | + switch (args.length) { |
| 126 | + case 2 -> { |
| 127 | + if ((!other && sender.hasPermission(Permission.NATION_CITIZENS_LIST)) |
| 128 | + || sender.hasPermission(Permission.NATION_CITIZENS_LIST_OTHER)) |
| 129 | + Collections.addAll(list, "list", "show", "get"); |
| 130 | + |
| 131 | + if ((!other && sender.hasPermission(Permission.NATION_CITIZENS_KICK)) |
| 132 | + || sender.hasPermission(Permission.NATION_CITIZENS_KICK_OTHER)) |
| 133 | + Collections.addAll(list, "kick", "remove", "delete", "rm", "del"); |
| 134 | + |
| 135 | + if ((!other && sender.hasPermission(Permission.NATION_INVITE)) |
| 136 | + || sender.hasPermission(Permission.NATION_INVITE_OTHER)) |
| 137 | + Collections.addAll(list, "invite", "request", "req", "cancel", "reject", "decline", "withdraw", "refuse", "deny"); |
| 138 | + |
| 139 | + if ((!other && sender.hasPermission(Permission.NATION_CITIZEN_ADD)) |
| 140 | + || sender.hasPermission(Permission.NATION_CITIZEN_ADD_OTHER)) |
| 141 | + Collections.addAll(list, "add"); |
| 142 | + } |
| 143 | + case 3 -> { |
| 144 | + switch (args[1]) { |
| 145 | + case "kick", "remove", "delete", "rm", "del" -> { |
| 146 | + if (other && !sender.hasPermission(Permission.NATION_CITIZENS_KICK_OTHER)) |
| 147 | + break; |
| 148 | + if (!sender.hasPermission(Permission.NATION_CITIZENS_KICK)) |
| 149 | + break; |
| 150 | + list.addAll((List<@NotNull String>) nation.get().citizens().stream() |
| 151 | + .filter(c -> !c.uuid.equals(nation.get().leaderUUID)) |
| 152 | + .map(c -> c.player().getName()) |
| 153 | + .filter(Objects::nonNull).toList()); |
| 154 | + } |
| 155 | + case "invite", "request", "req" -> { |
| 156 | + if (other && !sender.hasPermission(Permission.NATION_INVITE_OTHER)) |
| 157 | + break; |
| 158 | + if (!sender.hasPermission(Permission.NATION_INVITE)) |
| 159 | + break; |
| 160 | + list.addAll((List<@NotNull String>) Member.get().stream() |
| 161 | + .filter(m -> !nation.get().id.equals(m.nationID)) |
| 162 | + .map(c -> c.player().getName()) |
| 163 | + .filter(Objects::nonNull).toList()); |
| 164 | + } |
| 165 | + case "cancel", "reject", "decline", "withdraw", "refuse", "deny" -> { |
| 166 | + if (other && !sender.hasPermission(Permission.NATION_INVITE_OTHER)) |
| 167 | + break; |
| 168 | + if (!sender.hasPermission(Permission.NATION_INVITE)) |
| 169 | + break; |
| 170 | + list.addAll((List<@NotNull String>) Stream.concat( |
| 171 | + CitizenRequest.get(nation.get(), true).stream(), |
| 172 | + CitizenRequest.get(nation.get(), false).stream() |
| 173 | + ).map(req -> req.member().player().getName()).filter(Objects::nonNull).sorted().toList()); |
| 174 | + } |
| 175 | + case "add" -> { |
| 176 | + if (other && !sender.hasPermission(Permission.NATION_CITIZEN_ADD_OTHER)) |
| 177 | + break; |
| 178 | + if (!sender.hasPermission(Permission.NATION_CITIZEN_ADD)) |
| 179 | + break; |
| 180 | + list.addAll((List<@NotNull String>) Member.get().stream() |
| 181 | + .filter(m -> !nation.get().id.equals(m.nationID)) |
| 182 | + .map(c -> c.player().getName()) |
| 183 | + .filter(Objects::nonNull).toList()); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + case "join", "request", "req" -> { |
| 190 | + if (args.length > 2 || member.isEmpty()) |
| 191 | + break; |
| 192 | + if (member.get().nationID != null && !sender.hasPermission(Permission.NATION_JOIN_REQUEST_SWITCH)) |
| 193 | + break; |
| 194 | + if (sender.hasPermission(Permission.NATION_JOIN_REQUEST)) |
| 195 | + list.addAll(Nation.get().stream().map(n -> n.id).filter(n -> !n.equals(member.get().nationID)).sorted().toList()); |
| 196 | + else if (sender.hasPermission(Permission.NATION_INVITE_ACCEPT)) |
| 197 | + list.addAll(CitizenRequest.get(member.get(), false).stream().map(req -> req.nationID).sorted().toList()); |
| 198 | + } |
| 199 | + case "cancel", "reject", "decline", "withdraw", "refuse", "deny" -> { |
| 200 | + if (args.length > 2 || member.isEmpty()) |
| 201 | + break; |
| 202 | + if (sender.hasPermission(Permission.NATION_INVITE_ACCEPT)) |
| 203 | + list.addAll(Stream.concat(CitizenRequest.get(member.get(), true).stream(), CitizenRequest.get(member.get(), false).stream()) |
| 204 | + .map(req -> req.nationID).sorted().toList()); |
| 205 | + if (sender.hasPermission(Permission.NATION_JOIN_REQUEST)) |
| 206 | + list.addAll(CitizenRequest.get(member.get(), true).stream().map(req -> req.nationID).sorted().toList()); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + return list; |
71 | 211 | } |
72 | 212 |
|
73 | 213 | public static boolean helpSubCommands( |
|
0 commit comments