Add scheduled task to events

This commit is contained in:
Mathieu Strypsteen 2024-09-15 21:32:47 +02:00
parent 1f3dfa2d34
commit d53f9461bf
2 changed files with 11 additions and 2 deletions

View file

@ -7,13 +7,14 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
public abstract class Event implements Listener, Runnable {
public abstract class Event implements Listener {
protected Team team; protected Team team;
private int taskId;
protected final Difficulty difficulty; protected final Difficulty difficulty;
public Event(Team team, Difficulty difficulty) { public Event(Team team, Difficulty difficulty) {
@ -22,6 +23,8 @@ public abstract class Event implements Listener {
MC13DTL.board.sendMessageToTeam(team, Component.text("Difficulty is " + this.difficulty.toString())); MC13DTL.board.sendMessageToTeam(team, Component.text("Difficulty is " + this.difficulty.toString()));
MC13DTL.board.sendMessageToTeam(team, Component.text(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC)); MC13DTL.board.sendMessageToTeam(team, Component.text(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC));
MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance); MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance);
BukkitScheduler scheduler = MC13DTL.instance.getServer().getScheduler();
taskId = scheduler.runTaskTimer(MC13DTL.instance, this, 0, 20).getTaskId();
EventRunner.currentEvents.add(this); EventRunner.currentEvents.add(this);
if (MC13DTL.board.getPlayers(team).isEmpty()) { if (MC13DTL.board.getPlayers(team).isEmpty()) {
eventFailed(); eventFailed();
@ -30,6 +33,7 @@ public abstract class Event implements Listener {
private void eventFinished() { private void eventFinished() {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
MC13DTL.instance.getServer().getScheduler().cancelTask(taskId);
EventRunner.currentEvents.remove(this); EventRunner.currentEvents.remove(this);
} }
@ -51,6 +55,10 @@ public abstract class Event implements Listener {
} }
} }
@Override
public void run() {
}
protected abstract String getMessage(); protected abstract String getMessage();
protected abstract int scoreAwarded(); protected abstract int scoreAwarded();

View file

@ -38,6 +38,7 @@ public class GameStateUtil {
gameState = GameState.PAUSED; gameState = GameState.PAUSED;
break; break;
} }
world.setGameRule(GameRule.KEEP_INVENTORY, true);
if (gameState == GameState.LOBBY) { if (gameState == GameState.LOBBY) {
world.setDifficulty(Difficulty.PEACEFUL); world.setDifficulty(Difficulty.PEACEFUL);
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false); world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);