PlaytimeReloaded/src/main/java/li/inc/PlaytimeReloaded/DataStore/DB/MySQL.java

106 lines
3.7 KiB
Java
Raw Normal View History

2016-09-15 18:16:32 +00:00
/*
2020-12-28 08:11:09 +00:00
* 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/>.
*/
2016-09-15 18:16:32 +00:00
2016-09-12 20:19:11 +00:00
package li.inc.PlaytimeReloaded.DataStore.DB;
import li.inc.PlaytimeReloaded.DataStore.Config;
2017-02-08 00:23:26 +00:00
import li.inc.PlaytimeReloaded.DataStore.UUIDCache;
2016-09-12 20:19:11 +00:00
import org.bukkit.plugin.Plugin;
import java.sql.ResultSet;
import java.sql.SQLException;
2017-02-08 00:03:48 +00:00
import java.util.ArrayList;
import java.util.List;
2016-09-12 20:19:11 +00:00
import java.util.UUID;
2020-12-28 08:11:09 +00:00
public class MySQL implements IDB {
2016-09-12 20:19:11 +00:00
private Plugin m_plugin;
private Config m_config;
private MySQLTools m_MySqlTools;
2020-12-28 08:11:09 +00:00
public void init(Plugin _plugin, Config _config) {
2016-09-12 20:19:11 +00:00
m_plugin = _plugin;
m_config = _config;
2020-12-28 08:11:09 +00:00
try {
2016-09-12 20:19:11 +00:00
m_MySqlTools = new MySQLTools(m_config.getMysqlHost(), m_config.getMysqlPort(), m_config.getMysqlDB(), m_config.getMysqlUsername(), m_config.getMysqlPassword());
2016-09-17 16:00:37 +00:00
m_MySqlTools.saveMySQLUpdate("CREATE TABLE IF NOT EXISTS playtime(playeruuid varchar(36), playtime int(6), PRIMARY KEY (playeruuid))", new String[]{});
2020-12-28 08:11:09 +00:00
} catch (Exception _e) {
2016-09-12 20:19:11 +00:00
_plugin.getLogger().info("!!!!!!!!!!!! ERROR: CANT CONNECT TO MySQL Server !!!!!!!!!!!!");
}
}
2020-12-28 08:11:09 +00:00
public void update(UUID _playerUUID, int _newTime) {
2016-09-17 16:00:37 +00:00
m_MySqlTools.saveMySQLUpdate("REPLACE INTO playtime (`playeruuid`, `playtime`) VALUES ('" + _playerUUID.toString() + "', '" + _newTime + "')", new String[]{});
2017-02-08 00:03:48 +00:00
}
2020-12-28 08:11:09 +00:00
public List<String[]> getTopPlayers(int _count) {
2017-02-23 20:04:00 +00:00
ResultSet _rs = m_MySqlTools.saveMySQLQuarry("SELECT * FROM playtime ORDER BY playtime DESC LIMIT " + _count, new String[]{});
2017-02-08 00:03:48 +00:00
List<String[]> _returnList = new ArrayList<String[]>();
2016-09-12 20:19:11 +00:00
2020-12-28 08:11:09 +00:00
if (_rs != null) {
try {
while (_rs.next()) {
try {
2017-02-08 00:23:26 +00:00
_returnList.add(new String[]{UUIDCache.get(UUID.fromString(_rs.getString("playeruuid"))), _rs.getString("playtime")});
2020-12-28 08:11:09 +00:00
} catch (Exception _e) {
2017-02-08 00:03:48 +00:00
return new ArrayList<String[]>();
}
}
} catch (SQLException e) {
return new ArrayList<String[]>();
}
}
2016-09-12 20:19:11 +00:00
2017-02-08 00:03:48 +00:00
return _returnList;
2016-09-12 20:19:11 +00:00
}
2020-12-28 08:11:09 +00:00
public int getPlayerTime(UUID _playerUUID) {
2016-09-12 20:19:11 +00:00
ResultSet _rs = m_MySqlTools.saveMySQLQuarry("SELECT * FROM playtime WHERE `playeruuid` = '" + _playerUUID.toString() + "';", new String[]{});
2020-12-28 08:11:09 +00:00
if (_rs != null) {
try {
while (_rs.next()) {
try {
2016-09-12 20:19:11 +00:00
return _rs.getInt("playtime");
2020-12-28 08:11:09 +00:00
} catch (Exception _e) {
2016-09-12 20:19:11 +00:00
return 0;
}
}
} catch (SQLException e) {
return 0;
}
2020-12-28 08:11:09 +00:00
} else {
2016-09-12 20:19:11 +00:00
return 0;
}
return 0;
}
2020-12-28 08:10:40 +00:00
2020-12-28 08:11:09 +00:00
public void close() {
2020-12-28 08:10:40 +00:00
try {
m_MySqlTools.getConnection().close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
2016-09-12 20:19:11 +00:00
}