Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/artifacts/ReviveTechnicalTest_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<version>1.18.36</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
</dependency>
</dependencies>

</project>
</project>
67 changes: 65 additions & 2 deletions src/main/java/fr/revivemc/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
package fr.revivemc;

import fr.revivemc.data.Cache;
import fr.revivemc.events.EntityDamageListener;
import fr.revivemc.events.EntityDeathListener;
import fr.revivemc.events.EntityRegenListener;
import fr.revivemc.events.PlayerJoinListener;
import fr.revivemc.data.Sql;
import fr.revivemc.yaml.ConfigYaml;
import fr.revivemc.yaml.LangYaml;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.HashMap;
import java.util.UUID;

public class Main extends JavaPlugin {

/**
* Configuration variable, lang or data
*/
public static ConfigYaml config_yaml;
public static LangYaml lang_yaml;
public static Sql sql;

// Variable for the cache
public static HashMap<UUID, Cache> cache;

/**
* Create the plugin folder if it hasn't been created
*/
private void loadDataFolder() {
File data_folder = Main.getPlugin(Main.class).getDataFolder();

if (!data_folder.exists())
data_folder.mkdir();
}

/**
* Server Startup
*/
@Override
public void onEnable() {}
public void onEnable() {
loadDataFolder();

sql = new Sql();
sql.init();

config_yaml = new ConfigYaml();
config_yaml.init();

lang_yaml = new LangYaml();
lang_yaml.init();

cache = new HashMap<>();

Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this);
Bukkit.getPluginManager().registerEvents(new EntityDeathListener(), this);
Bukkit.getPluginManager().registerEvents(new EntityDamageListener(), this);
Bukkit.getPluginManager().registerEvents(new EntityRegenListener(), this);
System.out.println("Plugin enabled");
}

/**
* Server Stop
*/
@Override
public void onDisable() {}
public void onDisable() {
sql.saveData();
sql.close();

System.out.println("Plugin disabled");
}

}
43 changes: 43 additions & 0 deletions src/main/java/fr/revivemc/data/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fr.revivemc.data;

public class Cache {

// Kills and Deaths variables
private int kills;
private int deaths;

/**
* Constructor for cache
* @param kills Number of kills
* @param deaths Number of deaths
*/
public Cache(int kills, int deaths) {
this.kills = kills;
this.deaths = deaths;
}

/**
* @return Returns the number of kills
*/
public int getKills() {
return (kills);
}

/**
* @return Returns the number of deaths
*/
public int getDeaths() {
return (deaths);
}

// Add 1 to kill count
public void addKill() {
kills++;
}

// Add 1 to death count
public void addDeath() {
deaths++;
}

}
79 changes: 79 additions & 0 deletions src/main/java/fr/revivemc/data/Sql.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package fr.revivemc.data;

import fr.revivemc.Main;
import fr.revivemc.data.wrapper.Players;
import org.bukkit.entity.Player;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.UUID;

public class Sql {

// Information of connection
private Connection connection;

/**
* This function allows you to create the different tables
* @param stmt A parameter to run SQL commands
* @throws SQLException Returns an exception if a sql error occurred
*/
private void createTable(Statement stmt) throws SQLException {
stmt.execute("CREATE TABLE IF NOT EXISTS players (uuid TEXT PRIMARY KEY, kill INTEGER, death INTEGER)");
}

/**
* Allows you to close the db
*/
public void close() {
try {
if (connection != null && !connection.isClosed())
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Allows you to initialize the db when the server is launched
*/
public void init() {
try {
Class.forName("org.sqlite.JDBC");

File data_folder = Main.getPlugin(Main.class).getDataFolder();

connection = DriverManager.getConnection("jdbc:sqlite:" + data_folder + "/data.db");
Statement statement = connection.createStatement();
createTable(statement);
statement.close();
System.out.println("Database connection established");
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}


/**
* Save data in the db
*/
public void saveData() {
for (Map.Entry<UUID, Cache> cache : Main.cache.entrySet()) {
Players.addPlayer(cache.getKey());
Players.setKillByUuid(cache.getKey(), cache.getValue().getKills());
Players.setDeathByUuid(cache.getKey(), cache.getValue().getDeaths());
}
}

/**
* @return Returns the connection
*/
public Connection getConnection() {
return (connection);
}

}
128 changes: 128 additions & 0 deletions src/main/java/fr/revivemc/data/wrapper/Players.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package fr.revivemc.data.wrapper;

import fr.revivemc.Main;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class Players {

/**
* This function allows the player to be initialized in the table
* @param uuid Player uuid
*/
public static void addPlayer(UUID uuid) {
try {
Connection connection = Main.sql.getConnection();

String query = "INSERT INTO players (uuid, kill, death) VALUES (?, 0, 0)";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, uuid.toString());

ps.executeUpdate();

} catch (SQLException e) {
System.err.println("Error while executing SQL statement " + e.getMessage());
}
}

/**
* This function allows you to add n to the player's kills
* @param uuid Player uuid
* @param n Number to add
*/
public static void setKillByUuid(UUID uuid, int n) {
try {
Connection connection = Main.sql.getConnection();

String query = "UPDATE players SET kill = ? WHERE uuid = ?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, String.valueOf(n));
ps.setString(2, uuid.toString());

ps.executeUpdate();

} catch (SQLException e) {
System.err.println("Error while executing SQL statement " + e.getMessage());
}
}

/**
* This function allows you to add n to the player's deaths
* @param uuid Player uuid
* @param n Number to add
*/
public static void setDeathByUuid(UUID uuid, int n) {
try {
Connection connection = Main.sql.getConnection();

String query = "UPDATE players SET death = ? WHERE uuid = ?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, String.valueOf(n));
ps.setString(2, uuid.toString());

ps.executeUpdate();

} catch (SQLException e) {
System.err.println("Error while executing SQL statement " + e.getMessage());
}
}

/**
* Function that allows you to retrieve the number of kills in relation to the player's UUID
* @param uuid Player uuid
* @return Returns the correct value or 0
*/
public static int getKillByUuid(UUID uuid) {
int kills = 0;

try {
Connection connection = Main.sql.getConnection();

String query = "SELECT kill FROM players WHERE uuid=?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, uuid.toString());

ResultSet row = ps.executeQuery();

if (row.next())
kills = row.getInt("kill");

} catch (SQLException e) {
System.err.println("Error while executing SQL statement " + e.getMessage());
}

return (kills);
}

/**
* Function that allows you to retrieve the number of deaths in relation to the player's UUID
* @param uuid Player uuid
* @return Returns the correct value or 0
*/
public static int getDeathByUuid(UUID uuid) {
int deaths = 0;

try {
Connection connection = Main.sql.getConnection();

String query = "SELECT death FROM players WHERE uuid=?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, uuid.toString());

ResultSet row = ps.executeQuery();

if (row.next())
deaths = row.getInt("death");

} catch (SQLException e) {
System.err.println("Error while executing SQL statement " + e.getMessage());
}

return (deaths);
}

}
Loading