Skip to content

Commit

Permalink
Implemented MinPlayers feature where arena starts the countdown on re…
Browse files Browse the repository at this point in the history
…aching minplayers
  • Loading branch information
OJASisLive committed Jul 30, 2024
1 parent eeed280 commit c21a023
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.ojasislive</groupId>
<artifactId>blockdropminigame</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Blockdropminigame</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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<Location> spawnLocations = deserializeLocations(arenasConfig.getStringList(path + ".spawnLocations"));
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<String> 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;
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public List<String> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c21a023

Please sign in to comment.