reformat code

This commit is contained in:
Chris 2020-12-28 09:11:09 +01:00
parent 752da189c2
commit 712f732fb6
11 changed files with 540 additions and 673 deletions

View File

@ -1,22 +1,22 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore; package li.inc.PlaytimeReloaded.DataStore;
@ -26,121 +26,111 @@ import org.bukkit.plugin.Plugin;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Config public class Config {
{
//DBType //DBType
private String m_DBType = "SQLLite"; private String m_DBType = "SQLLite";
public String getDBType() //MySQL
{ private String m_MySqlHost = "127.0.0.1";
private int m_MySqlPort = 3306;
private String m_MySqlDB = "";
private String m_mySqlUserName = "USER";
private String m_mySqlPassword = "PASSWORD";
//Texte
private String m_textPrefix = "&6[&aPlayTime&6] &a";
private String m_textYourCurrentPlaytime = "Your current playtime is &6%t";
private String m_textPlayerPlaytimeIs = "The playtime from player &6%s&a is &6%t";
private String m_textPlayerNotFound = "&cThis player dont have play on this server.";
private String m_textTopPlayerListHead = "This is the Top %r player list.";
private String m_textPlayerEntry = "%r : %s - %t";
private String m_textNoPermission = "&cYou dont have permission to do this!";
private String m_textDateDays = "&aDays&6";
private String m_textDateHours = "&aHours&6";
private String m_textDateMinutes = "&aMinutes&a";
private List<TimeCommand> m_timeCommands = new ArrayList<TimeCommand>();
private Plugin m_plugin;
public Config(Plugin _plugin) {
m_plugin = _plugin;
setDefaultConfigValues(m_plugin.getConfig());
getConfigValues(m_plugin.getConfig());
saveConfigValues(m_plugin.getConfig());
}
public String getDBType() {
return m_DBType; return m_DBType;
} }
//MySQL public String getMysqlHost() {
private String m_MySqlHost = "127.0.0.1"; return m_MySqlHost;
public String getMysqlHost() }
{
return m_MySqlHost;
}
private int m_MySqlPort = 3306; public int getMysqlPort() {
public int getMysqlPort() return m_MySqlPort;
{ }
return m_MySqlPort;
}
private String m_MySqlDB = ""; public String getMysqlDB() {
public String getMysqlDB() return m_MySqlDB;
{ }
return m_MySqlDB;
}
private String m_mySqlUserName = "USER"; public String getMysqlUsername() {
public String getMysqlUsername() return m_mySqlUserName;
{ }
return m_mySqlUserName;
}
private String m_mySqlPassword = "PASSWORD"; public String getMysqlPassword() {
public String getMysqlPassword() return m_mySqlPassword;
{ }
return m_mySqlPassword;
}
//Texte public String getTextPrefix() {
private String m_textPrefix = "&6[&aPlayTime&6] &a";
public String getTextPrefix()
{
return m_textPrefix; return m_textPrefix;
} }
private String m_textYourCurrentPlaytime = "Your current playtime is &6%t"; public String getTextYourCurrentPlaytime() {
public String getTextYourCurrentPlaytime() return m_textYourCurrentPlaytime;
{ }
return m_textYourCurrentPlaytime;
}
private String m_textPlayerPlaytimeIs = "The playtime from player &6%s&a is &6%t"; public String getTextPlayerPlaytimeIs() {
public String getTextPlayerPlaytimeIs()
{
return m_textPlayerPlaytimeIs; return m_textPlayerPlaytimeIs;
} }
private String m_textPlayerNotFound = "&cThis player dont have play on this server."; public String getTextPlayerNotFound() {
public String getTextPlayerNotFound()
{
return m_textPlayerNotFound; return m_textPlayerNotFound;
} }
private String m_textTopPlayerListHead = "This is the Top %r player list."; public String getTextTopPlayerListHead() {
public String getTextTopPlayerListHead() return m_textTopPlayerListHead;
{ }
return m_textTopPlayerListHead;
}
private String m_textPlayerEntry = "%r : %s - %t"; public String getTextPlayerEntry() {
public String getTextPlayerEntry() return m_textPlayerEntry;
{ }
return m_textPlayerEntry;
}
private String m_textNoPermission = "&cYou dont have permission to do this!"; public String getTextNoPermission() {
public String getTextNoPermission()
{
return m_textNoPermission; return m_textNoPermission;
} }
private String m_textDateDays = "&aDays&6"; public String getTextDateDays() {
public String getTextDateDays()
{
return m_textDateDays; return m_textDateDays;
} }
private String m_textDateHours = "&aHours&6"; public String getTextDateHours() {
public String getTextDateHours()
{
return m_textDateHours; return m_textDateHours;
} }
private String m_textDateMinutes = "&aMinutes&a"; public String getTextDateMinutes() {
public String getTextDateMinutes()
{
return m_textDateMinutes; return m_textDateMinutes;
} }
private List<TimeCommand> m_timeCommands = new ArrayList<TimeCommand>(); public List<TimeCommand> getTimeCommandList() {
public List<TimeCommand> getTimeCommandList()
{
return m_timeCommands; return m_timeCommands;
} }
private void setDefaultConfigValues(FileConfiguration _config) private void setDefaultConfigValues(FileConfiguration _config) {
{
_config.addDefault("DB.Type", m_DBType); _config.addDefault("DB.Type", m_DBType);
_config.addDefault("mysql.host", m_MySqlHost); _config.addDefault("mysql.host", m_MySqlHost);
_config.addDefault("mysql.port", m_MySqlPort); _config.addDefault("mysql.port", m_MySqlPort);
_config.addDefault("mysql.db", m_MySqlDB); _config.addDefault("mysql.db", m_MySqlDB);
_config.addDefault("mysql.user", m_mySqlUserName); _config.addDefault("mysql.user", m_mySqlUserName);
_config.addDefault("mysql.pass", m_mySqlPassword); _config.addDefault("mysql.pass", m_mySqlPassword);
_config.addDefault("text.Prefix", m_textPrefix); _config.addDefault("text.Prefix", m_textPrefix);
@ -158,17 +148,16 @@ public class Config
_config.addDefault("text.TopPlayerEntry", m_textPlayerEntry); _config.addDefault("text.TopPlayerEntry", m_textPlayerEntry);
_config.addDefault("timeCommand", m_timeCommands); _config.addDefault("timeCommand", m_timeCommands);
} }
private void getConfigValues(FileConfiguration _config) private void getConfigValues(FileConfiguration _config) {
{
m_DBType = _config.getString("DB.Type"); m_DBType = _config.getString("DB.Type");
m_MySqlHost = _config.getString("mysql.host"); m_MySqlHost = _config.getString("mysql.host");
m_MySqlPort = _config.getInt("mysql.port"); m_MySqlPort = _config.getInt("mysql.port");
m_MySqlDB = _config.getString("mysql.db"); m_MySqlDB = _config.getString("mysql.db");
m_mySqlUserName = _config.getString("mysql.user"); m_mySqlUserName = _config.getString("mysql.user");
m_mySqlPassword = _config.getString("mysql.pass"); m_mySqlPassword = _config.getString("mysql.pass");
m_textPrefix = _config.getString("text.Prefix"); m_textPrefix = _config.getString("text.Prefix");
@ -184,25 +173,22 @@ public class Config
m_textTopPlayerListHead = _config.getString("text.TopPlayerTitel"); m_textTopPlayerListHead = _config.getString("text.TopPlayerTitel");
m_textPlayerEntry = _config.getString("text.TopPlayerEntry"); m_textPlayerEntry = _config.getString("text.TopPlayerEntry");
for (String _l: _config.getStringList("timeCommand")) for (String _l : _config.getStringList("timeCommand")) {
{
String[] _ld = _l.split(";"); String[] _ld = _l.split(";");
if(_ld.length == 2) if (_ld.length == 2) {
{
m_timeCommands.add(new TimeCommand(Integer.parseInt(_ld[0]), _ld[1])); m_timeCommands.add(new TimeCommand(Integer.parseInt(_ld[0]), _ld[1]));
} }
} }
} }
private void saveConfigValues(FileConfiguration _config) private void saveConfigValues(FileConfiguration _config) {
{
_config.set("DB.Type", m_DBType); _config.set("DB.Type", m_DBType);
_config.set("mysql.host", m_MySqlHost); _config.set("mysql.host", m_MySqlHost);
_config.set("mysql.port", m_MySqlPort); _config.set("mysql.port", m_MySqlPort);
_config.set("mysql.db", m_MySqlDB); _config.set("mysql.db", m_MySqlDB);
_config.set("mysql.user", m_mySqlUserName); _config.set("mysql.user", m_mySqlUserName);
_config.set("mysql.pass", m_mySqlPassword); _config.set("mysql.pass", m_mySqlPassword);
_config.set("text.Prefix", m_textPrefix); _config.set("text.Prefix", m_textPrefix);
@ -220,28 +206,17 @@ public class Config
_config.set("text.TopPlayerEntry", m_textPlayerEntry); _config.set("text.TopPlayerEntry", m_textPlayerEntry);
List<String> _timeCommands = new ArrayList<String>(); List<String> _timeCommands = new ArrayList<String>();
for (TimeCommand _tc: m_timeCommands) for (TimeCommand _tc : m_timeCommands) {
{
_timeCommands.add(_tc.getTime() + ";" + _tc.getCommand()); _timeCommands.add(_tc.getTime() + ";" + _tc.getCommand());
} }
_config.set("timeCommand", _timeCommands); _config.set("timeCommand", _timeCommands);
m_plugin.saveConfig(); m_plugin.saveConfig();
} }
private Plugin m_plugin; public void reload() {
public Config(Plugin _plugin) m_plugin.reloadConfig();
{ getConfigValues(m_plugin.getConfig());
m_plugin = _plugin; }
setDefaultConfigValues(m_plugin.getConfig());
getConfigValues(m_plugin.getConfig());
saveConfigValues(m_plugin.getConfig());
}
public void reload()
{
m_plugin.reloadConfig();
getConfigValues(m_plugin.getConfig());
}
} }

