1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-01 23:13:48 +01:00
Jobs/com/gamingmesh/jobs/container/JobProgression.java

165 lines
4.0 KiB
Java
Raw Normal View History

2015-08-21 15:10:08 +02:00
/**
* Jobs Plugin for Bukkit
* Copyright (C) 2011 Zak Ford <zak.j.ford@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gamingmesh.jobs.container;
import java.util.HashMap;
public class JobProgression {
2016-03-30 15:42:36 +02:00
private Job job;
private JobsPlayer jPlayer;
private double experience;
private int level;
private transient int maxExperience = -1;
public JobProgression(Job job, JobsPlayer jPlayer, int level, double experience) {
2016-03-30 15:42:36 +02:00
this.job = job;
this.jPlayer = jPlayer;
this.experience = experience;
this.level = level;
}
/**
* Can the job level up?
* @return true if the job can level up
* @return false if the job cannot
*/
public boolean canLevelUp() {
return experience >= maxExperience;
}
2016-03-30 15:42:36 +02:00
/**
* Return the job
* @return the job
*/
public Job getJob() {
return job;
}
/**
* Set the job
* @param job - the new job to be set
*/
public void setJob(Job job) {
2015-11-25 16:14:48 +01:00
// synchronized (jPlayer.saveLock) {
2016-03-30 15:42:36 +02:00
jPlayer.setSaved(false);
this.job = job;
reloadMaxExperienceAndCheckLevelUp();
2015-11-25 16:14:48 +01:00
// }
2016-03-30 15:42:36 +02:00
}
/**
* Get the experience in this job
* @return the experiece in this job
*/
public double getExperience() {
return experience;
}
/**
* Adds experience for this job
* @param experience - the experience in this job
* @return - job level up
*/
public boolean addExperience(double experience) {
2015-11-25 16:14:48 +01:00
// synchronized (jPlayer.saveLock) {
2016-03-30 15:42:36 +02:00
jPlayer.setSaved(false);
this.experience += experience;
return checkLevelUp();
2015-11-25 16:14:48 +01:00
// }
2016-03-30 15:42:36 +02:00
}
/**
* Get the maximum experience for this level
* @return the experience needed to level up
*/
public int getMaxExperience() {
return maxExperience;
}
/**
* Get the current level of this job
* @return the level of this job
*/
public int getLevel() {
return level;
}
/**
* Set the level of this job
* @param level - the new level for this job
*/
public void setLevel(int level) {
2015-11-25 16:14:48 +01:00
// synchronized (jPlayer.saveLock) {
2016-03-30 15:42:36 +02:00
jPlayer.setSaved(false);
this.level = level;
reloadMaxExperienceAndCheckLevelUp();
2015-11-25 16:14:48 +01:00
// }
2016-03-30 15:42:36 +02:00
}
/**
* Reloads max experience
*/
public void reloadMaxExperience() {
HashMap<String, Double> param = new HashMap<String, Double>();
param.put("joblevel", (double) level);
param.put("numjobs", (double) jPlayer.getJobProgression().size());
this.maxExperience = (int) job.getMaxExp(param);
}
/**
* Performs a level up
* @returns if level up was performed
*/
private boolean checkLevelUp() {
boolean ret = false;
while (canLevelUp()) {
int maxLevel = 0;
if (jPlayer.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0)
maxLevel = job.getVipMaxLevel();
else
maxLevel = job.getMaxLevel();
// Don't level up at max level
if (job.getMaxLevel() > 0 && level >= maxLevel)
break;
level++;
experience -= maxExperience;
ret = true;
reloadMaxExperience();
2015-08-21 15:10:08 +02:00
}
2016-03-30 15:42:36 +02:00
// At max level
if (experience > maxExperience)
experience = maxExperience;
return ret;
}
/**
* Reloads max experience and checks for level up
* Do this whenever job or level changes
* @return if leveled up
*/
private boolean reloadMaxExperienceAndCheckLevelUp() {
reloadMaxExperience();
return checkLevelUp();
}
2015-08-21 15:10:08 +02:00
}