From 70276f4906163fb5bddfcf15387accbcc8f8e63d Mon Sep 17 00:00:00 2001 From: Ninjdai Date: Sun, 29 Sep 2024 17:14:32 +0200 Subject: [PATCH] refactor role and team handling on player: save in UHCPlayer class instead of player tags --- .../werewolf/commands/LGRoleCommand.java | 2 +- .../werewolf/commands/LGTeamCommand.java | 2 +- .../dev/ninjdai/werewolf/uhc/UHCPlayer.java | 20 +++---------------- .../dev/ninjdai/werewolf/uhc/roles/Role.java | 19 +++++++++--------- .../dev/ninjdai/werewolf/uhc/roles/Roles.java | 2 +- .../dev/ninjdai/werewolf/uhc/teams/Team.java | 2 -- .../dev/ninjdai/werewolf/uhc/teams/Teams.java | 2 +- .../werewolf/uhc/teams/WinCondition.java | 8 ++++---- 8 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/main/java/dev/ninjdai/werewolf/commands/LGRoleCommand.java b/src/main/java/dev/ninjdai/werewolf/commands/LGRoleCommand.java index b4e219b..920c8f7 100644 --- a/src/main/java/dev/ninjdai/werewolf/commands/LGRoleCommand.java +++ b/src/main/java/dev/ninjdai/werewolf/commands/LGRoleCommand.java @@ -38,7 +38,7 @@ public class LGRoleCommand extends Command { EntityFinder finder = context.get(singlePlayerArgument); UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender); - Role role = context.get(roleArgument).role; + Role role = context.get(roleArgument).get(); if (player == null) { sender.sendMessage("Couldn't find player"); return; diff --git a/src/main/java/dev/ninjdai/werewolf/commands/LGTeamCommand.java b/src/main/java/dev/ninjdai/werewolf/commands/LGTeamCommand.java index 0041d36..354c452 100644 --- a/src/main/java/dev/ninjdai/werewolf/commands/LGTeamCommand.java +++ b/src/main/java/dev/ninjdai/werewolf/commands/LGTeamCommand.java @@ -36,7 +36,7 @@ public class LGTeamCommand extends Command { EntityFinder finder = context.get(singlePlayerArgument); UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender); - Team team = context.get(teamArgument).team; + Team team = context.get(teamArgument).get(); if (player == null) { sender.sendMessage("Couldn't find player"); return; diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/UHCPlayer.java b/src/main/java/dev/ninjdai/werewolf/uhc/UHCPlayer.java index f4bd500..f6caee3 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/UHCPlayer.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/UHCPlayer.java @@ -7,6 +7,7 @@ import dev.ninjdai.werewolf.uhc.teams.Teams; import io.github.togar2.pvp.MinestomPvP; import io.github.togar2.pvp.player.CombatPlayerImpl; import lombok.Getter; +import lombok.Setter; import net.minestom.server.entity.GameMode; import net.minestom.server.network.packet.server.play.WorldBorderSizePacket; import net.minestom.server.network.player.PlayerConnection; @@ -20,27 +21,12 @@ import java.util.UUID; public class UHCPlayer extends CombatPlayerImpl { @Getter private boolean spectating; + @Getter @Setter private Team UHCTeam = Teams.VILLAGERS.get(); + @Getter @Setter private Role UHCRole = Roles.VILLAGER.get(); public UHCPlayer(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection playerConnection) { super(uuid, username, playerConnection); } - public void setUHCTeam(Team team) { - this.updateTag(Team.teamTag, (t) -> team.getId()); - } - - public Team getUHCTeam() { - return Team.teamMap.getOrDefault(getTag(Team.teamTag), Teams.VILLAGERS.get()); - } - - public void setUHCRole(Role role) { - this.updateTag(Role.roleTag, (r) -> role.getId()); - } - - public Role getUHCRole() { - String teamId = getTag(Role.roleTag); - return Role.roleMap.getOrDefault(teamId, Roles.VILLAGER.role); - } - public void toggleSpectating() { if (isSpectating()) { spectating = false; diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/roles/Role.java b/src/main/java/dev/ninjdai/werewolf/uhc/roles/Role.java index aa516e7..3f44604 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/roles/Role.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/roles/Role.java @@ -5,12 +5,12 @@ import dev.ninjdai.werewolf.uhc.effects.Effect; import dev.ninjdai.werewolf.uhc.events.TimedPlayerDeath; import lombok.Getter; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextColor; -import net.minestom.server.color.Color; -import net.minestom.server.entity.Player; +import net.kyori.adventure.text.format.TextDecoration; import net.minestom.server.item.ItemStack; import net.minestom.server.tag.Tag; -import net.minestom.server.timer.Task; +import org.jetbrains.annotations.NotNull; import java.util.*; @@ -25,7 +25,7 @@ public class Role { roleMap.put(this.id, this); } - Role(Builder builder) { + Role(@NotNull Builder builder) { this.id = builder.id; this.name = builder.name; this.roleItems = builder.roleItems; @@ -41,14 +41,13 @@ public class Role { @Getter private List roleEffects; @Getter private Set roleProperties; - public void onPlayerDeath(UHCPlayer player, TimedPlayerDeath timedPlayerDeath) { - player.sendMessage(Component.text("", TextColor.color(184,14,14)) - .append(Component.text("You died, but don't leave too soon ! You may be resurrected in the next 15 seconds.\nYou can still watch the game after dying definitely.")) - ); + public void onPlayerDeath(@NotNull UHCPlayer player, TimedPlayerDeath timedPlayerDeath) { + player.sendMessage(Component.text( + "You died, but don't leave too soon ! You may be resurrected in the next 15 seconds.\nYou can still watch the game after dying definitely.", + Style.style(TextColor.color(184,14,14), TextDecoration.ITALIC) + )); } - public static Tag roleTag = Tag.String("role"); - // ID: Role public static final Map roleMap = new HashMap<>(); diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/roles/Roles.java b/src/main/java/dev/ninjdai/werewolf/uhc/roles/Roles.java index b72516c..9f3b9fe 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/roles/Roles.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/roles/Roles.java @@ -9,7 +9,7 @@ public enum Roles { WEREWOLF(new Role.Builder().setId("werewolf").setName("Werewolf").addRoleEffect(new WerewolfStrength()).build()), VILLAGER(new Role.Builder().setId("villager").setName("Villager").build()); - public final Role role; + private final Role role; Roles(Role role) { this.role = role; } diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/teams/Team.java b/src/main/java/dev/ninjdai/werewolf/uhc/teams/Team.java index c925787..b89f767 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/teams/Team.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/teams/Team.java @@ -31,8 +31,6 @@ public class Team { @Getter private Color color; @Getter private WinCondition.WinConditionType winConditionType; - public static Tag teamTag = Tag.String("team"); - // ID: Team public static final Map teamMap; static { diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/teams/Teams.java b/src/main/java/dev/ninjdai/werewolf/uhc/teams/Teams.java index 5210727..62f0b9f 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/teams/Teams.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/teams/Teams.java @@ -11,7 +11,7 @@ public enum Teams { VILLAGERS(new Team.Builder().setId("villagers").setName("Villagers").setColor(new Color(48,216,90)).build()), COUPLE(new Team.Builder().setId("couple").setName("Couple").setColor(new Color(195,61,148)).build()); - public final Team team; + private final Team team; Teams(Team team) { this.team = team; } diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/teams/WinCondition.java b/src/main/java/dev/ninjdai/werewolf/uhc/teams/WinCondition.java index 2fc4558..c637b7a 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/teams/WinCondition.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/teams/WinCondition.java @@ -27,10 +27,10 @@ public class WinCondition { public static boolean checkWin(Set inGamePlayers) { Main.LOGGER.info(inGamePlayers.toString()); for (Teams team: Teams.values()) { - Main.LOGGER.info("Checking if team {} wins...", team.team.getName()); - if(checkTeamWin(team.team, inGamePlayers)){ - winningTeam = team.team; - Main.LOGGER.info("Team {} wins !", team.team.getName()); + Main.LOGGER.info("Checking if team {} wins...", team.get().getName()); + if(checkTeamWin(team.get(), inGamePlayers)){ + winningTeam = team.get(); + Main.LOGGER.info("Team {} wins !", team.get().getName()); return true; } }