Skip to content

Commit

Permalink
Begin work on database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Aug 4, 2017
1 parent 88101f5 commit 2fae20c
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![logo](imgs/icon64.png)![carrier](imgs/logo_carrier.png)
![ClaimChunk Logo](imgs/icon64.png)![ClaimChunk Title](imgs/logo_carrier.png)

[![Version Info](https://img.shields.io/badge/version-0.0.5-brightgreen.svg)](https://github.com/cjburkey01/ClaimChunk/releases)
[![Download Info](https://img.shields.io/badge/Download-Spigot-blue.svg)](https://www.spigotmc.org/resources/claimchunk.44458/)
Expand All @@ -10,7 +10,7 @@ Spigot plugin for 1.8+ allowing the claiming of chunks.
*The destiny of chunks is to unite not to divide*<br>
*Let's make the world ours.*

Usage and more information can be found [HERE](https://github.com/cjburkey01/ClaimChunk/wiki), on the Wiki.
Usage and more information can be found [on the wiki](https://github.com/cjburkey01/ClaimChunk/wiki).

* 1.8 - 1.12.1 Work seamlessly.
* 1.6 - 1.7.10 Work when titles are disabled in config.
Expand All @@ -22,7 +22,7 @@ For more information:

Page on SpigotMC can be found [HERE](https://www.spigotmc.org/resources/claimchunk.44458/).

Current version: **0.0.5** for Minecraft **1.12**.
Current version: **[0.0.5](https://github.com/cjburkey01/ClaimChunk/releases)** for Minecraft **1.12**.

Optional:
* [Vault](https://www.spigotmc.org/resources/vault.41918/).
Expand Down
File renamed without changes
18 changes: 6 additions & 12 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cjburkey.claimchunk;

import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.java.JavaPlugin;
import com.cjburkey.claimchunk.chunk.ChunkHandler;
import com.cjburkey.claimchunk.cmd.CommandHandler;
Expand All @@ -20,10 +19,8 @@ public final class ClaimChunk extends JavaPlugin {

private boolean useEcon = false;
private boolean useDynmap = false;
private boolean useSql = false;

//private File dataFile;
//private File accessFile;
//private File namesFile;
private File chunkFile;
private File plyFile;

Expand All @@ -39,18 +36,15 @@ public void onLoad() {
}

public void onEnable() {
//dataFile = new File(getDataFolder(), "/data/claimed.chks");
//plyFile = new File(getDataFolder(), "/data/playerCache.dat");
//accessFile = new File(getDataFolder(), "/data/grantedAccess.dat");
chunkFile = new File(getDataFolder(), "/data/claimedChunks.json");
plyFile = new File(getDataFolder(), "/data/playerData.json");

cmd = new CommandHandler();
cmds = new Commands();
economy = new Econ();
map = new ClaimChunkDynmap();
playerHandler = new PlayerHandler(plyFile);
chunkHandler = new ChunkHandler(chunkFile);
playerHandler = new PlayerHandler(useSql, plyFile);
chunkHandler = new ChunkHandler(useSql, chunkFile);

File oldChunks = new File(getDataFolder(), "/data/claimed.chks");
File oldCache = new File(getDataFolder(), "/data/playerCache.dat");
Expand Down Expand Up @@ -100,7 +94,7 @@ public void onEnable() {
try {
chunkHandler.readFromDisk();
playerHandler.readFromDisk();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
Utils.log("Loaded data.");
Expand All @@ -116,7 +110,7 @@ public void onDisable() {
chunkHandler.writeToDisk();
playerHandler.writeToDisk();
Utils.log("Saved data.");
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
Utils.log("Finished disable.");
Expand Down Expand Up @@ -153,7 +147,7 @@ private void reloadData() {

chunkHandler.readFromDisk();
playerHandler.readFromDisk();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
Utils.log("Couldn't reload data: \"" + e.getMessage() + "\"");
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import org.bukkit.World;
import org.bukkit.entity.Player;
import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.data.IDataStorage;
import com.cjburkey.claimchunk.data.JsonDataStorage;
import com.cjburkey.claimchunk.data.SqlDataStorage;

public final class ChunkHandler {

private final Map<ChunkPos, UUID> claimed = new ConcurrentHashMap<>();
private final JsonDataStorage<DataChunk> data;
private final IDataStorage<DataChunk> data;

public ChunkHandler(File saveFile) {
data = new JsonDataStorage<>(DataChunk[].class, saveFile);
public ChunkHandler(boolean sql, File saveFile) {
data = (sql) ? new SqlDataStorage<>() : new JsonDataStorage<>(DataChunk[].class, saveFile);
}

/**
Expand Down Expand Up @@ -106,15 +108,15 @@ public boolean hasChunk(UUID uniqueId) {
return false;
}

public void writeToDisk() throws IOException {
public void writeToDisk() throws Exception {
data.clearData();
for (Entry<ChunkPos, UUID> entry : claimed.entrySet()) {
data.addData(new DataChunk(entry.getKey(), entry.getValue()));
}
data.saveData();
}

public void readFromDisk() throws IOException {
public void readFromDisk() throws Exception {
data.reloadData();
claimed.clear();
for (DataChunk c : data.getData()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ public static void check(File chunk, File cache, File access, ClaimChunk c) thro
}
}

private static void convertChunks(File file, ChunkHandler handler) throws IOException {
private static void convertChunks(File file, ChunkHandler handler) throws Exception {
Utils.log("Updating chunks.");
readChunks(file, handler);
handler.writeToDisk();
file.delete();
}

private static void convertCache(File file, PlayerHandler handler) throws ClassNotFoundException, IOException {
private static void convertCache(File file, PlayerHandler handler) throws Exception {
Utils.log("Updating cache.");
readOldCacher(file, handler);
handler.writeToDisk();
file.delete();
}

private static void convertAccess(File file, PlayerHandler handler) throws ClassNotFoundException, IOException {
private static void convertAccess(File file, PlayerHandler handler) throws Exception {
Utils.log("Updating access.");
readOldAccess(file, handler);
handler.writeToDisk();
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/data/SqlDataStorage.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package com.cjburkey.claimchunk.data;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.Config;
import com.cjburkey.claimchunk.Utils;
import com.cjburkey.claimchunk.database.DatabaseConnect;

public class SqlDataStorage<T> implements IDataStorage<T> {

private final List<T> storage = new ArrayList<>();
private final DatabaseConnect connect;

public SqlDataStorage() {
String host = Config.getString("database", "hostname");
int port = Config.getInt("database", "port");
String database = Config.getString("database", "database");
String user = Config.getString("database", "username");
String pass = Config.getString("database", "password");
connect = new DatabaseConnect(host, database, user, pass, port);
try {
boolean worked = connect.openConnection();
if (!worked) {
Utils.log("&4Couldn't create SQL connection. Connection could not be made or JDBC could not be found.");
Bukkit.getServer().getPluginManager().disablePlugin(ClaimChunk.getInstance());
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public void saveData() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.cjburkey.claimchunk.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.cjburkey.claimchunk.Utils;

public class DatabaseConnect {

private final String hostName;
private final String database;
private final String username;
private final String password;
private final int port;
private Connection connection;

public DatabaseConnect(String hostName, String database, String user, String pass, int port) {
this.hostName = hostName;
this.database = database;
username = user;
password = pass;
this.port = port;
}

public boolean openConnection() throws SQLException {
synchronized (this) {
if (connection != null && !connection.isClosed()) {
return false;
}
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
Utils.log("JDBC not found.");
return false;
}
StringBuilder connect = new StringBuilder();
connect.append("jdbc:mysql://");
connect.append(hostName);
connect.append(':');
connect.append(port);
connect.append('/');
connect.append(database);
connection = DriverManager.getConnection(connect.toString(), username, password);
return true;
}

}

}
12 changes: 7 additions & 5 deletions src/main/java/com/cjburkey/claimchunk/player/PlayerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.bukkit.entity.Player;
import com.cjburkey.claimchunk.data.IDataStorage;
import com.cjburkey.claimchunk.data.JsonDataStorage;
import com.cjburkey.claimchunk.data.SqlDataStorage;

public class PlayerHandler {

private final Queue<DataPlayer> playerData = new ConcurrentLinkedQueue<>();
private final JsonDataStorage<DataPlayer> data;
private final IDataStorage<DataPlayer> data;

public PlayerHandler(File file) {
data = new JsonDataStorage<>(DataPlayer[].class, file);
public PlayerHandler(boolean sql, File file) {
data = (sql) ? new SqlDataStorage<>() : new JsonDataStorage<>(DataPlayer[].class, file);
}

/**
Expand Down Expand Up @@ -125,15 +127,15 @@ public void addOldPlayerData(UUID id, String name) {
}
}

public void writeToDisk() throws IOException {
public void writeToDisk() throws Exception {
data.clearData();
for (DataPlayer a : playerData) {
data.addData(a.clone());
}
data.saveData();
}

public void readFromDisk() throws IOException {
public void readFromDisk() throws Exception {
data.reloadData();
playerData.clear();
for (DataPlayer ply : data.getData()) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ data:
# In minutes
saveDataInterval: 5

# Not implemented yet
database:
useDatabase: false
hostname: '127.0.0.1'
port: 3306
database: 'MyServer'
username: 'root'
password: ''

messages:
noPluginPerm: 'You do not have permission to use ClaimChunk.'
claimNoPerm: 'You do not have permission to claim chunks.'
Expand Down

0 comments on commit 2fae20c

Please sign in to comment.