use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.
the class Jobs method action.
/**
* Performed an action
*
* Give correct experience and income
* @param jPlayer - the player
* @param action - the action
* @param multiplier - the payment/xp multiplier
*/
public static void action(JobsPlayer jPlayer, ActionInfo info, double multiplier) {
List<JobProgression> progression = jPlayer.getJobProgression();
int numjobs = progression.size();
// no job
if (numjobs == 0) {
Job jobNone = Jobs.getNoneJob();
if (jobNone != null) {
Double income = jobNone.getIncome(info, 1, numjobs);
if (income != null)
Jobs.getEconomy().pay(jPlayer, income * multiplier);
}
} else {
for (JobProgression prog : progression) {
int level = prog.getLevel();
Double income = prog.getJob().getIncome(info, level, numjobs);
if (income != null) {
Double exp = prog.getJob().getExperience(info, level, numjobs);
if (ConfigManager.getJobsConfiguration().addXpPlayer()) {
Player player = Bukkit.getServer().getPlayer(jPlayer.getPlayerUUID());
if (player != null) {
/*
* Minecraft experience is calculated in whole numbers only.
* Calculate the fraction of an experience point and perform a dice roll.
* That way jobs that give fractions of experience points will slowly give
* experience in the aggregate
*/
int expInt = exp.intValue();
double remainder = exp.doubleValue() - expInt;
if (Math.abs(remainder) > Math.random()) {
if (exp.doubleValue() < 0) {
expInt--;
} else {
expInt++;
}
}
player.giveExp(expInt);
}
}
// give income
Jobs.getEconomy().pay(jPlayer, income * multiplier);
int oldLevel = prog.getLevel();
if (prog.addExperience(exp * multiplier))
Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel);
}
}
}
}
use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.
the class PermissionHandler method recalculatePermissions.
public void recalculatePermissions(JobsPlayer jPlayer) {
Player player = plugin.getServer().getPlayer(jPlayer.getPlayerUUID());
if (player == null)
return;
boolean changed = false;
// remove old permissions
String permName = "jobs.players." + player.getName();
Permission permission = plugin.getServer().getPluginManager().getPermission(permName);
if (permission != null) {
plugin.getServer().getPluginManager().removePermission(permission);
changed = true;
}
// Permissions should only apply if we have permission to use jobs in this world
if (hasWorldPermission(player, player.getWorld().getName())) {
List<JobProgression> progression = jPlayer.getJobProgression();
// calculate new permissions
HashMap<String, Boolean> permissions = new HashMap<String, Boolean>();
if (progression.size() == 0) {
Job job = Jobs.getNoneJob();
if (job != null) {
for (JobPermission perm : job.getPermissions()) {
if (perm.getLevelRequirement() <= 0) {
if (perm.getValue()) {
permissions.put(perm.getNode(), true);
} else {
/*
* If the key exists, don't put a false node in
* This is in case we already have a true node there
*/
if (!permissions.containsKey(perm.getNode())) {
permissions.put(perm.getNode(), false);
}
}
}
}
}
} else {
for (JobProgression prog : progression) {
for (JobPermission perm : prog.getJob().getPermissions()) {
if (prog.getLevel() >= perm.getLevelRequirement()) {
/*
* If the key exists, don't put a false node in
* This is in case we already have a true node there
*/
if (perm.getValue()) {
permissions.put(perm.getNode(), true);
} else {
if (!permissions.containsKey(perm.getNode())) {
permissions.put(perm.getNode(), false);
}
}
}
}
}
}
// add new permissions (if applicable)
if (permissions.size() > 0) {
plugin.getServer().getPluginManager().addPermission(new Permission(permName, PermissionDefault.FALSE, permissions));
changed = true;
}
}
// If the permissions changed, recalculate them
if (!changed)
return;
// find old attachment
PermissionAttachment attachment = null;
for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
if (pai.getAttachment() != null && pai.getAttachment().getPlugin() instanceof JobsPlugin) {
attachment = pai.getAttachment();
}
}
// create if attachment doesn't exist
if (attachment == null) {
attachment = player.addAttachment(plugin);
attachment.setPermission(permName, true);
}
// recalculate!
player.recalculatePermissions();
}
use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.
the class PlayerManager method saveAll.
/**
* Save all the information of all of the players in the game
*/
public void saveAll() {
JobsDAO dao = Jobs.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();
}
}
}
}
}
use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.
the class PlayerManager method performLevelUp.
/**
* Broadcasts level up about a player
* @param jPlayer
* @param job
* @param oldLevel
*/
public void performLevelUp(JobsPlayer jPlayer, Job job, int oldLevel) {
Player player = Bukkit.getServer().getPlayer(jPlayer.getPlayerUUID());
JobProgression prog = jPlayer.getJobProgression(job);
if (prog == null)
return;
String message;
if (ConfigManager.getJobsConfiguration().isBroadcastingLevelups()) {
message = Language.getMessage("message.levelup.broadcast");
} else {
message = Language.getMessage("message.levelup.nobroadcast");
}
message = message.replace("%jobname%", job.getChatColor() + job.getName() + ChatColor.WHITE);
Title oldTitle = ConfigManager.getJobsConfiguration().getTitleForLevel(oldLevel);
if (oldTitle != null) {
message = message.replace("%titlename%", oldTitle.getChatColor() + oldTitle.getName() + ChatColor.WHITE);
}
if (player != null) {
message = message.replace("%playername%", player.getDisplayName());
} else {
message = message.replace("%playername%", jPlayer.getUserName());
}
message = message.replace("%joblevel%", "" + prog.getLevel());
for (String line : message.split("\n")) {
if (ConfigManager.getJobsConfiguration().isBroadcastingLevelups()) {
Bukkit.getServer().broadcastMessage(line);
} else if (player != null) {
player.sendMessage(line);
}
}
Title newTitle = ConfigManager.getJobsConfiguration().getTitleForLevel(prog.getLevel());
if (newTitle != null && !newTitle.equals(oldTitle)) {
// user would skill up
if (ConfigManager.getJobsConfiguration().isBroadcastingSkillups()) {
message = Language.getMessage("message.skillup.broadcast");
} else {
message = Language.getMessage("message.skillup.nobroadcast");
}
if (player != null) {
message = message.replace("%playername%", player.getDisplayName());
} else {
message = message.replace("%playername%", jPlayer.getUserName());
}
message = message.replace("%titlename%", newTitle.getChatColor() + newTitle.getName() + ChatColor.WHITE);
message = message.replace("%jobname%", job.getChatColor() + job.getName() + ChatColor.WHITE);
for (String line : message.split("\n")) {
if (ConfigManager.getJobsConfiguration().isBroadcastingLevelups()) {
Bukkit.getServer().broadcastMessage(line);
} else if (player != null) {
player.sendMessage(line);
}
}
}
jPlayer.reloadHonorific();
Jobs.getPermissionHandler().recalculatePermissions(jPlayer);
}
use of com.gamingmesh.jobs.container.JobsPlayer in project Jobs by GamingMesh.
the class PlayerManager method playerQuit.
/**
* Handles player quit
* @param playername
*/
public void playerQuit(Player player) {
synchronized (players) {
if (ConfigManager.getJobsConfiguration().saveOnDisconnect()) {
JobsPlayer jPlayer = players.remove(player.getName().toLowerCase());
if (jPlayer != null) {
jPlayer.save(Jobs.getJobsDAO());
jPlayer.onDisconnect();
}
} else {
JobsPlayer jPlayer = players.get(player.getName().toLowerCase());
if (jPlayer != null) {
jPlayer.onDisconnect();
}
}
}
}
Aggregations