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 ActionBar actionbar;
private boolean running = false;
private static boolean running = false;
protected static VersionChecker versionCheckManager;
@ -523,7 +523,15 @@ public class Jobs extends JavaPlugin {
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
public void run() {
int i = 0;
@ -533,6 +541,7 @@ public class Jobs extends JavaPlugin {
// Cloning to avoid issues
HashMap<UUID, PlayerInfo> temp = new HashMap<UUID, PlayerInfo>(Jobs.getPlayerManager().getPlayersInfoUUIDMap());
Iterator<Entry<UUID, PlayerInfo>> it = temp.entrySet().iterator();
Jobs.consoleMsg("map size: " + temp.size());
while (it.hasNext()) {
Entry<UUID, PlayerInfo> one = it.next();
if (!running)
@ -557,10 +566,6 @@ public class Jobs extends JavaPlugin {
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();
}
public void clearCache() {
playersCache.clear();
playersUUIDCache.clear();
players.clear();
playersUUID.clear();
}
public void addPlayerToMap(PlayerInfo info) {
this.PlayerUUIDMap.put(info.getUuid(), info);
this.PlayerIDMap.put(info.getID(), info);
@ -215,9 +222,30 @@ public class PlayerManager {
/**
* 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()) {
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.JobCommand;
import com.gamingmesh.jobs.container.Convert;
import com.gamingmesh.jobs.dao.JobsManager.DataBaseType;
import com.gamingmesh.jobs.stuff.ChatColor;
public class convert implements Cmd {
@ -29,11 +30,9 @@ public class convert implements Cmd {
return true;
}
List<Convert> list = null;
List<Convert> archivelist = null;
try {
list = Jobs.getJobsDAO().convertDatabase("jobs");
archivelist = Jobs.getJobsDAO().convertDatabase("archive");
} catch (SQLException e) {
e.printStackTrace();
@ -43,15 +42,22 @@ public class convert implements Cmd {
Jobs.ChangeDatabase();
if (list == null & archivelist == null)
if (archivelist == null)
return false;
try {
Jobs.getJobsDAO().continueConvertions(list, "jobs");
Jobs.getJobsDAO().truncateAllTables();
Jobs.getPlayerManager().convertChacheOfPlayers(true);
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) {
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;
}
@ -64,14 +70,12 @@ public class convert implements Cmd {
String from = "MysSQL";
String to = "SqLite";
if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) {
if (Jobs.getDBManager().getDbType().equals(DataBaseType.SqLite)) {
from = "SqLite";
to = "MySQL";
}
sender.sendMessage(ChatColor.GOLD + "Data base was converted from " + ChatColor.GREEN + from + ChatColor.GOLD + " to " + ChatColor.GREEN + to + ChatColor.GOLD
+ "! 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.");
Jobs.consoleMsg("&eData base was converted from &2" + from + " &eto &2" + to + "&e!");
return true;
}

View File

@ -1,6 +1,7 @@
package com.gamingmesh.jobs.config;
import java.util.HashMap;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -49,6 +50,14 @@ public class ExploreManager {
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) {
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 String SoundLevelupSound, SoundTitleChangeSound, ServerAcountName, ServertaxesAcountName;
public ArrayList<String> keys;
public String storageMethod;
public boolean hideJobsInfoWithoutPermission;
public boolean UseTaxes;
public boolean TransferToServerAccount;

View File

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

View File

@ -1,6 +1,7 @@
package com.gamingmesh.jobs.dao;
import java.io.File;
import java.io.IOException;
import org.bukkit.configuration.file.YamlConfiguration;
@ -39,6 +40,17 @@ public class JobsManager {
dao.setDbType(DbType);
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);
}