From c21a023cf5a8bb7a8f6337195f4d10f2ddecf40d Mon Sep 17 00:00:00 2001 From: Om Date: Wed, 31 Jul 2024 00:15:30 +0530 Subject: [PATCH] Implemented MinPlayers feature where arena starts the countdown on reaching minplayers --- pom.xml | 2 +- .../blockdropminigame/arena/Arena.java | 9 ++ .../blockdropminigame/arena/ArenaUtils.java | 3 + .../SettingsCommandHandler.java | 84 ++++++++++++++----- .../commands/BlockDropTabCompleter.java | 4 +- .../game/GameStateHandler.java | 2 +- .../game/JoinLeaveHandler.java | 2 +- .../listeners/PlayerQuitListener.java | 4 +- 8 files changed, 85 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index bca2da0..7cef2fb 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.ojasislive blockdropminigame - 1.0-SNAPSHOT + 1.1.0-SNAPSHOT jar Blockdropminigame diff --git a/src/main/java/me/ojasislive/blockdropminigame/arena/Arena.java b/src/main/java/me/ojasislive/blockdropminigame/arena/Arena.java index d98062d..1976d05 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/arena/Arena.java +++ b/src/main/java/me/ojasislive/blockdropminigame/arena/Arena.java @@ -38,9 +38,18 @@ public static Arena createArena(String arenaName, World arenaWorld, Location min private Location minLocation; private Location maxLocation; private Location lobbyLocation; + private int minPlayersLimit; private int maxPlayersLimit; private String schematicFilePath; // Add this field to store the schematic file path + public int getMinPlayersLimit() { + return minPlayersLimit; + } + + public void setMinPlayersLimit(int minPlayersLimit) { + this.minPlayersLimit = minPlayersLimit; + } + public Location getLobbyLocation() { return lobbyLocation; } diff --git a/src/main/java/me/ojasislive/blockdropminigame/arena/ArenaUtils.java b/src/main/java/me/ojasislive/blockdropminigame/arena/ArenaUtils.java index a2e778b..b934791 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/arena/ArenaUtils.java +++ b/src/main/java/me/ojasislive/blockdropminigame/arena/ArenaUtils.java @@ -99,6 +99,7 @@ public static void saveArenas() { arenasConfig.set(path + ".spawnLocations", serializeLocations(arena.getSpawnLocations())); arenasConfig.set(path + ".schematicFilePath", arena.getSchematicFilePath()); // Save the schematic file path arenasConfig.set(path + ".maxplayers",arena.getMaxPlayersLimit()); + arenasConfig.set(path + ".minplayers",arena.getMinPlayersLimit()); arenasConfig.set(path + ".active",arena.isActive()); Bukkit.getLogger().info(ChatColor.AQUA+"[Blockdrop-Minigame] Saved Arena "+arena.getArenaName()); } @@ -123,6 +124,7 @@ private static void loadArenas() { } int maxplayers = arenasConfig.getInt(path + ".maxplayers"); + int minplayers = arenasConfig.getInt(path + ".minplayers",maxplayers); boolean active = Boolean.parseBoolean(arenasConfig.getString(path + ".active")); List spawnLocations = deserializeLocations(arenasConfig.getStringList(path + ".spawnLocations")); @@ -185,6 +187,7 @@ private static void loadArenas() { } Arena arena = Arena.createArena(arenaName, world, minLocation, maxLocation); arena.setMaxPlayersLimit(maxplayers); + arena.setMinPlayersLimit(minplayers); arena.setLobbyLocation(lobbyLocation); arena.setState(ArenaState.WAITING); arena.setSpawnLocations(spawnLocations); diff --git a/src/main/java/me/ojasislive/blockdropminigame/commandFunctionHandlers/SettingsCommandHandler.java b/src/main/java/me/ojasislive/blockdropminigame/commandFunctionHandlers/SettingsCommandHandler.java index 9822a93..741ace5 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/commandFunctionHandlers/SettingsCommandHandler.java +++ b/src/main/java/me/ojasislive/blockdropminigame/commandFunctionHandlers/SettingsCommandHandler.java @@ -3,13 +3,16 @@ import me.ojasislive.blockdropminigame.arena.Arena; import me.ojasislive.blockdropminigame.arena.ArenaUtils; import me.ojasislive.blockdropminigame.game.ArenaState; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.TextComponent; +import me.ojasislive.blockdropminigame.game.JoinLeaveHandler; +import net.md_5.bungee.api.chat.*; +import net.md_5.bungee.api.chat.hover.content.Text; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; public class SettingsCommandHandler { @@ -61,23 +64,30 @@ private void handleGet(Player sender, Arena arena, String setting) { case "spawnlocations": sender.sendMessage(ChatColor.GREEN + "Spawn Locations: "); int countLocation = 0; - TextComponent message = new TextComponent("[TP]"); - message.setColor(net.md_5.bungee.api.ChatColor.UNDERLINE); - message.setColor(net.md_5.bungee.api.ChatColor.LIGHT_PURPLE); - for (Location location : arena.getSpawnLocations()){ - message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND,"/bt " + for (Location location : arena.getSpawnLocations()) { + TextComponent message = new TextComponent("[TP]"); + message.setColor(net.md_5.bungee.api.ChatColor.UNDERLINE); + message.setColor(net.md_5.bungee.api.ChatColor.LIGHT_PURPLE); + message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click to teleport to the location"))); + message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/bt " + sender.getUniqueId() + " " + - arena.getArenaWorld().toString() + " " + + arena.getArenaWorld().getName() + " " + location.getX() + " " + location.getY() + " " + location.getZ() + " " + - location.getYaw()+ " " + + location.getYaw() + " " + location.getPitch())); - String locationString = arena.getArenaWorld().toString() + "," + + + String locationString = arena.getArenaWorld().getName() + "," + ((int) location.getX()) + "," + ((int) location.getY()) + "," + - ((int) location.getZ()) + ", "+message; - sender.sendMessage(ChatColor.GOLD +""+ countLocation +": "+ ChatColor.GREEN +locationString); + ((int) location.getZ()) + ", "; + + BaseComponent[] combinedMessage = new ComponentBuilder(ChatColor.GOLD + "" + countLocation + ": " + ChatColor.GREEN + locationString) + .append(message) + .create(); + + sender.spigot().sendMessage(combinedMessage); countLocation++; } break; @@ -105,6 +115,9 @@ private void handleGet(Player sender, Arena arena, String setting) { case "maxplayers": sender.sendMessage(ChatColor.GREEN + "MaxPlayers: " + arena.getMaxPlayersLimit()); break; + case "minplayers": + sender.sendMessage(ChatColor.GREEN + "MinPlayers: " + arena.getMinPlayersLimit()); + break; default: sender.sendMessage(ChatColor.YELLOW + "[" + ChatColor.RED + "🛑" + ChatColor.YELLOW + "]" + ChatColor.GRAY + "Invalid setting: " + setting); @@ -125,14 +138,26 @@ private void handleSet(Player sender, Arena arena, String setting, String[] args switch (setting) { case "active": - if(arena.getState().equals(ArenaState.WAITING) & arena.isActive()) { + if(arena.getState().equals(ArenaState.WAITING)) { boolean active = Boolean.parseBoolean(value); - arena.setActive(active); - if (active != arena.isActive()){ - sender.sendMessage(ChatColor.RED+"Required amount ("+arena.getMaxPlayersLimit()+") of spawn locations are not defined"); + if (arena.isActive()) { + List playerUUIDsCopy = new ArrayList<>(arena.getPlayers());//To avoid ConcurrentModificationException + for (String playerUUID : playerUUIDsCopy) { + JoinLeaveHandler.leaveArena(arena.getArenaName(), Bukkit.getOfflinePlayer(UUID.fromString(playerUUID)).getName()); + } + arena.setActive(active); + if (active != arena.isActive()) { + sender.sendMessage(ChatColor.RED + "An error occured, try restarting the server"); + } + sender.sendMessage(ChatColor.GREEN + "Active set to: " + active); + }else { + arena.setActive(active); + if (active != arena.isActive()) { + sender.sendMessage(ChatColor.RED + "Required amount (" + arena.getMaxPlayersLimit() + ") of spawn locations are not defined"); + } + sender.sendMessage(ChatColor.GREEN + "Active set to: " + active); } - sender.sendMessage(ChatColor.GREEN + "Active set to: " + active); - }else { + }else if(arena.isActive()){ sender.sendMessage(ChatColor.RED+"Cannot deactivate the arena because a game is going on in the arena"); } break; @@ -146,6 +171,22 @@ private void handleSet(Player sender, Arena arena, String setting, String[] args sender.sendMessage(ChatColor.GREEN+"Maxplayers set to: "+maxplayers); sender.sendMessage(ChatColor.RED + "(Reminder) Active is set to: false. You need to reactivate the arena"); break; + case "minplayers": + if(arena.isActive()){ + sender.sendMessage(ChatColor.RED + "(Error!) Active is set to: true. You need to deactivate the arena"); + break; + } + int minplayers = Integer.parseInt(value); + if (minplayers > arena.getMaxPlayersLimit()){ + sender.sendMessage(ChatColor.RED + "(Error!) Given value of Minplayers ("+minplayers+") " + + "cannot be greater than maxplayers ("+arena.getMinPlayersLimit()+")"); + break; + } + arena.setMinPlayersLimit(minplayers); + sender.sendMessage(ChatColor.GREEN+"Minplayers set to: "+minplayers); + sender.sendMessage(ChatColor.RED + "(Reminder) Active is set to: false. You need to reactivate the arena"); + break; + case "state": try { ArenaState state = ArenaState.valueOf(value.toUpperCase()); @@ -195,7 +236,12 @@ private void handleAdd(Player sender, Arena arena, String setting, String[] args sender.sendMessage(ChatColor.RED + "Your Location is out of bounds of region"); return; } - sender.sendMessage(ChatColor.GREEN + "Added spawn location: " + location); + sender.sendMessage(ChatColor.AQUA +"["+ ChatColor.GREEN+ "✔"+ ChatColor.AQUA + "]" + +ChatColor.GRAY+ + " Spawn location set to " + + (int) location.getX() + "," + + (int) location.getY() + "," + + (int) location.getZ() + " with index: "+arena.getSpawnLocations().indexOf(location)); } else { sender.sendMessage(ChatColor.YELLOW + "[" + ChatColor.RED + "🛑" + ChatColor.YELLOW + "]" + ChatColor.GRAY + "Invalid setting: " + setting); diff --git a/src/main/java/me/ojasislive/blockdropminigame/commands/BlockDropTabCompleter.java b/src/main/java/me/ojasislive/blockdropminigame/commands/BlockDropTabCompleter.java index 3ebf7fb..bbbfa07 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/commands/BlockDropTabCompleter.java +++ b/src/main/java/me/ojasislive/blockdropminigame/commands/BlockDropTabCompleter.java @@ -37,9 +37,9 @@ public List onTabComplete(CommandSender sender, Command command, String if (args.length == 5 && "settings".equalsIgnoreCase(args[1])) { if (Arrays.asList("get","g").contains(args[3].toLowerCase())) { - return Arrays.asList("spawnlocations", "active", "state","maxplayers","players","lobbylocation"); + return Arrays.asList("spawnlocations", "active", "state","maxplayers","minplayers","players","lobbylocation"); }if (Arrays.asList("set","s").contains(args[3].toLowerCase())) { - return Arrays.asList("active", "state","maxplayers","lobbylocation"); + return Arrays.asList("active", "state","maxplayers","minplayers","lobbylocation"); } if (Arrays.asList("add", "remove", "a", "r").contains(args[3].toLowerCase())) { return Collections.singletonList("spawnlocations"); diff --git a/src/main/java/me/ojasislive/blockdropminigame/game/GameStateHandler.java b/src/main/java/me/ojasislive/blockdropminigame/game/GameStateHandler.java index 5dad8e4..c4423b3 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/game/GameStateHandler.java +++ b/src/main/java/me/ojasislive/blockdropminigame/game/GameStateHandler.java @@ -69,7 +69,7 @@ private void startGameRunnable(Arena arena) { } public void gameStarter(Arena arena){ - if(arena.getPlayers().size()>=arena.getMaxPlayersLimit()){ + if(arena.getPlayers().size()>=arena.getMinPlayersLimit()){ ArenaUtils.getInstance().startCountdown(arena.getArenaName(), 10, () -> startGameRunnable(arena)); }else{ ArenaUtils.getInstance().cancelCountdown(arena.getArenaName()); diff --git a/src/main/java/me/ojasislive/blockdropminigame/game/JoinLeaveHandler.java b/src/main/java/me/ojasislive/blockdropminigame/game/JoinLeaveHandler.java index 7e28441..27eb118 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/game/JoinLeaveHandler.java +++ b/src/main/java/me/ojasislive/blockdropminigame/game/JoinLeaveHandler.java @@ -26,7 +26,7 @@ public static String leaveArena(String arenaName, String playerName){ if (arena == null){return ChatColor.RED+"No such arena: "+arenaName+" or arena does not exist";} if (!offlinePlayer.isOnline()){return ChatColor.RED+"Player: "+playerName+" is offline";} if (!arena.isActive()){return ChatColor.RED+"Arena: "+arenaName+" is not active";} - if (arena.getState()!=ArenaState.WAITING | arena.getState()!=ArenaState.RESULTS){return ChatColor.RED+"Arena: "+arenaName+" is in a game right now";} + if (arena.getState()!=ArenaState.WAITING && arena.getState()!=ArenaState.RESULTS){return ChatColor.RED+"Arena: "+arenaName+" is in a game right now";} if (!arena.getPlayers().contains(offlinePlayer.getUniqueId().toString())){return ChatColor.RED+"Player "+playerName+" is not already playing!";} arena.removePlayer(offlinePlayer.getUniqueId().toString()); diff --git a/src/main/java/me/ojasislive/blockdropminigame/listeners/PlayerQuitListener.java b/src/main/java/me/ojasislive/blockdropminigame/listeners/PlayerQuitListener.java index c7311be..118722e 100644 --- a/src/main/java/me/ojasislive/blockdropminigame/listeners/PlayerQuitListener.java +++ b/src/main/java/me/ojasislive/blockdropminigame/listeners/PlayerQuitListener.java @@ -22,7 +22,9 @@ public void onPlayerLeave(PlayerQuitEvent event){ if (arena != null) { arena.removePlayer(player.getUniqueId().toString()); arena.getEliminatedPlayers().remove(player.getUniqueId().toString()); - if(arena.getPlayers().size()==0){ + if(arena.getPlayers().size()==1){ + arena.removePlayer(player.getUniqueId().toString()); + arena.addEliminatedPlayers(player.getUniqueId().toString()); GameStateHandler.getInstance().gameEnder(arena); } }else {