diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..56e9e4d --- /dev/null +++ b/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + me.pgadual + PGADuals + 1.0-SNAPSHOT + jar + PgaDuals + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 16 + 16 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + paperapi-repo + https://repo.papermc.io/repository/maven-public/ + + + + + io.papermc.paper + paper-api + 26.2.build.121-stable + provided + + + + + 25 + 25 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/me/pgadual/PgaDuals.java b/src/main/java/me/pgadual/PgaDuals.java new file mode 100644 index 0000000..56aa013 --- /dev/null +++ b/src/main/java/me/pgadual/PgaDuals.java @@ -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; + } +} diff --git a/src/main/java/me/pgadual/commands/Dual.java b/src/main/java/me/pgadual/commands/Dual.java new file mode 100644 index 0000000..1712cf8 --- /dev/null +++ b/src/main/java/me/pgadual/commands/Dual.java @@ -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 . See /dual help for more commands."); + return true; + } + + if (args[0].equals("create")) { + + if (args.length < 3) { + p.sendMessage("Usage: /dual create "); + 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; + } + +} \ No newline at end of file diff --git a/src/main/java/me/pgadual/commands/admin/map/Create.java b/src/main/java/me/pgadual/commands/admin/map/Create.java new file mode 100644 index 0000000..7bda0b6 --- /dev/null +++ b/src/main/java/me/pgadual/commands/admin/map/Create.java @@ -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)."); + } +} \ No newline at end of file diff --git a/src/main/java/me/pgadual/commands/admin/map/remove.java b/src/main/java/me/pgadual/commands/admin/map/remove.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/me/pgadual/commands/admin/map/spawns.java b/src/main/java/me/pgadual/commands/admin/map/spawns.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/me/pgadual/commands/admin/map/state.java b/src/main/java/me/pgadual/commands/admin/map/state.java new file mode 100644 index 0000000..93afab3 --- /dev/null +++ b/src/main/java/me/pgadual/commands/admin/map/state.java @@ -0,0 +1 @@ +// Set the world backup state \ No newline at end of file diff --git a/src/main/java/me/pgadual/games/GameManager.java b/src/main/java/me/pgadual/games/GameManager.java new file mode 100644 index 0000000..3a4bfdd --- /dev/null +++ b/src/main/java/me/pgadual/games/GameManager.java @@ -0,0 +1,7 @@ +package me.pgadual.games; +import org.bukkit.World; +import java.util.HashMap; + +public class GameManager { + +} \ No newline at end of file diff --git a/src/main/java/me/pgadual/games/Games.java b/src/main/java/me/pgadual/games/Games.java new file mode 100644 index 0000000..17a96d6 --- /dev/null +++ b/src/main/java/me/pgadual/games/Games.java @@ -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()); + } + + } + + +} diff --git a/src/main/java/me/pgadual/games/InstanceManager.java b/src/main/java/me/pgadual/games/InstanceManager.java new file mode 100644 index 0000000..efe60e2 --- /dev/null +++ b/src/main/java/me/pgadual/games/InstanceManager.java @@ -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> instances = new HashMap<>(); + + public HashMap> 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 activeInstances = instances.computeIfAbsent(game, k -> new ArrayList<>()); + List 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 activeWorlds = instances.get(game); + + if (activeWorlds == null) { + return; + } + + activeWorlds.remove(world); + + if (activeWorlds.isEmpty()) { + instances.remove(game); + } + } + +} diff --git a/src/main/java/me/pgadual/games/Players.java b/src/main/java/me/pgadual/games/Players.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/me/pgadual/games/bowDual/bowDual.java b/src/main/java/me/pgadual/games/bowDual/bowDual.java new file mode 100644 index 0000000..a6c19eb --- /dev/null +++ b/src/main/java/me/pgadual/games/bowDual/bowDual.java @@ -0,0 +1,6 @@ +package me.pgadual.games.bowDual; +import org.bukkit.plugin.java.JavaPlugin; + +public class bowDual extends JavaPlugin { + +} \ No newline at end of file diff --git a/src/main/java/me/pgadual/games/mains.java b/src/main/java/me/pgadual/games/mains.java new file mode 100644 index 0000000..c5379b5 --- /dev/null +++ b/src/main/java/me/pgadual/games/mains.java @@ -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 + + } +} diff --git a/src/main/java/me/pgadual/handlers/Dbms.java b/src/main/java/me/pgadual/handlers/Dbms.java new file mode 100644 index 0000000..c11d3d1 --- /dev/null +++ b/src/main/java/me/pgadual/handlers/Dbms.java @@ -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 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); + } + + + + } + +} + diff --git a/src/main/java/me/pgadual/handlers/worldhandler.java b/src/main/java/me/pgadual/handlers/worldhandler.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/me/pgadual/selector/buildselector.java b/src/main/java/me/pgadual/selector/buildselector.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/me/pgadual/selector/selector.java b/src/main/java/me/pgadual/selector/selector.java new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..2119213 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,9 @@ +name: PGADual +version: '${project.version}' +main: me.pgadual.PgaDuals +api-version: 26.2 +commands: + dual: + description: dual stuff + usage: / + permission: dual.dual