Initial commit

This commit is contained in:
Louis Vallat 2020-04-06 11:00:51 +02:00
commit b79cdd05c3
16 changed files with 855 additions and 0 deletions

2
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

16
.idea/compiler.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="BackHome" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="BackHome" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: com.google.code.gson:gson:2.8.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.0/gson-2.8.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.0/gson-2.8.0-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: com.google.guava:guava:21.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/21.0/guava-21.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/21.0/guava-21.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/21.0/guava-21.0-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: commons-lang:commons-lang:2.6">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.6/commons-lang-2.6.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.6/commons-lang-2.6-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/commons-lang/commons-lang/2.6/commons-lang-2.6-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: net.md-5:bungeecord-chat:1.15-SNAPSHOT">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.15-SNAPSHOT/bungeecord-chat-1.15-20200325.072212-62.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.15-SNAPSHOT/bungeecord-chat-1.15-20200325.072212-62-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/net/md-5/bungeecord-chat/1.15-SNAPSHOT/bungeecord-chat-1.15-20200325.072212-62-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.15.2-R0.1-SNAPSHOT/spigot-api-1.15.2-R0.1-20200405.072256-83.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.15.2-R0.1-SNAPSHOT/spigot-api-1.15.2-R0.1-20200405.072256-83-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/spigotmc/spigot-api/1.15.2-R0.1-SNAPSHOT/spigot-api-1.15.2-R0.1-20200405.072256-83-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.yaml:snakeyaml:1.25">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.25/snakeyaml-1.25.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.25/snakeyaml-1.25-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.25/snakeyaml-1.25-sources.jar!/" />
</SOURCES>
</library>
</component>

18
.idea/misc.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="org.bukkit.event.EventHandler" />
</list>
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8 (Oracle)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/BackHome.iml" filepath="$PROJECT_DIR$/BackHome.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

31
BackHome.iml Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>SPIGOT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
</component>
</module>

77
pom.xml Normal file
View File