View File

@ -6,38 +6,35 @@ import org.bukkit.plugin.Plugin;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class DB public class DB {
{
private IDB m_DBClass = null; private IDB m_DBClass = null;
public DB(Plugin _plugin, Config _config) public DB(Plugin _plugin, Config _config) {
{ if (_config.getDBType().trim().equalsIgnoreCase("mysql"))
if(_config.getDBType().trim().equalsIgnoreCase("mysql"))
m_DBClass = new MySQL(); m_DBClass = new MySQL();
if(_config.getDBType().trim().equalsIgnoreCase("sqllite")) if (_config.getDBType().trim().equalsIgnoreCase("sqllite"))
m_DBClass = new SQLLite(); m_DBClass = new SQLLite();
if(m_DBClass == null) if (m_DBClass == null)
m_DBClass = new SQLLite(); m_DBClass = new SQLLite();
m_DBClass.init(_plugin, _config); m_DBClass.init(_plugin, _config);
} }
public void update(UUID _playerUUID, int _newTime) public void update(UUID _playerUUID, int _newTime) {
{
m_DBClass.update(_playerUUID, _newTime); m_DBClass.update(_playerUUID, _newTime);
} }
public List<String[]> getTopPlayers(int _count){ return m_DBClass.getTopPlayers(_count); } public List<String[]> getTopPlayers(int _count) {
return m_DBClass.getTopPlayers(_count);
}
public int getPlayerTime(UUID _playerUUID) public int getPlayerTime(UUID _playerUUID) {
{
return m_DBClass.getPlayerTime(_playerUUID); return m_DBClass.getPlayerTime(_playerUUID);
} }
public void close() public void close() {
{
m_DBClass.close(); m_DBClass.close();
} }
} }

