feat: better win checking and win message/sound

This commit is contained in:
Ninjdai 2024-09-28 22:04:17 +02:00
parent 897dd837ad
commit 66fd30d8b3
2 changed files with 44 additions and 9 deletions

View File

@ -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);

View File

@ -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<Player> players){
public static Team winningTeam;
public static boolean checkTeamWin(@NotNull Team team, @NotNull Set<UHCPlayer> 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<UHCPlayer> inGamePlayers = inGamePlayers(instance);
for (Teams team: Teams.values()) {
if(checkTeamWin(team.team, inGamePlayers)){
winningTeam = team.team;
return true;
}
}
return false;
}
public static Set<UHCPlayer> inGamePlayers(Instance instance) {
Set<UHCPlayer> 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,
}