feat: better win checking and test pllayer kill command

This commit is contained in:
Ninjdai 2024-09-29 08:52:01 +02:00
parent 66fd30d8b3
commit eb0f26d371
5 changed files with 58 additions and 17 deletions

View File

@ -1,6 +1,7 @@
package dev.ninjdai.werewolf; package dev.ninjdai.werewolf;
import dev.ninjdai.werewolf.commands.LGCommand; import dev.ninjdai.werewolf.commands.LGCommand;
import dev.ninjdai.werewolf.commands.PlayerKillCommand;
import dev.ninjdai.werewolf.uhc.UHCPlayer; import dev.ninjdai.werewolf.uhc.UHCPlayer;
import dev.ninjdai.werewolf.uhc.events.UHCPlayerDeathEvent; import dev.ninjdai.werewolf.uhc.events.UHCPlayerDeathEvent;
import io.github.togar2.pvp.MinestomPvP; import io.github.togar2.pvp.MinestomPvP;
@ -43,7 +44,7 @@ public class Main {
player.setRespawnPoint(new Pos(0, 42, 0)); player.setRespawnPoint(new Pos(0, 42, 0));
}); });
MinecraftServer.getCommandManager().register(new LGCommand()); MinecraftServer.getCommandManager().register(new LGCommand(), new PlayerKillCommand());
MinestomPvP.init(); MinestomPvP.init();
CombatFeatureSet legacyVanilla = CombatFeatures.legacyVanilla(); CombatFeatureSet legacyVanilla = CombatFeatures.legacyVanilla();
@ -55,7 +56,7 @@ public class Main {
OpenToLAN.open(); OpenToLAN.open();
MinecraftServer.setBrandName("Playground"); MinecraftServer.setBrandName("Playground");
VelocityProxy.enable("cMkrrJ8tdUS6"); //VelocityProxy.enable("cMkrrJ8tdUS6");
minecraftServer.start("0.0.0.0", 30065); minecraftServer.start("0.0.0.0", 30065);
} }

View File

@ -0,0 +1,29 @@
package dev.ninjdai.werewolf.commands;
import dev.ninjdai.werewolf.uhc.UHCPlayer;
import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.arguments.ArgumentType;
import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntity;
import net.minestom.server.entity.damage.Damage;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.utils.entity.EntityFinder;
public class PlayerKillCommand extends Command {
static final ArgumentEntity singlePlayerArgument = ArgumentType.Entity("player").onlyPlayers(true).singleEntity(true);
public PlayerKillCommand() {
super("playerkill");
addSyntax(((sender, context) -> {
if(! (sender instanceof UHCPlayer senderPlayer)) return;
EntityFinder finder = context.get(singlePlayerArgument);
UHCPlayer player = (UHCPlayer) finder.findFirstPlayer(sender);
if (player == null) {
sender.sendMessage("Couldn't find player");
return;
}
player.damage(new Damage(DamageType.MOB_ATTACK, senderPlayer, senderPlayer, senderPlayer.getPosition(), 40));
}), singlePlayerArgument);
}
}

View File

