init
This commit is contained in:
283
src/main/java/dev/protron/coordsave/CoordSave.java
Normal file
283
src/main/java/dev/protron/coordsave/CoordSave.java
Normal file
@@ -0,0 +1,283 @@
|
||||
package dev.protron.coordsave;
|
||||
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.*;
|
||||
import static net.kyori.adventure.text.format.TextColor.color;
|
||||
|
||||
public class CoordSave extends JavaPlugin {
|
||||
private static final String[] COMMANDS = {"save", "show", "list", "remove"};
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
File coordDir = new File("./plugins/CoordSave");
|
||||
if (!coordDir.exists()){
|
||||
getLogger().info("Creating directory");
|
||||
boolean couldCreateDir = coordDir.mkdir();
|
||||
if (couldCreateDir) {
|
||||
getLogger().info("Directory created successfully");
|
||||
} else {
|
||||
getLogger().warning("Error creating directory");
|
||||
}
|
||||
}else {
|
||||
getLogger().info("Directory already exist");
|
||||
}
|
||||
getLogger().info("Initialing database");
|
||||
try {
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:./plugins/CoordSave/coordsave.db");
|
||||
statement = connection.createStatement();
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS coords (uuid TEXT, coordname TEXT, X REAL, Y REAL, Z REAL);");
|
||||
getLogger().info("Database has been initialized");
|
||||
} catch (SQLException e) {
|
||||
getLogger().warning(e.getMessage());
|
||||
}
|
||||
getLogger().info("CoordSave has been enabled");
|
||||
}
|
||||
|
||||
public void doSaveSubcommand(Player player, String coordname) throws SQLException {
|
||||
Location location = player.getLocation();
|
||||
String playerUUID = player.getUniqueId().toString();
|
||||
|
||||
// Preparing the query string
|
||||
String query = "INSERT INTO coords (uuid, coordname, X, Y, Z) VALUES(?, ?, ?, ?, ?)";
|
||||
PreparedStatement pstmt = connection.prepareStatement(query);
|
||||
pstmt.setString(1, playerUUID);
|
||||
pstmt.setString(2, coordname);
|
||||
pstmt.setFloat(3, location.getBlockX());
|
||||
pstmt.setFloat(4, location.getBlockY());
|
||||
pstmt.setFloat(5, location.getBlockZ());
|
||||
|
||||
// Executing the query
|
||||
pstmt.executeUpdate();
|
||||
|
||||
// Building the message component
|
||||
final TextComponent conformationMessage = text()
|
||||
.content("The coordinates for ")
|
||||
.append(text(coordname).color(TextColor.color(BLUE)))
|
||||
.append(text(" where saved!")).build();
|
||||
|
||||
// Sending the message component to player
|
||||
player.sendMessage(conformationMessage);
|
||||
}
|
||||
|
||||
public void doShowSubcommand(Player player, String subarg) throws SQLException {
|
||||
// Prepare SQL query
|
||||
String query = "SELECT X, Y, Z FROM coords WHERE coordname = ? AND uuid = ?";
|
||||
PreparedStatement pstmt = connection.prepareStatement(query);
|
||||
pstmt.setString(1, subarg);
|
||||
pstmt.setString(2, player.getUniqueId().toString());
|
||||
|
||||
// Execute SQL query
|
||||
ResultSet result = pstmt.executeQuery();
|
||||
|
||||
// Check if query returned something
|
||||
if (result.getString("X") == null){
|
||||
// Building error message
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No entry with that name").color(color(RED))
|
||||
.build();
|
||||
//Sending error message
|
||||
player.sendMessage(errorMessage);
|
||||
}else {
|
||||
// Building return message
|
||||
final TextComponent returnMessage = text()
|
||||
.content("The coordinates for ")
|
||||
.append(text(subarg))
|
||||
.append(text(" are "))
|
||||
.append(text(" X: " + result.getString("X")).color(color(LIGHT_PURPLE)))
|
||||
.append(text(" Y: " + result.getString("Y")).color(color(LIGHT_PURPLE)))
|
||||
.append(text(" Z: " + result.getString("Z")).color(color(LIGHT_PURPLE)))
|
||||
.build();
|
||||
//sending return message to player
|
||||
player.sendMessage(returnMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public void doListSubcommand(Player player) throws SQLException {
|
||||
String query = "SELECT * FROM coords WHERE uuid = ?";
|
||||
PreparedStatement pstmt = connection.prepareStatement(query);
|
||||
pstmt.setString(1, player.getUniqueId().toString());
|
||||
ResultSet results= pstmt.executeQuery();
|
||||
if (results.getString("coordname") == null){
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No entry's").color(color(RED))
|
||||
.build();
|
||||
player.sendMessage(errorMessage);
|
||||
}else {
|
||||
while (results.next()){
|
||||
// Building return message
|
||||
final TextComponent returnMessage = text()
|
||||
.content(results.getString("coordname")).color(color(BLUE))
|
||||
.build();
|
||||
//sending return message to player
|
||||
player.sendMessage(returnMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void doRemoveSubcommand(Player player, String arg) throws SQLException {
|
||||
String query = "SELECT X FROM coords WHERE uuid = ? AND coordname = ?";
|
||||
PreparedStatement pstmt = connection.prepareStatement(query);
|
||||
pstmt.setString(1, player.getUniqueId().toString());
|
||||
pstmt.setString(2, arg);
|
||||
ResultSet result = pstmt.executeQuery();
|
||||
if (result.getString("X") == null){
|
||||
//Building error message
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No entry with the name ").color(color(RED))
|
||||
.append(text(arg).color(color(RED)))
|
||||
.append(text(" exists").color(color(RED)))
|
||||
.build();
|
||||
|
||||
//Sending the text component
|
||||
player.sendMessage(errorMessage);
|
||||
}else {
|
||||
String deleteQuery = "DELETE FROM coords WHERE uuid = ? AND coordname = ?";
|
||||
PreparedStatement pstmtEX = connection.prepareStatement(deleteQuery);
|
||||
pstmtEX.setString(1, player.getUniqueId().toString());
|
||||
pstmtEX.setString(2, arg);
|
||||
pstmtEX.executeUpdate();
|
||||
|
||||
final TextComponent returnMessage = text()
|
||||
.content("The entry ")
|
||||
.append(text(arg).color(color(BLUE)))
|
||||
.append(text(" was deleted!"))
|
||||
.build();
|
||||
|
||||
//Sending return message
|
||||
player.sendMessage(returnMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, Command cmd, @NotNull String label, String[] args){
|
||||
if (sender instanceof Player){
|
||||
if (cmd.getName().equalsIgnoreCase("coordsave")){
|
||||
Player player = (Player) sender;
|
||||
return switch (args[0]) {
|
||||
case "save" -> {
|
||||
if (args.length < 2){
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No name provided").color(color(RED))
|
||||
.build();
|
||||
|
||||
player.sendMessage(errorMessage);
|
||||
yield false;
|
||||
}
|
||||
try {
|
||||
doSaveSubcommand(player, args[1]);
|
||||
} catch (SQLException e) {
|
||||
getLogger().warning(e.getMessage());
|
||||
}
|
||||
yield true;
|
||||
}
|
||||
case "show" -> {
|
||||
if (args.length < 2){
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No name provided").color(color(RED))
|
||||
.build();
|
||||
|
||||
player.sendMessage(errorMessage);
|
||||
yield false;
|
||||
}
|
||||
try {
|
||||
doShowSubcommand(player, args[1]);
|
||||
} catch (SQLException e) {
|
||||
getLogger().warning(e.getMessage());
|
||||
}
|
||||
yield true;
|
||||
}
|
||||
case "list" -> {
|
||||
if (args.length > 1){
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No arguments for this command").color(color(RED))
|
||||
.build();
|
||||
|
||||
player.sendMessage(errorMessage);
|
||||
yield false;
|
||||
}
|
||||
try {
|
||||
doListSubcommand(player);
|
||||
} catch (SQLException e) {
|
||||
getLogger().warning(e.getMessage());
|
||||
}
|
||||
yield true;
|
||||
}
|
||||
case "remove" -> {
|
||||
if (args.length < 2){
|
||||
final TextComponent errorMessage = text()
|
||||
.content("No arguments for this command").color(color(RED))
|
||||
.build();
|
||||
|
||||
player.sendMessage(errorMessage);
|
||||
yield false;
|
||||
}
|
||||
try {
|
||||
doRemoveSubcommand(player, args[1]);
|
||||
} catch (SQLException e) {
|
||||
getLogger().warning(e.getMessage());
|
||||
}
|
||||
yield true;
|
||||
}
|
||||
default -> {
|
||||
final TextComponent errorMessage = text()
|
||||
.content("Unknown subcommand ").color(color(RED))
|
||||
.append(text(args[0]).color(color(RED)))
|
||||
.build();
|
||||
|
||||
player.sendMessage(errorMessage);
|
||||
yield false;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
final TextComponent errorMessage = text()
|
||||
.content("Unknown command").color(color(RED))
|
||||
.build();
|
||||
|
||||
sender.sendMessage(errorMessage);
|
||||
return false;
|
||||
}
|
||||
sender.sendMessage("Not a Player");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||
final List<String> completions = new ArrayList<>();
|
||||
if (args.length == 1){
|
||||
StringUtil.copyPartialMatches(args[0], List.of(COMMANDS), completions);
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
getLogger().info("Closing Database");
|
||||
try {
|
||||
if (connection != null)
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
getLogger().warning(e.getMessage());
|
||||
}
|
||||
getLogger().info("Database was closed successfully");
|
||||
getLogger().info("CoordSave has been disabled");
|
||||
}
|
||||
|
||||
}
|
||||
25
src/main/resources/plugin.yml
Normal file
25
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,25 @@
|
||||
name: CoordSave
|
||||
version: 0.0.4
|
||||
main: dev.protron.coordsave.CoordSave
|
||||
description: A plugin to simply save your Coordinates
|
||||
author: Doc
|
||||
website: https://papermc.io
|
||||
api-version: '1.20'
|
||||
|
||||
commands:
|
||||
coordsave:
|
||||
description: "With this command you can save your coordinates"
|
||||
usage: |
|
||||
/coordsave save <name> - Will save your current coordinates
|
||||
/coordsave show <name> - Will show you the coordinates of that name
|
||||
/coordsave list - Will show you all saved coordinates names
|
||||
/coordsave remove <name> - Removes the named entries
|
||||
aliases: [cs]
|
||||
permission: coordsave.basic
|
||||
permission-message: "You do not have permission to use this command"
|
||||
|
||||
|
||||
permissions:
|
||||
coordsave.basic:
|
||||
description: "Standard permissions for the command"
|
||||
default: true
|
||||
Reference in New Issue
Block a user