1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-03 14:58:34 +01:00

Preload all player data

This commit is contained in:
Zrips 2016-08-05 13:28:31 +03:00
parent a0f95b9108
commit e5929191a3
11 changed files with 384 additions and 329 deletions

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -52,6 +53,7 @@ import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.container.JobInfo;
import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.container.PlayerInfo;
import com.gamingmesh.jobs.container.FastPayment; import com.gamingmesh.jobs.container.FastPayment;
import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAO;
import com.gamingmesh.jobs.economy.BufferedEconomy; import com.gamingmesh.jobs.economy.BufferedEconomy;
@ -430,36 +432,40 @@ public class Jobs {
* Executes startup * Executes startup
* @throws IOException * @throws IOException
*/ */
public static void startup() throws IOException { public static void startup(JobsPlugin plugin) {
reload(); try {
reload();
// add all online players } catch (IOException e1) {
if (!Jobs.getGCManager().MultiServerCompatability() && Jobs.getGCManager().PreLoadUse) { e1.printStackTrace();
int i = 0;
long time = System.currentTimeMillis();
for (OfflinePlayer offline : Bukkit.getServer().getOfflinePlayers()) {
try {
if (offline.isOnline())
continue;
long lastPlayed = offline.getLastPlayed();
int dif = (int) ((time - lastPlayed) / 1000 / 60 / 60 / 24);
if (dif >= 7)
continue;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(offline);
if (jPlayer == null)
continue;
Jobs.getJobsDAO().loadLog(jPlayer);
Jobs.getPlayerManager().getPlayersCache().put(offline.getName().toLowerCase(), jPlayer);
} catch (Exception e) {
}
i++;
}
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + i + " players data from last week");
} }
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
int i = 0;
int y = 0;
int total = Jobs.getPlayerManager().getPlayerMap().size();
long time = System.currentTimeMillis();
for (Entry<String, PlayerInfo> one : Jobs.getPlayerManager().getPlayerMap().entrySet()) {
try {
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one);
if (jPlayer == null)
continue;
Jobs.getPlayerManager().getPlayersCache().put(one.getValue().getName().toLowerCase(), jPlayer);
} catch (Exception e) {
}
i++;
y++;
if (y >= 1000) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + i + "/" + total + " players data");
y = 0;
}
}
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + i + " players data in " + ((int) (((System.currentTimeMillis() - time)
/ 1000d) * 100) / 100D));
return;
}
});
// add all online players // add all online players
for (Player online : Bukkit.getServer().getOnlinePlayers()) { for (Player online : Bukkit.getServer().getOnlinePlayers()) {
Jobs.getPlayerManager().playerJoin(online); Jobs.getPlayerManager().playerJoin(online);

View File

@ -18,7 +18,6 @@
package com.gamingmesh.jobs; package com.gamingmesh.jobs;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
@ -96,7 +95,7 @@ public class JobsPlugin extends JavaPlugin {
this.getCommand("jobs").setTabCompleter(new TabComplete()); this.getCommand("jobs").setTabCompleter(new TabComplete());
Jobs.startup(); Jobs.startup(this);
// register the listeners // register the listeners
getServer().getPluginManager().registerEvents(new JobsListener(this), this); getServer().getPluginManager().registerEvents(new JobsListener(this), this);
@ -132,7 +131,7 @@ public class JobsPlugin extends JavaPlugin {
Jobs.getJobsDAO().loadExplore(); Jobs.getJobsDAO().loadExplore();
Jobs.getCommandManager().fillCommands(); Jobs.getCommandManager().fillCommands();
} catch (IOException e) { } catch (Exception e) {
System.out.println("There was some issues when starting plugin. Please contact dev about this. Plugin will be disabled."); System.out.println("There was some issues when starting plugin. Please contact dev about this. Plugin will be disabled.");
this.setEnabled(false); this.setEnabled(false);
e.printStackTrace(); e.printStackTrace();

View File

@ -48,10 +48,9 @@ public class PermissionHandler {
if (jPlayer == null) if (jPlayer == null)
return; return;
Player player = this.plugin.getServer().getPlayer(jPlayer.getPlayerUUID()); Player player = jPlayer.getPlayer();
if (player == null) { if (player == null)
return; return;
}
boolean changed = false; boolean changed = false;

View File

@ -49,6 +49,7 @@ import com.gamingmesh.jobs.dao.JobsDAO;
import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.dao.JobsDAOData;
import com.gamingmesh.jobs.economy.PointsData; import com.gamingmesh.jobs.economy.PointsData;
import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.stuff.ChatColor;
import com.gamingmesh.jobs.stuff.Debug;
import com.gamingmesh.jobs.stuff.PerformCommands; import com.gamingmesh.jobs.stuff.PerformCommands;
import com.gamingmesh.jobs.stuff.Perm; import com.gamingmesh.jobs.stuff.Perm;
@ -118,10 +119,13 @@ public class PlayerManager {
public void playerJoin(Player player) { public void playerJoin(Player player) {
JobsPlayer jPlayer = this.playersCache.get(player.getName().toLowerCase()); JobsPlayer jPlayer = this.playersCache.get(player.getName().toLowerCase());
if (jPlayer == null) { if (jPlayer == null) {
jPlayer = JobsPlayer.loadFromDao(Jobs.getJobsDAO(), player); Debug.D("not in cache");
JobsPlayer.loadLogFromDao(jPlayer); jPlayer = Jobs.getJobsDAO().loadFromDao(player);
jPlayer.loadLogFromDao();
this.playersCache.put(player.getName().toLowerCase(), jPlayer); this.playersCache.put(player.getName().toLowerCase(), jPlayer);
} } else
Debug.D("in cache");
this.players.put(player.getName().toLowerCase(), jPlayer); this.players.put(player.getName().toLowerCase(), jPlayer);
jPlayer.setPlayer(player); jPlayer.setPlayer(player);
@ -140,7 +144,7 @@ public class PlayerManager {
if (Jobs.getGCManager().saveOnDisconnect()) { if (Jobs.getGCManager().saveOnDisconnect()) {
JobsPlayer jPlayer = this.players.remove(player.getName().toLowerCase()); JobsPlayer jPlayer = this.players.remove(player.getName().toLowerCase());
if (jPlayer != null) { if (jPlayer != null) {
jPlayer.save(Jobs.getJobsDAO()); jPlayer.save();
jPlayer.onDisconnect(); jPlayer.onDisconnect();
} }
} else { } else {
@ -155,8 +159,6 @@ public class PlayerManager {
* Save all the information of all of the players in the game * Save all the information of all of the players in the game
*/ */
public void saveAll() { public void saveAll() {
JobsDAO dao = Jobs.getJobsDAO();
/* /*
* Saving is a three step process to minimize synchronization locks when called asynchronously. * Saving is a three step process to minimize synchronization locks when called asynchronously.
* *
@ -168,7 +170,7 @@ public class PlayerManager {
list = new ArrayList<JobsPlayer>(this.players.values()); list = new ArrayList<JobsPlayer>(this.players.values());
for (JobsPlayer jPlayer : list) { for (JobsPlayer jPlayer : list) {
jPlayer.save(dao); jPlayer.save();
} }
Iterator<JobsPlayer> iter = this.players.values().iterator(); Iterator<JobsPlayer> iter = this.players.values().iterator();
@ -239,10 +241,48 @@ public class PlayerManager {
Jobs.getJobsDAO().loadPoints(jPlayer); Jobs.getJobsDAO().loadPoints(jPlayer);
JobsPlayer.loadLogFromDao(jPlayer); jPlayer.loadLogFromDao();
return jPlayer; return jPlayer;
} }
/**
* Get the player job info for specific player
* @param player - the player who's job you're getting
* @return the player job info of the player
*/
public JobsPlayer getJobsPlayerOffline(Entry<String, PlayerInfo> info) {
if (info == null)
return null;
if (info.getValue().getName() == null)
return null;
JobsPlayer jPlayer = new JobsPlayer(info.getValue().getName(), null);
jPlayer.setPlayerUUID(UUID.fromString(info.getKey()));
jPlayer.setUserId(info.getValue().getID());
List<JobsDAOData> list = Jobs.getJobsDAO().getAllJobs(info.getValue().getName(), jPlayer.getPlayerUUID());
for (JobsDAOData jobdata : list) {
if (Jobs.getJob(jobdata.getJobName()) == null)
continue;
Job job = Jobs.getJob(jobdata.getJobName());
if (job == null)
continue;
JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience());
jPlayer.progression.add(jobProgression);
jPlayer.reloadMaxExperience();
jPlayer.reloadLimits();
}
Jobs.getJobsDAO().loadPoints(jPlayer);
jPlayer.loadLogFromDao();
return jPlayer;
}
/** /**
* Causes player to join their job * Causes player to join their job
* @param jPlayer * @param jPlayer
@ -253,7 +293,7 @@ public class PlayerManager {
if (jPlayer.isInJob(job)) if (jPlayer.isInJob(job))
return; return;
// let the user join the job // let the user join the job
if (!jPlayer.joinJob(job, jPlayer)) if (!jPlayer.joinJob(job))
return; return;
// JobsJoin event // JobsJoin event
@ -324,7 +364,7 @@ public class PlayerManager {
*/ */
public void transferJob(JobsPlayer jPlayer, Job oldjob, Job newjob) { public void transferJob(JobsPlayer jPlayer, Job oldjob, Job newjob) {
// synchronized (jPlayer.saveLock) { // synchronized (jPlayer.saveLock) {
if (!jPlayer.transferJob(oldjob, newjob, jPlayer)) if (!jPlayer.transferJob(oldjob, newjob))
return; return;
JobsDAO dao = Jobs.getJobsDAO(); JobsDAO dao = Jobs.getJobsDAO();
@ -332,7 +372,7 @@ public class PlayerManager {
oldjob.updateTotalPlayers(); oldjob.updateTotalPlayers();
dao.joinJob(jPlayer, newjob); dao.joinJob(jPlayer, newjob);
newjob.updateTotalPlayers(); newjob.updateTotalPlayers();
jPlayer.save(dao); jPlayer.save();
// } // }
} }
@ -344,8 +384,8 @@ public class PlayerManager {
*/ */
public void promoteJob(JobsPlayer jPlayer, Job job, int levels) { public void promoteJob(JobsPlayer jPlayer, Job job, int levels) {
// synchronized (jPlayer.saveLock) { // synchronized (jPlayer.saveLock) {
jPlayer.promoteJob(job, levels, jPlayer); jPlayer.promoteJob(job, levels);
jPlayer.save(Jobs.getJobsDAO()); jPlayer.save();
Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate(job.getName());
Jobs.getSignUtil().SignUpdate("gtoplist"); Jobs.getSignUtil().SignUpdate("gtoplist");
@ -361,7 +401,7 @@ public class PlayerManager {
public void demoteJob(JobsPlayer jPlayer, Job job, int levels) { public void demoteJob(JobsPlayer jPlayer, Job job, int levels) {
// synchronized (jPlayer.saveLock) { // synchronized (jPlayer.saveLock) {
jPlayer.demoteJob(job, levels); jPlayer.demoteJob(job, levels);
jPlayer.save(Jobs.getJobsDAO()); jPlayer.save();
Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate(job.getName());
Jobs.getSignUtil().SignUpdate("gtoplist"); Jobs.getSignUtil().SignUpdate("gtoplist");
// } // }
@ -382,7 +422,7 @@ public class PlayerManager {
if (prog.addExperience(experience)) if (prog.addExperience(experience))
performLevelUp(jPlayer, job, oldLevel); performLevelUp(jPlayer, job, oldLevel);
jPlayer.save(Jobs.getJobsDAO()); jPlayer.save();
Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate(job.getName());
Jobs.getSignUtil().SignUpdate("gtoplist"); Jobs.getSignUtil().SignUpdate("gtoplist");
// } // }
@ -401,7 +441,7 @@ public class PlayerManager {
return; return;
prog.addExperience(-experience); prog.addExperience(-experience);
jPlayer.save(Jobs.getJobsDAO()); jPlayer.save();
Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate(job.getName());
Jobs.getSignUtil().SignUpdate("gtoplist"); Jobs.getSignUtil().SignUpdate("gtoplist");
// } // }

View File

@ -45,3 +45,4 @@
/editpoints.class /editpoints.class
/blockinfo.class /blockinfo.class
/test.class /test.class
/purge.class

View File

@ -20,7 +20,7 @@ public class convert implements Cmd {
@JobCommand(2600) @JobCommand(2600)
public boolean perform(JobsPlugin plugin, final CommandSender sender, final String[] args) { public boolean perform(JobsPlugin plugin, final CommandSender sender, final String[] args) {
if (!(sender instanceof Player)) { if (sender instanceof Player) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.fromconsole")); sender.sendMessage(Jobs.getLanguage().getMessage("general.error.fromconsole"));
return false; return false;
} }

View File

@ -141,7 +141,6 @@ public class GeneralConfigManager {
public Parser maxPointEquation; public Parser maxPointEquation;
public boolean DisabledWorldsUse; public boolean DisabledWorldsUse;
public boolean PreLoadUse;
public List<String> DisabledWorldsList = new ArrayList<String>(); public List<String> DisabledWorldsList = new ArrayList<String>();
public List<Schedule> BoostSchedule = new ArrayList<Schedule>(); public List<Schedule> BoostSchedule = new ArrayList<Schedule>();
@ -441,11 +440,7 @@ public class GeneralConfigManager {
"Only commands can be performed from disabled worlds with jobs.disabledworld.commands permission node"); "Only commands can be performed from disabled worlds with jobs.disabledworld.commands permission node");
DisabledWorldsUse = c.get("Optimizations.DisabledWorlds.Use", false); DisabledWorldsUse = c.get("Optimizations.DisabledWorlds.Use", false);
DisabledWorldsList = c.getStringList("Optimizations.DisabledWorlds.List", Arrays.asList(Bukkit.getWorlds().get(0).getName())); DisabledWorldsList = c.getStringList("Optimizations.DisabledWorlds.List", Arrays.asList(Bukkit.getWorlds().get(0).getName()));
c.getW().addComment("Optimizations.PreLoad.Use", "By setting this to true, Jobs plugin will preload some of recent players data to be used instead of loading them from data base on players join");
PreLoadUse = c.get("Optimizations.PreLoad.Use", false);
// c.getW().addComment("Optimizations.Purge.Use", "By setting this to true, Jobs plugin will clean data base on startup from all jobs with level 1 and at 0 exp"); // c.getW().addComment("Optimizations.Purge.Use", "By setting this to true, Jobs plugin will clean data base on startup from all jobs with level 1 and at 0 exp");
// PurgeUse = c.get("Optimizations.Purge.Use", false); // PurgeUse = c.get("Optimizations.Purge.Use", false);

View File

@ -30,7 +30,6 @@ import org.bukkit.entity.Player;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAO;
import com.gamingmesh.jobs.dao.JobsDAOData;
import com.gamingmesh.jobs.resources.jfep.Parser; import com.gamingmesh.jobs.resources.jfep.Parser;
import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.stuff.ChatColor;
import com.gamingmesh.jobs.stuff.Perm; import com.gamingmesh.jobs.stuff.Perm;
@ -39,7 +38,7 @@ public class JobsPlayer {
// the player the object belongs to // the player the object belongs to
private String userName; private String userName;
// progression of the player in each job // progression of the player in each job
private UUID playerUUID; public UUID playerUUID;
public ArrayList<JobProgression> progression = new ArrayList<JobProgression>(); public ArrayList<JobProgression> progression = new ArrayList<JobProgression>();
private HashMap<String, ArrayList<BoostCounter>> boostCounter = new HashMap<String, ArrayList<BoostCounter>>(); private HashMap<String, ArrayList<BoostCounter>> boostCounter = new HashMap<String, ArrayList<BoostCounter>>();
@ -73,75 +72,15 @@ public class JobsPlayer {
public JobsPlayer(String userName, OfflinePlayer player) { public JobsPlayer(String userName, OfflinePlayer player) {
this.userName = userName; this.userName = userName;
this.OffPlayer = player; this.OffPlayer = player;
} this.player = Bukkit.getPlayer(userName);
public static JobsPlayer loadFromDao(JobsDAO dao, OfflinePlayer player) {
JobsPlayer jPlayer = new JobsPlayer(player.getName(), player);
jPlayer.playerUUID = player.getUniqueId();
List<JobsDAOData> list = dao.getAllJobs(player);
// synchronized (jPlayer.saveLock) {
jPlayer.progression.clear();
for (JobsDAOData jobdata : list) {
if (Jobs.getJob(jobdata.getJobName()) == null)
continue;
// add the job
Job job = Jobs.getJob(jobdata.getJobName());
if (job == null)
continue;
// create the progression object
JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience());
// calculate the max level
// add the progression level.
jPlayer.progression.add(jobProgression);
}
jPlayer.reloadMaxExperience();
jPlayer.reloadLimits();
jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID());
Jobs.getJobsDAO().loadPoints(jPlayer);
// }
return jPlayer;
}
public static JobsPlayer loadFromDao(JobsDAO dao, Player player) {
JobsPlayer jPlayer = new JobsPlayer(player.getName(), player);
jPlayer.playerUUID = player.getUniqueId();
List<JobsDAOData> list = new ArrayList<JobsDAOData>();
list = dao.getAllJobs(player);
// synchronized (jPlayer.saveLock) {
jPlayer.progression.clear();
for (JobsDAOData jobdata : list) {
if (Jobs.getJob(jobdata.getJobName()) == null)
continue;
// add the job
Job job = Jobs.getJob(jobdata.getJobName());
if (job == null)
continue;
// create the progression object
JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience());
// calculate the max level
// add the progression level.
jPlayer.progression.add(jobProgression);
}
jPlayer.reloadMaxExperience();
jPlayer.reloadLimits();
jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID());
Jobs.getJobsDAO().loadPoints(jPlayer);
// }
return jPlayer;
} }
public void setPlayer(Player p) { public void setPlayer(Player p) {
this.player = p; this.player = p;
} }
public static void loadLogFromDao(JobsPlayer jPlayer) { public void loadLogFromDao() {
Jobs.getJobsDAO().loadLog(jPlayer); Jobs.getJobsDAO().loadLog(this);
} }
public synchronized List<String> getUpdateBossBarFor() { public synchronized List<String> getUpdateBossBarFor() {
@ -388,16 +327,16 @@ public class JobsPlayer {
* Player joins a job * Player joins a job
* @param job - the job joined * @param job - the job joined
*/ */
public boolean joinJob(Job job, JobsPlayer jPlayer) { public boolean joinJob(Job job) {
// synchronized (saveLock) { // synchronized (saveLock) {
if (!isInJob(job)) { if (!isInJob(job)) {
int level = 1; int level = 1;
int exp = 0; int exp = 0;
if (Jobs.getJobsDAO().checkArchive(jPlayer, job).size() > 0) { if (Jobs.getJobsDAO().checkArchive(this, job).size() > 0) {
List<Integer> info = Jobs.getJobsDAO().checkArchive(jPlayer, job); List<Integer> info = Jobs.getJobsDAO().checkArchive(this, job);
level = info.get(0); level = info.get(0);
//exp = info.get(1); //exp = info.get(1);
Jobs.getJobsDAO().deleteArchive(jPlayer, job); Jobs.getJobsDAO().deleteArchive(this, job);
} }
progression.add(new JobProgression(job, this, level, exp)); progression.add(new JobProgression(job, this, level, exp));
@ -448,7 +387,7 @@ public class JobsPlayer {
* @param job - the job being promoted * @param job - the job being promoted
* @param levels - number of levels to promote * @param levels - number of levels to promote
*/ */
public void promoteJob(Job job, int levels, JobsPlayer player) { public void promoteJob(Job job, int levels) {
// synchronized (saveLock) { // synchronized (saveLock) {
JobProgression prog = getJobProgression(job); JobProgression prog = getJobProgression(job);
if (prog == null) if (prog == null)
@ -459,7 +398,7 @@ public class JobsPlayer {
int maxLevel = job.getMaxLevel(); int maxLevel = job.getMaxLevel();
if (player.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0) if (this.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0)
maxLevel = job.getVipMaxLevel(); maxLevel = job.getVipMaxLevel();
if (maxLevel > 0 && newLevel > maxLevel) { if (maxLevel > 0 && newLevel > maxLevel) {
@ -513,7 +452,7 @@ public class JobsPlayer {
* @param oldjob - the old job * @param oldjob - the old job
* @param newjob - the new job * @param newjob - the new job
*/ */
public boolean transferJob(Job oldjob, Job newjob, JobsPlayer jPlayer) { public boolean transferJob(Job oldjob, Job newjob) {
// synchronized (saveLock) { // synchronized (saveLock) {
if (!isInJob(newjob)) { if (!isInJob(newjob)) {
for (JobProgression prog : progression) { for (JobProgression prog : progression) {
@ -523,7 +462,7 @@ public class JobsPlayer {
prog.setJob(newjob); prog.setJob(newjob);
int maxLevel = 0; int maxLevel = 0;
if (jPlayer.havePermission("jobs." + newjob.getName() + ".vipmaxlevel")) if (this.havePermission("jobs." + newjob.getName() + ".vipmaxlevel"))
maxLevel = newjob.getVipMaxLevel(); maxLevel = newjob.getVipMaxLevel();
else else
maxLevel = newjob.getMaxLevel(); maxLevel = newjob.getMaxLevel();
@ -649,9 +588,10 @@ public class JobsPlayer {
* Performs player save * Performs player save
* @param dao * @param dao
*/ */
public void save(JobsDAO dao) { public void save() {
// synchronized (saveLock) { // synchronized (saveLock) {
if (!isSaved()) { if (!isSaved()) {
JobsDAO dao = Jobs.getJobsDAO();
dao.save(this); dao.save(this);
dao.saveLog(this); dao.saveLog(this);
dao.savePoints(this); dao.savePoints(this);

View File

@ -162,24 +162,53 @@ public abstract class JobsDAO {
if (conn == null) if (conn == null)
return jobs; return jobs;
PreparedStatement prest = null;
ResultSet res = null; ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;");
prest.setInt(1, id); prest.setInt(1, id);
res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
jobs.add(new JobsDAOData(uuid, res.getString(1), res.getInt(2), res.getInt(3))); jobs.add(new JobsDAOData(res.getString(1), res.getInt(2), res.getInt(3)));
} }
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (res != null) close(res);
try { close(prest);
res.close(); }
} catch (SQLException e) { return jobs;
e.printStackTrace(); }
}
/**
* Get all jobs the player is part of.
* @param playerUUID - the player being searched for
* @return list of all of the names of the jobs the players are part of.
*/
public List<JobsDAOData> getAllJobs(JobsPlayer jPlayer) {
ArrayList<JobsDAOData> jobs = new ArrayList<JobsDAOData>();
int id = jPlayer.getUserId();
JobsConnection conn = getConnection();
if (conn == null)
return jobs;
PreparedStatement prest = null;
ResultSet res = null;
try {
prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;");
prest.setInt(1, id);
res = prest.executeQuery();
while (res.next()) {
jobs.add(new JobsDAOData(res.getString(1), res.getInt(2), res.getInt(3)));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return jobs; return jobs;
} }
@ -196,26 +225,31 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prestt = null;
try { try {
PreparedStatement prest = conn.prepareStatement("INSERT INTO `" + prefix + "users` (`player_uuid`, `username`) VALUES (?, ?);"); prestt = conn.prepareStatement("INSERT INTO `" + prefix + "users` (`player_uuid`, `username`) VALUES (?, ?);");
prest.setString(1, uuid.toString()); prestt.setString(1, uuid.toString());
prest.setString(2, playerName); prestt.setString(2, playerName);
prest.executeUpdate(); prestt.executeUpdate();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prestt);
} }
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT `id` FROM `" + this.prefix + "users` WHERE `player_uuid` = ?;"); prest = conn.prepareStatement("SELECT `id` FROM `" + this.prefix + "users` WHERE `player_uuid` = ?;");
prest.setString(1, uuid.toString()); prest.setString(1, uuid.toString());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
res.next(); res.next();
int id = res.getInt("id"); int id = res.getInt("id");
Jobs.getPlayerManager().getPlayerMap().put(uuid.toString(), new PlayerInfo(playerName, id)); Jobs.getPlayerManager().getPlayerMap().put(uuid.toString(), new PlayerInfo(playerName, id));
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
} }
@ -229,24 +263,20 @@ public abstract class JobsDAO {
if (conn == null) if (conn == null)
return 0; return 0;
int count = 0; int count = 0;
PreparedStatement prest = null;
ResultSet res = null; ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;"); prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;");
prest.setString(1, JobName); prest.setString(1, JobName);
res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
count = res.getInt(1); count = res.getInt(1);
} }
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (res != null) close(res);
try { close(prest);
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
return count; return count;
} }
@ -260,16 +290,19 @@ public abstract class JobsDAO {
if (conn == null) if (conn == null)
return 0; return 0;
int count = 0; int count = 0;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs`;"); prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs`;");
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
count = res.getInt(1); count = res.getInt(1);
} }
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return count; return count;
} }
@ -290,17 +323,20 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return jobs; return jobs;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;");
prest.setInt(1, info.getValue().getID()); prest.setInt(1, info.getValue().getID());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
jobs.add(new JobsDAOData(UUID.fromString(info.getKey()), res.getString(2), res.getInt(3), res.getInt(4))); jobs.add(new JobsDAOData(res.getString(2), res.getInt(3), res.getInt(4)));
} }
prest.close();
res.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return jobs; return jobs;
} }
@ -314,6 +350,7 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { try {
int level = 1; int level = 1;
int exp = 0; int exp = 0;
@ -322,15 +359,16 @@ public abstract class JobsDAO {
level = info.get(0); level = info.get(0);
deleteArchive(jPlayer, job); deleteArchive(jPlayer, job);
} }
PreparedStatement prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);");
prest.setInt(1, jPlayer.getUserId()); prest.setInt(1, jPlayer.getUserId());
prest.setString(2, job.getName()); prest.setString(2, job.getName());
prest.setInt(3, level); prest.setInt(3, level);
prest.setInt(4, exp); prest.setInt(4, exp);
prest.execute(); prest.execute();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest);
} }
} }
@ -346,16 +384,19 @@ public abstract class JobsDAO {
return null; return null;
List<Convert> list = new ArrayList<Convert>(); List<Convert> list = new ArrayList<Convert>();
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT * FROM `" + prefix + table + "`"); prest = conn.prepareStatement("SELECT * FROM `" + prefix + table + "`");
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
list.add(new Convert(res.getInt("id"), res.getInt("userid"), res.getString("job"), res.getInt("level"), res.getInt("experience"))); list.add(new Convert(res.getInt("id"), res.getInt("userid"), res.getString("job"), res.getInt("level"), res.getInt("experience")));
} }
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
try { try {
@ -371,10 +412,10 @@ public abstract class JobsDAO {
if (conns == null) if (conns == null)
return; return;
PreparedStatement insert = null; PreparedStatement insert = null;
Statement statement = null;
int i = list.size(); int i = list.size();
try { try {
statement = conns.createStatement();
Statement statement = conns.createStatement();
if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) {
statement.executeUpdate("TRUNCATE `" + getPrefix() + table + "`"); statement.executeUpdate("TRUNCATE `" + getPrefix() + table + "`");
} else { } else {
@ -395,14 +436,9 @@ public abstract class JobsDAO {
insert.executeBatch(); insert.executeBatch();
conns.commit(); conns.commit();
conns.setAutoCommit(true); conns.setAutoCommit(true);
statement.close();
} finally { } finally {
if (insert != null) { close(statement);
try { close(insert);
insert.close();
} catch (SQLException e) {
}
}
} }
} }
@ -415,14 +451,16 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { try {
PreparedStatement prest = conn.prepareStatement("DELETE FROM `" + prefix + "jobs` WHERE `userid` = ? AND `job` = ?;"); prest = conn.prepareStatement("DELETE FROM `" + prefix + "jobs` WHERE `userid` = ? AND `job` = ?;");
prest.setInt(1, jPlayer.getUserId()); prest.setInt(1, jPlayer.getUserId());
prest.setString(2, job.getName()); prest.setString(2, job.getName());
prest.execute(); prest.execute();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest);
} }
} }
@ -435,6 +473,7 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { try {
int level = 1; int level = 1;
int exp = 0; int exp = 0;
@ -444,16 +483,16 @@ public abstract class JobsDAO {
exp = (int) progression.getExperience(); exp = (int) progression.getExperience();
} }
} }
PreparedStatement prest = conn.prepareStatement("INSERT INTO `" + prefix prest = conn.prepareStatement("INSERT INTO `" + prefix + "archive` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);");
+ "archive` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);");
prest.setInt(1, jPlayer.getUserId()); prest.setInt(1, jPlayer.getUserId());
prest.setString(2, job.getName()); prest.setString(2, job.getName());
prest.setInt(3, level); prest.setInt(3, level);
prest.setInt(4, exp); prest.setInt(4, exp);
prest.execute(); prest.execute();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest);
} }
} }
@ -467,11 +506,13 @@ public abstract class JobsDAO {
List<Integer> info = new ArrayList<Integer>(); List<Integer> info = new ArrayList<Integer>();
if (conn == null) if (conn == null)
return info; return info;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); prest = conn.prepareStatement("SELECT `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;");
prest.setInt(1, jPlayer.getUserId()); prest.setInt(1, jPlayer.getUserId());
prest.setString(2, job.getName()); prest.setString(2, job.getName());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
if (res.next()) { if (res.next()) {
int level = (int) ((res.getInt(1) - (res.getInt(1) * (Jobs.getGCManager().levelLossPercentage / 100.0)))); int level = (int) ((res.getInt(1) - (res.getInt(1) * (Jobs.getGCManager().levelLossPercentage / 100.0))));
if (level < 1) if (level < 1)
@ -488,11 +529,11 @@ public abstract class JobsDAO {
info.add(level); info.add(level);
info.add(res.getInt(2)); info.add(res.getInt(2));
} }
res.close();
prest.close();
return info;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return info; return info;
} }
@ -512,12 +553,14 @@ public abstract class JobsDAO {
List<TopList> names = new ArrayList<TopList>(); List<TopList> names = new ArrayList<TopList>();
if (conn == null) if (conn == null)
return null; return names;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT userid, COUNT(*) AS amount, sum(level) AS totallvl FROM `" + prefix prest = conn.prepareStatement("SELECT userid, COUNT(*) AS amount, sum(level) AS totallvl FROM `" + prefix
+ "jobs` GROUP BY userid ORDER BY totallvl DESC LIMIT " + start + ",20;"); + "jobs` GROUP BY userid ORDER BY totallvl DESC LIMIT " + start + ",20;");
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
@ -533,11 +576,11 @@ public abstract class JobsDAO {
names.add(top); names.add(top);
} }
res.close();
prest.close();
return names;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return names; return names;
} }
@ -551,11 +594,13 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return null; return null;
List<String> info = new ArrayList<String>();
PreparedStatement prest = null;
ResultSet res = null;
try { try {
List<String> info = new ArrayList<String>(); prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ?;");
PreparedStatement prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ?;");
prest.setInt(1, jPlayer.getUserId()); prest.setInt(1, jPlayer.getUserId());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
int level = (int) ((res.getInt(2) - (res.getInt(2) * (Jobs.getGCManager().levelLossPercentage / 100.0)))); int level = (int) ((res.getInt(2) - (res.getInt(2) * (Jobs.getGCManager().levelLossPercentage / 100.0))));
@ -579,13 +624,13 @@ public abstract class JobsDAO {
info.add(res.getString(1) + ":" + res.getInt(2) + ":" + level + ":" + res.getInt(3)); info.add(res.getString(1) + ":" + res.getInt(2) + ":" + level + ":" + res.getInt(3));
} }
res.close();
prest.close();
return info;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return null; return info;
} }
public PlayerInfo loadPlayerData(UUID uuid) { public PlayerInfo loadPlayerData(UUID uuid) {
@ -594,24 +639,20 @@ public abstract class JobsDAO {
if (conn == null) if (conn == null)
return pInfo; return pInfo;
PreparedStatement prest = null; PreparedStatement prest = null;
ResultSet res = null;
try { try {
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users` WHERE `player_uuid` = ?;"); prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users` WHERE `player_uuid` = ?;");
prest.setString(1, uuid.toString()); prest.setString(1, uuid.toString());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
pInfo = new PlayerInfo(res.getString("username"), res.getInt("id")); pInfo = new PlayerInfo(res.getString("username"), res.getInt("id"));
Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), pInfo); Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), pInfo);
} }
res.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (prest != null) close(res);
try { close(prest);
prest.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
return pInfo; return pInfo;
} }
@ -622,23 +663,70 @@ public abstract class JobsDAO {
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null; PreparedStatement prest = null;
ResultSet res = null;
try { try {
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users`;"); prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users`;");
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id"))); Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id")));
} }
res.close();
return;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (prest != null) close(res);
try { close(prest);
prest.close(); }
} catch (SQLException e) { return;
e.printStackTrace(); }
}
public JobsPlayer loadFromDao(OfflinePlayer player) {
JobsPlayer jPlayer = new JobsPlayer(player.getName(), player);
jPlayer.playerUUID = player.getUniqueId();
List<JobsDAOData> list = getAllJobs(player);
// synchronized (jPlayer.saveLock) {
jPlayer.progression.clear();
for (JobsDAOData jobdata : list) {
if (Jobs.getJob(jobdata.getJobName()) == null)
continue;
// add the job
Job job = Jobs.getJob(jobdata.getJobName());
if (job == null)
continue;
// create the progression object
JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience());
// calculate the max level
// add the progression level.
jPlayer.progression.add(jobProgression);
}
jPlayer.reloadMaxExperience();
jPlayer.reloadLimits();
jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID());
Jobs.getJobsDAO().loadPoints(jPlayer);
// }
return jPlayer;
}
public void loadAllData() {
Jobs.getPlayerManager().getPlayerMap().clear();
JobsConnection conn = getConnection();
if (conn == null)
return;
PreparedStatement prest = null;
ResultSet res = null;
try {
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users`;");
res = prest.executeQuery();
while (res.next()) {
Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return; return;
} }
@ -652,14 +740,16 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { try {
PreparedStatement prest = conn.prepareStatement("DELETE FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); prest = conn.prepareStatement("DELETE FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;");
prest.setInt(1, jPlayer.getUserId()); prest.setInt(1, jPlayer.getUserId());
prest.setString(2, job.getName()); prest.setString(2, job.getName());
prest.execute(); prest.execute();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest);
} }
} }
@ -671,8 +761,9 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { try {
PreparedStatement prest = conn.prepareStatement("UPDATE `" + prefix prest = conn.prepareStatement("UPDATE `" + prefix
+ "jobs` SET `level` = ?, `experience` = ? WHERE `userid` = ? AND `job` = ?;"); + "jobs` SET `level` = ?, `experience` = ? WHERE `userid` = ? AND `job` = ?;");
for (JobProgression progression : player.getJobProgression()) { for (JobProgression progression : player.getJobProgression()) {
prest.setInt(1, progression.getLevel()); prest.setInt(1, progression.getLevel());
@ -681,9 +772,10 @@ public abstract class JobsDAO {
prest.setString(4, progression.getJob().getName()); prest.setString(4, progression.getJob().getName());
prest.execute(); prest.execute();
} }
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest);
} }
} }
@ -691,24 +783,23 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { try {
PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getPlayerUUID()); PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getPlayerUUID());
String req = "UPDATE `" + prefix + "points` SET `totalpoints` = ?, `currentpoints` = ? WHERE `userid` = ?;"; String req = "UPDATE `" + prefix + "points` SET `totalpoints` = ?, `currentpoints` = ? WHERE `userid` = ?;";
if (pointInfo.isNewEntry()) { if (pointInfo.isNewEntry()) {
pointInfo.setNewEntry(false); pointInfo.setNewEntry(false);
req = "INSERT INTO `" + prefix + "points` (`totalpoints`, `currentpoints`, `userid`) VALUES (?, ?, ?);"; req = "INSERT INTO `" + prefix + "points` (`totalpoints`, `currentpoints`, `userid`) VALUES (?, ?, ?);";
} }
prest = conn.prepareStatement(req);
PreparedStatement prest = conn.prepareStatement(req);
prest.setDouble(1, pointInfo.getTotalPoints()); prest.setDouble(1, pointInfo.getTotalPoints());
prest.setDouble(2, pointInfo.getCurrentPoints()); prest.setDouble(2, pointInfo.getCurrentPoints());
prest.setInt(3, player.getUserId()); prest.setInt(3, player.getUserId());
prest.execute(); prest.execute();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest);
} }
} }
@ -716,20 +807,23 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT `totalpoints`, `currentpoints` FROM `" + prefix + "points` WHERE `userid` = ?;"); prest = conn.prepareStatement("SELECT `totalpoints`, `currentpoints` FROM `" + prefix + "points` WHERE `userid` = ?;");
prest.setInt(1, player.getUserId()); prest.setInt(1, player.getUserId());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
if (res.next()) { if (res.next()) {
Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID(), res.getDouble("currentpoints"), res.getDouble("totalpoints")); Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID(), res.getDouble("currentpoints"), res.getDouble("totalpoints"));
} else { } else {
Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID()); Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID());
} }
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
} }
@ -785,18 +879,8 @@ public abstract class JobsDAO {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (prest1 != null) close(prest1);
try { close(prest2);
prest1.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (prest2 != null)
try {
prest2.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
@ -809,32 +893,24 @@ public abstract class JobsDAO {
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null; PreparedStatement prest = null;
ResultSet res = null;
try { try {
int time = TimeManage.timeInInt(); int time = TimeManage.timeInInt();
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "log` WHERE `userid` = ? AND `time` = ? ;"); prest = conn.prepareStatement("SELECT * FROM `" + prefix + "log` WHERE `userid` = ? AND `time` = ? ;");
prest.setInt(1, player.getUserId()); prest.setInt(1, player.getUserId());
prest.setInt(2, time); prest.setInt(2, time);
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
Jobs.getLoging().loadToLog(player, res.getString("action"), res.getString("itemname"), res.getInt("count"), res.getDouble("money"), res.getDouble("exp")); Jobs.getLoging().loadToLog(player, res.getString("action"), res.getString("itemname"), res.getInt("count"), res.getDouble("money"), res.getDouble("exp"));
} }
res.close();
} catch (Exception e) { } catch (Exception e) {
if (prest != null) close(res);
try { close(prest);
prest.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
this.dropDataBase("log"); this.dropDataBase("log");
this.createDefaultLogBase(); this.createDefaultLogBase();
} finally { } finally {
if (prest != null) close(res);
try { close(prest);
prest.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
@ -849,9 +925,10 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest2 = null;
try { try {
PreparedStatement prest2 = conn.prepareStatement("INSERT INTO `" + prefix + "explore` (`worldname`, `chunkX`, `chunkZ`, `playerName`) VALUES (?, ?, ?, ?);"); prest2 = conn.prepareStatement("INSERT INTO `" + prefix + "explore` (`worldname`, `chunkX`, `chunkZ`, `playerName`) VALUES (?, ?, ?, ?);");
conn.setAutoCommit(false); conn.setAutoCommit(false);
int i = 0; int i = 0;
for (Entry<String, ExploreRegion> worlds : Jobs.getExplore().getWorlds().entrySet()) { for (Entry<String, ExploreRegion> worlds : Jobs.getExplore().getWorlds().entrySet()) {
@ -871,7 +948,6 @@ public abstract class JobsDAO {
prest2.executeBatch(); prest2.executeBatch();
conn.commit(); conn.commit();
conn.setAutoCommit(true); conn.setAutoCommit(true);
prest2.close();
if (i > 0) { if (i > 0) {
String message = ChatColor.translateAlternateColorCodes('&', "&e[Jobs] Saved " + i + " new explorer entries."); String message = ChatColor.translateAlternateColorCodes('&', "&e[Jobs] Saved " + i + " new explorer entries.");
@ -880,6 +956,8 @@ public abstract class JobsDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(prest2);
} }
} }
@ -894,17 +972,19 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
prest = conn.prepareStatement("SELECT * FROM `" + prefix + "explore`;");
PreparedStatement prest = conn.prepareStatement("SELECT * FROM `" + prefix + "explore`;"); res = prest.executeQuery();
ResultSet res = prest.executeQuery();
while (res.next()) { while (res.next()) {
Jobs.getExplore().ChunkRespond(res.getString("playerName"), res.getString("worldname"), res.getInt("chunkX"), res.getInt("chunkZ"), false); Jobs.getExplore().ChunkRespond(res.getString("playerName"), res.getString("worldname"), res.getInt("chunkX"), res.getInt("chunkZ"), false);
} }
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
} }
@ -918,21 +998,22 @@ public abstract class JobsDAO {
List<Integer> nameList = new ArrayList<Integer>(); List<Integer> nameList = new ArrayList<Integer>();
if (conn == null) if (conn == null)
return nameList; return nameList;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
prest = conn.prepareStatement("SELECT `userid` FROM `" + prefix + "log` WHERE `time` >= ? AND `time` <= ? ;");
PreparedStatement prest = conn.prepareStatement("SELECT `userid` FROM `" + prefix + "log` WHERE `time` >= ? AND `time` <= ? ;");
prest.setInt(1, fromtime); prest.setInt(1, fromtime);
prest.setInt(2, untiltime); prest.setInt(2, untiltime);
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
if (!nameList.contains(res.getInt("userid"))) if (!nameList.contains(res.getInt("userid")))
nameList.add(res.getInt("userid")); nameList.add(res.getInt("userid"));
} }
res.close();
prest.close();
return nameList;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return nameList; return nameList;
} }
@ -947,11 +1028,13 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return jobs; return jobs;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT DISTINCT(userid), `level`, `experience` FROM `" + prefix prest = conn.prepareStatement("SELECT DISTINCT(userid), `level`, `experience` FROM `" + prefix
+ "jobs` WHERE `job` LIKE ? ORDER BY `level` DESC, LOWER(experience) DESC LIMIT " + limit + ", 15;"); + "jobs` WHERE `job` LIKE ? ORDER BY `level` DESC, LOWER(experience) DESC LIMIT " + limit + ", 15;");
prest.setString(1, jobsname); prest.setString(1, jobsname);
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(res.getInt("userid")); Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoById(res.getInt("userid"));
@ -977,10 +1060,11 @@ public abstract class JobsDAO {
jobs.add(new TopList(res.getInt("userid"), res.getInt("level"), res.getInt("experience"))); jobs.add(new TopList(res.getInt("userid"), res.getInt("level"), res.getInt("experience")));
} }
} }
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return jobs; return jobs;
} }
@ -995,17 +1079,20 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return slot; return slot;
PreparedStatement prest = null;
ResultSet res = null;
try { try {
PreparedStatement prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;"); prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;");
prest.setString(1, job.getName()); prest.setString(1, job.getName());
ResultSet res = prest.executeQuery(); res = prest.executeQuery();
if (res.next()) { if (res.next()) {
slot = res.getInt(1); slot = res.getInt(1);
} }
res.close();
prest.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(res);
close(prest);
} }
return slot; return slot;
} }
@ -1034,18 +1121,8 @@ public abstract class JobsDAO {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (res != null) { close(res);
try { close(prest);
res.close();
} catch (SQLException e) {
}
}
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
}
} }
return schema; return schema;
} }
@ -1076,12 +1153,7 @@ public abstract class JobsDAO {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (prest != null) { close(prest);
try {
prest.close();
} catch (SQLException e) {
}
}
} }
} }
@ -1092,14 +1164,12 @@ public abstract class JobsDAO {
*/ */
public void executeSQL(String sql) throws SQLException { public void executeSQL(String sql) throws SQLException {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
Statement stmt = conn.createStatement(); Statement stmt = null;
try { try {
stmt = conn.createStatement();
stmt.execute(sql); stmt.execute(sql);
} finally { } finally {
try { close(stmt);
stmt.close();
} catch (SQLException e) {
}
} }
} }
@ -1123,4 +1193,22 @@ public abstract class JobsDAO {
public synchronized void closeConnections() { public synchronized void closeConnections() {
pool.closeConnection(); pool.closeConnection();
} }
private static void close(ResultSet res) {
if (res != null)
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void close(Statement stmt) {
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }

View File

@ -18,8 +18,6 @@
package com.gamingmesh.jobs.dao; package com.gamingmesh.jobs.dao;
import java.util.UUID;
/** /**
* Container class to hold information out of the database. * Container class to hold information out of the database.
* *
@ -30,53 +28,43 @@ import java.util.UUID;
* *
*/ */
public class JobsDAOData { public class JobsDAOData {
private UUID playerUUID;
private String job; private String job;
private int level; private int level;
private int experience; private int experience;
/** /**
* Constructor class for the DAO side of things. * Constructor class for the DAO side of things.
* @param job - the name of the job * @param job - the name of the job
* @param level - the level of the job * @param level - the level of the job
* @param experience - the experience of the job * @param experience - the experience of the job
*/ */
public JobsDAOData(UUID playerUUID, String job, int level, int experience) { public JobsDAOData(String job, int level, int experience) {
this.playerUUID = playerUUID; this.job = job;
this.job = job; this.level = level;
this.level = level; this.experience = experience;
this.experience = experience;
} }
/**
* Getter function for the playerUUID
* @return the job name
*/
public UUID getPlayerUUID() {
return playerUUID;
}
/** /**
* Getter function for the job name * Getter function for the job name
* @return the job name * @return the job name
*/ */
public String getJobName() { public String getJobName() {
return job; return job;
} }
/** /**
* Getter function for the level * Getter function for the level
* @return the level in the job * @return the level in the job
*/ */
public int getLevel() { public int getLevel() {
return level; return level;
} }
/** /**
* Getter function for the experience. * Getter function for the experience.
* @return the experience in the job * @return the experience in the job
*/ */
public double getExperience() { public double getExperience() {
return experience; return experience;
} }
} }

View File

@ -19,7 +19,6 @@ import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.gamingmesh.jobs.NMS; import com.gamingmesh.jobs.NMS;
import com.gamingmesh.jobs.stuff.Debug;
public class v1_10 implements NMS { public class v1_10 implements NMS {
@Override @Override