refactor role and team handling on player: save in UHCPlayer class instead of player tags

This commit is contained in:
Ninjdai 2024-09-29 17:14:32 +02:00
parent eb0f26d371
commit 70276f4906
8 changed files with 20 additions and 37 deletions

View File

@ -38,7 +38,7 @@ public class LGRoleCommand extends Command {
EntityFinder finder = context.get(singlePlayerArgument); EntityFinder finder = context.get(singlePlayerArgument);
UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender); UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender);
Role role = context.get(roleArgument).role; Role role = context.get(roleArgument).get();
if (player == null) { if (player == null) {
sender.sendMessage("Couldn't find player"); sender.sendMessage("Couldn't find player");
return; return;

View File

@ -36,7 +36,7 @@ public class LGTeamCommand extends Command {
EntityFinder finder = context.get(singlePlayerArgument); EntityFinder finder = context.get(singlePlayerArgument);
UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender); UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender);
Team team = context.get(teamArgument).team; Team team = context.get(teamArgument).get();
if (player == null) { if (player == null) {
sender.sendMessage("Couldn't find player"); sender.sendMessage("Couldn't find player");
return; return;

View File

@ -7,6 +7,7 @@ import dev.ninjdai.werewolf.uhc.teams.Teams;
import io.github.togar2.pvp.MinestomPvP; import io.github.togar2.pvp.MinestomPvP;
import io.github.togar2.pvp.player.CombatPlayerImpl; import io.github.togar2.pvp.player.CombatPlayerImpl;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import net.minestom.server.entity.GameMode; import net.minestom.server.entity.GameMode;
import net.minestom.server.network.packet.server.play.WorldBorderSizePacket; import net.minestom.server.network.packet.server.play.WorldBorderSizePacket;
import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerConnection;
@ -20,27 +21,12 @@ import java.util.UUID;
public class UHCPlayer extends CombatPlayerImpl { public class UHCPlayer extends CombatPlayerImpl {
@Getter private boolean spectating; @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) { public UHCPlayer(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection playerConnection) {
super(uuid, username, 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() { public void toggleSpectating() {
if (isSpectating()) { if (isSpectating()) {
spectating = false; spectating = false;

View File

@ -5,12 +5,12 @@ import dev.ninjdai.werewolf.uhc.effects.Effect;
import dev.ninjdai.werewolf.uhc.events.TimedPlayerDeath; import dev.ninjdai.werewolf.uhc.events.TimedPlayerDeath;
import lombok.Getter; import lombok.Getter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.color.Color; import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.server.entity.Player;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.tag.Tag; import net.minestom.server.tag.Tag;
import net.minestom.server.timer.Task; import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
@ -25,7 +25,7 @@ public class Role {
roleMap.put(this.id, this); roleMap.put(this.id, this);
} }
Role(Builder builder) { Role(@NotNull Builder builder) {
this.id = builder.id; this.id = builder.id;
this.name = builder.name; this.name = builder.name;
this.roleItems = builder.roleItems; this.roleItems = builder.roleItems;
@ -41,14 +41,13 @@ public class Role {
@Getter private List<Effect> roleEffects; @Getter private List<Effect> roleEffects;
@Getter private Set<RoleProperties> roleProperties; @Getter private Set<RoleProperties> roleProperties;
public void onPlayerDeath(UHCPlayer player, TimedPlayerDeath timedPlayerDeath) { public void onPlayerDeath(@NotNull UHCPlayer player, TimedPlayerDeath timedPlayerDeath) {
player.sendMessage(Component.text("", TextColor.color(184,14,14)) player.sendMessage(Component.text(
.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.")) "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<String> roleTag = Tag.String("role");
// ID: Role // ID: Role
public static final Map<String, Role> roleMap = new HashMap<>(); public static final Map<String, Role> roleMap = new HashMap<>();

View File

@ -9,7 +9,7 @@ public enum Roles {
WEREWOLF(new Role.Builder().setId("werewolf").setName("Werewolf").addRoleEffect(new WerewolfStrength()).build()), WEREWOLF(new Role.Builder().setId("werewolf").setName("Werewolf").addRoleEffect(new WerewolfStrength()).build()),
VILLAGER(new Role.Builder().setId("villager").setName("Villager").build()); VILLAGER(new Role.Builder().setId("villager").setName("Villager").build());
public final Role role; private final Role role;
Roles(Role role) { Roles(Role role) {
this.role = role; this.role = role;
} }

View File

@ -31,8 +31,6 @@ public class Team {
@Getter private Color color; @Getter private Color color;
@Getter private WinCondition.WinConditionType winConditionType; @Getter private WinCondition.WinConditionType winConditionType;
public static Tag<String> teamTag = Tag.String("team");
// ID: Team // ID: Team
public static final Map<String, Team> teamMap; public static final Map<String, Team> teamMap;
static { static {

View File

@ -11,7 +11,7 @@ public enum Teams {
VILLAGERS(new Team.Builder().setId("villagers").setName("Villagers").setColor(new Color(48,216,90)).build()), 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()); 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) { Teams(Team team) {
this.team = team; this.team = team;
} }

View File

@ -27,10 +27,10 @@ public class WinCondition {
public static boolean checkWin(Set<UHCPlayer> inGamePlayers) { public static boolean checkWin(Set<UHCPlayer> inGamePlayers) {
Main.LOGGER.info(inGamePlayers.toString()); Main.LOGGER.info(inGamePlayers.toString());
for (Teams team: Teams.values()) { for (Teams team: Teams.values()) {
Main.LOGGER.info("Checking if team {} wins...", team.team.getName()); Main.LOGGER.info("Checking if team {} wins...", team.get().getName());
if(checkTeamWin(team.team, inGamePlayers)){ if(checkTeamWin(team.get(), inGamePlayers)){
winningTeam = team.team; winningTeam = team.get();
Main.LOGGER.info("Team {} wins !", team.team.getName()); Main.LOGGER.info("Team {} wins !", team.get().getName());
return true; return true;
} }
} }