Automatically clear old events

This commit is contained in:
Mathieu Strypsteen 2024-09-15 11:01:29 +02:00
parent fedb9a56a3
commit 1fc7c9f1fc
6 changed files with 16 additions and 14 deletions

View file

@ -7,7 +7,6 @@ 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.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
@ -19,6 +18,7 @@ public abstract class Event implements Listener {
this.team = team; this.team = team;
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);
RandomEventExecutor.currentEvents.add(this);
if (MC13DTL.board.getPlayers(team).isEmpty()) { if (MC13DTL.board.getPlayers(team).isEmpty()) {
eventFailed(); eventFailed();
} }
@ -26,6 +26,7 @@ public abstract class Event implements Listener {
private void eventFinished() { private void eventFinished() {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
RandomEventExecutor.currentEvents.remove(this);
} }
protected void eventSuccess() { protected void eventSuccess() {
@ -46,13 +47,6 @@ public abstract class Event implements Listener {
} }
} }
@EventHandler
public void onPlayerDeath(PlayerQuitEvent event) {
if (MC13DTL.board.getPlayers(team).contains(event.getPlayer())) {
eventFailed();
}
}
protected abstract String getMessage(); protected abstract String getMessage();
protected abstract int scoreAwarded(); protected abstract int scoreAwarded();

View file

@ -3,12 +3,14 @@ package gent.zeus.mc13dtl.events;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import java.util.Arrays; import java.util.Arrays;
import gent.zeus.mc13dtl.events.catalog.TestEvent; import gent.zeus.mc13dtl.events.catalog.TestEvent;
public class RandomEventExecutor { public class RandomEventExecutor {
static List<Class<? extends Event>> events = Arrays.asList(TestEvent.class); static List<Class<? extends Event>> events = Arrays.asList(TestEvent.class);
static List<Event> currentEvents = new ArrayList<>();
public static void executeRandomEvent(Team team) { public static void executeRandomEvent(Team team) {
Random random = new Random(); Random random = new Random();

View file

@ -16,6 +16,10 @@ public class RandomEventRunner implements Runnable {
long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG); long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG);
long day = Bukkit.getWorld("world").getFullTime() / 24000; long day = Bukkit.getWorld("world").getFullTime() / 24000;
if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) { if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) {
while (!RandomEventExecutor.currentEvents.isEmpty()) {
Event event = RandomEventExecutor.currentEvents.get(0);
event.eventFailed();
}
for (Team i : MC13DTL.board.getTeams()) { for (Team i : MC13DTL.board.getTeams()) {
RandomEventExecutor.executeRandomEvent(i); RandomEventExecutor.executeRandomEvent(i);
} }

View file

@ -26,6 +26,6 @@ public class TestEvent extends Event {
@Override @Override
protected int scoreAwarded() { protected int scoreAwarded() {
return 5; return 20;
} }
} }

View file

@ -9,7 +9,8 @@ import org.bukkit.persistence.PersistentDataType;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.command.brigadier.BasicCommand; import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.md_5.bungee.api.ChatColor; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class GameStateCommand implements BasicCommand { public class GameStateCommand implements BasicCommand {
@Override @Override
@ -17,7 +18,7 @@ public class GameStateCommand implements BasicCommand {
World world = Bukkit.getWorld("world"); World world = Bukkit.getWorld("world");
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state"); NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
if (!world.getPersistentDataContainer().get(key, PersistentDataType.STRING).equals("lobby")) { if (!world.getPersistentDataContainer().get(key, PersistentDataType.STRING).equals("lobby")) {
stack.getSender().sendMessage(ChatColor.RED + "Game is already active"); stack.getSender().sendMessage(Component.text("Game is already active").color(NamedTextColor.RED));
return; return;
} }
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active"); world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");

View file

@ -11,7 +11,8 @@ import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
import net.md_5.bungee.api.ChatColor; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class GameStateUtil { public class GameStateUtil {
static void loadWorld(World world) { static void loadWorld(World world) {
@ -41,7 +42,7 @@ public class GameStateUtil {
if (gameState.equals("lobby")) { if (gameState.equals("lobby")) {
player.setGameMode(GameMode.ADVENTURE); player.setGameMode(GameMode.ADVENTURE);
player.setInvulnerable(true); player.setInvulnerable(true);
player.sendMessage(ChatColor.DARK_PURPLE + "Game hasn't started yet, please wait..."); player.sendMessage(Component.text("Game hasn't started yet, please wait...").color(NamedTextColor.DARK_PURPLE));
} }
if (gameState.equals("active")) { if (gameState.equals("active")) {
NamespacedKey processedKey = new NamespacedKey(MC13DTL.instance, "player-processed"); NamespacedKey processedKey = new NamespacedKey(MC13DTL.instance, "player-processed");
@ -51,7 +52,7 @@ public class GameStateUtil {
} }
player.setGameMode(GameMode.SURVIVAL); player.setGameMode(GameMode.SURVIVAL);
player.setInvulnerable(false); player.setInvulnerable(false);
player.sendMessage(ChatColor.DARK_PURPLE + "Game is active, have fun!"); player.sendMessage(Component.text("Game is active, have fun!").color(NamedTextColor.DARK_PURPLE));
} }
} }