add progress code

This commit is contained in:
niederwerfee committed 2026-11-15 20:29:18 -06:00
1 parent 877ea1d0d1
commit 6d955d55a7
18 files changed
+410

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
package me.pgadual;
import me.pgadual.commands.Dual;
import me.pgadual.games.InstanceManager;
import me.pgadual.handlers.Dbms;
import org.bukkit.plugin.java.JavaPlugin;
public class PgaDuals extends JavaPlugin {
private static PgaDuals instance;
private InstanceManager instanceManager;
@Override
public void onEnable() {
instance = this;
instanceManager = new InstanceManager();
Dbms db = new Dbms();
db.init();
getCommand("dual").setExecutor(new Dual());
}
public InstanceManager getInstanceManager() {
return instanceManager;
}
public static PgaDuals getInstance() {
return instance;
}
}
@@ -0,0 +1,66 @@
package me.pgadual.commands;
import me.pgadual.commands.admin.map.Create;
import me.pgadual.games.Games;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class Dual implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
if (sender instanceof Player) {
if (sender.isOp()) { // TODO: fix perms for regular dual command
Player p = (Player) sender;
if (args.length == 0) {
p.sendMessage("Usage: /dual <player_name>. See /dual help for more commands.");
return true;
}
if (args[0].equals("create")) {
if (args.length < 3) {
p.sendMessage("Usage: /dual create <game> <map_name>");
return true;
}
if (!args[1].isEmpty() && !args[2].isEmpty()) {
Create c = new Create();
String game = args[1].toUpperCase();
if (game.equals(Games.gamesList.BOW_DUAL.toString())) {
c.create_cmd_tasks(p, Games.gamesList.BOW_DUAL, args[2]);
}
else if (game.equals(Games.gamesList.ONE_V_ONE.toString())) {
c.create_cmd_tasks(p, Games.gamesList.ONE_V_ONE, args[2]);
}
else {
p.sendMessage("Invalid game name! Options are: " + Arrays.toString(Games.gamesList.values()));
}
}
}
else if (args.length == 1) {
Games g = new Games();
g.start_game(Games.gamesList.BOW_DUAL, p, p);
}
else {
p.sendMessage("Invalid Argument!");
}
}
}
return true;
}
}
@@ -0,0 +1,16 @@
package me.pgadual.commands.admin.map;// Create a map
import me.pgadual.games.Games;
import me.pgadual.handlers.Dbms;
import org.bukkit.World;
import org.bukkit.entity.Player;
public class Create {
public void create_cmd_tasks(Player p, Games.gamesList game_name, String map_name) { //TODO: implement map name
Dbms db = new Dbms();
World world = p.getWorld();
db.add_world(game_name, world);
p.sendMessage("Success! Created new map with " + map_name + " at world " + p.getWorld().getName() + " (current).");
}
}
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1 @@
// Set the world backup state
@@ -0,0 +1,7 @@
package me.pgadual.games;
import org.bukkit.World;
import java.util.HashMap;
public class GameManager {
}
+46
View File
@@ -0,0 +1,46 @@
package me.pgadual.games;
import me.pgadual.PgaDuals;
import org.bukkit.World;
import org.bukkit.entity.Player;
public class Games {
public enum gamesList {
BOW_DUAL,
ONE_V_ONE
}
public void start_game(gamesList game, Player p1, Player p2) {
InstanceManager i = PgaDuals.getInstance().getInstanceManager();
if (game == gamesList.BOW_DUAL) {
World game_world = i.create_instance(Games.gamesList.BOW_DUAL);
if (game_world == null) {
p1.getPlayer().sendMessage("Could not start dual as all worlds associated with the game are in use! (" + i.get_instances().get(gamesList.BOW_DUAL).size() + "/" + i.get_instances().get(gamesList.BOW_DUAL).size() + " worlds taken)");
//p2.getPlayer().sendMessage("Could not start dual as all worlds associated with the game are in use! (" + i.get_instances().get(gamesList.BOW_DUAL).size() + "/" + i.get_instances().size() + " worlds taken)");
return;
}
//TODO: add tp to the correct defined spawn positions
p1.getPlayer().teleport(game_world.getSpawnLocation());
//p2.getPlayer().teleport(game_world.getSpawnLocation());
}
else if (game == gamesList.ONE_V_ONE) {
World game_world = i.create_instance(gamesList.ONE_V_ONE);
if (game_world == null) {
p1.getPlayer().sendMessage("Could not start dual as all worlds associated with the game are in use! (" + i.get_instances().get(gamesList.ONE_V_ONE).size() + "/" + i.get_instances().get(gamesList.ONE_V_ONE).size() + " worlds taken)");
//p2.getPlayer().sendMessage("Could not start dual as all worlds associated with the game are in use! (" + i.get_instances().get(gamesList.ONE_V_ONE).size() + "/" + i.get_instances().size() + " worlds taken)");
return;
}
p1.getPlayer().teleport(game_world.getSpawnLocation());
//p2.getPlayer().teleport(game_world.getSpawnLocation());
}
}
}
@@ -0,0 +1,63 @@
package me.pgadual.games;
import me.pgadual.handlers.Dbms;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.*;
public class InstanceManager {
private final HashMap<Games.gamesList, List<World>> instances = new HashMap<>();
public HashMap<Games.gamesList, List<World>> get_instances() {
return instances;
}
public World create_instance(Games.gamesList game) {
Bukkit.getLogger().info(game.name());
Dbms db = new Dbms();
World[] worlds = db.get_worlds(game);
List<World> activeInstances = instances.computeIfAbsent(game, k -> new ArrayList<>());
List<World> availableWorlds = new ArrayList<>();
if (worlds == null){return null;};
if (activeInstances.size() >= worlds.length) {
return null;
}
Random random = new Random();
for (World world : worlds) {
if (!activeInstances.contains(world)) {
availableWorlds.add(world);
}
}
World pickedWorld = availableWorlds.get(random.nextInt(availableWorlds.size()));
activeInstances.add(pickedWorld);
return pickedWorld;
}
public void remove_instance(Games.gamesList game, World world) {
List<World> activeWorlds = instances.get(game);
if (activeWorlds == null) {
return;
}
activeWorlds.remove(world);
if (activeWorlds.isEmpty()) {
instances.remove(game);
}
}
}
Whitespace-only changes.
@@ -0,0 +1,6 @@
package me.pgadual.games.bowDual;
import org.bukkit.plugin.java.JavaPlugin;
public class bowDual extends JavaPlugin {
}
+12
View File
@@ -0,0 +1,12 @@
package me.pgadual.games;
import org.bukkit.World;
public class mains {
static void main() {
InstanceManager i = new InstanceManager();
World world_to_tp = i.create_instance(Games.gamesList.BOW_DUAL);
// Tp player and start the game
}
}
@@ -0,0 +1,83 @@
package me.pgadual.handlers;
import me.pgadual.games.Games;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Dbms {
String url = "jdbc:sqlite:dual_games.db";
public void init() {
String games_and_worlds_table = "CREATE TABLE IF NOT EXISTS 'worlds' ('game_name' TEXT, 'world_name' TEXT UNIQUE)";
try {
Connection conn = DriverManager.getConnection(url);
Statement statement = conn.createStatement();
statement.addBatch(games_and_worlds_table);
statement.executeBatch();
conn.close();
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
public World[] get_worlds(Games.gamesList game) {
List<World> worlds = new ArrayList<>();
String world_query = "SELECT * FROM worlds WHERE game_name = ?";
try {
Connection conn = DriverManager.getConnection(url);
PreparedStatement ps = conn.prepareStatement(world_query);
ps.setString(1, game.toString());
ResultSet rs = ps.executeQuery();
while (rs.next()) {
World world = Bukkit.getWorld(rs.getString("world_name"));
if (world != null) {
worlds.add(world);
}
}
return worlds.toArray(new World[0]);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void add_world(Games.gamesList game, World world) {
String world_query = "INSERT OR IGNORE INTO worlds (game_name, world_name) VALUES (?, ?) ";
try {
Connection conn = DriverManager.getConnection(url);
PreparedStatement ps = conn.prepareStatement(world_query);
ps.setString(1, game.toString());
ps.setString(2, world.getName());
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.