From 6af27156df8d0f6e6eda14adc97e031fa61a300d Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 11 Jun 2023 18:38:43 +0700 Subject: [PATCH] FIX?! --- .../chayapak/chomens_bot/data/Team.java | 1 - .../chomens_bot/plugins/TeamPlugin.java | 111 +++++++++--------- 2 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/Team.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/Team.java index 973f286..a87cb05 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/Team.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/Team.java @@ -14,7 +14,6 @@ import java.util.List; @AllArgsConstructor public class Team { @Getter @Setter private String teamName; - @Getter private List players; @Getter @Setter private Component displayName; @Getter @Setter private boolean friendlyFire; @Getter @Setter private boolean seeFriendlyInvisibles; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TeamPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TeamPlugin.java index 7acd07d..c0d4edc 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TeamPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TeamPlugin.java @@ -7,11 +7,12 @@ import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.Team; import lombok.Getter; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; public class TeamPlugin extends Bot.Listener { - @Getter private final List teams = new ArrayList<>(); + @Getter private final Map teams = new HashMap<>(); + @Getter private final Map teamsByPlayer = new HashMap<>(); public TeamPlugin (Bot bot) { bot.addListener(this); @@ -23,62 +24,58 @@ public class TeamPlugin extends Bot.Listener { } public void packetReceived(ClientboundSetPlayerTeamPacket packet) { - switch (packet.getAction()) { - case CREATE -> { - final Team team = new Team( - packet.getTeamName(), - List.of(packet.getPlayers()), - packet.getDisplayName(), - packet.isFriendlyFire(), - packet.isSeeFriendlyInvisibles(), - packet.getNameTagVisibility(), - packet.getCollisionRule(), - packet.getColor(), - packet.getPrefix(), - packet.getSuffix() - ); + try { + switch (packet.getAction()) { + case CREATE -> { + final Team team = new Team( + packet.getTeamName(), + packet.getDisplayName(), + packet.isFriendlyFire(), + packet.isSeeFriendlyInvisibles(), + packet.getNameTagVisibility(), + packet.getCollisionRule(), + packet.getColor(), + packet.getPrefix(), + packet.getSuffix() + ); - teams.add(team); + teams.put(packet.getTeamName(), team); + } + case REMOVE -> teams.remove(packet.getTeamName()); + case UPDATE -> { + final Team team = teams.get(packet.getTeamName()); + + if (team == null) return; + + team.teamName(packet.getTeamName()); + team.displayName(packet.getDisplayName()); + team.friendlyFire(packet.isFriendlyFire()); + team.seeFriendlyInvisibles(packet.isSeeFriendlyInvisibles()); + team.nametagVisibility(packet.getNameTagVisibility()); + team.collisionRule(packet.getCollisionRule()); + team.color(packet.getColor()); + team.prefix(packet.getPrefix()); + team.suffix(packet.getSuffix()); + } + case ADD_PLAYER -> { + final Team team = teams.get(packet.getTeamName()); + + if (team == null) return; + + for (String player : packet.getPlayers()) teamsByPlayer.put(player, team); + } + case REMOVE_PLAYER -> { + final Team team = teams.get(packet.getTeamName()); + + if (team == null) return; + + for (String player : packet.getPlayers()) teamsByPlayer.remove(player); + } } - case REMOVE -> teams.removeIf(team -> team.teamName().equals(packet.getTeamName())); - case UPDATE -> { - final Team team = teams - .stream() - .filter(eachTeam -> eachTeam.teamName().equals(packet.getTeamName())) - .toArray(Team[]::new)[0]; + } catch (Exception e) { e.printStackTrace(); } + } - if (team == null) return; - - team.teamName(packet.getTeamName()); - team.displayName(packet.getDisplayName()); - team.friendlyFire(packet.isFriendlyFire()); - team.seeFriendlyInvisibles(packet.isSeeFriendlyInvisibles()); - team.nametagVisibility(packet.getNameTagVisibility()); - team.collisionRule(packet.getCollisionRule()); - team.color(packet.getColor()); - team.prefix(packet.getPrefix()); - team.suffix(packet.getSuffix()); - } - case ADD_PLAYER -> { - final Team team = teams - .stream() - .filter(eachTeam -> eachTeam.teamName().equals(packet.getTeamName())) - .toArray(Team[]::new)[0]; - - if (team == null) return; - - team.players().addAll(List.of(packet.getPlayers())); - } - case REMOVE_PLAYER -> { - final Team team = teams - .stream() - .filter(eachTeam -> eachTeam.teamName().equals(packet.getTeamName())) - .toArray(Team[]::new)[0]; - - if (team == null) return; - - team.players().removeAll(List.of(packet.getPlayers())); - } - } + public Team getTeamByUsername (String username) { + return teamsByPlayer.get(username); } }