@ -0,0 +1,77 @@
<?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>louis-vallat.xyz</groupId>
<artifactId>BackHome</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>BackHome</name>
<description>A plugin to set and go back to homes.</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>louis-vallat.xyz</url>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,603 @@
package louisvallat.xyz.backhome;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* The main code for this Home and Back plugin.
*
* @author Louis Vallat
*/
public class BackHome extends JavaPlugin implements Listener, TabCompleter {
/**
* The path for the plugin's files.
*/
private final String path = "./plugins/BackHome";
/**
* Database name.
*/
private final String dbName = "BackHome.data";
/**
* Home table name.
*/
private final String tableNameHome = "Home";
/**
* Home table name.
*/
private final String tableNameBack = "Back";
/**
* Home table history name.
*/
private final String tableNameHomeHistory = "HomeHistory";
/**
* Back table history name.
*/
private final String tableNameBackHistory = "BackHistory";
/**
* The connection to the database.
*/
private Connection connexion;
@Override
public void onEnable() {
try {
initDirectory();
createTable();
Bukkit.getServer().getPluginManager().registerEvents(this, this);
getLogger().info("Done enabling.");
} catch (SQLException ex) {
getLogger().warning(ex.getMessage());
getLogger().warning("You cannot use this plugin while this error "
+ "hasn't been fixed.");
}
}
@Override
public void onDisable() {
getLogger().info("Done disabling.");
}
/**
* Initialize the plugin's directory.
*/
private void initDirectory() {
new File(path).mkdirs();
}
/**
* Create the needed tables for storing the data we are using.
*
* @throws SQLException if there is a SQL problem.
*/
private void createTable() throws SQLException {
this.connexion = getConnection();
createHomeTable();
createHomeHistoryTable();
createBackTable();
createBackHistoryTable();
this.connexion.close();
}
/**
* Get a fresh connection to the database.
*
* @return a new connection to the database.
* @throws SQLException if the connection couldn't be established.
*/
private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:sqlite:"
+ this.path + File.separator
+ this.dbName);
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity().getPlayer();
try {
removeBack(player.getUniqueId());
addBack(player.getUniqueId(),
player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(),
player.getLocation().getYaw(), player.getLocation().getPitch(),
player.getLocation().getWorld().getName());
} catch (SQLException e) {
event.getEntity().getPlayer().sendMessage(ChatColor.RED + "Your back hasn't been set due to an error, " +
"but your death coordinates were X:" + player.getLocation().getX()
+ " Y:" + player.getLocation().getY() + " Z:" + player.getLocation().getZ());
getLogger().warning(e.getMessage());
}
}
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd,
String label, String[] args) {
if ((cmd.getName().equalsIgnoreCase("delhome")
|| cmd.getName().equalsIgnoreCase("home")) && args.length == 1) {
try {
return getHomeList(((Player) sender).getUniqueId()).stream()
.filter(s -> s.toLowerCase().contains(args[0].toLowerCase())).collect(Collectors.toList());
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "Auto-completion isn't working well right now on this " +
"command. Try again later.");
getLogger().warning(e.getMessage());
}
}
return new ArrayList<>();
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) {
if (sender instanceof Player) {
if (setHomeCommand(sender, cmd, args)) return true;
if (setBackCommand(sender, cmd, args)) return true;
if (homeCommand(sender, cmd, args)) return true;
if (backCommand(sender, cmd, args)) return true;
if (delHomeCommand(sender, cmd, args)) return true;
} else {
sender.sendMessage(ChatColor.RED + "This command can't be issued by a non-player.");
}
return false;
}
/**
* Maybe the player wants to delete one of its home?
*
* @param sender the player that issued the command.
* @param cmd the command that has been issued.
* @param args the args following the command.
* @return true if it worked, false otherwise.
*/
private boolean delHomeCommand(CommandSender sender, Command cmd, String[] args) {
if (cmd.getName().equalsIgnoreCase("delhome") && args.length >= 1) {
try {
StringBuilder name = new StringBuilder();
for (String arg : args) {
name.append(arg);
}
if (getHomeList(((Player) sender).getUniqueId()).contains(name.toString())) {
removeHome(((Player) sender).getUniqueId(), name.toString());
sender.sendMessage(ChatColor.GREEN + "Your home named '" + name.toString() + "' has been deleted.");
} else {
sender.sendMessage(ChatColor.RED + "This home '" + name.toString() + "' doesn't seem to exist.");
return false;
}
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while trying to list your homes. Try again later.");
getLogger().warning(e.getMessage());
}
return true;
}
return false;
}
/**
* Maybe the player issued the /back command?
*
* @param sender the player that sent the command.
* @param cmd the command.
* @param args the args following the command.
* @return true if the command didn't fail and true if it has worked correctly.
*/
private boolean backCommand(CommandSender sender, Command cmd, String[] args) {
if (cmd.getName().equalsIgnoreCase("back")) {
try {
this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + this.tableNameBack + " WHERE uuid = ?;");
recherche.setString(1, ((Player) sender).getUniqueId().toString());
double x = ((Player) sender).getLocation().getX();
double y = ((Player) sender).getLocation().getY();
double z = ((Player) sender).getLocation().getZ();
float yaw = ((Player) sender).getLocation().getYaw();
float pitch = ((Player) sender).getLocation().getPitch();
String worldName = ((Player) sender).getLocation().getWorld().getName();
teleportPlayerFromPreparedStatement(sender, recherche);
removeBack(((Player) sender).getUniqueId());
addBack(((Player) sender).getUniqueId(), x, y, z, yaw, pitch, worldName);
this.connexion.close();
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while trying to execute this command. " +
"Try again later.");
getLogger().warning(e.getMessage());
}
return true;
}
return false;
}
/**
* Maybe the player issue the /home command
*
* @param sender the player that sent the command
* @param cmd the command itself
* @param args the args that are following the command
* @return true if it worked, false otherwise.
*/
private boolean homeCommand(CommandSender sender, Command cmd, String[] args) {
if (cmd.getName().equalsIgnoreCase("home") && args.length >= 1) {
String name = getArgsAsString(args);
try {
if (getHomeList(((Player) sender).getUniqueId()).contains(name)) {
this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + this.tableNameHome + " WHERE uuid = ? AND name = ?;");
recherche.setString(1, ((Player) sender).getUniqueId().toString());
recherche.setString(2, name);
removeBack(((Player) sender).getUniqueId());
addBack(((Player) sender).getUniqueId(),
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch(),
((Player) sender).getLocation().getWorld().getName());
teleportPlayerFromPreparedStatement(sender, recherche);
this.connexion.close();
} else {
sender.sendMessage(ChatColor.RED + "The home named '" + name + "' doesn't exist.");
return false;
}
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while trying to execute this command. " +
"Try again later.");
getLogger().warning(e.getMessage());
}
return true;
}
return false;
}
/**
* Teleport a player by getting x, y and z from the prepared statement.
*
* @param sender the player to teleport.
* @param statement the prepared statement.
* @throws SQLException if the request failed.
*/
private void teleportPlayerFromPreparedStatement(CommandSender sender, PreparedStatement statement)
throws SQLException {
try (ResultSet res = statement.executeQuery()) {
if (res.next()) {
double x = res.getDouble("x");
double y = res.getDouble("y");
double z = res.getDouble("z");
float yaw = res.getFloat("yaw");
float pitch = res.getFloat("pitch");
String world = res.getString("world");
((Player) sender).teleport(new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch));
sender.sendMessage(ChatColor.GREEN + "Teleported you back successfully.");
}
}
}
/**
* Maybe the /sethome command has been issued?
*
* @param sender the player that sent the command
* @param cmd the command
* @param args the command arguments
* @return false if the command failed, true otherwise.
*/
private boolean setHomeCommand(CommandSender sender, Command cmd, String[] args) {
if (cmd.getName().equalsIgnoreCase("sethome") && args.length >= 1) {
String name = getArgsAsString(args);
try {
if (getHomeList(((Player) sender).getUniqueId()).contains(name)) {
sender.sendMessage(ChatColor.GOLD + "You already named a home with the name" +
" '" + name + "'.");
return true;
}
addHome(((Player) sender).getUniqueId(),
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getWorld().getName(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch(),
name);
sender.sendMessage(ChatColor.GREEN + "Your home '" + name + "' has been set successfully.");
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later.");
getLogger().warning(e.getMessage());
}
return true;
}
return false;
}
/**
* Add and append all args from table to one string.
*
* @param args the args to append
* @return the appended string
*/
private String getArgsAsString(String[] args) {
StringBuilder name = new StringBuilder();
for (String arg : args) {
if (name.length() != 0) {
name.append(" ");
}
name.append(arg);
}
return name.toString();
}
/**
* Maybe the player issued the /setback command?
*
* @param sender the player.
* @param cmd the command the player sent.
* @param args the args after the command
* @return true if the command was good and false otherwise
*/
private boolean setBackCommand(CommandSender sender, Command cmd, String[] args) {
if (cmd.getName().equalsIgnoreCase("setback")) {
try {
removeBack(((Player) sender).getUniqueId());
addBack(((Player) sender).getUniqueId(),
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch(),
((Player) sender).getLocation().getWorld().getName());
sender.sendMessage(ChatColor.GREEN + "Your back has been set successfully.");
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later.");
getLogger().warning(e.getMessage());
}
return true;
}
return false;
}
/**
* Remove all back from a player.
*
* @param backedPlayer the player that is being backed.
* @throws SQLException if there were a SQL problem
*/
private void removeBack(UUID backedPlayer) throws SQLException {
this.connexion = getConnection();
PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + this.tableNameBack
+ " WHERE uuid = ?;"
);
stmt.setString(1, backedPlayer.toString());
stmt.execute();
connexion.close();
}
/**
* Add a home to a player.
*
* @param homedPlayer the player that needs a new home.
* @param x x coordinate for the player
* @param y y coordinate for the player
* @param z z coordinate for the player
* @param world the world name
* @param yaw yaw for the player
* @param pitch pitch for a player
* @param name name for the new home
* @throws SQLException if there were a SQL exception.
*/
private void addHome(UUID homedPlayer, double x, double y, double z, String world, float yaw, float pitch, String name) throws SQLException {
this.connexion = getConnection();
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
"INSERT INTO "
+ this.tableNameHome + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + this.tableNameHomeHistory
+ "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, homedPlayer.toString());
stmt.setString(2, name);
stmt.setString(3, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
stmt.setDouble(4, x);
stmt.setDouble(5, y);
stmt.setDouble(6, z);
stmt.setFloat(7, yaw);
stmt.setFloat(8, pitch);
stmt.setString(9, world);
stmt.execute();
}
connexion.close();
}
/**
* Add a back for a player.
*
* @param backedPlayer the player to add a back to.
* @param x its x coordinate
* @param y its y coordinate
* @param z its z coordinate
* @param world the world where the player were
* @param yaw the player's yaw
* @param pitch the player's pitch
* @throws SQLException if an SQL error occured.
*/
private void addBack(UUID backedPlayer, double x, double y, double z, float yaw, float pitch, String world) throws SQLException {
this.connexion = getConnection();
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + this.tableNameBack + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + this.tableNameBackHistory
+ "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, backedPlayer.toString());
stmt.setString(2, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
stmt.setDouble(3, x);
stmt.setDouble(4, y);
stmt.setDouble(5, z);
stmt.setFloat(6, yaw);
stmt.setFloat(7, pitch);
stmt.setString(8, world);
stmt.execute();
}
connexion.close();
}
/**
* Remove a home from a player.
*
* @param backedPlayer the player that is being backed.
* @throws SQLException if there were a SQL problem
*/
private void removeHome(UUID backedPlayer, String homeName) throws SQLException {
this.connexion = getConnection();
PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + this.tableNameHome + " WHERE uuid = ? AND name = ?;"
);
stmt.setString(1, backedPlayer.toString());
stmt.setString(2, homeName);
stmt.execute();
connexion.close();
}
/**
* Get all homes from a player UUIDs.
*
* @param player the player to get the /home name list from.
* @return a fresh list with all the muted players UUIDs
* @throws SQLException if there were a SQL exception.
*/
private List<String> getHomeList(UUID player) throws SQLException {
this.connexion = getConnection();
List<String> homeNames = new ArrayList<>();
PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT name FROM " + this.tableNameHome + " WHERE uuid = ?;");
recherche.setString(1, player.toString());
try (ResultSet res = recherche.executeQuery()) {
while (res.next()) {
homeNames.add(res.getString("name"));
}
}
this.connexion.close();
return homeNames;
}
/**
* Create the /back history table.
*
* @throws SQLException if there is a SQL problem.
*/
private void createBackHistoryTable() throws SQLException {
PreparedStatement stmt;
stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameBackHistory
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
+ "date DATETIME, "
+ "x DOUBLE, "
+ "y DOUBLE, "
+ "z DOUBLE, "
+ "yaw FLOAT, "
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
stmt.execute();
}
/**
* Create the /back table.
*
* @throws SQLException if there were a SQL problem.
*/
private void createBackTable() throws SQLException {
PreparedStatement stmt;
stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameBack
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT UNIQUE, "
+ "date DATETIME, "
+ "x DOUBLE, "
+ "y DOUBLE, "
+ "z DOUBLE, "
+ "yaw FLOAT, "
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
stmt.execute();
}
/**
* Create the /home history table.
*
* @throws SQLException if there were a SQL problem.
*/
private void createHomeHistoryTable() throws SQLException {
PreparedStatement stmt;
stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameHomeHistory
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
+ "name TEXT, "
+ "date DATETIME, "
+ "x DOUBLE, "
+ "y DOUBLE, "
+ "z DOUBLE, "
+ "yaw FLOAT, "
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
stmt.execute();
}
/**
* Create the /home table.
*
* @throws SQLException if there were a SQL problem.
*/
private void createHomeTable() throws SQLException {
PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameHome
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
+ "name TEXT, "
+ "date DATETIME, "
+ "x DOUBLE, "
+ "y DOUBLE, "
+ "z DOUBLE, "
+ "yaw FLOAT, "
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
stmt.execute();
}
}

View File

@ -0,0 +1,9 @@
name: BackHome
version: ${project.version}
main: louisvallat.xyz.backhome.BackHome
api-version: 1.13
prefix: BackHome
load: STARTUP
authors: [LouisVallat]
description: A plugin to set and go back to homes.
website: louis-vallat.xyz