From 2262ca36a66ae6605cf12dfc0fb7c170468e71a3 Mon Sep 17 00:00:00 2001 From: 5vl Date: Fri, 8 Apr 2022 14:13:04 +0200 Subject: [PATCH] added double jump and mysql support for the most part --- pom.xml | 24 ------- .../java/me/fivevl/doublejump/Database.java | 17 +++++ src/main/java/me/fivevl/doublejump/Main.java | 57 ++++++++++++++++ src/main/java/me/fivevl/doublejump/MySQL.java | 66 +++++++++++++++++++ .../me/fivevl/doublejump/events/OnFly.java | 21 ++++++ .../me/fivevl/doublejump/events/OnJump.java | 24 +++++++ src/main/resources/config.yml | 6 ++ src/main/resources/plugin.yml | 3 + 8 files changed, 194 insertions(+), 24 deletions(-) create mode 100644 src/main/java/me/fivevl/doublejump/Database.java create mode 100644 src/main/java/me/fivevl/doublejump/MySQL.java create mode 100644 src/main/java/me/fivevl/doublejump/events/OnFly.java create mode 100644 src/main/java/me/fivevl/doublejump/events/OnJump.java create mode 100644 src/main/resources/config.yml diff --git a/pom.xml b/pom.xml index 3007ffa..fdbba5b 100644 --- a/pom.xml +++ b/pom.xml @@ -36,30 +36,6 @@ - - org.jetbrains.kotlin - kotlin-maven-plugin - ${kotlin.version} - - - compile - process-sources - - compile - - - - test-compile - test-compile - - test-compile - - - - - 1.8 - - org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/me/fivevl/doublejump/Database.java b/src/main/java/me/fivevl/doublejump/Database.java new file mode 100644 index 0000000..a68accf --- /dev/null +++ b/src/main/java/me/fivevl/doublejump/Database.java @@ -0,0 +1,17 @@ +package me.fivevl.doublejump; + +import javax.sql.rowset.CachedRowSet; +import java.sql.SQLException; + +public interface Database { + void connect() throws SQLException; + void disconnect() throws SQLException; + boolean isConnected() throws SQLException; + void setUsername(String username); + void setPassword(String password); + void setHost(String host); + void setDatabase(String database); + void setPort(String port); + CachedRowSet query(String query) throws SQLException; + void execute(String query) throws SQLException; +} diff --git a/src/main/java/me/fivevl/doublejump/Main.java b/src/main/java/me/fivevl/doublejump/Main.java index da1e028..26632b4 100644 --- a/src/main/java/me/fivevl/doublejump/Main.java +++ b/src/main/java/me/fivevl/doublejump/Main.java @@ -1,15 +1,72 @@ package me.fivevl.doublejump; +import me.fivevl.doublejump.events.OnFly; +import me.fivevl.doublejump.events.OnJump; +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class Main extends JavaPlugin { + public static List doDoubleJump = new ArrayList<>(); + private static Main instance; + public static Main getInstance() { + return instance; + } + private static MySQL sql; + public static MySQL getMySQL() { + return sql; + } @Override public void onEnable() { + instance = this; + sql = new MySQL(); + try { + setupDatabase(); + } catch (SQLException e) { + e.printStackTrace(); + } + Bukkit.getPluginManager().registerEvents(new OnFly(), this); + Bukkit.getPluginManager().registerEvents(new OnJump(), this); getLogger().info("DoubleJump enabled!"); } @Override public void onDisable() { + try { + sql.disconnect(); + } catch (SQLException e) { + e.printStackTrace(); + } getLogger().info("DoubleJump disabled!"); } + + private void setupDatabase() throws SQLException { + ConfigurationSection config = getConfig().getConfigurationSection("database"); + if (config == null) { + getLogger().severe("Database configuration not found!"); + return; + } + String[] credentials = {config.getString("host"), config.getString("port"), config.getString("database"), config.getString("username"), config.getString("password")}; + for (String s : credentials) { + if (s == null) { + getLogger().severe("Correct database configuration not found!"); + return; + } + } + if (credentials[0].equalsIgnoreCase("12.34.56.78")) { + getLogger().severe("Please set the database credentials in the config.yml!"); + return; + } + sql.setUsername(credentials[3]); + sql.setPassword(credentials[4]); + sql.setHost(credentials[0]); + sql.setPort(credentials[1]); + sql.setDatabase(credentials[2]); + sql.connect(); + sql.execute("CREATE TABLE IF NOT EXISTS `settings` (`uuid` VARCHAR(36) NOT NULL, `strength` DOUBLE NOT NULL DEFAULT '1', PRIMARY KEY (`uuid`))"); + } } diff --git a/src/main/java/me/fivevl/doublejump/MySQL.java b/src/main/java/me/fivevl/doublejump/MySQL.java new file mode 100644 index 0000000..ed84797 --- /dev/null +++ b/src/main/java/me/fivevl/doublejump/MySQL.java @@ -0,0 +1,66 @@ +package me.fivevl.doublejump; + +import javax.sql.rowset.CachedRowSet; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MySQL implements Database { + private Connection connection; + private String username; + private String password; + private String database; + private String host; + private String port; + @Override + public void connect() throws SQLException { + connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password); + } + + @Override + public void disconnect() throws SQLException { + connection.close(); + } + + @Override + public boolean isConnected() throws SQLException { + return !connection.isClosed(); + } + + @Override + public void setUsername(String username) { + this.username = username; + } + + @Override + public void setPassword(String password) { + this.password = password; + } + + @Override + public void setHost(String host) { + this.host = host; + } + + @Override + public void setDatabase(String database) { + this.database = database; + } + + @Override + public void setPort(String port) { + this.port = port; + } + + @Override + public CachedRowSet query(String query) throws SQLException{ + CachedRowSet cachedRowSet = new com.sun.rowset.CachedRowSetImpl(); + cachedRowSet.populate(connection.prepareStatement(query).executeQuery()); + return cachedRowSet; + } + + @Override + public void execute(String query) throws SQLException { + connection.prepareStatement(query).execute(); + } +} diff --git a/src/main/java/me/fivevl/doublejump/events/OnFly.java b/src/main/java/me/fivevl/doublejump/events/OnFly.java new file mode 100644 index 0000000..93c2207 --- /dev/null +++ b/src/main/java/me/fivevl/doublejump/events/OnFly.java @@ -0,0 +1,21 @@ +package me.fivevl.doublejump.events; + +import me.fivevl.doublejump.Main; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerToggleFlightEvent; + +public class OnFly implements Listener { + @EventHandler + public void onFly(PlayerToggleFlightEvent e) { + Player p = e.getPlayer(); + if (p.hasPermission("doublejump.use") && Main.doDoubleJump.contains(p) && (p.getGameMode() == GameMode.ADVENTURE || p.getGameMode() == GameMode.SURVIVAL)) { + e.setCancelled(true); + p.setAllowFlight(false); + p.setVelocity(p.getLocation().getDirection().multiply(2.0D)); + p.setFallDistance(0); + } + } +} diff --git a/src/main/java/me/fivevl/doublejump/events/OnJump.java b/src/main/java/me/fivevl/doublejump/events/OnJump.java new file mode 100644 index 0000000..00d03c1 --- /dev/null +++ b/src/main/java/me/fivevl/doublejump/events/OnJump.java @@ -0,0 +1,24 @@ +package me.fivevl.doublejump.events; + +import com.destroystokyo.paper.event.player.PlayerJumpEvent; +import me.fivevl.doublejump.Main; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class OnJump implements Listener { + @EventHandler + public void onJump(PlayerJumpEvent e) { + Player p = e.getPlayer(); + if (p.hasPermission("doublejump.use") && (p.getGameMode() == GameMode.ADVENTURE || p.getGameMode() == GameMode.SURVIVAL)) { + p.setAllowFlight(true); + Main.doDoubleJump.add(p); + Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), () -> { + p.setAllowFlight(false); + Main.doDoubleJump.remove(p); + }, 20L); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..473e7bd --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,6 @@ +database: # Database configuration, please change it to a valid one. + host: "12.34.56.78" + port: 3306 + username: "admin" + password: "password" + database: "database" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 070bc86..0b83ee4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,3 +4,6 @@ main: me.fivevl.doublejump.Main api-version: 1.18 authors: [ 5vl ] description: A trial project for DevRoom. +commands: + jump: + description: Opens the main GUI \ No newline at end of file