Optimize game

This commit is contained in:
Mathieu Strypsteen 2024-09-15 12:31:29 +02:00
parent 091b95ea2e
commit db1dd18051
2 changed files with 28 additions and 21 deletions

View file

@ -1,7 +1,5 @@
package gent.zeus.mc13dtl.gamestate;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -11,9 +9,8 @@ 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 gent.zeus.mc13dtl.gamestate.GameStateUtil.GameState;
import io.papermc.paper.event.entity.EntityMoveEvent;
public class GameStateHandler implements Listener {
@ -32,9 +29,7 @@ public class GameStateHandler implements Listener {
}
private boolean shouldCancel() {
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
return gameState.equals("lobby") || gameState.equals("paused");
return GameStateUtil.gameState == GameState.LOBBY || GameStateUtil.gameState == GameState.PAUSED;
}
@EventHandler
@ -60,16 +55,14 @@ public class GameStateHandler implements Listener {
@EventHandler
public void onEntityMove(EntityMoveEvent event) {
if (shouldCancel()) {
if (shouldCancel() && event.getTo().getY() >= event.getFrom().getY()) {
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()) {
if (GameStateUtil.gameState == GameState.PAUSED && event.getTo().getY() >= event.getFrom().getY()) {
event.setCancelled(true);
}
}

View file

@ -1,6 +1,5 @@
package gent.zeus.mc13dtl.gamestate;
import org.bukkit.Bukkit;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.GameRule;
@ -14,6 +13,12 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class GameStateUtil {
public enum GameState {
LOBBY, ACTIVE, PAUSED
};
static GameState gameState = GameState.LOBBY;
static void loadWorld(World world) {
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
if (!world.getPersistentDataContainer().has(key)) {
@ -21,19 +26,30 @@ public class GameStateUtil {
NamespacedKey eventKey = new NamespacedKey(MC13DTL.instance, "last-event-day");
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, -1L);
}
String gameState = world.getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (gameState.equals("lobby")) {
String gameStateStr = world.getPersistentDataContainer().get(key, PersistentDataType.STRING);
switch (gameStateStr) {
case "lobby":
gameState = GameState.LOBBY;
break;
case "active":
gameState = GameState.ACTIVE;
break;
case "paused":
gameState = GameState.PAUSED;
break;
}
if (gameState == GameState.LOBBY) {
world.setDifficulty(Difficulty.PEACEFUL);
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
world.setFullTime(0);
world.getWorldBorder().setSize(200);
}
if (gameState.equals("active")) {
if (gameState == GameState.ACTIVE) {
world.setDifficulty(Difficulty.NORMAL);
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, true);
world.getWorldBorder().setSize(10000);
}
if (gameState.equals("paused")) {
if (gameState == GameState.PAUSED) {
world.setDifficulty(Difficulty.NORMAL);
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
world.getWorldBorder().setSize(10000);
@ -41,17 +57,15 @@ public class GameStateUtil {
}
static void loadPlayer(Player player) {
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "game-state");
String gameState = Bukkit.getWorld("world").getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (gameState.equals("lobby")) {
if (gameState == GameState.LOBBY) {
player.setGameMode(GameMode.ADVENTURE);
player.sendMessage(Component.text("Game hasn't started yet, please wait...").color(NamedTextColor.DARK_PURPLE));
}
if (gameState.equals("active")) {
if (gameState == GameState.ACTIVE) {
player.setGameMode(GameMode.SURVIVAL);
player.sendMessage(Component.text("Game is active, have fun!").color(NamedTextColor.DARK_PURPLE));
}
if (gameState.equals("paused")) {
if (gameState == GameState.PAUSED) {
player.setGameMode(GameMode.ADVENTURE);
player.sendMessage(Component.text("Game is currently paused").color(NamedTextColor.DARK_PURPLE));
}