This commit is contained in:
Chayapak 2023-06-11 18:38:43 +07:00
parent 572dfbc9f7
commit 6af27156df
2 changed files with 54 additions and 58 deletions

View file

@ -14,7 +14,6 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public class Team { public class Team {
@Getter @Setter private String teamName; @Getter @Setter private String teamName;
@Getter private List<String> players;
@Getter @Setter private Component displayName; @Getter @Setter private Component displayName;
@Getter @Setter private boolean friendlyFire; @Getter @Setter private boolean friendlyFire;
@Getter @Setter private boolean seeFriendlyInvisibles; @Getter @Setter private boolean seeFriendlyInvisibles;

View file

@ -7,11 +7,12 @@ import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.Team; import land.chipmunk.chayapak.chomens_bot.data.Team;
import lombok.Getter; import lombok.Getter;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.Map;
public class TeamPlugin extends Bot.Listener { public class TeamPlugin extends Bot.Listener {
@Getter private final List<Team> teams = new ArrayList<>(); @Getter private final Map<String, Team> teams = new HashMap<>();
@Getter private final Map<String, Team> teamsByPlayer = new HashMap<>();
public TeamPlugin (Bot bot) { public TeamPlugin (Bot bot) {
bot.addListener(this); bot.addListener(this);
@ -23,62 +24,58 @@ public class TeamPlugin extends Bot.Listener {
} }
public void packetReceived(ClientboundSetPlayerTeamPacket packet) { public void packetReceived(ClientboundSetPlayerTeamPacket packet) {
switch (packet.getAction()) { try {
case CREATE -> { switch (packet.getAction()) {
final Team team = new Team( case CREATE -> {
packet.getTeamName(), final Team team = new Team(
List.of(packet.getPlayers()), packet.getTeamName(),
packet.getDisplayName(), packet.getDisplayName(),
packet.isFriendlyFire(), packet.isFriendlyFire(),
packet.isSeeFriendlyInvisibles(), packet.isSeeFriendlyInvisibles(),
packet.getNameTagVisibility(), packet.getNameTagVisibility(),
packet.getCollisionRule(), packet.getCollisionRule(),
packet.getColor(), packet.getColor(),
packet.getPrefix(), packet.getPrefix(),
packet.getSuffix() 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())); } catch (Exception e) { e.printStackTrace(); }
case UPDATE -> { }
final Team team = teams
.stream()
.filter(eachTeam -> eachTeam.teamName().equals(packet.getTeamName()))
.toArray(Team[]::new)[0];
if (team == null) return; public Team getTeamByUsername (String username) {
return teamsByPlayer.get(username);
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()));
}
}
} }
} }