/** Save all the information of all of the players in the game */ public void saveAll() { JobsDAO dao = plugin.getJobsCore().getJobsDAO(); /* * Saving is a three step process to minimize synchronization locks when called asynchronously. * * 1) Safely copy list for saving. * 2) Perform save on all players on copied list. * 3) Garbage collect the real list to remove any offline players with saved data */ ArrayList<JobsPlayer> list = null; synchronized (players) { list = new ArrayList<JobsPlayer>(players.values()); } for (JobsPlayer jPlayer : list) { jPlayer.save(dao); } synchronized (players) { Iterator<JobsPlayer> iter = players.values().iterator(); while (iter.hasNext()) { JobsPlayer jPlayer = iter.next(); synchronized (jPlayer.saveLock) { if (!jPlayer.isOnline() && jPlayer.isSaved()) { iter.remove(); } } } } }
/** * Handles player quit * * @param playername */ public void playerQuit(String playername) { synchronized (players) { if (plugin.getJobsConfiguration().saveOnDisconnect()) { JobsPlayer jPlayer = players.remove(playername); if (jPlayer != null) { jPlayer.save(plugin.getJobsCore().getJobsDAO()); jPlayer.onDisconnect(); } } else { JobsPlayer jPlayer = players.get(playername); if (jPlayer != null) { jPlayer.onDisconnect(); } } } }
/** * Demote player in their job * * @param jPlayer * @param job - the job * @param levels - number of levels to demote */ public void demoteJob(JobsPlayer jPlayer, Job job, int levels) { synchronized (jPlayer.saveLock) { jPlayer.demoteJob(job, levels); jPlayer.save(plugin.getJobsCore().getJobsDAO()); } Player player = plugin.getServer().getPlayer(jPlayer.getName()); if (player != null) { String message = plugin.getMessageConfig().getMessage("demote-target"); message = message.replace("%jobcolour%", job.getChatColour().toString()); message = message.replace("%jobname%", job.getName()); message = message.replace("%levelslost%", Integer.valueOf(levels).toString()); for (String line : message.split("\n")) { player.sendMessage(line); } } }
/** * Removes experience to the player * * @param jPlayer * @param job - the job * @param experience - experience gained */ public void removeExperience(JobsPlayer jPlayer, Job job, double experience) { synchronized (jPlayer.saveLock) { JobProgression prog = jPlayer.getJobProgression(job); if (prog == null) return; prog.addExperience(-experience); jPlayer.save(plugin.getJobsCore().getJobsDAO()); } Player player = plugin.getServer().getPlayer(jPlayer.getName()); if (player != null) { String message = plugin.getMessageConfig().getMessage("removexp-target"); message = message.replace("%jobcolour%", job.getChatColour().toString()); message = message.replace("%jobname%", job.getName()); message = message.replace("%expgained%", Double.valueOf(experience).toString()); for (String line : message.split("\n")) { player.sendMessage(line); } } }
/** * Transfers player job * * @param jPlayer * @param oldjob - the old job * @param newjob - the new job */ public void transferJob(JobsPlayer jPlayer, Job oldjob, Job newjob) { synchronized (jPlayer.saveLock) { if (!jPlayer.transferJob(oldjob, newjob)) return; JobsDAO dao = plugin.getJobsCore().getJobsDAO(); dao.quitJob(jPlayer, oldjob); dao.joinJob(jPlayer, newjob); jPlayer.save(dao); } Player player = plugin.getServer().getPlayer(jPlayer.getName()); if (player != null) { String message = plugin.getMessageConfig().getMessage("transfer-target"); message = message.replace("%oldjobcolour%", oldjob.getChatColour().toString()); message = message.replace("%oldjobname%", oldjob.getName()); message = message.replace("%newjobcolour%", newjob.getChatColour().toString()); message = message.replace("%newjobname%", newjob.getName()); for (String line : message.split("\n")) { player.sendMessage(line); } } }