Skip to content

Commit

Permalink
Merge pull request #29 from Grubnest/hotfix-sql-spigot-link
Browse files Browse the repository at this point in the history
Hotfix sql spigot link
  • Loading branch information
Wyzards authored May 27, 2022
2 parents d820ccb + a1028fd commit 480db6a
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/grubnest/game/core/PluginMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.grubnest.game.core.paper.GrubnestCorePlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.grubnest.game.core.bridge.entities;

public interface GrubnestPlayer {

void updateUsername();

}
36 changes: 30 additions & 6 deletions src/main/java/com/grubnest/game/core/databasehandler/MySQL.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.grubnest.game.core.databasehandler;

import java.sql.Connection;
import com.velocitypowered.api.proxy.Player;

import java.sql.PreparedStatement;
import java.sql.SQLException;

Expand All @@ -20,30 +21,53 @@ public MySQL(MySQLData data) {
super(data);
}

//You make method to fetch / add data in this class

/**
* Close pool on plugin Disable
*/
public void onDisable() {
closePool();
}

/**
* Creates any tables required for the database. Runs at proxy server initialization
*/
public void createTables() {
try {
Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("""
PreparedStatement statement = getConnection().prepareStatement("""
CREATE TABLE IF NOT EXISTS `player` (
uuid varchar(36) PRIMARY KEY,
username varchar(16)
)
""");
statement.executeUpdate();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Updates a players username stored in the database
*
* @param player the player to store
*/
public void updatePlayerUsername(Player player) {
try {
PreparedStatement statement = getConnection().prepareStatement("""
INSERT INTO player
(uuid, username)
VALUES
(?, ?)
ON DUPLICATE KEY UPDATE
username = ?;
""");
statement.setString(1, player.getUniqueId().toString());
statement.setString(2, player.getUsername());
statement.setString(3, player.getUsername());
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.grubnest.game.core.databasehandler;

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Map;

/**
* MysqlData object to store credentials etc
*
* @author tamilpp25
* @version 1.0 at 15-5-2022
* @author tamilpp25, Theeef
* @version 1.0 at 5/26/2022
*/
public class MySQLData {
public final String HOST;
Expand All @@ -27,4 +32,28 @@ public MySQLData(String host, String username, String password, int port, String
this.maximumConnections = maximumConnections;
this.connectionTimeout = connectionTimeout;
}

/**
* Initialize data from config.yml
*
* @return MySQLData
*/
public static MySQLData dataInitializer() {
Yaml yaml = new Yaml();
InputStream inputStream = MySQLData.class.getClassLoader().getResourceAsStream("config.yml");
Map<String, Object> config;
config = (Map<String, Object>) ((Map<String, Object>) yaml.load(inputStream)).get("Database");

String host = (String) config.get("hostname");
int port = (int) config.get("port");
String database = (String) config.get("database");
String username = (String) config.get("username");
String password = (String) config.get("password");

int minimumConnections = (int) config.get("minimumConnections");
int maximumConnections = (int) config.get("maximumConnections");
long connectionTimeout = (long) (int) config.get("connectionTimeout");

return new MySQLData(host, username, password, port, database, minimumConnections, maximumConnections, connectionTimeout);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.grubnest.game.core;
package com.grubnest.game.core.paper;

import com.grubnest.game.core.PluginMessage;
import com.grubnest.game.core.databasehandler.MySQL;
import com.grubnest.game.core.databasehandler.MySQLData;
import com.grubnest.game.core.databasehandler.utils.Disabler;
Expand All @@ -25,7 +26,7 @@ public void onEnable() {
getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "GrubnestCore is Enabled");

loadConfig();
this.sql = new MySQL(dataInitializer());
this.sql = new MySQL(MySQLData.dataInitializer());
}

/**
Expand All @@ -47,24 +48,6 @@ public void onDisable() {
this.getServer().getMessenger().unregisterIncomingPluginChannel(this);
}

/**
* Initialize data from config.yml
*
* @return MySQLData
*/
private MySQLData dataInitializer() {
String host = getConfig().getString("Database.hostname");
int port = getConfig().getInt("Database.port");
String database = getConfig().getString("Database.database");
String username = getConfig().getString("Database.username");
String password = getConfig().getString("Database.password");

int minimumConnections = getConfig().getInt("Database.minimumConnections");
int maximumConnections = getConfig().getInt("Database.maximumConnections");
long connectionTimeout = getConfig().getLong("Database.connectionTimeout");
return new MySQLData(host, username, password, port, database, minimumConnections, maximumConnections, connectionTimeout);
}

/**
* Get SQL Object
*
Expand Down
33 changes: 7 additions & 26 deletions src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* running on the Grubnest network
*
* @author Theeef
* @version 1.1 at 5/23/2022
* @version 1.2 at 5/26/2022
*/
@Plugin(id = "grubnestcore", name = "Grubnest Core Plugin", version = "0.1.0-SNAPSHOT",
url = "htts://grubnest.com", description = "Grubnest Core running on Velocity", authors = {"Theeef"})
Expand All @@ -43,42 +43,23 @@ public VelocityPlugin(ProxyServer server, Logger logger) {
this.server = server;

this.server.sendMessage(Component.text("GrubnestCore is enabled on Velocity!"));
this.sql = new MySQL(dataInitializer());
this.sql = new MySQL(MySQLData.dataInitializer());

instance = this;
}

/**
* Runs when the Proxy server initializes
*
* @param event event used in callback
*/
@Subscribe
public void onInitialize(ProxyInitializeEvent event) {
this.server.getEventManager().register(this, new CoreEventListener());

getMySQL().createTables();
}

/**
* Initialize data from config.yml
*
* @return MySQLData
*/
private MySQLData dataInitializer() {
Yaml yaml = new Yaml();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.yml");
Map<String, Object> config;
config = (Map<String, Object>) ((Map<String, Object>) yaml.load(inputStream)).get("Database");

String host = (String) config.get("hostname");
int port = (int) config.get("port");
String database = (String) config.get("database");
String username = (String) config.get("username");
String password = (String) config.get("password");

int minimumConnections = (int) config.get("minimumConnections");
int maximumConnections = (int) config.get("maximumConnections");
long connectionTimeout = (long) (int) config.get("connectionTimeout");

return new MySQLData(host, username, password, port, database, minimumConnections, maximumConnections, connectionTimeout);
}

/**
* Get SQL Object
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.grubnest.game.core.velocity.entities;

import com.grubnest.game.core.bridge.entities.GrubnestPlayer;
import com.grubnest.game.core.velocity.VelocityPlugin;
import com.velocitypowered.api.proxy.Player;

/**
* Represents a custom player entity on the Velocity server
*
* @author Theeef
* @version 1.0 at 5/26/2022
*/
public class VPlayer implements GrubnestPlayer {

private Player player;

/**
* Creates a custom player instance for Velocity
*
* @param player the player this represents
*/
public VPlayer(Player player) {
this.player = player;
}

/**
* Updates the cached username of this player in the database
*/
public void updateUsername() {
VelocityPlugin.getInstance().getMySQL().updatePlayerUsername(this.player);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.grubnest.game.core.velocity.events;

import com.grubnest.game.core.velocity.VelocityPlugin;
import com.grubnest.game.core.velocity.entities.VPlayer;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import net.kyori.adventure.text.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* Listens for core events, like Server Connection for Velocity
Expand All @@ -26,27 +21,8 @@ public class CoreEventListener {
*/
@Subscribe
public void onServerConnect(ServerConnectedEvent event) {
try {
Connection connection = VelocityPlugin.getInstance().getMySQL().getConnection();
PreparedStatement statement = connection.prepareStatement("""
INSERT INTO player
(uuid, username)
VALUES
(?, ?)
ON DUPLICATE KEY UPDATE
username = ?;
""");
statement.setString(1, event.getPlayer().getUniqueId().toString());
statement.setString(2, event.getPlayer().getUsername());
statement.setString(3, event.getPlayer().getUsername());
statement.executeUpdate();
statement.close();
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}


VPlayer player = new VPlayer(event.getPlayer());
player.updateUsername();
}

}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: GrubnestCore
main: com.grubnest.game.core.GrubnestCorePlugin
main: com.grubnest.game.core.paper.GrubnestCorePlugin
version: 1.0
authors:
- Theeef
Expand Down

0 comments on commit 480db6a

Please sign in to comment.