From 74e23fc460d57de24ec508f304f4d773e1547f33 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 16 Jun 2022 12:06:36 +0200 Subject: [PATCH] Update TeamCommand --- .../bungeecore/commands/TeamCommand.java | 419 +++++++----------- 1 file changed, 168 insertions(+), 251 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/TeamCommand.java b/src/de/steamwar/bungeecore/commands/TeamCommand.java index 9ae3799..9b7f3e9 100644 --- a/src/de/steamwar/bungeecore/commands/TeamCommand.java +++ b/src/de/steamwar/bungeecore/commands/TeamCommand.java @@ -24,6 +24,8 @@ import de.steamwar.bungeecore.Storage; import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.inventory.SWListInv; import de.steamwar.bungeecore.sql.*; +import de.steamwar.command.SWCommand; +import de.steamwar.command.TypeMapper; import de.steamwar.messages.ChatSender; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.ChatColor; @@ -41,15 +43,13 @@ import net.md_5.bungee.api.event.ServerConnectEvent; import java.net.*; import java.time.Instant; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; import static de.steamwar.bungeecore.Storage.teamInvitations; -public class TeamCommand extends BasicCommand { +public class TeamCommand extends SWCommand { private static final List> COLOR_CODES = new ArrayList<>(); @@ -77,10 +77,11 @@ public class TeamCommand extends BasicCommand { } public TeamCommand() { - super("team", ""); + super("team"); } - private void help(CommandSender sender){ + @Register(help = true) + public void help(CommandSender sender, String... args) { Message.send("TEAM_HELP_HEADER", sender); Message.send("TEAM_HELP_LIST", sender); Message.send("TEAM_HELP_INFO", sender); @@ -111,93 +112,30 @@ public class TeamCommand extends BasicCommand { } } - @Override - public void execute(CommandSender sender, String[] args){ - if(args.length < 1){ - help(sender); - return; - } - - if(!(sender instanceof ProxiedPlayer)) - return; - - ProxiedPlayer player = (ProxiedPlayer) sender; + @Register(value = "create", description = "TEAM_CREATE_USAGE") + public void create(ProxiedPlayer player, String kuerzel, String name) { SteamwarUser user = SteamwarUser.get(player.getUniqueId()); Team team = Team.get(user.getTeam()); - switch(args[0].toLowerCase()){ - case "create": - create(player, user, team, args); - break; - case "join": - join(player, user, args); - break; - case "stepback": - stepBack(player,user,team); - break; - case "leave": - leave(player, user, team); - break; - case "invite": - invite(player, user, team, args); - break; - case "remove": - remove(player, user, team, args); - break; - case "changekuerzel": - changekuerzel(player, user, team, args); - break; - case "changename": - changename(player, user, team, args); - break; - case "promote": - promote(player, user, team, args); - break; - case "changecolor": - changeColor(player, user, team); - break; - case "info": - info(player, user, team, args); - break; - case "list": - list(player, args); - break; - case "event": - event(player, user, team, args); - break; - case "tp": - tp(player, user, team, args); - break; - case "server": - server(player, user, team, args); - break; - default: - help(player); - } - } - - private void create(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ if(unwantedInTeam(player, user)) return; - if(args.length < 3){ - Message.send("TEAM_CREATE_USAGE", player); - return; - } - - if(checkTeamKuerzel(player, team, args[1])) + if(checkTeamKuerzel(player, team, kuerzel)) return; - if(checkTeamName(player, team, args[2])) + if(checkTeamName(player, team, name)) return; - Team.create(args[1], args[2]); - user.setTeam(Team.get(args[1]).getTeamId()); + Team.create(kuerzel, name); + user.setTeam(Team.get(kuerzel).getTeamId()); user.setLeader(true); - Message.send("TEAM_CREATE_CREATED", player, args[2]); + Message.send("TEAM_CREATE_CREATED", player, name); } - private void join(ProxiedPlayer player, SteamwarUser user, String[] args){ + @Register(value = "join", description = "TEAM_CREATE_USAGE") + public void join(ProxiedPlayer player, @OptionalValue("") String toJoin) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + if(unwantedInTeam(player, user)) return; @@ -215,7 +153,7 @@ public class TeamCommand extends BasicCommand { if(invs.size() == 1){ t = invs.get(0); }else{ - if(args.length != 2){ + if (toJoin.equals("")) { Message.send("TEAM_JOIN_USAGE", player); StringBuilder sb = new StringBuilder(); for(int inv : invs){ @@ -228,7 +166,7 @@ public class TeamCommand extends BasicCommand { for(int inv : invs){ Team team = Team.get(inv); - if(team.getTeamName().equals(args[1])){ + if(team.getTeamName().equals(toJoin)){ t = inv; break; } @@ -245,7 +183,11 @@ public class TeamCommand extends BasicCommand { Message.send("TEAM_JOIN_JOINED", player, Team.get(t).getTeamName()); } - private void stepBack(ProxiedPlayer player, SteamwarUser user, Team team) { + @Register("stepback") + public void stepback(ProxiedPlayer player) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notLeader(player, user, team)) return; @@ -258,7 +200,11 @@ public class TeamCommand extends BasicCommand { Message.send("TEAM_STEP_BACK", player); } - private void leave(ProxiedPlayer player, SteamwarUser user, Team team){ + @Register("leave") + public void leave(ProxiedPlayer player) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notInTeam(player, user)) return; @@ -277,20 +223,18 @@ public class TeamCommand extends BasicCommand { Message.send("TEAM_LEAVE_LEFT", player); } + @Register(value = "invite", description = "TEAM_INVITE_USAGE") + public void invite(ProxiedPlayer player, String toInvite) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); - private void invite(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ if(notLeader(player, user, team)) return; if(notDuringEvent(player)) return; - if(args.length != 2){ - Message.send("TEAM_INVITE_USAGE", player); - return; - } - - SteamwarUser target = SteamwarUser.get(args[1]); + SteamwarUser target = SteamwarUser.get(toInvite); if(target == null){ Message.send("TEAM_INVITE_NO_PLAYER", player); return; @@ -309,22 +253,21 @@ public class TeamCommand extends BasicCommand { teamInvitations.get(target.getId()).add(team.getTeamId()); - Message.send("TEAM_INVITE_INVITED", player, args[1]); + Message.send("TEAM_INVITE_INVITED", player, toInvite); ProxiedPlayer p = ProxyServer.getInstance().getPlayer(target.getUuid()); if(p != null) Message.send("TEAM_INVITE_INVITED_TARGET", p, team.getTeamColor(), team.getTeamName()); } - private void remove(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ + @Register(value = "remove", description = "TEAM_REMOVE_USAGE") + public void remove(ProxiedPlayer player, String toRemove) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notLeader(player, user, team)) return; - if(args.length != 2){ - Message.send("TEAM_REMOVE_USAGE", player); - return; - } - - SteamwarUser target = SteamwarUser.get(args[1]); + SteamwarUser target = SteamwarUser.get(toRemove); if(target == null){ Message.send("TEAM_REMOVE_NOT_PLAYER", player); return; @@ -359,59 +302,56 @@ public class TeamCommand extends BasicCommand { Message.send("TEAM_REMOVE_REMOVED_TARGET", player); } - private void changekuerzel(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ + @Register(value = "changekuerzel", description = "TEAM_KUERZEL_USAGE") + public void changeKuerzel(ProxiedPlayer player, String kuerzel) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notLeader(player, user, team)) return; if(notDuringEvent(player)) return; - if(args.length < 2){ - Message.send("TEAM_KUERZEL_USAGE", player); - return; - } - - if(checkTeamKuerzel(player, team, args[1])) + if(checkTeamKuerzel(player, team, kuerzel)) return; - team.setTeamKuerzel(args[1]); + team.setTeamKuerzel(kuerzel); Message.send("TEAM_KUERZEL_CHANGED", player); } - private void changename(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ + @Register(value = "changename", description = "TEAM_NAME_USAGE") + public void changeName(ProxiedPlayer player, String name) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notLeader(player, user, team)) return; if(notDuringEvent(player)) return; - if(args.length < 2){ - Message.send("TEAM_NAME_USAGE", player); - return; - } - - if(checkTeamName(player, team, args[1])) + if(checkTeamName(player, team, name)) return; - team.setTeamName(args[1]); + team.setTeamName(name); Message.send("TEAM_NAME_CHANGED", player); } - private void promote(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ + @Register(value = "promote", description = "TEAM_LEADER_USAGE") + public void promote(ProxiedPlayer player, String toPromote) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notLeader(player, user, team)) return; if(notDuringEvent(player)) return; - if(args.length < 2){ - Message.send("TEAM_LEADER_USAGE", player); - return; - } - - SteamwarUser target = SteamwarUser.get(args[1]); + SteamwarUser target = SteamwarUser.get(toPromote); if(target == null){ - Message.send("TEAM_LEADER_NOT_USER", player, args[1]); + Message.send("TEAM_LEADER_NOT_USER", player, toPromote); return; } @@ -421,24 +361,21 @@ public class TeamCommand extends BasicCommand { } target.setLeader(true); - Message.send("TEAM_LEADER_PROMOTED", player, args[1]); + Message.send("TEAM_LEADER_PROMOTED", player, toPromote); } - private String playerName(SteamwarUser user){ - return ProxyServer.getInstance().getPlayer(user.getUuid()) != null ? "§a" + user.getUserName() : "§e" + user.getUserName(); + @Register("info") + public void info(ProxiedPlayer player) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + info(player, team); } - private void info(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ - if(args.length == 1 && user.getTeam() == 0){ + @Register("info") + public void info(ProxiedPlayer player, Team team) { // UNKNOWN_TEAM + if (team.getTeamId() == 0) { Message.send("TEAM_INFO_USAGE", player); return; - }else if(user.getTeam() == 0 || args.length == 2){ - team = Team.get(args[1]); - } - - if(team == null){ - Message.send("UNKNOWN_TEAM", player); - return; } Message.sendPrefixless("TEAM_INFO_TEAM", player, team.getTeamName(), team.getTeamColor(), team.getTeamKuerzel()); @@ -459,31 +396,25 @@ public class TeamCommand extends BasicCommand { } } - private String getMemberList(List users, boolean leaders) { - StringBuilder sb = new StringBuilder(); - for(SteamwarUser user : users) { - if(user.isLeader() == leaders) { - sb.append(playerName(user)).append(" "); + @ClassMapper(value = Team.class, local = true) + public TypeMapper teamTypeMapper() { + return new TypeMapper() { + @Override + public Collection tabCompletes(CommandSender sender, String[] previousArguments, String s) { + return Team.getAll().stream().flatMap(team -> Stream.of(team.getTeamName(), team.getTeamKuerzel())).collect(Collectors.toList()); } - } - return sb.toString(); + + @Override + public Team map(CommandSender commandSender, String[] previousArguments, String s) { + return Team.get(s); + } + }; } - private void list(ProxiedPlayer player, String[] args){ - + @Register("list") + public void list(ProxiedPlayer player, @OptionalValue("1") int page) { // TEAM_LIST_NOT_PAGE final int TEAMS_PER_PAGE = 10; - int page; - if(args.length > 1){ - try{ - page = Integer.parseInt(args[1]); - }catch(NumberFormatException e){ - Message.send("TEAM_LIST_NOT_PAGE", player); - return; - } - }else - page = 1; - List all = Team.getAll(); final int lastPage = ((all.size() - 1) / 10) + 1; if(page < 1 || page > lastPage){ @@ -520,21 +451,30 @@ public class TeamCommand extends BasicCommand { player.sendMessage(beforePage); } - private void event(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ + @Register(value = "event") + public void event(ProxiedPlayer player) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notInTeam(player, user)) return; - if(args.length < 2){ - Message.send("TEAM_EVENT_USAGE", player); - Set events = TeamTeilnahme.getEvents(team.getTeamId()); - if(!events.isEmpty()){ - Message.send("TEAM_EVENT_HEADER", player); - DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(Message.parse("EVENT_DATE_FORMAT", player)); - for(Event e : events) - Message.sendPrefixless("TEAM_EVENT_EVENT", player, e.getStart().toLocalDateTime().format(dateFormat), e.getEventName()); - } - return; + Set events = TeamTeilnahme.getEvents(team.getTeamId()); + if(!events.isEmpty()){ + Message.send("TEAM_EVENT_HEADER", player); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(Message.parse("EVENT_DATE_FORMAT", player)); + for(Event e : events) + Message.sendPrefixless("TEAM_EVENT_EVENT", player, e.getStart().toLocalDateTime().format(dateFormat), e.getEventName()); } + } + + @Register(value = "event", description = "TEAM_EVENT_USAGE") + public void event(ProxiedPlayer player, String eventName) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + + if(notInTeam(player, user)) + return; if(notLeader(player, user, team)) return; @@ -542,7 +482,7 @@ public class TeamCommand extends BasicCommand { if(notDuringEvent(player)) return; - Event event = Event.get(args[1]); + Event event = Event.get(eventName); if(event == null){ Message.send("TEAM_EVENT_NO_EVENT", player); return; @@ -563,22 +503,18 @@ public class TeamCommand extends BasicCommand { } } - private void tp(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ - if(args.length == 1){ - if(notInTeam(player, user)) - return; - tp(player, team); + @Register("tp") + public void tp(ProxiedPlayer player) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + + if(notInTeam(player, user)) return; - } - Team targetTeam = Team.get(args[1]); - if(targetTeam == null){ - Message.send("TEAM_TP_NO_TEAM", player); - return; - } - tp(player, targetTeam); + tp(player, team); } - private void tp(ProxiedPlayer player, Team targetTeam) { + @Register("tp") + public void tp(ProxiedPlayer player, Team targetTeam) { // TEAM_TP_NO_TEAM if (targetTeam.getAddress() == null) { Message.send("TEAM_NO_ADDRESS", player); return; @@ -590,15 +526,15 @@ public class TeamCommand extends BasicCommand { return info; }); player.connect(ServerConnectRequest.builder() - .target(serverInfo) - .connectTimeout(BungeeCord.getInstance().getConfig().getServerConnectTimeout()) - .retry(false) - .reason(ServerConnectEvent.Reason.PLUGIN) - .callback((success, error) -> { - if (error != null) { - Message.send("TEAM_OFFLINE", player); - } - }) + .target(serverInfo) + .connectTimeout(BungeeCord.getInstance().getConfig().getServerConnectTimeout()) + .retry(false) + .reason(ServerConnectEvent.Reason.PLUGIN) + .callback((success, error) -> { + if (error != null) { + Message.send("TEAM_OFFLINE", player); + } + }) .build()); /* ((UserConnection) player).connect(serverInfo, (success, error) -> { @@ -609,17 +545,17 @@ public class TeamCommand extends BasicCommand { */ } - private void server(ProxiedPlayer player, SteamwarUser user, Team team, String[] args){ + @Register(value = "server", description = "TEAM_SERVER_USAGE") + public void server(ProxiedPlayer player, String server, @OptionalValue("25565") int port) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); + if(notLeader(player, user, team)) return; if (user.isPunishedWithMessage(ChatSender.of(player), Punishment.PunishmentType.NoTeamServer)) { return; } - if (args.length < 2) { - Message.send("TEAM_SERVER_USAGE", player); - return; - } - String server = args[1]; + try { if (isLocalhost(InetAddress.getByName(server))) { Message.send("TEAM_SERVER_ADDRESS_INVALID", player); @@ -629,39 +565,26 @@ public class TeamCommand extends BasicCommand { Message.send("TEAM_SERVER_ADDRESS_INVALID", player); return; } - int port = 25565; - if (args.length == 3) { - try { - port = Integer.parseInt(args[2]); - if (port < 1 || port > 65535) { - Message.send("TEAM_SERVER_PORT_INVALID", player); - return; - } - } catch (NumberFormatException e) { + try { + if (port < 1 || port > 65535) { Message.send("TEAM_SERVER_PORT_INVALID", player); return; } + } catch (NumberFormatException e) { + Message.send("TEAM_SERVER_PORT_INVALID", player); + return; } + team.setAddress(server); team.setPort(port); Storage.teamServers.remove(team.getTeamId()); Message.send("TEAM_SERVER_SET", player); } - public static boolean isLocalhost(InetAddress addr) { - // Check if the address is a valid special local or loop back - if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) - return true; + public void changeColor(ProxiedPlayer player) { + SteamwarUser user = SteamwarUser.get(player.getUniqueId()); + Team team = Team.get(user.getTeam()); - // Check if the address is defined on any interface - try { - return NetworkInterface.getByInetAddress(addr) != null; - } catch (SocketException e) { - return false; - } - } - - private void changeColor(ProxiedPlayer player, SteamwarUser user, Team team) { if(notLeader(player, user, team)) return; @@ -683,6 +606,33 @@ public class TeamCommand extends BasicCommand { inv.open(); } + private String playerName(SteamwarUser user){ + return ProxyServer.getInstance().getPlayer(user.getUuid()) != null ? "§a" + user.getUserName() : "§e" + user.getUserName(); + } + + private String getMemberList(List users, boolean leaders) { + StringBuilder sb = new StringBuilder(); + for(SteamwarUser user : users) { + if(user.isLeader() == leaders) { + sb.append(playerName(user)).append(" "); + } + } + return sb.toString(); + } + + public static boolean isLocalhost(InetAddress addr) { + // Check if the address is a valid special local or loop back + if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) + return true; + + // Check if the address is defined on any interface + try { + return NetworkInterface.getByInetAddress(addr) != null; + } catch (SocketException e) { + return false; + } + } + private boolean checkTeamName(ProxiedPlayer player, Team team, String arg){ if(arg.length() < 4 || arg.length() > 15){ Message.send("TEAM_NAME_LENGHT", player); @@ -744,37 +694,4 @@ public class TeamCommand extends BasicCommand { } return false; } - - @Override - public Iterable onTabComplete(CommandSender commandSender, String[] args){ - List tab = new ArrayList<>(); - if(args.length == 1){ - tab.add("create"); - tab.add("join"); - tab.add("invite"); - tab.add("stepback"); - tab.add("leave"); - tab.add("info"); - tab.add("remove"); - tab.add("event"); - tab.add("changekuerzel"); - tab.add("changename"); - tab.add("promote"); - tab.add("changecolor"); - tab.add("tp"); - tab.add("server"); - }else if(args.length == 2){ - if(args[1].equalsIgnoreCase("event")){ - List coming = Event.getComing(); - coming.forEach(event -> tab.add(event.getEventName())); - }else if(args[1].equalsIgnoreCase("join") || args[1].equalsIgnoreCase("info") || args[1].equalsIgnoreCase("tp")){ - List teams = Team.getAll(); - teams.forEach(team -> { - tab.add(team.getTeamName()); - tab.add(team.getTeamKuerzel()); - }); - } - } - return tab; - } }