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.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL;
public abstract class Event implements Listener {
public abstract class Event implements Listener, Runnable {
protected Team team;
private int taskId;
protected final 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(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC));
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);
if (MC13DTL.board.getPlayers(team).isEmpty()) {
eventFailed();
@ -30,6 +33,7 @@ public abstract class Event implements Listener {
private void eventFinished() {
HandlerList.unregisterAll(this);
MC13DTL.instance.getServer().getScheduler().cancelTask(taskId);
EventRunner.currentEvents.remove(this);
}
@ -51,6 +55,10 @@ public abstract class Event implements Listener {
}
}
@Override
public void run() {
}
protected abstract String getMessage();
protected abstract int scoreAwarded();

View file

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