diff --git a/src/main/java/dev/ninjdai/werewolf/uhc/events/UHCPlayerDeathEvent.java b/src/main/java/dev/ninjdai/werewolf/uhc/events/UHCPlayerDeathEvent.java index 5c2c72b..525e270 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/events/UHCPlayerDeathEvent.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/events/UHCPlayerDeathEvent.java @@ -7,19 +7,33 @@ import dev.ninjdai.werewolf.uhc.teams.Team; import dev.ninjdai.werewolf.uhc.teams.Teams; import dev.ninjdai.werewolf.uhc.teams.WinCondition; import io.github.togar2.pvp.events.EntityPreDeathEvent; +import net.kyori.adventure.sound.Sound; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; import net.minestom.server.event.EventFilter; import net.minestom.server.event.EventNode; import net.minestom.server.event.trait.EntityEvent; +import net.minestom.server.sound.SoundEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class UHCPlayerDeathEvent { public static void onPlayerDeath(EntityPreDeathEvent event) { UHCPlayer player = (UHCPlayer) event.getEntity(); - if(event.getDamage().getAttacker() instanceof UHCPlayer killer) { - Team team = Team.teamMap.get(killer.getUHCTeam().getId()); - WinCondition.checkTeamWin(team, event.getInstance().getPlayers()); + if(WinCondition.checkWin(event.getInstance())) { + event.getInstance().playSound(Sound.sound().type(SoundEvent.ENTITY_ENDER_DRAGON_GROWL).source(Sound.Source.PLAYER).build()); + event.getInstance().sendMessage(Component.empty() + .append(Component.text("Team ")) + .append(Component.text(WinCondition.winningTeam.getName(), TextColor.color(WinCondition.winningTeam.getColor()))) + .append(Component.text(" wins !")) + ); } event.setCancelDeath(true); 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 26a58df..c3ea9cc 100644 --- a/src/main/java/dev/ninjdai/werewolf/uhc/teams/WinCondition.java +++ b/src/main/java/dev/ninjdai/werewolf/uhc/teams/WinCondition.java @@ -1,17 +1,21 @@ package dev.ninjdai.werewolf.uhc.teams; -import net.minestom.server.entity.Player; +import dev.ninjdai.werewolf.uhc.UHCPlayer; +import net.minestom.server.entity.GameMode; +import net.minestom.server.instance.Instance; +import org.jetbrains.annotations.NotNull; -import java.util.List; -import java.util.Objects; +import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; public class WinCondition { - public static boolean checkTeamWin(Team team, Set players){ + public static Team winningTeam; + public static boolean checkTeamWin(@NotNull Team team, @NotNull Set players){ switch (team.getWinConditionType()){ case WinConditionType.ONLY_TEAM_REMAINING -> { - for (Player player: players) { - if (! Objects.equals(player.getTag(Team.teamTag), team.getId())) return false; + for (UHCPlayer player: players) { + if (!(player.getUHCTeam() == team)) return false; } return true; } @@ -19,6 +23,23 @@ public class WinCondition { return false; } + public static boolean checkWin(Instance instance) { + Set inGamePlayers = inGamePlayers(instance); + for (Teams team: Teams.values()) { + if(checkTeamWin(team.team, inGamePlayers)){ + winningTeam = team.team; + return true; + } + } + return false; + } + + public static Set inGamePlayers(Instance instance) { + Set players = HashSet.newHashSet(instance.getPlayers().size()); + instance.getPlayers().forEach((player -> players.add((UHCPlayer) player))); + return players.stream().filter((p) -> p.getGameMode()== GameMode.SURVIVAL).collect(Collectors.toSet()); + } + public enum WinConditionType { ONLY_TEAM_REMAINING, }