1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Data base conversion with one command. More data are being converted

This commit is contained in:
Zrips 2017-07-18 19:16:23 +03:00
parent 6536e402b4
commit 8eed14b765
7 changed files with 216 additions and 84 deletions

View File

@ -150,7 +150,7 @@ public class Jobs extends JavaPlugin {
private static NMS nms; private static NMS nms;
private static ActionBar actionbar; private static ActionBar actionbar;
private boolean running = false; private static boolean running = false;
protected static VersionChecker versionCheckManager; protected static VersionChecker versionCheckManager;
@ -523,7 +523,15 @@ public class Jobs extends JavaPlugin {
e1.printStackTrace(); e1.printStackTrace();
} }
Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() { loadAllPlayersData();
// add all online players
for (Player online : Bukkit.getServer().getOnlinePlayers()) {
Jobs.getPlayerManager().playerJoin(online);
}
}
public static void loadAllPlayersData() {
Bukkit.getScheduler().runTaskAsynchronously(Jobs.getInstance(), new Runnable() {
@Override @Override
public void run() { public void run() {
int i = 0; int i = 0;
@ -533,6 +541,7 @@ public class Jobs extends JavaPlugin {
// Cloning to avoid issues // Cloning to avoid issues
HashMap<UUID, PlayerInfo> temp = new HashMap<UUID, PlayerInfo>(Jobs.getPlayerManager().getPlayersInfoUUIDMap()); HashMap<UUID, PlayerInfo> temp = new HashMap<UUID, PlayerInfo>(Jobs.getPlayerManager().getPlayersInfoUUIDMap());
Iterator<Entry<UUID, PlayerInfo>> it = temp.entrySet().iterator(); Iterator<Entry<UUID, PlayerInfo>> it = temp.entrySet().iterator();
Jobs.consoleMsg("map size: " + temp.size());
while (it.hasNext()) { while (it.hasNext()) {
Entry<UUID, PlayerInfo> one = it.next(); Entry<UUID, PlayerInfo> one = it.next();
if (!running) if (!running)
@ -557,10 +566,6 @@ public class Jobs extends JavaPlugin {
return; return;
} }
}); });
// add all online players
for (Player online : Bukkit.getServer().getOnlinePlayers()) {
Jobs.getPlayerManager().playerJoin(online);
}
} }
/** /**

View File

@ -88,6 +88,13 @@ public class PlayerManager {
PlayerNameMap.clear(); PlayerNameMap.clear();
} }
public void clearCache() {
playersCache.clear();
playersUUIDCache.clear();
players.clear();
playersUUID.clear();
}
public void addPlayerToMap(PlayerInfo info) { public void addPlayerToMap(PlayerInfo info) {
this.PlayerUUIDMap.put(info.getUuid(), info); this.PlayerUUIDMap.put(info.getUuid(), info);
this.PlayerIDMap.put(info.getID(), info); this.PlayerIDMap.put(info.getID(), info);
@ -215,9 +222,30 @@ public class PlayerManager {
/** /**
* Save all the information of all of the players * Save all the information of all of the players
*/ */
public void saveEntireChacheOfPlayers() { public void convertChacheOfPlayers(boolean resetID) {
int y = 0;
int i = 0;
int total = playersUUIDCache.size();
for (Entry<UUID, JobsPlayer> one : playersUUIDCache.entrySet()) { for (Entry<UUID, JobsPlayer> one : playersUUIDCache.entrySet()) {
one.getValue().save(); JobsPlayer jPlayer = one.getValue();
if (resetID)
jPlayer.setUserId(-1);
JobsDAO dao = Jobs.getJobsDAO();
dao.updateSeen(jPlayer);
if (jPlayer.getUserId() == -1)
continue;
for (JobProgression oneJ : jPlayer.getJobProgression()) {
dao.insertJob(jPlayer, oneJ);
}
dao.saveLog(jPlayer);
dao.savePoints(jPlayer);
dao.recordPlayersLimits(jPlayer);
i++;
y++;
if (y >= 1000) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Saved " + i + "/" + total + " players data");
y = 0;
}
} }
} }

View File