View File

@ -1,22 +1,22 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore.DB; package li.inc.PlaytimeReloaded.DataStore.DB;
@ -26,11 +26,14 @@ import org.bukkit.plugin.Plugin;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public interface IDB public interface IDB {
{
public void init(Plugin _plugin, Config _config); public void init(Plugin _plugin, Config _config);
public void update(UUID _playerUUID, int _newTime); public void update(UUID _playerUUID, int _newTime);
public List<String[]> getTopPlayers(int _count); public List<String[]> getTopPlayers(int _count);
public int getPlayerTime(UUID _playerUUID); public int getPlayerTime(UUID _playerUUID);
public void close(); public void close();
} }

View File

@ -1,22 +1,22 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore.DB; package li.inc.PlaytimeReloaded.DataStore.DB;
@ -30,49 +30,38 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class MySQL implements IDB public class MySQL implements IDB {
{
private Plugin m_plugin; private Plugin m_plugin;
private Config m_config; private Config m_config;
private MySQLTools m_MySqlTools; private MySQLTools m_MySqlTools;
public void init(Plugin _plugin, Config _config) public void init(Plugin _plugin, Config _config) {
{
m_plugin = _plugin; m_plugin = _plugin;
m_config = _config; m_config = _config;
try try {
{
m_MySqlTools = new MySQLTools(m_config.getMysqlHost(), m_config.getMysqlPort(), m_config.getMysqlDB(), m_config.getMysqlUsername(), m_config.getMysqlPassword()); m_MySqlTools = new MySQLTools(m_config.getMysqlHost(), m_config.getMysqlPort(), m_config.getMysqlDB(), m_config.getMysqlUsername(), m_config.getMysqlPassword());
m_MySqlTools.saveMySQLUpdate("CREATE TABLE IF NOT EXISTS playtime(playeruuid varchar(36), playtime int(6), PRIMARY KEY (playeruuid))", new String[]{}); m_MySqlTools.saveMySQLUpdate("CREATE TABLE IF NOT EXISTS playtime(playeruuid varchar(36), playtime int(6), PRIMARY KEY (playeruuid))", new String[]{});
}catch(Exception _e) } catch (Exception _e) {
{
_plugin.getLogger().info("!!!!!!!!!!!! ERROR: CANT CONNECT TO MySQL Server !!!!!!!!!!!!"); _plugin.getLogger().info("!!!!!!!!!!!! ERROR: CANT CONNECT TO MySQL Server !!!!!!!!!!!!");
} }
} }
public void update(UUID _playerUUID, int _newTime) public void update(UUID _playerUUID, int _newTime) {
{
m_MySqlTools.saveMySQLUpdate("REPLACE INTO playtime (`playeruuid`, `playtime`) VALUES ('" + _playerUUID.toString() + "', '" + _newTime + "')", new String[]{}); m_MySqlTools.saveMySQLUpdate("REPLACE INTO playtime (`playeruuid`, `playtime`) VALUES ('" + _playerUUID.toString() + "', '" + _newTime + "')", new String[]{});
} }
public List<String[]> getTopPlayers(int _count) public List<String[]> getTopPlayers(int _count) {
{
ResultSet _rs = m_MySqlTools.saveMySQLQuarry("SELECT * FROM playtime ORDER BY playtime DESC LIMIT " + _count, new String[]{}); ResultSet _rs = m_MySqlTools.saveMySQLQuarry("SELECT * FROM playtime ORDER BY playtime DESC LIMIT " + _count, new String[]{});
List<String[]> _returnList = new ArrayList<String[]>(); List<String[]> _returnList = new ArrayList<String[]>();
if(_rs != null) if (_rs != null) {
{ try {
try while (_rs.next()) {
{ try {
while(_rs.next())
{
try
{
_returnList.add(new String[]{UUIDCache.get(UUID.fromString(_rs.getString("playeruuid"))), _rs.getString("playtime")}); _returnList.add(new String[]{UUIDCache.get(UUID.fromString(_rs.getString("playeruuid"))), _rs.getString("playtime")});
}catch(Exception _e) } catch (Exception _e) {
{
return new ArrayList<String[]>(); return new ArrayList<String[]>();
} }
} }
@ -84,36 +73,29 @@ public class MySQL implements IDB
return _returnList; return _returnList;
} }
public int getPlayerTime(UUID _playerUUID) public int getPlayerTime(UUID _playerUUID) {
{
ResultSet _rs = m_MySqlTools.saveMySQLQuarry("SELECT * FROM playtime WHERE `playeruuid` = '" + _playerUUID.toString() + "';", new String[]{}); ResultSet _rs = m_MySqlTools.saveMySQLQuarry("SELECT * FROM playtime WHERE `playeruuid` = '" + _playerUUID.toString() + "';", new String[]{});
if(_rs != null) if (_rs != null) {
{ try {
try while (_rs.next()) {
{ try {
while(_rs.next())
{
try
{
return _rs.getInt("playtime"); return _rs.getInt("playtime");
}catch(Exception _e) } catch (Exception _e) {
{
return 0; return 0;
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
return 0; return 0;
} }
}else{ } else {
return 0; return 0;
} }
return 0; return 0;
} }
public void close() public void close() {
{
try { try {
m_MySqlTools.getConnection().close(); m_MySqlTools.getConnection().close();
} catch (SQLException throwables) { } catch (SQLException throwables) {

View File

@ -1,128 +1,114 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore.DB; package li.inc.PlaytimeReloaded.DataStore.DB;
import java.sql.*; import java.sql.*;
public class MySQLTools public class MySQLTools {
{ Connection m_MySQLConnection;
Connection m_MySQLConnection;
String m_host;
int m_port;
String m_dbname;
String m_username;
String m_passwort;
//Public Funktionen
public MySQLTools(String _host, int _port, String _dbname, String _username, String _passwort)
{
m_host = _host;
m_port = _port;
m_dbname = _dbname;
m_username = _username;
m_passwort = _passwort;
m_MySQLConnection = createSQLConnection(m_host, m_port, m_dbname, m_username, m_passwort); String m_host;
} int m_port;
String m_dbname;
public Connection getConnection() String m_username;
{ String m_passwort;
return m_MySQLConnection;
}
//Private Funktionen
private Connection createSQLConnection(String _host, int _port, String _dbname, String _username, String _passwort)
{
Connection _MySQLConnection;
try {
_MySQLConnection = DriverManager.getConnection("jdbc:mysql://" + _host + ":" + _port + "/" + _dbname, _username, _passwort);
_MySQLConnection.setAutoCommit(true);
return _MySQLConnection; //Public Funktionen
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
private void pingConnection()
{
try {
if(m_MySQLConnection.isClosed() || !m_MySQLConnection.isValid(3))
{
m_MySQLConnection.close();
m_MySQLConnection = createSQLConnection(m_host, m_port, m_dbname, m_username, m_passwort);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void saveMySQLUpdate(String _quarry, String[] _values)
{
pingConnection();
try public MySQLTools(String _host, int _port, String _dbname, String _username, String _passwort) {
{ m_host = _host;
PreparedStatement _ps = m_MySQLConnection.prepareStatement(_quarry); m_port = _port;
m_dbname = _dbname;
int index = 0; m_username = _username;
for( String _s: _values ) m_passwort = _passwort;
{
String thisValue = (String)_s;
_ps.setString(++index, thisValue);
}
_ps.executeUpdate(); m_MySQLConnection = createSQLConnection(m_host, m_port, m_dbname, m_username, m_passwort);
}catch(Exception e) }
{
e.printStackTrace();
}
}
public ResultSet saveMySQLQuarry(String _quarry, String[] _values)
{
pingConnection();
try public Connection getConnection() {
{ return m_MySQLConnection;
PreparedStatement _ps = m_MySQLConnection.prepareStatement(_quarry); }
int index = 0;
for( String _s: _values ) //Private Funktionen
{
String thisValue = (String)_s; private Connection createSQLConnection(String _host, int _port, String _dbname, String _username, String _passwort) {
_ps.setString(++index, thisValue); Connection _MySQLConnection;
} try {
_MySQLConnection = DriverManager.getConnection("jdbc:mysql://" + _host + ":" + _port + "/" + _dbname, _username, _passwort);
return _ps.executeQuery(); _MySQLConnection.setAutoCommit(true);
}catch(Exception e)
{ return _MySQLConnection;
e.printStackTrace(); } catch (SQLException e) {
return null; e.printStackTrace();
} return null;
} }
}
private void pingConnection() {
try {
if (m_MySQLConnection.isClosed() || !m_MySQLConnection.isValid(3)) {
m_MySQLConnection.close();
m_MySQLConnection = createSQLConnection(m_host, m_port, m_dbname, m_username, m_passwort);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void saveMySQLUpdate(String _quarry, String[] _values) {
pingConnection();
try {
PreparedStatement _ps = m_MySQLConnection.prepareStatement(_quarry);
int index = 0;
for (String _s : _values) {
String thisValue = (String) _s;
_ps.setString(++index, thisValue);
}
_ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet saveMySQLQuarry(String _quarry, String[] _values) {
pingConnection();
try {
PreparedStatement _ps = m_MySQLConnection.prepareStatement(_quarry);
int index = 0;
for (String _s : _values) {
String thisValue = (String) _s;
_ps.setString(++index, thisValue);
}
return _ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
} }

View File

@ -1,22 +1,22 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore.DB; package li.inc.PlaytimeReloaded.DataStore.DB;
@ -29,20 +29,16 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class SQLLite implements IDB public class SQLLite implements IDB {
{ Connection m_connection = null;
private Plugin m_plugin; private Plugin m_plugin;
private Config m_config; private Config m_config;
Connection m_connection = null; public void init(Plugin _plugin, Config _config) {
public void init(Plugin _plugin, Config _config)
{
m_plugin = _plugin; m_plugin = _plugin;
m_config = _config; m_config = _config;
try try {
{
m_connection = DriverManager.getConnection("jdbc:sqlite:" + m_plugin.getDataFolder().getAbsolutePath() + "/playtime.db"); m_connection = DriverManager.getConnection("jdbc:sqlite:" + m_plugin.getDataFolder().getAbsolutePath() + "/playtime.db");
Statement _statement = m_connection.createStatement(); Statement _statement = m_connection.createStatement();
@ -50,46 +46,37 @@ public class SQLLite implements IDB
_statement.executeUpdate("CREATE TABLE IF NOT EXISTS playtime(playeruuid varchar(36), playtime int(6))"); _statement.executeUpdate("CREATE TABLE IF NOT EXISTS playtime(playeruuid varchar(36), playtime int(6))");
_statement.close(); _statement.close();
}catch (SQLException _ex) { } catch (SQLException _ex) {
_ex.printStackTrace(); _ex.printStackTrace();
} }
} }
public void update(UUID _playerUUID, int _newTime) public void update(UUID _playerUUID, int _newTime) {
{ try {
try if (m_connection != null) {
{
if(m_connection != null)
{
Statement _statement = m_connection.createStatement(); Statement _statement = m_connection.createStatement();
_statement.executeUpdate("DELETE FROM playtime WHERE playeruuid = '" + _playerUUID.toString() + "'"); _statement.executeUpdate("DELETE FROM playtime WHERE playeruuid = '" + _playerUUID.toString() + "'");
_statement.executeUpdate("INSERT INTO playtime (`playeruuid`, `playtime`) VALUES ('" + _playerUUID.toString() + "', '" + _newTime + "')"); _statement.executeUpdate("INSERT INTO playtime (`playeruuid`, `playtime`) VALUES ('" + _playerUUID.toString() + "', '" + _newTime + "')");
_statement.close(); _statement.close();
} }
}catch (SQLException _ex) { } catch (SQLException _ex) {
_ex.printStackTrace(); _ex.printStackTrace();
} }
} }
public List<String[]> getTopPlayers(int _count) public List<String[]> getTopPlayers(int _count) {
{
List<String[]> _returnList = new ArrayList<String[]>(); List<String[]> _returnList = new ArrayList<String[]>();
try try {
{ if (m_connection != null) {
if(m_connection != null)
{
Statement _statement = m_connection.createStatement(); Statement _statement = m_connection.createStatement();
ResultSet _results = _statement.executeQuery("SELECT * FROM playtime ORDER BY playtime DESC LIMIT " + _count); ResultSet _results = _statement.executeQuery("SELECT * FROM playtime ORDER BY playtime DESC LIMIT " + _count);
while (_results.next()) while (_results.next()) {
{ try {
try
{
_returnList.add(new String[]{UUIDCache.get(UUID.fromString(_results.getString("playeruuid"))), _results.getString("playtime")}); _returnList.add(new String[]{UUIDCache.get(UUID.fromString(_results.getString("playeruuid"))), _results.getString("playtime")});
}catch(Exception _e) } catch (Exception _e) {
{
return new ArrayList<String[]>(); return new ArrayList<String[]>();
} }
} }
@ -97,29 +84,23 @@ public class SQLLite implements IDB
_results.close(); _results.close();
_statement.close(); _statement.close();
} }
}catch (SQLException _ex) { } catch (SQLException _ex) {
_ex.printStackTrace(); _ex.printStackTrace();
} }
return _returnList; return _returnList;
} }
public int getPlayerTime(UUID _playerUUID) public int getPlayerTime(UUID _playerUUID) {
{ try {
try if (m_connection != null) {
{
if(m_connection != null)
{
Statement _statement = m_connection.createStatement(); Statement _statement = m_connection.createStatement();
ResultSet _results = _statement.executeQuery("SELECT * FROM playtime WHERE `playeruuid` = '" + _playerUUID.toString() + "'"); ResultSet _results = _statement.executeQuery("SELECT * FROM playtime WHERE `playeruuid` = '" + _playerUUID.toString() + "'");
while (_results.next()) while (_results.next()) {
{ try {
try
{
return _results.getInt("playtime"); return _results.getInt("playtime");
}catch(Exception _e) } catch (Exception _e) {
{
return 0; return 0;
} }
} }
@ -127,15 +108,14 @@ public class SQLLite implements IDB
_results.close(); _results.close();
_statement.close(); _statement.close();
} }
}catch (SQLException _ex) { } catch (SQLException _ex) {
_ex.printStackTrace(); _ex.printStackTrace();
} }
return 0; return 0;
} }
public void close() public void close() {
{
try { try {
m_connection.close(); m_connection.close();
} catch (SQLException throwables) { } catch (SQLException throwables) {

View File

@ -1,45 +1,41 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore; package li.inc.PlaytimeReloaded.DataStore;
import java.util.UUID; import java.util.UUID;
public class PlayerObject public class PlayerObject {
{
private UUID m_playerUUID; private UUID m_playerUUID;
private String m_playerName = ""; private String m_playerName = "";
public PlayerObject(UUID _playerUUID, String _playerName) public PlayerObject(UUID _playerUUID, String _playerName) {
{
m_playerUUID = _playerUUID; m_playerUUID = _playerUUID;
m_playerName = _playerName; m_playerName = _playerName;
} }
public UUID getUUID() public UUID getUUID() {
{
return m_playerUUID; return m_playerUUID;
} }
public String getName() public String getName() {
{
return m_playerName; return m_playerName;
} }
} }

View File

@ -1,42 +1,39 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore; package li.inc.PlaytimeReloaded.DataStore;
public class TimeCommand public class TimeCommand {
{
private int m_timeInMin = 0; private int m_timeInMin = 0;
private String m_command = ""; private String m_command = "";
public int getTime() public TimeCommand(int _time, String _command) {
{
return m_timeInMin;
}
public String getCommand()
{
return m_command;
}
public TimeCommand(int _time, String _command)
{
m_timeInMin = _time; m_timeInMin = _time;
m_command = _command; m_command = _command;
} }
public int getTime() {
return m_timeInMin;
}
public String getCommand() {
return m_command;
}
} }

View File

@ -1,22 +1,22 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore; package li.inc.PlaytimeReloaded.DataStore;
@ -25,16 +25,12 @@ import org.bukkit.Bukkit;
import java.util.*; import java.util.*;
public class UUIDCache public class UUIDCache {
{
private static List<PlayerObject> m_players = new ArrayList<PlayerObject>(); private static List<PlayerObject> m_players = new ArrayList<PlayerObject>();
public static void update(String _playerName, UUID _playerUUID) public static void update(String _playerName, UUID _playerUUID) {
{ for (PlayerObject _dieserPlayer : m_players) {
for (PlayerObject _dieserPlayer : m_players) if (_dieserPlayer.getUUID() == _playerUUID) {
{
if(_dieserPlayer.getUUID() == _playerUUID)
{
return; return;
} }
} }
@ -42,18 +38,14 @@ public class UUIDCache
m_players.add(new PlayerObject(_playerUUID, _playerName)); m_players.add(new PlayerObject(_playerUUID, _playerName));
} }
public static UUID get(String _playerName) public static UUID get(String _playerName) {
{ for (PlayerObject _dieserPlayer : m_players) {
for (PlayerObject _dieserPlayer : m_players) if (_dieserPlayer.getName() == _playerName) {
{
if(_dieserPlayer.getName() == _playerName)
{
return _dieserPlayer.getUUID(); return _dieserPlayer.getUUID();
} }
} }
try try {
{
UUIDFetcher _uuidFetcher = new UUIDFetcher(Arrays.asList(_playerName)); UUIDFetcher _uuidFetcher = new UUIDFetcher(Arrays.asList(_playerName));
Map<String, UUID> _fetcherResponse = _uuidFetcher.call(); Map<String, UUID> _fetcherResponse = _uuidFetcher.call();
UUID _playerUUID = _fetcherResponse.get(_playerName); UUID _playerUUID = _fetcherResponse.get(_playerName);
@ -61,19 +53,15 @@ public class UUIDCache
update(_playerName, _playerUUID); update(_playerName, _playerUUID);
return _playerUUID; return _playerUUID;
}catch(Exception _e) } catch (Exception _e) {
{
update(_playerName, null); update(_playerName, null);
return null; return null;
} }
} }
public static String get(UUID _playerUUID) public static String get(UUID _playerUUID) {
{ for (PlayerObject _dieserPlayer : m_players) {
for (PlayerObject _dieserPlayer : m_players) if (_dieserPlayer.getUUID() == _playerUUID) {
{
if(_dieserPlayer.getUUID() == _playerUUID)
{
return _dieserPlayer.getName(); return _dieserPlayer.getName();
} }
} }

View File

@ -1,22 +1,22 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded.DataStore; package li.inc.PlaytimeReloaded.DataStore;
@ -49,28 +49,6 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
this(names, true); this(names, true);
} }
public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id");
String name = (String) jsonProfile.get("name");
UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L);
}
}
return uuidMap;
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception { private static void writeBody(HttpURLConnection connection, String body) throws Exception {
OutputStream stream = connection.getOutputStream(); OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes()); stream.write(body.getBytes());
@ -90,7 +68,7 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
} }
private static UUID getUUID(String id) { private static UUID getUUID(String id) {
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32)); return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
} }
public static byte[] toBytes(UUID uuid) { public static byte[] toBytes(UUID uuid) {
@ -113,4 +91,26 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
public static UUID getUUIDOf(String name) throws Exception { public static UUID getUUIDOf(String name) throws Exception {
return new UUIDFetcher(Arrays.asList(name)).call().get(name); return new UUIDFetcher(Arrays.asList(name)).call().get(name);
} }
public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id");
String name = (String) jsonProfile.get("name");
UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L);
}
}
return uuidMap;
}
} }

View File

@ -1,31 +1,29 @@
/* /*
* Copyright (c) 2016 * Copyright (c) 2016
* See CONTRIBUTORS.TXT for a full list of copyright holders. * See CONTRIBUTORS.TXT for a full list of copyright holders.
* *
* This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'. * This file is part of the spigot Minecraft server plugin 'PlaytimeReloaded'.
* *
* PlaytimeReloaded is free software: you can redistribute it and/or modify * PlaytimeReloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaytimeReloaded is distributed in the hope that it will be useful, * PlaytimeReloaded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>. * along with PlaytimeReloaded. If not, see <http://www.gnu.org/licenses/>.
*/ */
package li.inc.PlaytimeReloaded; package li.inc.PlaytimeReloaded;
import li.inc.PlaytimeReloaded.DataStore.Config; import li.inc.PlaytimeReloaded.DataStore.Config;
import li.inc.PlaytimeReloaded.DataStore.DB.DB; 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.TimeCommand;
import li.inc.PlaytimeReloaded.DataStore.UUIDCache; import li.inc.PlaytimeReloaded.DataStore.UUIDCache;
import li.inc.PlaytimeReloaded.DataStore.UUIDFetcher;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -36,22 +34,34 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.*;
import java.util.List;
import java.util.UUID;
public class Playtime extends JavaPlugin implements Listener public class Playtime extends JavaPlugin implements Listener {
{
private Config m_config = null; private Config m_config = null;
private DB m_db = null; private DB m_db = null;
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
@Override @Override
public void onEnable() public void onEnable() {
{
//Load config from file into Config class. //Load config from file into Config class.
m_config = new Config(this); m_config = new Config(this);
//Load the text from the lang config. //Load the text from the lang config.
if(m_db == null) if (m_db == null)
m_db = new DB(this, m_config); m_db = new DB(this, m_config);
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(this, this);
@ -59,125 +69,94 @@ public class Playtime extends JavaPlugin implements Listener
int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() { public void run() {
refreshPlaytime(); refreshPlaytime();
}}, 0, 1200); }
}, 0, 1200);
} }
@Override @Override
public void onDisable() public void onDisable() {
{
//Load the text from the lang config. //Load the text from the lang config.
if(m_db != null) if (m_db != null) {
{
m_db.close(); m_db.close();
m_db = null; m_db = null;
} }
} }
public static boolean isInteger(String s) { public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
try { if (cmd.getName().equalsIgnoreCase("toptime")) {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
if (cmd.getName().equalsIgnoreCase("toptime"))
{
int _count = 5; int _count = 5;
if(args.length == 1) if (args.length == 1) {
{ if (isInteger(args[0])) {
if(isInteger(args[0]))
{
_count = Integer.parseInt(args[0]); _count = Integer.parseInt(args[0]);
} }
} }
if (sender instanceof Player) if (sender instanceof Player) {
{ Player _player = (Player) sender;
Player _player = (Player)sender;
if(_player.hasPermission("playtime.top")) if (_player.hasPermission("playtime.top")) {
{
_player.sendMessage(getChatMessage(m_config.getTextTopPlayerListHead(), _player.getName(), 0, _count, true)); _player.sendMessage(getChatMessage(m_config.getTextTopPlayerListHead(), _player.getName(), 0, _count, true));
List<String[]> _topPlayers = m_db.getTopPlayers(_count); List<String[]> _topPlayers = m_db.getTopPlayers(_count);
int _rang = 1; int _rang = 1;
for (String[] _playerData: _topPlayers) for (String[] _playerData : _topPlayers) {
{ if (_playerData.length >= 2) {
if(_playerData.length >= 2)
{
_player.sendMessage(getChatMessage(m_config.getTextPlayerEntry(), _playerData[0], Integer.parseInt(_playerData[1]), _rang++, true)); _player.sendMessage(getChatMessage(m_config.getTextPlayerEntry(), _playerData[0], Integer.parseInt(_playerData[1]), _rang++, true));
} }
} }
}else{ } else {
_player.sendMessage(getChatMessage(m_config.getTextNoPermission(), _player.getName(), 0, 0, true)); _player.sendMessage(getChatMessage(m_config.getTextNoPermission(), _player.getName(), 0, 0, true));
} }
}else{ } else {
this.getLogger().info(getChatMessage(m_config.getTextTopPlayerListHead(), "", 0, _count, false)); this.getLogger().info(getChatMessage(m_config.getTextTopPlayerListHead(), "", 0, _count, false));
List<String[]> _topPlayers = m_db.getTopPlayers(_count); List<String[]> _topPlayers = m_db.getTopPlayers(_count);
int _rang = 1; int _rang = 1;
for (String[] _playerData: _topPlayers) for (String[] _playerData : _topPlayers) {
{ if (_playerData.length >= 2) {
if(_playerData.length >= 2)
{
this.getLogger().info(getChatMessage(m_config.getTextPlayerEntry(), _playerData[0], Integer.parseInt(_playerData[1]), _rang++, false)); this.getLogger().info(getChatMessage(m_config.getTextPlayerEntry(), _playerData[0], Integer.parseInt(_playerData[1]), _rang++, false));
} }
} }
} }
} }
if (cmd.getName().equalsIgnoreCase("playtime")) if (cmd.getName().equalsIgnoreCase("playtime")) {
{ if (args.length == 0) {
if(args.length == 0)
{
//Player will seine eigene Playtime wissen. //Player will seine eigene Playtime wissen.
if (sender instanceof Player) if (sender instanceof Player) {
{ Player _player = (Player) sender;
Player _player = (Player)sender;
if(_player.hasPermission("playtime.use")) if (_player.hasPermission("playtime.use")) {
{
_player.sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), _player.getName(), m_db.getPlayerTime(_player.getUniqueId()), 0, true)); _player.sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), _player.getName(), m_db.getPlayerTime(_player.getUniqueId()), 0, true));
}else{ } else {
_player.sendMessage(getChatMessage(m_config.getTextNoPermission(), _player.getName(), 0, 0, true)); _player.sendMessage(getChatMessage(m_config.getTextNoPermission(), _player.getName(), 0, 0, true));
} }
}else{ } else {
this.getLogger().info("Ey you bread! You cant display the playtime from the console!"); this.getLogger().info("Ey you bread! You cant display the playtime from the console!");
} }
}else{ } else {
//Player will andere Spielzeit wissen. //Player will andere Spielzeit wissen.
UUID _searchPlayer = getPlayerUUID(args[0]); UUID _searchPlayer = getPlayerUUID(args[0]);
if(_searchPlayer != null) if (_searchPlayer != null) {
{
//Spieler wurde gefunden. //Spieler wurde gefunden.
if (sender instanceof Player) if (sender instanceof Player) {
{ Player _player = (Player) sender;
Player _player = (Player)sender; if (_player.hasPermission("playtime.use.others")) {
if(_player.hasPermission("playtime.use.others"))
{
_player.sendMessage(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_db.getPlayerTime(_searchPlayer), 0, true)); _player.sendMessage(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_db.getPlayerTime(_searchPlayer), 0, true));
}else{ } else {
_player.sendMessage(getChatMessage(m_config.getTextNoPermission(), args[0], 0, 0, true)); _player.sendMessage(getChatMessage(m_config.getTextNoPermission(), args[0], 0, 0, true));
} }
}else{ } else {
this.getLogger().info(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_db.getPlayerTime(_searchPlayer), 0, false)); this.getLogger().info(getChatMessage(m_config.getTextPlayerPlaytimeIs(), args[0], m_db.getPlayerTime(_searchPlayer), 0, false));
} }
}else{ } else {
//Spieler wurde nicht gefunden / Hat noch nicht auf dem Server gespielt. //Spieler wurde nicht gefunden / Hat noch nicht auf dem Server gespielt.
if (sender instanceof Player) if (sender instanceof Player) {
{ Player _player = (Player) sender;
Player _player = (Player)sender;
_player.sendMessage(getChatMessage(m_config.getTextPlayerNotFound(), "", 0, 0, true)); _player.sendMessage(getChatMessage(m_config.getTextPlayerNotFound(), "", 0, 0, true));
}else{ } else {
this.getLogger().info(getChatMessage(m_config.getTextPlayerNotFound(), "", 0, 0, false)); this.getLogger().info(getChatMessage(m_config.getTextPlayerNotFound(), "", 0, 0, false));
} }
} }
@ -188,10 +167,8 @@ public class Playtime extends JavaPlugin implements Listener
} }
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) public void onPlayerJoin(PlayerJoinEvent event) {
{ if (event.getPlayer().hasPermission("playtime.use") && event.getPlayer().hasPermission("playtime.login")) {
if(event.getPlayer().hasPermission("playtime.use") && event.getPlayer().hasPermission("playtime.login"))
{
event.getPlayer().sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), event.getPlayer().getName(), m_db.getPlayerTime(event.getPlayer().getUniqueId()), 0, true)); event.getPlayer().sendMessage(getChatMessage(m_config.getTextYourCurrentPlaytime(), event.getPlayer().getName(), m_db.getPlayerTime(event.getPlayer().getUniqueId()), 0, true));
} }
@ -199,16 +176,13 @@ public class Playtime extends JavaPlugin implements Listener
addPlayTime(event.getPlayer().getUniqueId(), 0); addPlayTime(event.getPlayer().getUniqueId(), 0);
} }
private void refreshPlaytime() private void refreshPlaytime() {
{ for (Player _dieserSpieler : getServer().getOnlinePlayers()) {
for(Player _dieserSpieler : getServer().getOnlinePlayers())
{
addPlayTime(_dieserSpieler.getUniqueId(), 1); addPlayTime(_dieserSpieler.getUniqueId(), 1);
} }
} }
private void addPlayTime(UUID _playerUUID, int _time) private void addPlayTime(UUID _playerUUID, int _time) {
{
int _spielerPlaytime = m_db.getPlayerTime(_playerUUID); int _spielerPlaytime = m_db.getPlayerTime(_playerUUID);
_spielerPlaytime = _spielerPlaytime + _time; _spielerPlaytime = _spielerPlaytime + _time;
@ -218,40 +192,35 @@ public class Playtime extends JavaPlugin implements Listener
checkPlaytimeCommand(_playerUUID, _spielerPlaytime); checkPlaytimeCommand(_playerUUID, _spielerPlaytime);
} }
private UUID getPlayerUUID(String _playerName) private UUID getPlayerUUID(String _playerName) {
{
Player _returnPlayer = Bukkit.getPlayerExact(_playerName); Player _returnPlayer = Bukkit.getPlayerExact(_playerName);
if(_returnPlayer != null) if (_returnPlayer != null)
return _returnPlayer.getUniqueId(); return _returnPlayer.getUniqueId();
for(Player _dieserSpieler : getServer().getOnlinePlayers()) for (Player _dieserSpieler : getServer().getOnlinePlayers()) {
{ if (_dieserSpieler.getName() == _playerName) {
if(_dieserSpieler.getName() == _playerName)
{
return _dieserSpieler.getUniqueId(); return _dieserSpieler.getUniqueId();
} }
} }
UUID _playerUUID = UUIDCache.get(_playerName); UUID _playerUUID = UUIDCache.get(_playerName);
if(_playerUUID == null) if (_playerUUID == null)
return null; return null;
OfflinePlayer _offlinePlayer = Bukkit.getOfflinePlayer(_playerUUID); OfflinePlayer _offlinePlayer = Bukkit.getOfflinePlayer(_playerUUID);
if(_offlinePlayer != null) if (_offlinePlayer != null) {
{ if (!_offlinePlayer.hasPlayedBefore())
if(!_offlinePlayer.hasPlayedBefore())
return null; return null;
return _offlinePlayer.getUniqueId(); return _offlinePlayer.getUniqueId();
}else{ } else {
return null; return null;
} }
} }
private long[] calculateTime(long seconds) private long[] calculateTime(long seconds) {
{
long sec = seconds % 60; long sec = seconds % 60;
long minutes = seconds % 3600 / 60; long minutes = seconds % 3600 / 60;
long hours = seconds % 86400 / 3600; long hours = seconds % 86400 / 3600;
@ -260,33 +229,30 @@ public class Playtime extends JavaPlugin implements Listener
return new long[]{days, hours, minutes, sec}; return new long[]{days, hours, minutes, sec};
} }
private String getTimeString(int _timeInMin) private String getTimeString(int _timeInMin) {
{
String _ausgabe = ""; String _ausgabe = "";
long seconds = _timeInMin * 60; long seconds = _timeInMin * 60;
if(seconds != 0) if (seconds != 0) {
{
long[] _time = calculateTime(seconds); long[] _time = calculateTime(seconds);
if(_time[0] > 0) if (_time[0] > 0)
_ausgabe += _time[0] + " " + m_config.getTextDateDays() + " "; _ausgabe += _time[0] + " " + m_config.getTextDateDays() + " ";
if(_time[1] > 0) if (_time[1] > 0)
_ausgabe += _time[1] + " " + m_config.getTextDateHours() + " "; _ausgabe += _time[1] + " " + m_config.getTextDateHours() + " ";
if(_time[2] > 0) if (_time[2] > 0)
_ausgabe += _time[2] + " " + m_config.getTextDateMinutes(); _ausgabe += _time[2] + " " + m_config.getTextDateMinutes();
}else{ } else {
_ausgabe += "0 " + m_config.getTextDateMinutes(); _ausgabe += "0 " + m_config.getTextDateMinutes();
} }
return _ausgabe; return _ausgabe;
} }
private String getChatMessage(String _plainText, String _player, int _time, int _rang, boolean _color) private String getChatMessage(String _plainText, String _player, int _time, int _rang, boolean _color) {
{
String returnText = m_config.getTextPrefix() + _plainText; String returnText = m_config.getTextPrefix() + _plainText;
returnText = returnText.replace("%s", _player); returnText = returnText.replace("%s", _player);
@ -296,20 +262,17 @@ public class Playtime extends JavaPlugin implements Listener
returnText = ChatColor.translateAlternateColorCodes('&', returnText); returnText = ChatColor.translateAlternateColorCodes('&', returnText);
if(_color == false) if (_color == false)
returnText = ChatColor.stripColor(returnText); returnText = ChatColor.stripColor(returnText);
return returnText; return returnText;
} }
private void checkPlaytimeCommand(UUID _player, int _time) private void checkPlaytimeCommand(UUID _player, int _time) {
{
List<TimeCommand> _timeCommands = m_config.getTimeCommandList(); List<TimeCommand> _timeCommands = m_config.getTimeCommandList();
for (TimeCommand _tc: _timeCommands) for (TimeCommand _tc : _timeCommands) {
{ if (_tc.getTime() == _time) {
if(_tc.getTime() == _time)
{
String _commandWithParas = _tc.getCommand(); String _commandWithParas = _tc.getCommand();
_commandWithParas = _commandWithParas.replace("%%player%%", UUIDCache.get(_player)); _commandWithParas = _commandWithParas.replace("%%player%%", UUIDCache.get(_player));