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; package dev.ninjdai.werewolf;
import dev.ninjdai.werewolf.uhc.events.UHCPlayerDeathEvent;
import io.github.togar2.pvp.MinestomPvP; import io.github.togar2.pvp.MinestomPvP;
import io.github.togar2.pvp.feature.CombatFeatureSet; import io.github.togar2.pvp.feature.CombatFeatureSet;
import io.github.togar2.pvp.feature.CombatFeatures; import io.github.togar2.pvp.feature.CombatFeatures;
@ -34,6 +35,8 @@ public class Main {
CombatFeatureSet legacyVanilla = CombatFeatures.legacyVanilla(); CombatFeatureSet legacyVanilla = CombatFeatures.legacyVanilla();
MinecraftServer.getGlobalEventHandler().addChild(legacyVanilla.createNode()); MinecraftServer.getGlobalEventHandler().addChild(legacyVanilla.createNode());
UHCPlayerDeathEvent.register();
minecraftServer.start("0.0.0.0", 30065); 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.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.timer.Task;
import net.minestom.server.timer.TaskSchedule; import net.minestom.server.timer.TaskSchedule;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -17,13 +18,18 @@ public class TimedPlayerDeath {
private final ItemStack[] inventory; private final ItemStack[] inventory;
private final Instance instance; private final Instance instance;
private final Player targetPlayer; private final Player targetPlayer;
private final Task triggerTask;
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.targetPlayer = player; 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(){ private TaskSchedule trigger(){

View File

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