@ -11,6 +11,7 @@ import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.commands.JobCommand;
import com.gamingmesh.jobs.container.Convert; import com.gamingmesh.jobs.container.Convert;
import com.gamingmesh.jobs.dao.JobsManager.DataBaseType;
import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.stuff.ChatColor;
public class convert implements Cmd { public class convert implements Cmd {
@ -29,11 +30,9 @@ public class convert implements Cmd {
return true; return true;
} }
List<Convert> list = null;
List<Convert> archivelist = null; List<Convert> archivelist = null;
try { try {
list = Jobs.getJobsDAO().convertDatabase("jobs");
archivelist = Jobs.getJobsDAO().convertDatabase("archive"); archivelist = Jobs.getJobsDAO().convertDatabase("archive");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -43,15 +42,22 @@ public class convert implements Cmd {
Jobs.ChangeDatabase(); Jobs.ChangeDatabase();
if (list == null & archivelist == null) if (archivelist == null)
return false; return false;
try { try {
Jobs.getJobsDAO().continueConvertions(list, "jobs"); Jobs.getJobsDAO().truncateAllTables();
Jobs.getPlayerManager().convertChacheOfPlayers(true);
Jobs.getJobsDAO().continueConvertions(archivelist, "archive"); Jobs.getJobsDAO().continueConvertions(archivelist, "archive");
Jobs.getJobsDAO().transferUsers(); Jobs.getPlayerManager().clearMaps();
Jobs.getPlayerManager().clearCache();
Jobs.getJobsDAO().saveExplore(false);
Jobs.getJobsDAO().saveBlockProtection();
Jobs.loadAllPlayersData();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
sender.sendMessage(ChatColor.RED + "Can't write data to data base, please send error log to dev's."); Jobs.consoleMsg("&cCan't write data to data base, please send error log to dev's.");
return true; return true;
} }
@ -64,14 +70,12 @@ public class convert implements Cmd {
String from = "MysSQL"; String from = "MysSQL";
String to = "SqLite"; String to = "SqLite";
if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { if (Jobs.getDBManager().getDbType().equals(DataBaseType.SqLite)) {
from = "SqLite"; from = "SqLite";
to = "MySQL"; to = "MySQL";
} }
sender.sendMessage(ChatColor.GOLD + "Data base was converted from " + ChatColor.GREEN + from + ChatColor.GOLD + " to " + ChatColor.GREEN + to + ChatColor.GOLD Jobs.consoleMsg("&eData base was converted from &2" + from + " &eto &2" + to + "&e!");
+ "! Now you can stop the server, change storage-method to " + ChatColor.GREEN + to + ChatColor.GOLD
+ " in general config file and start server again on your new database system.");
return true; return true;
} }

View File

@ -1,6 +1,7 @@
package com.gamingmesh.jobs.config; package com.gamingmesh.jobs.config;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -49,6 +50,14 @@ public class ExploreManager {
return worlds; return worlds;
} }
public int getSize() {
int i = 0;
for (Entry<String, ExploreRegion> one : this.getWorlds().entrySet()) {
i += one.getValue().getChunks().size();
}
return i;
}
public ExploreRespond ChunkRespond(Player player, Chunk chunk, boolean isNew) { public ExploreRespond ChunkRespond(Player player, Chunk chunk, boolean isNew) {
return ChunkRespond(player.getName(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ(), isNew); return ChunkRespond(player.getName(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ(), isNew);
} }

View File

@ -112,7 +112,6 @@ public class GeneralConfigManager {
public double TaxesAmount; public double TaxesAmount;
public String SoundLevelupSound, SoundTitleChangeSound, ServerAcountName, ServertaxesAcountName; public String SoundLevelupSound, SoundTitleChangeSound, ServerAcountName, ServertaxesAcountName;
public ArrayList<String> keys; public ArrayList<String> keys;
public String storageMethod;
public boolean hideJobsInfoWithoutPermission; public boolean hideJobsInfoWithoutPermission;
public boolean UseTaxes; public boolean UseTaxes;
public boolean TransferToServerAccount; public boolean TransferToServerAccount;

View File

@ -513,6 +513,12 @@ public abstract class JobsDAO {
return true; return true;
} }
public void truncateAllTables() {
for (DBTables one : DBTables.values()) {
this.truncate(one.getTableName());
}
}
public DataBaseType getDbType() { public DataBaseType getDbType() {
return dbType; return dbType;
} }
@ -625,33 +631,33 @@ public abstract class JobsDAO {
} }
} }
private void loadAllSavedJobs() { // private void loadAllSavedJobs() {
JobsConnection conn = getConnection(); // JobsConnection conn = getConnection();
if (conn == null) // if (conn == null)
return; // return;
PreparedStatement prest = null; // PreparedStatement prest = null;
ResultSet res = null; // ResultSet res = null;
try { // try {
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "jobs`;"); // prest = conn.prepareStatement("SELECT * FROM `" + prefix + "jobs`;");
res = prest.executeQuery(); // res = prest.executeQuery();
while (res.next()) { // while (res.next()) {
int id = res.getInt("userid"); // int id = res.getInt("userid");
ArrayList<JobsDAOData> list = map.get(id); // ArrayList<JobsDAOData> list = map.get(id);
if (list == null) { // if (list == null) {
list = new ArrayList<JobsDAOData>(); // list = new ArrayList<JobsDAOData>();
list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience"))); // list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience")));
map.put(id, list); // map.put(id, list);
} else { // } else {
list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience"))); // list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience")));
} // }
} // }
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
} finally { // } finally {
close(res); // close(res);
close(prest); // close(prest);
} // }
} // }
public void recordNewPlayer(Player player) { public void recordNewPlayer(Player player) {
recordNewPlayer((OfflinePlayer) player); recordNewPlayer((OfflinePlayer) player);
@ -870,6 +876,33 @@ public abstract class JobsDAO {
} }
} }
/**
* Join a job (create player-job entry from storage)
* @param player - player that wishes to join the job
* @param job - job that the player wishes to join
*/
public synchronized void insertJob(JobsPlayer jPlayer, JobProgression prog) {
JobsConnection conn = getConnection();
if (conn == null)
return;
PreparedStatement prest = null;
try {
int exp = (int) prog.getExperience();
if (exp < 0)
exp = 0;
prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);");
prest.setInt(1, jPlayer.getUserId());
prest.setString(2, prog.getJob().getName());
prest.setInt(3, prog.getLevel());
prest.setInt(4, exp);
prest.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(prest);
}
}
/** /**
* Join a job (create player-job entry from storage) * Join a job (create player-job entry from storage)
* @param player - player that wishes to join the job * @param player - player that wishes to join the job
@ -914,7 +947,7 @@ public abstract class JobsDAO {
int i = list.size(); int i = list.size();
try { try {
statement = conns.createStatement(); statement = conns.createStatement();
if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { if (Jobs.getDBManager().getDbType().toString().equalsIgnoreCase("sqlite")) {
statement.executeUpdate("TRUNCATE `" + getPrefix() + table + "`"); statement.executeUpdate("TRUNCATE `" + getPrefix() + table + "`");
} else { } else {
statement.executeUpdate("DELETE from `" + getPrefix() + table + "`"); statement.executeUpdate("DELETE from `" + getPrefix() + table + "`");
@ -940,38 +973,38 @@ public abstract class JobsDAO {
} }
} }
public void transferUsers() throws SQLException { // public void transferUsers() throws SQLException {
JobsConnection conns = this.getConnection(); // JobsConnection conns = this.getConnection();
if (conns == null) // if (conns == null)
return; // return;
PreparedStatement insert = null; // PreparedStatement insert = null;
Statement statement = null; // Statement statement = null;
try { // try {
statement = conns.createStatement(); // statement = conns.createStatement();
if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { // if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) {
statement.executeUpdate("TRUNCATE `" + getPrefix() + "users`"); // statement.executeUpdate("TRUNCATE `" + getPrefix() + "users`");
} else { // } else {
statement.executeUpdate("DELETE from `" + getPrefix() + "users`"); // statement.executeUpdate("DELETE from `" + getPrefix() + "users`");
} // }
//
insert = conns.prepareStatement("INSERT INTO `" + getPrefix() + "users` (`id`, `player_uuid`, `username`, `seen`) VALUES (?, ?, ?, ?);"); // insert = conns.prepareStatement("INSERT INTO `" + getPrefix() + "users` (`id`, `player_uuid`, `username`, `seen`) VALUES (?, ?, ?, ?);");
conns.setAutoCommit(false); // conns.setAutoCommit(false);
//
for (Entry<UUID, JobsPlayer> oneUser : Jobs.getPlayerManager().getPlayersCache().entrySet()) { // for (Entry<UUID, JobsPlayer> oneUser : Jobs.getPlayerManager().getPlayersCache().entrySet()) {
insert.setInt(1, oneUser.getValue().getUserId()); // insert.setInt(1, oneUser.getValue().getUserId());
insert.setString(2, oneUser.getValue().getPlayerUUID().toString()); // insert.setString(2, oneUser.getValue().getPlayerUUID().toString());
insert.setString(3, oneUser.getValue().getUserName()); // insert.setString(3, oneUser.getValue().getUserName());
insert.setLong(4, oneUser.getValue().getSeen() == null ? System.currentTimeMillis() : oneUser.getValue().getSeen()); // insert.setLong(4, oneUser.getValue().getSeen() == null ? System.currentTimeMillis() : oneUser.getValue().getSeen());
insert.addBatch(); // insert.addBatch();
} // }
insert.executeBatch(); // insert.executeBatch();
conns.commit(); // conns.commit();
conns.setAutoCommit(true); // conns.setAutoCommit(true);
} finally { // } finally {
close(statement); // close(statement);
close(insert); // close(insert);
} // }
} // }
/** /**
* Quit a job (delete player-job entry from storage) * Quit a job (delete player-job entry from storage)
@ -1320,6 +1353,10 @@ public abstract class JobsDAO {
} }
public void updateSeen(JobsPlayer player) { public void updateSeen(JobsPlayer player) {
if (player.getUserId() == -1) {
insertPlayer(player);
return;
}
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
@ -1336,6 +1373,40 @@ public abstract class JobsDAO {
} }
} }
private void insertPlayer(JobsPlayer player) {
JobsConnection conn = getConnection();
if (conn == null)
return;
PreparedStatement prestt = null;
try {
prestt = conn.prepareStatement("INSERT INTO `" + prefix + "users` (`player_uuid`, `username`, `seen`) VALUES (?, ?, ?);");
prestt.setString(1, player.getPlayerUUID().toString());
prestt.setString(2, player.getUserName());
prestt.setLong(3, player.getSeen());
prestt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(prestt);
}
PreparedStatement prest = null;
ResultSet res = null;
try {
prest = conn.prepareStatement("SELECT `id` FROM `" + prefix + "users` WHERE `player_uuid` = ?;");
prest.setString(1, player.getPlayerUUID().toString());
res = prest.executeQuery();
res.next();
int id = res.getInt("id");
player.setUserId(id);
Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(player.getUserName(), id, player.getPlayerUUID(), player.getSeen()));
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(res);
close(prest);
}
}
public void savePoints(JobsPlayer jPlayer) { public void savePoints(JobsPlayer jPlayer) {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
@ -1670,6 +1741,10 @@ public abstract class JobsDAO {
* @param jobexplore - the information getting saved * @param jobexplore - the information getting saved
*/ */
public void saveExplore() { public void saveExplore() {
saveExplore(true);
}
public void saveExplore(boolean ignoreOld) {
if (!Jobs.getExplore().isExploreEnabled()) if (!Jobs.getExplore().isExploreEnabled())
return; return;
@ -1687,7 +1762,7 @@ public abstract class JobsDAO {
for (Entry<String, ExploreRegion> worlds : temp.entrySet()) { for (Entry<String, ExploreRegion> worlds : temp.entrySet()) {
for (Entry<String, ExploreChunk> oneChunk : worlds.getValue().getChunks().entrySet()) { for (Entry<String, ExploreChunk> oneChunk : worlds.getValue().getChunks().entrySet()) {
if (!oneChunk.getValue().isNew()) if (!oneChunk.getValue().isNew() && ignoreOld)
continue; continue;
for (String oneuser : oneChunk.getValue().getPlayers()) { for (String oneuser : oneChunk.getValue().getPlayers()) {
prest2.setString(1, worlds.getKey()); prest2.setString(1, worlds.getKey());

View File

@ -1,6 +1,7 @@
package com.gamingmesh.jobs.dao; package com.gamingmesh.jobs.dao;
import java.io.File; import java.io.File;
import java.io.IOException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -39,6 +40,17 @@ public class JobsManager {
dao.setDbType(DbType); dao.setDbType(DbType);
break; break;
} }
File f = new File(plugin.getDataFolder(), "generalConfig.yml");
YamlConfiguration config = YamlConfiguration.loadConfiguration(f);
config.set("storage.method", DbType.toString());
try {
config.save(f);
} catch (IOException e) {
e.printStackTrace();
}
Jobs.setDAO(dao); Jobs.setDAO(dao);
} }