@ -4,7 +4,10 @@ import dev.ninjdai.werewolf.uhc.UHCPlayer;
import dev.ninjdai.werewolf.uhc.roles.Role; import dev.ninjdai.werewolf.uhc.roles.Role;
import dev.ninjdai.werewolf.uhc.roles.RoleProperties; import dev.ninjdai.werewolf.uhc.roles.RoleProperties;
import dev.ninjdai.werewolf.uhc.roles.Roles; import dev.ninjdai.werewolf.uhc.roles.Roles;
import dev.ninjdai.werewolf.uhc.teams.WinCondition;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos; import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
@ -13,11 +16,13 @@ import net.minestom.server.entity.Player;
import net.minestom.server.entity.damage.Damage; import net.minestom.server.entity.damage.Damage;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.sound.SoundEvent;
import net.minestom.server.timer.Task; import net.minestom.server.timer.Task;
import net.minestom.server.timer.TaskSchedule; import net.minestom.server.timer.TaskSchedule;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.Duration; import java.time.Duration;
import java.util.Set;
public class TimedPlayerDeath { public class TimedPlayerDeath {
private final ItemStack[] inventory; private final ItemStack[] inventory;
@ -25,12 +30,14 @@ public class TimedPlayerDeath {
private final Pos deathPos; private final Pos deathPos;
private final UHCPlayer targetPlayer; private final UHCPlayer targetPlayer;
private final Task triggerTask; private final Task triggerTask;
private final Set<UHCPlayer> playersAtDeath;
public TimedPlayerDeath(@NotNull Player player, @NotNull Damage damage, int seconds) { public TimedPlayerDeath(@NotNull Player player, @NotNull Damage damage, int seconds) {
this.inventory = player.getInventory().getItemStacks(); this.inventory = player.getInventory().getItemStacks();
this.instance = player.getInstance(); this.instance = player.getInstance();
this.deathPos = player.getPosition(); this.deathPos = player.getPosition();
this.targetPlayer = (UHCPlayer) player; this.targetPlayer = (UHCPlayer) player;
this.playersAtDeath = WinCondition.inGamePlayers(instance);
this.triggerTask = MinecraftServer.getSchedulerManager().scheduleTask(this::trigger, TaskSchedule.seconds(seconds)); this.triggerTask = MinecraftServer.getSchedulerManager().scheduleTask(this::trigger, TaskSchedule.seconds(seconds));
} }
@ -40,16 +47,26 @@ public class TimedPlayerDeath {
} }
private TaskSchedule trigger(){ private TaskSchedule trigger(){
if(!targetPlayer.getUHCRole().getRoleProperties().contains(RoleProperties.NO_DEATH_MESSAGE)) {
this.instance.sendMessage(Component.text(targetPlayer.getUsername()).append(Component.text(" died")));
}
if(WinCondition.checkWin(playersAtDeath)) {
instance.playSound(Sound.sound().type(SoundEvent.ENTITY_ENDER_DRAGON_GROWL).source(Sound.Source.PLAYER).build());
instance.sendMessage(Component.empty()
.append(Component.text("Team "))
.append(Component.text(WinCondition.winningTeam.getName(), TextColor.color(WinCondition.winningTeam.getColor())))
.append(Component.text(" wins !"))
);
return TaskSchedule.stop();
}
for (ItemStack itemStack: inventory) { for (ItemStack itemStack: inventory) {
ItemEntity e = new ItemEntity(itemStack); ItemEntity e = new ItemEntity(itemStack);
e.setPickupDelay(Duration.ofSeconds(1)); e.setPickupDelay(Duration.ofSeconds(1));
e.setInstance(this.instance, deathPos); e.setInstance(this.instance, deathPos);
} }
if(!targetPlayer.getUHCRole().getRoleProperties().contains(RoleProperties.NO_DEATH_MESSAGE)) {
this.instance.sendMessage(Component.text(targetPlayer.getUsername()).append(Component.text(" died")));
}
return TaskSchedule.stop(); return TaskSchedule.stop();
} }
} }

View File

@ -27,19 +27,10 @@ import java.util.stream.Stream;
public class UHCPlayerDeathEvent { public class UHCPlayerDeathEvent {
public static void onPlayerDeath(EntityPreDeathEvent event) { public static void onPlayerDeath(EntityPreDeathEvent event) {
UHCPlayer player = (UHCPlayer) event.getEntity(); UHCPlayer player = (UHCPlayer) event.getEntity();
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); event.setCancelDeath(true);
player.toggleSpectating(); player.toggleSpectating();
var timedPlayerDeath = new TimedPlayerDeath(player, event.getDamage(), 15); var timedPlayerDeath = new TimedPlayerDeath(player, event.getDamage(), 15);
player.getUHCRole().onPlayerDeath(player, timedPlayerDeath); player.getUHCRole().onPlayerDeath(player, timedPlayerDeath);

View File

@ -1,5 +1,6 @@
package dev.ninjdai.werewolf.uhc.teams; package dev.ninjdai.werewolf.uhc.teams;
import dev.ninjdai.werewolf.Main;
import dev.ninjdai.werewolf.uhc.UHCPlayer; import dev.ninjdai.werewolf.uhc.UHCPlayer;
import net.minestom.server.entity.GameMode; import net.minestom.server.entity.GameMode;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
@ -23,11 +24,13 @@ public class WinCondition {
return false; return false;
} }
public static boolean checkWin(Instance instance) { public static boolean checkWin(Set<UHCPlayer> inGamePlayers) {
Set<UHCPlayer> inGamePlayers = inGamePlayers(instance); 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());
if(checkTeamWin(team.team, inGamePlayers)){ if(checkTeamWin(team.team, inGamePlayers)){
winningTeam = team.team; winningTeam = team.team;
Main.LOGGER.info("Team {} wins !", team.team.getName());
return true; return true;
} }
} }