added double jump and mysql support for the most part

This commit is contained in:
5vl 2022-04-08 14:13:04 +02:00
parent db288cd997
commit 2262ca36a6
No known key found for this signature in database
GPG Key ID: DA8938F22548E4D5
8 changed files with 194 additions and 24 deletions

24
pom.xml
View File

@ -36,30 +36,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

View File

@ -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;
}

View File

@ -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<Player> 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`))");
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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"

View File

@ -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