From 7a77dc30e63fbc4570d5f6c502e9b210fb686e55 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 28 Jul 2019 18:36:36 +0200 Subject: [PATCH] Teamsystem (untested, without chat prefix) --- src/de/steamwar/bungeecore/BungeeCore.java | 1 + .../bungeecore/commands/HelpCommand.java | 7 +- .../bungeecore/commands/TeamCommand.java | 341 ++++++++++++++++++ src/de/steamwar/bungeecore/sql/Team.java | 137 +++++++ .../steamwar/bungeecore/sql/WarkingUser.java | 11 + 5 files changed, 496 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/bungeecore/commands/TeamCommand.java create mode 100644 src/de/steamwar/bungeecore/sql/Team.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 3ff8031..571ee0e 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -112,6 +112,7 @@ public class BungeeCore extends Plugin { addCmd(new BanCommand()); addCmd(new DenyCommand("watchcat", "wc")); addCmd(new ChallengeCommand()); + addCmd(new TeamCommand()); BroadCastMsgs = config.getStringList("broadcasts").toArray(new String[1]); getProxy().getScheduler().schedule(this, () -> { diff --git a/src/de/steamwar/bungeecore/commands/HelpCommand.java b/src/de/steamwar/bungeecore/commands/HelpCommand.java index 668aaae..5219657 100644 --- a/src/de/steamwar/bungeecore/commands/HelpCommand.java +++ b/src/de/steamwar/bungeecore/commands/HelpCommand.java @@ -36,11 +36,16 @@ public class HelpCommand extends Command { tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/fight")); sender.sendMessage(tc); - tc = new TextComponent(BungeeCore.ChatPrefix + "§7Tippe §8/§echallenge §7, um jemanden herauszufordern!"); + tc = new TextComponent(BungeeCore.ChatPrefix + "§7Tippe §8/§echallenge§7, um jemanden herauszufordern!"); tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eHerausfordern").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/challenge")); sender.sendMessage(tc); + tc = new TextComponent(BungeeCore.ChatPrefix + "§8/§eteam§7 für das Teamsystem!"); + tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eTeamverwaltung").create())); + tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/team")); + sender.sendMessage(tc); + tc = new TextComponent(BungeeCore.ChatPrefix + "§7Trete mit §8/§ejoin §8[§eSpieler§8] §7einem Kampf bei!"); tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§eSpieler beitreten").create())); tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/join")); diff --git a/src/de/steamwar/bungeecore/commands/TeamCommand.java b/src/de/steamwar/bungeecore/commands/TeamCommand.java new file mode 100644 index 0000000..7fcfdca --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/TeamCommand.java @@ -0,0 +1,341 @@ +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.sql.Team; +import de.steamwar.bungeecore.sql.WarkingUser; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.*; + +public class TeamCommand extends Command { + + public TeamCommand() { + super("team", ""); + } + + private static Map> invitations = new HashMap<>(); + + private void help(CommandSender sender){ + sender.sendMessage(BungeeCore.ChatPrefix + "§7Mit §e/team §7verwaltest du dein Team"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam create §8- §7Erstelle dein eigenes Team"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam join §8- §7Trete einem Team bei"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam leave §8- §7Verlasse dein Team"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam info §8- §7Informiere dich über ein Team"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam invite §8- §7Lade jemanden in dein Team ein"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam remove §8- §7Entferne jemanden aus deinem Team"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam changekuerzel §8- §7Ändere dein Teamkürzel"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam changename §8- §7Ändere deinen Teamnamen"); + sender.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam changeleader §8- §7Ernenne jemanden zum Teamleader"); + } + + @Override + public void execute(CommandSender sender, String[] args){ + if(args.length < 1){ + help(sender); + return; + } + + if(sender instanceof ProxiedPlayer) + return; + + ProxiedPlayer player = (ProxiedPlayer) sender; + WarkingUser user = WarkingUser.get(player.getUniqueId()); + Team team = Team.get(user.getId()); + WarkingUser target; + ProxiedPlayer p; + + switch(args[0].toLowerCase()){ + case "create": + if(unwantedInTeam(player, user)) + return; + + if(args.length < 3){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam create §8[§eTeamkürzel§8] §8[§eTeamname§8]"); + return; + } + + if(checkTeamKuerzel(player, team, args[1])) + return; + + if(checkTeamName(player, team, args[2])) + return; + + Team.create(args[1], args[2], user.getId()); + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast das Team §e" + args[2] + " §7gegründet!"); + break; + case "join": + if(unwantedInTeam(player, user)) + return; + + if(!invitations.containsKey(user)){ + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast keine Einladungen erhalten."); + return; + } + + List invs = invitations.get(user); + Team t = null; + + if(invs.size() == 1){ + t = invs.get(0); + }else{ + if(args.length != 2){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam join §8[§eTeam§8]"); + StringBuilder sb = new StringBuilder().append("§7Du wurdest von diesen Teams eingeladen§8: §e"); + for(Team inv : invs){ + sb.append(inv.getTeamName()).append(" "); + } + player.sendMessage(sb.toString()); + return; + } + + for(Team inv : invs){ + if(inv.getTeamName().equals(args[1])){ + t = inv; + break; + } + } + if(t == null){ + player.sendMessage(BungeeCore.ChatPrefix + "§cVon diesem Team wurdest du nicht eingeladen"); + return; + } + } + + user.setTeam(t.getTeamId()); + invitations.remove(user); + player.sendMessage(BungeeCore.ChatPrefix + "§7Du bist dem Team §e" + t.getTeamName() + " §7beigetreten!"); + break; + case "leave": + if(notInTeam(player, user)) + return; + + if(team.getTeamLeader() == user.getId() && team.size() > 1){ + player.sendMessage(BungeeCore.ChatPrefix + "§cBitte ernenne zunächst ein anderes Teammitglied zum Leader"); + return; + } + + user.setTeam(0); + + if(team.getTeamLeader() == user.getId()){ + team.disband(); + } + + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast das Team §e" + args[2] + " §7verlassen!"); + break; + case "invite": + if(notLeader(player, user, team)) + return; + + if(args.length != 2){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam invite §8[§eSpieler§8]"); + return; + } + + target = WarkingUser.get(args[1]); + if(target == null){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDiesen Spieler gibt es nicht"); + return; + }else if(target.getTeam() != 0){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDieser Spieler ist bereits in einem Team"); + return; + } + + if(!invitations.containsKey(target)) + invitations.put(target, new LinkedList<>()); + invitations.get(target).add(team); + + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast §e" + args[1] + " §7in das Team eingeladen!"); + p = ProxyServer.getInstance().getPlayer(target.getUUID()); + if(p != null) + player.sendMessage(BungeeCore.ChatPrefix + "§7Du wurdest in das Team §e" + team.getTeamName() + " §7eingeladen!"); + break; + case "remove": + if(notLeader(player, user, team)) + return; + + if(args.length != 2){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam remove §8[§eSpieler§8]"); + return; + } + + target = WarkingUser.get(args[1]); + if(target == null){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDiesen Spieler gibt es nicht"); + return; + } + + if(invitations.containsKey(target)){ + if(invitations.get(target).remove(team)){ + player.sendMessage(BungeeCore.ChatPrefix + "§7Die Einladung wurde zurückgezogen"); + if(invitations.get(target).isEmpty()) + invitations.remove(target); + }else{ + player.sendMessage(BungeeCore.ChatPrefix + "§cDieser Spieler hat keine Einladung erhalten"); + } + return; + } + + if(target.getTeam() != user.getTeam()){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDieser Spieler ist nicht in deinem Team"); + return; + } + + target.setTeam(0); + + player.sendMessage(BungeeCore.ChatPrefix + "§7Der Spieler wurde aus dem Team entfernt"); + p = ProxyServer.getInstance().getPlayer(target.getUUID()); + if(p != null) + p.sendMessage(BungeeCore.ChatPrefix + "§cDu wurdest aus dem Team entfernt"); + break; + case "changekuerzel": + if(notLeader(player, user, team)) + return; + + if(args.length < 2){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam changekuerzel §8[§eTeamkürzel§8]"); + return; + } + + if(checkTeamKuerzel(player, team, args[1])) + return; + + team.setTeamKuerzel(args[1]); + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast das Kürzel des Teams geändert!"); + break; + case "changename": + if(notLeader(player, user, team)) + return; + + if(args.length < 2){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam changename §8[§eTeamname§8]"); + return; + } + + if(checkTeamName(player, team, args[1])) + return; + + team.setTeamName(args[1]); + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast das Team umbenannt!"); + break; + case "changeleader": + if(notLeader(player, user, team)) + return; + + if(args.length < 2){ + player.sendMessage(BungeeCore.ChatPrefix + "§cBitte gib ein Teammitglied an"); + return; + } + + target = WarkingUser.get(args[1]); + if(target == null){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDen Spieler " + args[1] + " gibt es nicht"); + return; + } + + if(target.getTeam() != user.getTeam()){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDer Spieler ist nicht in deinem Team"); + return; + } + + team.setTeamLeader(target.getId()); + player.sendMessage(BungeeCore.ChatPrefix + "§7Du hast den Spieler §e" + args[1] + " §7zum Leader gemacht!"); + break; + case "info": + if(args.length > 1){ + team = Team.get(args[1]); + }else if(user.getTeam() == 0){ + player.sendMessage(BungeeCore.ChatPrefix + "§8/§eteam info §8[§eTeamname§8]"); + return; + } + + player.sendMessage("§7Team §e" + team.getTeamName() + " §8[§e" + team.getTeamKuerzel() + "§8]"); + player.sendMessage("§7Leader§8: " + WarkingUser.get(team.getTeamLeader()).getUserName()); + + StringBuilder sb = new StringBuilder(); + sb.append("$7Member§8: §e"); + List members = team.getMembers(); + + for(int i : members){ + WarkingUser m = WarkingUser.get(i); + sb.append(m.getUserName()).append(" "); + } + + player.sendMessage(sb.toString()); + break; + default: + help(player); + } + } + + private boolean checkTeamName(ProxiedPlayer player, Team team, String arg){ + if(arg.length() < 4 || arg.length() > 15){ + player.sendMessage(BungeeCore.ChatPrefix + "§cEin Teamname muss aus 4 bis 15 Buchstaben bestehen"); + return true; + } + + Team t = Team.get(arg); + if(t != null && t.getTeamId() != team.getTeamId()){ + player.sendMessage(BungeeCore.ChatPrefix + "§cEs gibt bereits ein Team mit diesem Namen"); + return true; + } + return false; + } + + private boolean checkTeamKuerzel(ProxiedPlayer player, Team team, String arg){ + if(arg.length() < 2 || arg.length() > 4){ + player.sendMessage(BungeeCore.ChatPrefix + "§cEin Teamkürzel muss aus 2 bis 4 Buchstaben bestehen"); + return true; + } + + Team t = Team.get(arg); + if(t != null && t.getTeamId() != team.getTeamId()){ + player.sendMessage(BungeeCore.ChatPrefix + "§cEs gibt bereits ein Team mit diesem Namen"); + return true; + } + return false; + } + + private boolean unwantedInTeam(ProxiedPlayer player, WarkingUser user){ + if(user.getTeam() != 0){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDu bist bereits in einem Team"); + return true; + } + return false; + } + + private boolean notInTeam(ProxiedPlayer player, WarkingUser user){ + if(user.getTeam() == 0){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDu bist in keinem Team"); + return true; + } + return false; + } + + private boolean notLeader(ProxiedPlayer player, WarkingUser user, Team team){ + if(notInTeam(player, user)) + return true; + if(team.getTeamLeader() != user.getId()){ + player.sendMessage(BungeeCore.ChatPrefix + "§cDu bist nicht der Teamleader"); + return true; + } + 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("leave"); + tab.add("info"); + tab.add("remove"); + tab.add("changekuerzel"); + tab.add("changename"); + tab.add("changeleader"); + } + return tab; + } +} diff --git a/src/de/steamwar/bungeecore/sql/Team.java b/src/de/steamwar/bungeecore/sql/Team.java new file mode 100644 index 0000000..09444a5 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Team.java @@ -0,0 +1,137 @@ +package de.steamwar.bungeecore.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class Team { + private final int teamId; + private String teamKuerzel; + private String teamName; + private int teamLeader; + + private static final List teamCache = new ArrayList<>(); + + private Team(int id, String kuerzel, String name, int leader){ + teamId = id; + teamKuerzel = kuerzel; + teamName = name; + teamLeader = leader; + } + + public static void create(String kuerzel, String name, int leader){ + sql.update("INSERT INTO Team" + + " (TeamKuerzel, TeamName, TeamLeader)" + + " VALUES" + + " ('" + kuerzel + "', '" + name + "', '" + leader + "')"); + } + + public static Team get(int id){ + for(Team team : teamCache) + if(team.teamId == id) + return team; + return load(sql.select("SELECT * FROM Team WHERE TeamID = " + id)); + } + + public static Team get(String name){ + for(Team team : teamCache) + if(team.teamName.equals(name)) + return team; + for(Team team : teamCache) + if(team.teamKuerzel.equals(name)) + return team; + return load(sql.select("SELECT * FROM Team WHERE TeamName = '" + sql.disarmString(name) + "' OR TeamKuerzel = '" + sql.disarmString(name) + "'")); + } + + public static void clearCache(){ + teamCache.clear(); + } + + private static Team load(ResultSet dbteam){ + try { + if(!dbteam.next()){ + return null; + } + int id = dbteam.getInt("TeamID"); + String kuerzel = dbteam.getString("TeamKuerzel"); + String name = dbteam.getString("TeamName"); + int leader = dbteam.getInt("TeamLeader"); + Team team = new Team(id, kuerzel, name, leader); + teamCache.add(team); + return team; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + private void updateDB(){ + sql.update("INSERT INTO Team" + + " (TeamID, TeamKuerzel, TeamName, TeamLeader)" + + " VALUES" + + " ('" + teamId + "', '" + teamKuerzel + "', '" + teamName + "', '" + teamLeader + "')" + + " ON DUPLICATE KEY UPDATE" + + " TeamName = VALUES(TeamName), TeamKuerzel = VALUES(TeamKuerzel), TeamLeader = VALUES(TeamLeader)"); + } + + public int getTeamId() { + return teamId; + } + + public String getTeamKuerzel() { + return teamKuerzel; + } + + public void setTeamKuerzel(String teamKuerzel) { + this.teamKuerzel = sql.disarmString(teamKuerzel); + updateDB(); + } + + public String getTeamName() { + return teamName; + } + + public void setTeamName(String teamName) { + this.teamName = sql.disarmString(teamName); + updateDB(); + } + + public int getTeamLeader() { + return teamLeader; + } + + public void setTeamLeader(int teamLeader) { + this.teamLeader = teamLeader; + updateDB(); + } + + public int size(){ + ResultSet rs = sql.select("SELECT COUNT(id) FROM UserData WHERE Team = " + teamId); + try { + return rs.getInt("COUNT(id)"); + }catch (SQLException e) { + e.printStackTrace(); + return 0; + } + } + + public void disband(){ + sql.update("DELETE FROM Team WHERE TeamID = " + teamId); + teamCache.remove(this); + } + + public List getMembers(){ + try{ + ResultSet memberlist = sql.select("SELECT id FROM UserData WHERE Team = '" + teamId + "'"); + List members = new ArrayList<>(); + while(memberlist.next()){ + members.add(memberlist.getInt("id")); + } + return members; + }catch(SQLException e){ + e.printStackTrace(); + } + return new ArrayList<>(); + } +} diff --git a/src/de/steamwar/bungeecore/sql/WarkingUser.java b/src/de/steamwar/bungeecore/sql/WarkingUser.java index a4f363e..28530a0 100644 --- a/src/de/steamwar/bungeecore/sql/WarkingUser.java +++ b/src/de/steamwar/bungeecore/sql/WarkingUser.java @@ -18,6 +18,7 @@ public class WarkingUser { private String UserGroup; private Timestamp BanTime; private String BanReason; + private int Team; private static final List allUsers = new ArrayList<>(); public static Timestamp PermaBan = Timestamp.from(Instant.ofEpochSecond(946684800)); @@ -95,6 +96,7 @@ public class WarkingUser { UserGroup = dbplayer.getString("UserGroup"); BanTime = dbplayer.getTimestamp("BanTime"); BanReason = dbplayer.getString("BanReason"); + Team = dbplayer.getInt("Team"); allUsers.add(this); } catch (SQLException e) { e.printStackTrace(); @@ -153,4 +155,13 @@ public class WarkingUser { public String getBanReason() { return BanReason; } + + public int getTeam(){ + return Team; + } + + public void setTeam(int team){ + Team = team; + sql.update("Update UserData SET Team = " + team + " WHERE id = " + id); + } }