feat: better timed death handling

This commit is contained in:
Ninjdai 2024-09-28 09:12:25 +02:00
parent 177d35f097
commit 116515a598
3 changed files with 13 additions and 4 deletions

View File

@ -1,5 +1,6 @@
package dev.ninjdai.werewolf;
import dev.ninjdai.werewolf.uhc.events.UHCPlayerDeathEvent;
import io.github.togar2.pvp.MinestomPvP;
import io.github.togar2.pvp.feature.CombatFeatureSet;
import io.github.togar2.pvp.feature.CombatFeatures;
@ -34,6 +35,8 @@ public class Main {
CombatFeatureSet legacyVanilla = CombatFeatures.legacyVanilla();
MinecraftServer.getGlobalEventHandler().addChild(legacyVanilla.createNode());
UHCPlayerDeathEvent.register();
minecraftServer.start("0.0.0.0", 30065);
}
}

View File

@ -10,6 +10,7 @@ import net.minestom.server.entity.Player;
import net.minestom.server.entity.damage.Damage;
import net.minestom.server.instance.Instance;
import net.minestom.server.item.ItemStack;
import net.minestom.server.timer.Task;
import net.minestom.server.timer.TaskSchedule;
import org.jetbrains.annotations.NotNull;
@ -17,13 +18,18 @@ public class TimedPlayerDeath {
private final ItemStack[] inventory;
private final Instance instance;
private final Player targetPlayer;
private final Task triggerTask;
public TimedPlayerDeath(@NotNull Player player, @NotNull Damage damage, int seconds) {
this.inventory = player.getInventory().getItemStacks();
this.instance = player.getInstance();
this.targetPlayer = player;
MinecraftServer.getSchedulerManager().scheduleTask(this::trigger, TaskSchedule.seconds(seconds));
this.triggerTask = MinecraftServer.getSchedulerManager().scheduleTask(this::trigger, TaskSchedule.seconds(seconds));
}
public void cancel() {
this.triggerTask.cancel();
}
private TaskSchedule trigger(){

View File

@ -11,7 +11,7 @@ import net.minestom.server.event.EventNode;
import net.minestom.server.event.trait.EntityEvent;
public class UHCPlayerDeathEvent {
public void onPlayerDeath(EntityPreDeathEvent event) {
public static void onPlayerDeath(EntityPreDeathEvent event) {
Player player = (Player) event.getEntity();
if(event.getDamage().getAttacker() instanceof Player killer && !killer.getTag(Team.teamTag).isBlank()) {
Team team = Team.teamMap.get(killer.getTag(Team.teamTag));
@ -21,10 +21,10 @@ public class UHCPlayerDeathEvent {
event.setCancelDeath(true);
player.setGameMode(GameMode.SPECTATOR);
new TimedPlayerDeath(player, event.getDamage(), 15);
var timedPlayerDeath = new TimedPlayerDeath(player, event.getDamage(), 15);
}
public void register() {
public static void register() {
EventNode<EntityEvent> playerNode = EventNode.type("player-listener", EventFilter.ENTITY);
MinecraftServer.getGlobalEventHandler().addChild(playerNode);