Skip to content

Commit

Permalink
add base lobby queuing system
Browse files Browse the repository at this point in the history
  • Loading branch information
Smartich0ke committed Sep 16, 2022
1 parent a7cbea6 commit 01d3780
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,86 @@
package com.nikolaipatrick.minigamesmanager2.commands;

import com.nikolaipatrick.minigamesmanager2.config.MinigameData;
import com.nikolaipatrick.minigamesmanager2.config.config;
import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.NPC;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.util.*;

public class minigame implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(sender instanceof Player) {
String minigameToJoin = args[1];
Player player = (Player) sender;
if (args[0].equals("join")) {

HashMap<String, Integer> lobbyNamesAndPlayers = new HashMap<String, Integer>();
Set<String> lobbyWorldNamesSet = new HashSet<>();
try {
MinigameData.minigameDataFile.reload();
} catch (IOException e) {
throw new RuntimeException(e);
}
lobbyWorldNamesSet = MinigameData.minigameDataFile.getSection("minigames." + minigameToJoin + ".lobbies").getRoutesAsStrings(false);
ArrayList<String> lobbyWorldNamesList = new ArrayList<>(lobbyWorldNamesSet);

for (String worldName : lobbyWorldNamesList) {
try {
MinigameData.minigameDataFile.reload();
} catch (IOException e) {
throw new RuntimeException(e);
}
int onlinePlayers = MinigameData.minigameDataFile.getInt("minigames." + minigameToJoin + ".lobbies." + worldName + ".onlinePlayers");
lobbyNamesAndPlayers.put(worldName, onlinePlayers);
}
int lobbyMaxPlayerValue = 0;
String lobbyMaxPlayerWorld = null;
for (String worldName : lobbyNamesAndPlayers.keySet()) {
int valueToTest = lobbyNamesAndPlayers.get(worldName);
if (valueToTest > lobbyMaxPlayerValue) {
lobbyMaxPlayerValue = valueToTest;
lobbyMaxPlayerWorld = worldName;
}
}
if (lobbyMaxPlayerWorld == null) {
lobbyMaxPlayerWorld = lobbyWorldNamesList.get(0);
}
Location lobbySpawn = new Location(Bukkit.getWorld(lobbyMaxPlayerWorld), 0, 0, 0);
player.sendMessage(config.messagePrefix() + ChatColor.AQUA + "Sending you to " + ChatColor.YELLOW + lobbyMaxPlayerWorld + ChatColor.AQUA + "...");
player.teleport(lobbySpawn);
MinigameData.minigameDataFile.set("minigames." + minigameToJoin + ".lobbies." + lobbyMaxPlayerWorld + ".onlinePlayers", MinigameData.minigameDataFile.getInt("minigames." + minigameToJoin + ".lobbies." + lobbyMaxPlayerWorld + ".onlinePlayers")+1);
try {
MinigameData.minigameDataFile.save();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
MinigameData.minigameDataFile.reload();
} catch (IOException e) {
throw new RuntimeException(e);
}
for(Player p : Bukkit.getOnlinePlayers()){
if(p.getWorld().getName().equals(lobbyMaxPlayerWorld)) {
p.sendTitle(MinigameData.minigameDataFile.getInt("minigames." + minigameToJoin + ".lobbies." + lobbyMaxPlayerWorld + ".onlinePlayers") + " / " + MinigameData.minigameDataFile.getInt("minigames." + minigameToJoin + ".maxPlayers") , null, 5, 100, 30);
p.playNote(p.getLocation(), Instrument.SNARE_DRUM, Note.sharp(1, Note.Tone.C));

}
}
return true;
}
} else {
sender.sendMessage(config.messagePrefix() + ChatColor.RED + "This command must be run in-game to work properly.");
return true;
}


return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import com.nikolaipatrick.minigamesmanager2.MinigamesManager2;
import dev.dejvokep.boostedyaml.YamlDocument;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MinigameData {
public static YamlDocument minigameDataFile;
Expand Down Expand Up @@ -48,11 +53,12 @@ public static void deleteMinigameProfile(String minigameName) {
}

public static void addMinigameWorld(String minigameName, String worldName, String worldType ) {

if (worldType.equals("arena")) {
minigameDataFile.set("minigames." + minigameName + ".arenas." + worldName + ".onlinePlayers", 0);
minigameDataFile.set("minigames." + minigameName + ".arenas." + worldName + ".isInUse", false);
} else if (worldType.equals("lobby")) {
minigameDataFile.set("minigames." + minigameName + ".lobbies." + worldName + ".onlinePlayers", 0);
minigameDataFile.set("minigames." + minigameName + ".lobbies." + worldName + ".isInUse", false);
} else {
throw new RuntimeException(worldType + "is not a valid worldType. use arena or lobby.");
}
Expand Down Expand Up @@ -95,6 +101,11 @@ public static void setTpPoint (String minigame, int point, int x, int y, int z){
throw new RuntimeException(e);
}
}
public static String getOnlinePlayersInArena(String minigame, String world){
return minigameDataFile.getString("minigames." + minigame + ".arena." + minigame);

}

}


Expand Down

0 comments on commit 01d3780

Please sign in to comment.