Simplify game state and allow pausing
This commit is contained in:
parent
8d0acad407
commit
091b95ea2e
5 changed files with 96 additions and 26 deletions
|
@ -8,8 +8,9 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.bukkit.scheduler.BukkitScheduler;
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
|
||||||
import gent.zeus.mc13dtl.events.RandomEventRunner;
|
import gent.zeus.mc13dtl.events.RandomEventRunner;
|
||||||
import gent.zeus.mc13dtl.gamestate.GameStateCommand;
|
|
||||||
import gent.zeus.mc13dtl.gamestate.GameStateHandler;
|
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.command.brigadier.Commands;
|
||||||
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
|
||||||
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||||
|
@ -25,7 +26,8 @@ public class MC13DTL extends JavaPlugin {
|
||||||
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
|
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
|
||||||
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
||||||
Commands commands = event.registrar();
|
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-add", "Add player to team", new GroupAddCommand());
|
||||||
commands.register("group-remove", "Remove player from team", new GroupRemoveCommand());
|
commands.register("group-remove", "Remove player from team", new GroupRemoveCommand());
|
||||||
commands.register("group-difficulty", "Set group difficulty", new GroupDifficultyCommand());
|
commands.register("group-difficulty", "Set group difficulty", new GroupDifficultyCommand());
|
||||||
|
|
|
@ -5,12 +5,16 @@ import org.bukkit.NamespacedKey;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
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.PlayerInteractEvent;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
import org.bukkit.event.world.WorldLoadEvent;
|
import org.bukkit.event.world.WorldLoadEvent;
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
|
||||||
import gent.zeus.mc13dtl.MC13DTL;
|
import gent.zeus.mc13dtl.MC13DTL;
|
||||||
|
import io.papermc.paper.event.entity.EntityMoveEvent;
|
||||||
|
|
||||||
public class GameStateHandler implements Listener {
|
public class GameStateHandler implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
@ -27,11 +31,45 @@ public class GameStateHandler implements Listener {
|
||||||
GameStateUtil.loadWorld(world);
|
GameStateUtil.loadWorld(world);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
private boolean shouldCancel() {
|
||||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
|
||||||
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
|
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
|
||||||
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
|
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);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import org.bukkit.NamespacedKey;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
import org.bukkit.potion.PotionEffect;
|
|
||||||
|
|
||||||
import gent.zeus.mc13dtl.MC13DTL;
|
import gent.zeus.mc13dtl.MC13DTL;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
@ -34,6 +33,11 @@ public class GameStateUtil {
|
||||||
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
|
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
|
||||||
world.getWorldBorder().setSize(10000);
|
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) {
|
static void loadPlayer(Player player) {
|
||||||
|
@ -41,30 +45,15 @@ public class GameStateUtil {
|
||||||
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
|
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
|
||||||
if (gameState.equals("lobby")) {
|
if (gameState.equals("lobby")) {
|
||||||
player.setGameMode(GameMode.ADVENTURE);
|
player.setGameMode(GameMode.ADVENTURE);
|
||||||
player.setInvulnerable(true);
|
|
||||||
player.sendMessage(Component.text("Game hasn't started yet, please wait...").color(NamedTextColor.DARK_PURPLE));
|
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");
|
|
||||||
if (!player.getPersistentDataContainer().getOrDefault(processedKey, PersistentDataType.BOOLEAN, false)) {
|
|
||||||
processPlayer(player);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
player.setGameMode(GameMode.SURVIVAL);
|
player.setGameMode(GameMode.SURVIVAL);
|
||||||
player.setInvulnerable(false);
|
|
||||||
player.sendMessage(Component.text("Game is active, have fun!").color(NamedTextColor.DARK_PURPLE));
|
player.sendMessage(Component.text("Game is active, have fun!").color(NamedTextColor.DARK_PURPLE));
|
||||||
}
|
}
|
||||||
}
|
if (gameState.equals("paused")) {
|
||||||
|
player.setGameMode(GameMode.ADVENTURE);
|
||||||
static void processPlayer(Player player) {
|
player.sendMessage(Component.text("Game is currently paused").color(NamedTextColor.DARK_PURPLE));
|
||||||
player.getInventory().clear();
|
}
|
||||||
player.heal(100);
|
|
||||||
player.getActivePotionEffects().clear();
|
|
||||||
for (PotionEffect effect : player.getActivePotionEffects()) {
|
|
||||||
player.removePotionEffect(effect.getType());
|
|
||||||
}
|
|
||||||
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "player-processed");
|
|
||||||
player.getPersistentDataContainer().set(key, PersistentDataType.BOOLEAN, true);
|
|
||||||
loadPlayer(player);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
public class GameStateCommand implements BasicCommand {
|
public class StartGameCommand implements BasicCommand {
|
||||||
@Override
|
@Override
|
||||||
public void execute(CommandSourceStack stack, String[] args) {
|
public void execute(CommandSourceStack stack, String[] args) {
|
||||||
World world = Bukkit.getWorld("world");
|
World world = Bukkit.getWorld("world");
|
||||||
|
@ -24,7 +24,7 @@ public class GameStateCommand implements BasicCommand {
|
||||||
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");
|
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");
|
||||||
GameStateUtil.loadWorld(world);
|
GameStateUtil.loadWorld(world);
|
||||||
for (Player i : Bukkit.getOnlinePlayers()) {
|
for (Player i : Bukkit.getOnlinePlayers()) {
|
||||||
GameStateUtil.processPlayer(i);
|
GameStateUtil.loadPlayer(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue