Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.themikishou.zcore;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.plugin.java.JavaPlugin;
- import net.themikishou.zcore.basic.User;
- import net.themikishou.zcore.database.MySQL;
- import net.themikishou.zcore.listeners.PlayerJoin;
- import net.themikishou.zcore.managers.UserManager;
- import net.themikishou.zcore.utils.LocationUtils;
- import net.themikishou.zcore.utils.UserUtils;
- public class ZCore extends JavaPlugin {
- public static Connection conn;
- private static ZCore instance;
- public static MySQL mysql;
- public static ZCore getInstance() {
- return instance;
- }
- public MySQL getmysql() {
- return mysql;
- }
- public void onEnable(){
- instance = this;
- mysql = new MySQL(this);
- checkTable();
- try {
- loadData();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
- }
- public void onDisable(){
- try {
- saveData();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void checkTable(){
- openConnection();
- StringBuilder sb = new StringBuilder();
- sb.append("create table if not exists users(");
- sb.append("uuid varchar(100) not null,");
- sb.append("name varchar(50) not null,");
- sb.append("kills int not null,");
- sb.append("deaths int not null,");
- sb.append("recs int not null,");
- sb.append("badges int not null,");
- sb.append("money int not null,");
- sb.append("apples int not null,");
- sb.append("stone int not null,");
- sb.append("level int not null,");
- sb.append("donatecoins int not null,");
- sb.append("points int not null,");
- sb.append("firstIp varchar(50) not null,");
- sb.append("lastIp varchar(50) not null,");
- sb.append("homeLocation text not null,");
- sb.append("lastLocation text not null,");
- sb.append("god tinyint(1) not null,");
- sb.append("noblesse tinyint(1) not null,");
- sb.append("primary key(uuid));");
- try {
- conn.createStatement().executeUpdate(sb.toString());
- } catch (SQLException e) {
- e.printStackTrace();
- }
- closeConnection();
- }
- public void loadData() throws SQLException{
- openConnection();
- int i = 0;
- ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM `users`");
- while(rs.next()){
- User u = UserManager.getUser(UUID.fromString(rs.getString("uuid")));
- u.setKills(rs.getInt("kills"));
- u.setDeaths(rs.getInt("deaths"));
- u.setRecs(rs.getInt("recs"));
- u.setBadges(rs.getInt("badges"));
- u.setMoney(rs.getInt("money"));
- u.setApples(rs.getInt("apples"));
- u.setStone(rs.getInt("stone"));
- u.setLevel(rs.getInt("level"));
- u.setDonateCoins(rs.getInt("donatecoins"));
- u.setPoints(rs.getInt("points"));
- u.setHomeLocation(LocationUtils.locationFromString(rs.getString("homeLocation")));
- u.setLastLocation(LocationUtils.locationFromString(rs.getString("lastLocation")));
- u.setFirstIP(rs.getString("firstIp"));
- u.setLastIP(rs.getString("lastIp"));
- u.setGod(rs.getBoolean("god"));
- u.setName(rs.getString("name"));
- u.setNoblesse(rs.getBoolean("noblesse"));
- i++;
- }
- Bukkit.getConsoleSender().sendMessage("§a§lLoaded §6§l" + i + " §a§lusers");
- closeConnection();
- }
- public void saveData() throws SQLException{
- openConnection();
- int i = 0;
- for(User u : UserUtils.getUsers()){
- StringBuilder sb = new StringBuilder();
- sb.append("INSERT INTO users (uuid, name, kills, deaths, recs, badges, apples, money, donatecoins, stone, level,"
- + "points, firstIp, lastIp, homeLocation, lastLocation, god, noblesse) VALUES (");
- sb.append("'" + u.getUUID().toString() +"',");
- sb.append("'" + u.getName() +"',");
- sb.append("'" + u.getKills() +"',");
- sb.append("'" + u.getDeaths() +"',");
- sb.append("'" + u.getRecs() +"',");
- sb.append("'" + u.getBadges() +"',");
- sb.append("'" + u.getApples() +"',");
- sb.append("'" + u.getMoney() +"',");
- sb.append("'" + u.getDonateCoins() +"',");
- sb.append("'" + u.getStone() +"',");
- sb.append("'" + u.getLevel() +"',");
- sb.append("'" + u.getPoints() +"',");
- sb.append("'" + u.getFirstIP() +"',");
- sb.append("'" + u.getLastIP() +"',");
- sb.append("'" + u.getHomeLocation() +"',");
- sb.append("'" + u.getLastLocation() +"',");
- sb.append("'" + (u.isGod() ? 1 : 0) +"',");
- sb.append("'" + (u.isNoblesse() ? 1 : 0) +"'");
- sb.append(") ON DUPLICATE KEY UPDATE ");
- sb.append("name='" + u.getName() +"',");
- sb.append("kills='" + u.getKills() +"',");
- sb.append("deaths='" + u.getDeaths() +"',");
- sb.append("recs='" + u.getRecs() +"',");
- sb.append("badges='" + u.getBadges() +"',");
- sb.append("apples='" + u.getApples() +"',");
- sb.append("money='" + u.getMoney() +"',");
- sb.append("donatecoins='" + u.getDonateCoins() +"',");
- sb.append("stone='" + u.getStone() +"',");
- sb.append("level='" + u.getLevel() +"',");
- sb.append("points='" + u.getPoints() +"',");
- sb.append("firstIp='" + u.getFirstIP() +"',");
- sb.append("lastIp'" + u.getLastIP() +"',");
- sb.append("homeLocation='" + u.getHomeLocation() +"',");
- sb.append("lastLocation='" + u.getLastLocation() +"',");
- sb.append("god='" + (u.isGod() ? 1 : 0) +"',");
- sb.append("noblesse='" + (u.isNoblesse() ? 1 : 0) +"';");
- conn.createStatement().executeUpdate(sb.toString());
- i++;
- }
- Bukkit.getConsoleSender().sendMessage("§a§lSaved §6§l" + i + " §a§lusers");
- closeConnection();
- }
- public void openConnection(){
- if(!isConnected()){
- try{
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/youtube?user=root&password=");
- } catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- public void closeConnection(){
- if(isConnected()){
- try{
- conn.close();
- } catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- try{
- if(conn == null) return false;
- if(conn.isClosed()) return false;
- } catch(SQLException e){
- e.printStackTrace();
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement