Simplify game state and allow pausing

This commit is contained in:
Mathieu Strypsteen 2024-09-15 12:06:53 +02:00
parent 8d0acad407
commit 091b95ea2e
5 changed files with 96 additions and 26 deletions

View file

@ -8,8 +8,9 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import gent.zeus.mc13dtl.events.RandomEventRunner;
import gent.zeus.mc13dtl.gamestate.GameStateCommand;
import gent.zeus.mc13dtl.gamestate.GameStateHandler;
import gent.zeus.mc13dtl.gamestate.StartGameCommand;
import gent.zeus.mc13dtl.gamestate.TogglePauseCommand;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
@ -25,7 +26,8 @@ public class MC13DTL extends JavaPlugin {
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Commands commands = event.registrar();
commands.register("start-game", "Start a game of 13 days to live", new GameStateCommand());
commands.register("start-game", "Start a game of 13 days to live", new StartGameCommand());
commands.register("toggle-pause", "Toggle paused state", new TogglePauseCommand());
commands.register("group-add", "Add player to team", new GroupAddCommand());
commands.register("group-remove", "Remove player from team", new GroupRemoveCommand());
commands.register("group-difficulty", "Set group difficulty", new GroupDifficultyCommand());

View file

@ -5,12 +5,16 @@ import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.persistence.PersistentDataType;
import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.event.entity.EntityMoveEvent;
public class GameStateHandler implements Listener {
@EventHandler
@ -27,11 +31,45 @@ public class GameStateHandler implements Listener {
GameStateUtil.loadWorld(world);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
private boolean shouldCancel() {
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (gameState.equals("lobby")) {
return gameState.equals("lobby") || gameState.equals("paused");
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (shouldCancel()) {
event.setCancelled(true);
}
}
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (shouldCancel()) {
event.setCancelled(true);
}
}
@EventHandler
public void onEntityPickupItem(EntityPickupItemEvent event) {
if (shouldCancel()) {
event.setCancelled(true);
}
}
@EventHandler
public void onEntityMove(EntityMoveEvent event) {
if (shouldCancel()) {
event.setCancelled(true);
}
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (gameState.equals("paused") && event.getTo().getY() >= event.getFrom().getY()) {
event.setCancelled(true);
}
}

View file

@ -8,7 +8,6 @@ import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import gent.zeus.mc13dtl.MC13DTL;
import net.kyori.adventure.text.Component;
@ -34,6 +33,11 @@ public class GameStateUtil {
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
world.getWorldBorder().setSize(10000);
}
if (gameState.equals("paused")) {
world.setDifficulty(Difficulty.NORMAL);
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
world.getWorldBorder().setSize(10000);
}
}
static void loadPlayer(Player player) {
@ -41,30 +45,15 @@ public class GameStateUtil {
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (gameState.equals("lobby")) {
player.setGameMode(GameMode.ADVENTURE);
player.setInvulnerable(true);
player.sendMessage(Component.text("Game hasn't started yet, please wait...").color(NamedTextColor.DARK_PURPLE));
}
if (gameState.equals("active")) {
NamespacedKey processedKey = new NamespacedKey(MC13DTL.instance, "player-processed");
if (!player.getPersistentDataContainer().getOrDefault(processedKey, PersistentDataType.BOOLEAN, false)) {
processPlayer(player);
return;
}
player.setGameMode(GameMode.SURVIVAL);
player.setInvulnerable(false);
player.sendMessage(Component.text("Game is active, have fun!").color(NamedTextColor.DARK_PURPLE));
}
}
static void processPlayer(Player player) {
player.getInventory().clear();
player.heal(100);
player.getActivePotionEffects().clear();
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
if (gameState.equals("paused")) {
player.setGameMode(GameMode.ADVENTURE);
player.sendMessage(Component.text("Game is currently paused").color(NamedTextColor.DARK_PURPLE));
}
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "player-processed");
player.getPersistentDataContainer().set(key, PersistentDataType.BOOLEAN, true);
loadPlayer(player);
}
}

View file

@ -12,7 +12,7 @@ import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class GameStateCommand implements BasicCommand {
public class StartGameCommand implements BasicCommand {
@Override
public void execute(CommandSourceStack stack, String[] args) {
World world = Bukkit.getWorld("world");
@ -24,7 +24,7 @@ public class GameStateCommand implements BasicCommand {
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");
GameStateUtil.loadWorld(world);
for (Player i : Bukkit.getOnlinePlayers()) {
GameStateUtil.processPlayer(i);
GameStateUtil.loadPlayer(i);
}
}

View file

@ -0,0 +1,41 @@
package gent.zeus.mc13dtl.gamestate;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class TogglePauseCommand implements BasicCommand {
@Override
public void execute(CommandSourceStack stack, String[] args) {
World world = Bukkit.getWorld("world");
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
if (world.getPersistentDataContainer().get(key, PersistentDataType.STRING).equals("active")) {
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "paused");
GameStateUtil.loadWorld(world);
for (Player i : Bukkit.getOnlinePlayers()) {
GameStateUtil.loadPlayer(i);
}
} else if (world.getPersistentDataContainer().get(key, PersistentDataType.STRING).equals("paused")) {
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");
GameStateUtil.loadWorld(world);
for (Player i : Bukkit.getOnlinePlayers()) {
GameStateUtil.loadPlayer(i);
}
} else {
stack.getSender().sendMessage(Component.text("Game is not started yet").color(NamedTextColor.RED));
}
}
@Override
public String permission() {
return "13dtl.manage-game";
}
}