add sqllite db option
This commit is contained in:
parent
f701e5d1fc
commit
48d0a4ccd2
|
@ -28,6 +28,13 @@ import java.util.List;
|
|||
|
||||
public class Config
|
||||
{
|
||||
//DBType
|
||||
private String m_DBType = "SQLLite";
|
||||
public String getDBType()
|
||||
{
|
||||
return m_DBType;
|
||||
}
|
||||
|
||||
//MySQL
|
||||
private String m_MySqlHost = "127.0.0.1";
|
||||
public String getMysqlHost()
|
||||
|
@ -128,6 +135,8 @@ public class Config
|
|||
|
||||
private void setDefaultConfigValues(FileConfiguration _config)
|
||||
{
|
||||
_config.addDefault("DB.Type", m_DBType);
|
||||
|
||||
_config.addDefault("mysql.host", m_MySqlHost);
|
||||
_config.addDefault("mysql.port", m_MySqlPort);
|
||||
_config.addDefault("mysql.db", m_MySqlDB);
|
||||
|
@ -153,6 +162,8 @@ public class Config
|
|||
|
||||
private void getConfigValues(FileConfiguration _config)
|
||||
{
|
||||
m_DBType = _config.getString("DB.Type");
|
||||
|
||||
m_MySqlHost = _config.getString("mysql.host");
|
||||
m_MySqlPort = _config.getInt("mysql.port");
|
||||
m_MySqlDB = _config.getString("mysql.db");
|
||||
|
@ -186,6 +197,8 @@ public class Config
|
|||
|
||||
private void saveConfigValues(FileConfiguration _config)
|
||||
{
|
||||
_config.set("DB.Type", m_DBType);
|
||||
|
||||
_config.set("mysql.host", m_MySqlHost);
|
||||
_config.set("mysql.port", m_MySqlPort);
|
||||
_config.set("mysql.db", m_MySqlDB);
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package li.inc.PlaytimeReloaded.DataStore.DB;
|
||||
|
||||
import li.inc.PlaytimeReloaded.DataStore.Config;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DB
|
||||
{
|
||||
private IDB m_DBClass = null;
|
||||
|
||||
public DB(Plugin _plugin, Config _config)
|
||||
{
|
||||
if(_config.getDBType().toLowerCase().trim() == "MySQL")
|
||||
m_DBClass = new MySQL();
|
||||
|
||||
if(_config.getDBType().toLowerCase().trim() == "SQLLite")
|
||||
m_DBClass = new SQLLite();
|
||||
|
||||
if(m_DBClass == null)
|
||||
m_DBClass = new SQLLite();
|
||||
|
||||
m_DBClass.init(_plugin, _config);
|
||||
}
|
||||
|
||||
public void update(UUID _playerUUID, int _newTime)
|
||||
{
|
||||
m_DBClass.update(_playerUUID, _newTime);
|
||||
}
|
||||
|
||||
public List<String[]> getTopPlayers()
|
||||
{
|
||||
return m_DBClass.getTopPlayers();
|
||||
}
|
||||
|
||||
public int getPlayerTime(UUID _playerUUID)
|
||||
{
|
||||
return m_DBClass.getPlayerTime(_playerUUID);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2016
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
|
||||
*
|
||||
* PlaytimeReloaded is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PlaytimeReloaded is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package li.inc.PlaytimeReloaded.DataStore.DB;
|
||||
|
||||
import li.inc.PlaytimeReloaded.DataStore.Config;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface IDB
|
||||
{
|
||||
public void init(Plugin _plugin, Config _config);
|
||||
public void update(UUID _playerUUID, int _newTime);
|
||||
public List<String[]> getTopPlayers();
|
||||
public int getPlayerTime(UUID _playerUUID);
|
||||
}
|
|
@ -30,14 +30,14 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MySQL
|
||||
public class MySQL implements IDB
|
||||
{
|
||||
private Plugin m_plugin;
|
||||
private Config m_config;
|
||||
|
||||
private MySQLTools m_MySqlTools;
|
||||
|
||||
public MySQL(Plugin _plugin, Config _config)
|
||||
public void init(Plugin _plugin, Config _config)
|
||||
{
|
||||
m_plugin = _plugin;
|
||||
m_config = _config;
|
||||
|
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* Copyright (c) 2016
|
||||
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
||||
*
|
||||
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
|
||||
*
|
||||
* PlaytimeReloaded is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PlaytimeReloaded is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package li.inc.PlaytimeReloaded.DataStore.DB;
|
||||
|
||||
import li.inc.PlaytimeReloaded.DataStore.Config;
|
||||
import li.inc.PlaytimeReloaded.DataStore.UUIDCache;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SQLLite implements IDB
|
||||
{
|
||||
private Plugin m_plugin;
|
||||
private Config m_config;
|
||||
|
||||
Connection m_connection = null;
|
||||
|
||||
public void init(Plugin _plugin, Config _config)
|
||||
{
|
||||
m_plugin = _plugin;
|
||||
m_config = _config;
|
||||
|
||||
try
|
||||
{
|
||||
m_connection = DriverManager.getConnection("jdbc:sqlite:Plugins/PlaytimeReloaded/playtime.db");
|
||||
|
||||
Statement _statement = m_connection.createStatement();
|
||||
|
||||
_statement.executeUpdate("CREATE TABLE IF NOT EXISTS playtime(playeruuid varchar(36), playtime int(6))");
|
||||
_statement.close();
|
||||
|
||||
}catch (SQLException _ex) {
|
||||
_ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void update(UUID _playerUUID, int _newTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
Statement _statement = m_connection.createStatement();
|
||||
_statement.executeUpdate("DELETE FROM playtime WHERE playeruuid = '" + _playerUUID.toString() + "'");
|
||||
_statement.executeUpdate("INSERT INTO playtime (`playeruuid`, `playtime`) VALUES ('" + _playerUUID.toString() + "', '" + _newTime + "')");
|
||||
|
||||
_statement.close();
|
||||
}catch (SQLException _ex) {
|
||||
_ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String[]> getTopPlayers()
|
||||
{
|
||||
List<String[]> _returnList = new ArrayList<String[]>();
|
||||
|
||||
try
|
||||
{
|
||||
Statement _statement = m_connection.createStatement();
|
||||
ResultSet _results = _statement.executeQuery("SELECT * FROM playtime ORDER BY playtime DESC LIMIT 5");
|
||||
|
||||
while (_results.next())
|
||||
{
|
||||
try
|
||||
{
|
||||
_returnList.add(new String[]{UUIDCache.get(UUID.fromString(_results.getString("playeruuid"))), _results.getString("playtime")});
|
||||
}catch(Exception _e)
|
||||
{
|
||||
return new ArrayList<String[]>();
|
||||
}
|
||||
}
|
||||
|
||||
_results.close();
|
||||
_statement.close();
|
||||
}catch (SQLException _ex) {
|
||||
_ex.printStackTrace();
|
||||
}
|
||||
|
||||
return _returnList;
|
||||
}
|
||||
|
||||
public int getPlayerTime(UUID _playerUUID)
|
||||
{
|
||||
try
|
||||
{
|
||||
Statement _statement = m_connection.createStatement();
|
||||
ResultSet _results = _statement.executeQuery("SELECT * FROM playtime WHERE `playeruuid` = '" + _playerUUID.toString() + "'");
|
||||
|
||||
while (_results.next())
|
||||
{
|
||||
try
|
||||
{
|
||||
return _results.getInt("playtime");
|
||||
}catch(Exception _e)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
_results.close();
|
||||
_statement.close();
|
||||
}catch (SQLException _ex) {
|
||||
_ex.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@ package li.inc.PlaytimeReloaded;
|
|||
import com.earth2me.essentials.Essentials;
|
||||
import com.earth2me.essentials.User;
|
||||
import li.inc.PlaytimeReloaded.DataStore.Config;
|
||||
import li.inc.PlaytimeReloaded.DataStore.DB.DB;
|
||||
import li.inc.PlaytimeReloaded.DataStore.DB.MySQL;
|
||||
import li.inc.PlaytimeReloaded.DataStore.TimeCommand;
|
||||
import li.inc.PlaytimeReloaded.DataStore.UUIDCache;
|
||||
|
@ -43,7 +44,7 @@ import java.util.*;
|
|||
public class Playtime extends JavaPlugin implements Listener
|
||||
{
|
||||
private Config m_config;
|
||||
private MySQL m_mysql;
|
||||
private DB m_db;
|
||||
|
||||
private Essentials m_pluginEssentials;
|
||||
|
||||
|
@ -54,7 +55,7 @@ public class Playtime extends JavaPlugin implements Listener
|
|||
m_config = new Config(this);
|
||||
|
||||
//Load the text from the lang config.
|
||||
m_mysql = new MySQL(this, m_config);
|
||||
m_db = new DB(this, m_config);
|
||||
|
||||
//Load Essentials
|
||||
m_pluginEssentials = (Essentials)Bukkit.getServer().getPluginManager().getPlugin("Essentials");
|
||||
|
@ -79,7 +80,7 @@ public class Playtime extends JavaPlugin implements Listener
|
|||
{
|
||||
_player.sendMessage(getChatMessage(m_config.getTextTopPlayerListHead(), _player.getName(), 0, 0, true));
|
||||
|
||||
List<String[]> _topPlayers = m_mysql.getTopPlayers();
|
||||
List<String[]> _topPlayers = m_db.getTopPlayers();
|
||||
int _rang = 1;
|
||||
for (String[] _playerData: _topPlayers)
|
||||
{
|
||||
|
@ -107,7 +108,7 @@ public class Playtime extends JavaPlugin implements Listener
|
|||
|
||||
if(_player.hasPermission("playtime.use"))
|
||||
{
|
||||
_player.sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), _player.getName(), m_mysql.getPlayerTime(_player.getUniqueId()), 0, true));
|
||||
_player.sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), _player.getName(), m_db.getPlayerTime(_player.getUniqueId()), 0, true));
|
||||
}else{
|
||||
_player.sendMessage(getChatMessage(m_config.getTextNoPermission(), _player.getName(), 0, 0, true));
|
||||
}
|
||||
|
@ -126,12 +127,12 @@ public class Playtime extends JavaPlugin implements Listener
|
|||
Player _player = (Player)sender;
|
||||
if(_player.hasPermission("playtime.use.others"))
|
||||
{
|
||||
_player.sendMessage(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_mysql.getPlayerTime(_searchPlayer), 0, true));
|
||||
_player.sendMessage(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_db.getPlayerTime(_searchPlayer), 0, true));
|
||||
}else{
|
||||
_player.sendMessage(getChatMessage(m_config.getTextNoPermission(), args[0], 0, 0, true));
|
||||
}
|
||||
}else{
|
||||
this.getLogger().info(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_mysql.getPlayerTime(_searchPlayer), 0, false));
|
||||
this.getLogger().info(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_db.getPlayerTime(_searchPlayer), 0, false));
|
||||
}
|
||||
}else{
|
||||
//Spieler wurde nicht gefunden / Hat noch nicht auf dem Server gespielt.
|
||||
|
@ -154,7 +155,7 @@ public class Playtime extends JavaPlugin implements Listener
|
|||
{
|
||||
if(event.getPlayer().hasPermission("playtime.use") && event.getPlayer().hasPermission("playtime.login"))
|
||||
{
|
||||
event.getPlayer().sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), event.getPlayer().getName(), m_mysql.getPlayerTime(event.getPlayer().getUniqueId()), 0, true));
|
||||
event.getPlayer().sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), event.getPlayer().getName(), m_db.getPlayerTime(event.getPlayer().getUniqueId()), 0, true));
|
||||
}
|
||||
|
||||
UUIDCache.update(event.getPlayer().getName(), event.getPlayer().getUniqueId());
|
||||
|
@ -179,11 +180,11 @@ public class Playtime extends JavaPlugin implements Listener
|
|||
|
||||
private void addPlayTime(UUID _playerUUID, int _time)
|
||||
{
|
||||
int _spielerPlaytime = m_mysql.getPlayerTime(_playerUUID);
|
||||
int _spielerPlaytime = m_db.getPlayerTime(_playerUUID);
|
||||
|
||||
_spielerPlaytime = _spielerPlaytime + _time;
|
||||
|
||||
m_mysql.update(_playerUUID, _spielerPlaytime);
|
||||
m_db.update(_playerUUID, _spielerPlaytime);
|
||||
|
||||
checkPlaytimeCommand(_playerUUID, _spielerPlaytime);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue