diff options
author | Myles <43725835+MylesAndMore@users.noreply.github.com> | 2022-12-08 20:53:57 +0000 |
---|---|---|
committer | Myles <43725835+MylesAndMore@users.noreply.github.com> | 2022-12-08 20:53:57 +0000 |
commit | 126b83f1ca45457dc1c9e08c1d0fc30b4fc8b88d (patch) | |
tree | b01df2737380a0ec313d0608cc6f01803a9fe4b9 | |
parent | 673659d38ae4ad90bf86ff94b798960b0e3199df (diff) | |
download | Tumble-126b83f1ca45457dc1c9e08c1d0fc30b4fc8b88d.tar.gz Tumble-126b83f1ca45457dc1c9e08c1d0fc30b4fc8b88d.tar.bz2 Tumble-126b83f1ca45457dc1c9e08c1d0fc30b4fc8b88d.zip |
program autostart logic
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/main/java/com/MylesAndMore/tumble/EventListener.java | 18 | ||||
-rw-r--r-- | src/main/java/com/MylesAndMore/tumble/Game.java | 21 |
3 files changed, 39 insertions, 2 deletions
@@ -41,7 +41,7 @@ once this list is complete and all bugs are fixed, we *should* be ready for rele - [x] add two configs where you can: - [x] set if you want the game to auto-start - [x] set the amt of players you want the game to auto-start at - - [ ] program the auto-start (just add an if statement on the PlayerJoin listener to run the StartGame method on a certain amt of players in the config) + - [x] program the auto-start (just add an if statement on the PlayerJoin listener to run the StartGame method on a certain amt of players in the config) ## etc diff --git a/src/main/java/com/MylesAndMore/tumble/EventListener.java b/src/main/java/com/MylesAndMore/tumble/EventListener.java index a0f5061..8f9ab25 100644 --- a/src/main/java/com/MylesAndMore/tumble/EventListener.java +++ b/src/main/java/com/MylesAndMore/tumble/EventListener.java @@ -35,6 +35,14 @@ public class EventListener implements Listener{ // send them back to the lobby. event.getPlayer().teleport(Bukkit.getWorld(TumbleManager.getLobbyWorld()).getSpawnLocation()); } + // For auto-start function: check if the autoStart is enabled + if (TumbleManager.getPlugin().getConfig().getBoolean("autoStart.enabled")) { + // If so, check if the amount of players has been reached + if (Bukkit.getWorld(TumbleManager.getGameWorld()).getPlayers().size() == TumbleManager.getPlugin().getConfig().getInt("autoStart.players")) { + // The autoStart should begin; pass this to the Game + Game.getGame().autoStart(); + } + } } } @@ -46,6 +54,16 @@ public class EventListener implements Listener{ if (TumbleManager.getPlugin().getConfig().getBoolean("hideJoinLeaveMessages")) { event.setQuitMessage(null); } + // Check if a player left in the lobbyWorld + if (TumbleManager.getLobbyWorld() != null) { + if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { + // Check if the game is in the process of autostarting + if (Objects.equals(Game.getGame().getGameState(), "waiting")) { + // Cancel the autostart + Bukkit.getServer().getScheduler().cancelTask(Game.getGame().getAutoStartID()); + } + } + } } @EventHandler diff --git a/src/main/java/com/MylesAndMore/tumble/Game.java b/src/main/java/com/MylesAndMore/tumble/Game.java index c4d5c1f..8099c80 100644 --- a/src/main/java/com/MylesAndMore/tumble/Game.java +++ b/src/main/java/com/MylesAndMore/tumble/Game.java @@ -38,6 +38,8 @@ public class Game { private String gameState; // Define a variable for the roundType private String roundType; + // Define a variable for the autostart PID + private int autoStartID; // Initialize a new instance of the Random class for use later private final Random Random = new Random(); @@ -143,6 +145,16 @@ public class Game { return true; } + public void autoStart() { + gameState = "waiting"; + displayActionbar(lobbyPlayers, ChatColor.GREEN + "Game will begin in 15 seconds!"); + playSound(lobbyPlayers, Sound.BLOCK_NOTE_BLOCK_CHIME, SoundCategory.BLOCKS, 1, 1); + // Schedule a process to start the game in 300t (15s) and save the PID so we can cancel it later if needed + autoStartID = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(TumbleManager.getPlugin(), () -> { + startGame(TumbleManager.getGameType()); + }, 300); + } + /** * This method should be called on the death of one of the Game's players * @param player The player who died @@ -171,10 +183,17 @@ public class Game { public String getRoundType() { return roundType; } /** - * @return The game's current state as a String ("starting", "running", "complete") + * @return The game's current state as a String ("waiting", "starting", "running", "complete") + * Can also be null if not initialized. */ public String getGameState() { return gameState; } + /** + * @return The Bukkit process ID of the autostart process, if applicable + * Can also be null if not initialized, and -1 if the process failed to schedule. + */ + public int getAutoStartID() { return autoStartID; } + // BEGIN PRIVATE METHODS |