diff options
author | Benji Dial <benji6283@gmail.com> | 2022-11-12 13:39:07 -0500 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2022-11-12 13:39:07 -0500 |
commit | da7924881ab14e4580ae6c56dbdf8346dbf2a267 (patch) | |
tree | 3631cbb3a0eaa6c57c6b8476d130472561dc16c6 | |
download | new-simple-waypoints-da7924881ab14e4580ae6c56dbdf8346dbf2a267.tar.gz |
first commit for new version
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | pom.xml | 88 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/Database.java | 157 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/Plugin.java | 34 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/Waypoint.java | 13 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java | 20 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/ListWaypoints.java | 39 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java | 22 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java | 27 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/WaypointBack.java | 27 | ||||
-rw-r--r-- | src/main/java/net/benjidial/nswp/commands/WaypointCommand.java | 71 | ||||
-rw-r--r-- | src/main/resources/plugin.yml | 26 |
12 files changed, 526 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bcbfeee --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +target/ @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>net.benjidial.nswp</groupId> + <artifactId>NSWP</artifactId> + <version>2.0-SNAPSHOT</version> + + <name>NewSimpleWaypoints</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.source>1.8</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + </properties> + + <repositories> + <repository> + <id>spigot-repo</id> + <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>org.spigotmc</groupId> + <artifactId>spigot-api</artifactId> + <version>1.19.2-R0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + </dependencies> + + <build> + <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> + <plugins> + <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> + <plugin> + <artifactId>maven-clean-plugin</artifactId> + <version>3.1.0</version> + </plugin> + <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>3.0.2</version> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.0</version> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.22.1</version> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <version>3.0.2</version> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <version>2.5.2</version> + </plugin> + <plugin> + <artifactId>maven-deploy-plugin</artifactId> + <version>2.8.2</version> + </plugin> + <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <version>3.7.1</version> + </plugin> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <version>3.0.0</version> + </plugin> + </plugins> + </pluginManagement> + <resources> + <resource> + <directory>${project.basedir}/src/main/resources</directory> + <includes> + <include>plugin.yml</include> + </includes> + </resource> + </resources> + </build> +</project> diff --git a/src/main/java/net/benjidial/nswp/Database.java b/src/main/java/net/benjidial/nswp/Database.java new file mode 100644 index 0000000..21f52f5 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/Database.java @@ -0,0 +1,157 @@ +package net.benjidial.nswp; + +import org.bukkit.entity.Player; +import org.bukkit.Location; +import org.bukkit.Bukkit; + +import java.sql.PreparedStatement; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.ArrayList; +import java.sql.Connection; +import java.sql.ResultSet; + +public class Database { + static Connection connection; + + public static void connect(String path) throws SQLException { + connection = DriverManager.getConnection("jdbc:sqlite:" + path); + } + + static String playerID(Player player) { + return player.getUniqueId().toString().replaceAll("-", ""); + } + + static String waypointTableName(Player player) { + return "waypoints_" + playerID(player); + } + + public static void createWaypointTable(Player player) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "CREATE TABLE IF NOT EXISTS " + waypointTableName(player) + " (" + + "name TEXT PRIMARY KEY, " + + "world TEXT NOT NULL, " + + "x REAL NOT NULL, " + + "y REAL NOT NULL, " + + "z REAL NOT NULL, " + + "pitch REAL NOT NULL, " + + "yaw REAL NOT NULL" + + ")" + ); + statement.executeUpdate(); + } + + static void createWBacks() throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "CREATE TABLE IF NOT EXISTS wbacks (" + + "player_uuid TEXT PRIMARY KEY, " + + "world TEXT NOT NULL, " + + "x REAL NOT NULL, " + + "y REAL NOT NULL, " + + "z REAL NOT NULL, " + + "pitch REAL NOT NULL, " + + "yaw REAL NOT NULL" + + ")" + ); + statement.executeUpdate(); + } + + public static Location getWBack(Player player) throws SQLException { + createWBacks(); + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM wbacks WHERE player_uuid = ?" + ); + statement.setString(1, playerID(player)); + ResultSet results = statement.executeQuery(); + if (!results.next()) + return null; + return cursorToLocation(results); + } + + public static void setWBack(Player player, Location location) throws SQLException { + createWBacks(); + PreparedStatement statement = connection.prepareStatement( + "REPLACE INTO wbacks(player_uuid, world, x, y, z, pitch, yaw) VALUES(?, ?, ?, ?, ?, ?, ?)" + ); + statement.setString(1, playerID(player)); + statement.setString(2, location.getWorld().getName()); + statement.setDouble(3, location.getX()); + statement.setDouble(4, location.getY()); + statement.setDouble(5, location.getZ()); + statement.setDouble(6, location.getPitch()); + statement.setDouble(7, location.getYaw()); + statement.executeUpdate(); + } + + static Location cursorToLocation(ResultSet results) throws SQLException { + return new Location( + Bukkit.getWorld(results.getString("world")), + results.getDouble("x"), + results.getDouble("y"), + results.getDouble("z"), + (float)results.getDouble("yaw"), + (float)results.getDouble("pitch") + ); + } + + static Waypoint cursorToWaypoint(ResultSet results) throws SQLException { + return new Waypoint( + results.getString("name"), + cursorToLocation(results) + ); + } + + public static ArrayList<Waypoint> searchWaypoints(Player player, String start) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + waypointTableName(player) + " WHERE name LIKE ? ESCAPE '!' ORDER BY world, name ASC" + ); + statement.setString(1, "%" + start.replace("!", "!!").replace("_", "!_") + "%"); + ResultSet results = statement.executeQuery(); + ArrayList<Waypoint> list = new ArrayList<Waypoint>(); + while (results.next()) + list.add(cursorToWaypoint(results)); + return list; + } + + public static Waypoint lookupWaypoint(Player player, String name) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + waypointTableName(player) + " WHERE name = ?" + ); + statement.setString(1, name); + ResultSet results = statement.executeQuery(); + if (!results.next()) + return null; + return cursorToWaypoint(results); + } + + public static void addWaypoint(Player player, Waypoint waypoint) throws SQLException { + if (lookupWaypoint(player, waypoint.name) != null) + player.sendMessage("There is already a waypoint with that name."); + else { + PreparedStatement statement = connection.prepareStatement( + "INSERT INTO " + waypointTableName(player) + "(name, world, x, y, z, pitch, yaw) " + + "VALUES(?, ?, ?, ?, ?, ?, ?)" + ); + statement.setString(1, waypoint.name); + statement.setString(2, waypoint.location.getWorld().getName()); + statement.setDouble(3, waypoint.location.getX()); + statement.setDouble(4, waypoint.location.getY()); + statement.setDouble(5, waypoint.location.getZ()); + statement.setDouble(6, waypoint.location.getPitch()); + statement.setDouble(7, waypoint.location.getYaw()); + statement.executeUpdate(); + player.sendMessage("Waypoint added."); + } + } + + public static void deleteWaypoint(Player player, String name) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "DELETE FROM " + waypointTableName(player) + " WHERE name = ?" + ); + statement.setString(1, name); + if (statement.executeUpdate() == 0) + player.sendMessage("No waypoint with that name."); + else + player.sendMessage("Waypoint deleted."); + } +} diff --git a/src/main/java/net/benjidial/nswp/Plugin.java b/src/main/java/net/benjidial/nswp/Plugin.java new file mode 100644 index 0000000..9a46cb6 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/Plugin.java @@ -0,0 +1,34 @@ +package net.benjidial.nswp; + +import net.benjidial.nswp.commands.*; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.logging.Logger; +import java.util.logging.Level; +import java.sql.SQLException; +import java.io.File; + +public class Plugin extends JavaPlugin { + public static Logger logger; + + public void onEnable() { + logger = getLogger(); + getDataFolder().mkdirs(); + + try { + Database.connect(new File(getDataFolder(), "database.db").getPath()); + } + catch (SQLException ex) { + logger.log(Level.SEVERE, "Database error: (not enabling plugin)"); + ex.printStackTrace(); + return; + } + + getCommand("wlist").setExecutor(new ListWaypoints()); + getCommand("wsave").setExecutor(new SaveWaypoint()); + getCommand("wtp" ).setExecutor(new TeleportToWaypoint()); + getCommand("wdel" ).setExecutor(new DeleteWaypoint()); + getCommand("wback").setExecutor(new WaypointBack()); + } +} diff --git a/src/main/java/net/benjidial/nswp/Waypoint.java b/src/main/java/net/benjidial/nswp/Waypoint.java new file mode 100644 index 0000000..ec3daf6 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/Waypoint.java @@ -0,0 +1,13 @@ +package net.benjidial.nswp; + +import org.bukkit.Location; + +public class Waypoint { + public Waypoint(String name, Location location) { + this.name = name; + this.location = location; + } + + public String name; + public Location location; +} diff --git a/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java new file mode 100644 index 0000000..d24d133 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java @@ -0,0 +1,20 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; + +public class DeleteWaypoint extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.Waypoint; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 1) + return false; + Database.deleteWaypoint(player, args[0]); + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java new file mode 100644 index 0000000..b6b61a3 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java @@ -0,0 +1,39 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; + +import org.bukkit.entity.Player; +import org.bukkit.World; + +import java.sql.SQLException; +import java.util.ArrayList; + +public class ListWaypoints extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.Waypoint; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length > 1) + return false; + ArrayList<Waypoint> waypoints = Database.searchWaypoints(player, args.length == 0 ? "" : args[0]); + if (waypoints.size() == 0) { + player.sendMessage("No waypoints found."); + return true; + } + if (waypoints.size() == 1) + player.sendMessage("1 waypoint found:"); + else + player.sendMessage(waypoints.size() + " waypoints found:"); + World lastWorld = null; + for (Waypoint waypoint : waypoints) { + if (waypoint.location.getWorld() != lastWorld) { + lastWorld = waypoint.location.getWorld(); + player.sendMessage(" " + lastWorld.getName() + ": "); + } + player.sendMessage(" " + waypoint.name); + } + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java new file mode 100644 index 0000000..f0b9dcd --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java @@ -0,0 +1,22 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; + +public class SaveWaypoint extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.None; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 1) + return false; + Waypoint waypoint = new Waypoint(args[0], player.getLocation()); + Database.addWaypoint(player, waypoint); + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java new file mode 100644 index 0000000..4e84e3c --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java @@ -0,0 +1,27 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; + +public class TeleportToWaypoint extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.Waypoint; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 1) + return false; + Waypoint waypoint = Database.lookupWaypoint(player, args[0]); + if (waypoint == null) + player.sendMessage("No waypoint with that name."); + else { + Database.setWBack(player, player.getLocation()); + player.teleport(waypoint.location); + } + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointBack.java b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java new file mode 100644 index 0000000..3fa0de3 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java @@ -0,0 +1,27 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; +import org.bukkit.Location; + +import java.sql.SQLException; + +public class WaypointBack extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.None; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 0) + return false; + Location wback = Database.getWBack(player); + if (wback == null) + player.sendMessage("You have not teleported to any waypoints."); + else { + Database.setWBack(player, player.getLocation()); + player.teleport(wback); + } + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java new file mode 100644 index 0000000..618ab74 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java @@ -0,0 +1,71 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; +import net.benjidial.nswp.Plugin; + +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +import java.util.logging.Level; +import java.sql.SQLException; +import java.util.ArrayList; + +public abstract class WaypointCommand implements TabExecutor { + enum CompletionType { + None, Waypoint + } + + public abstract CompletionType getCompletionType(); + + public ArrayList<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("Only players may use waypoints."); + return new ArrayList<>(); + } + Player player = (Player)sender; + + switch (getCompletionType()) { + case Waypoint: + if (args.length > 1) + return new ArrayList<>(); + try { + Database.createWaypointTable(player); + ArrayList<String> results = new ArrayList<>(); + for (Waypoint waypoint : Database.searchWaypoints(player, args.length == 0 ? "" : args[0])) + results.add(waypoint.name); + return results; + } + catch (SQLException ex) { + Plugin.logger.log(Level.SEVERE, "Database error:"); + ex.printStackTrace(); + sender.sendMessage("Database error."); + return new ArrayList<>(); + } + default: + return new ArrayList<>(); + } + } + + public abstract boolean body(Player player, String[] args) throws SQLException; + + public boolean onCommand(CommandSender sender, Command command, String name, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("Only players may use waypoints."); + return true; + } + try { + Player player = (Player)sender; + Database.createWaypointTable(player); + return body(player, args); + } + catch (SQLException ex) { + Plugin.logger.log(Level.SEVERE, "Database error:"); + ex.printStackTrace(); + sender.sendMessage("Database error."); + return true; + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..63dcf0e --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,26 @@ +main: net.benjidial.nswp.Plugin +name: NewSimpleWaypoints +version: 2.0-SNAPSHOT +api-version: 1.19 + +commands: + wlist: + description: Lists a user's waypoints + usage: /wlist [<query>] + permission: nswp.use + wsave: + description: Saves a waypoint + usage: /wsave <name> + permission: nswp.use + wtp: + description: Teleports to a waypoint + usage: /wtp <name> + permission: nswp.use + wdel: + description: Deletes a waypoint + usage: /wdel <name> + permission: nswp.use + wback: + description: Teleports a player to where they were the last time they ran wtp or wback + usage: /wback + permission: nswp.use |