mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-12-02 09:56:45 +01:00
Rolling xp diminishing returns
This adds on top of the diminishing returns system a mechanic such that gains will expire the configured number of minutes after they occured, rather than all being reset at once. This prevents someone from not getting diminishing returns on the xp gain they recieved just before the reset would have occured. Obligatory explanatory graphs: http://i.imgur.com/uSzicIR.png
This commit is contained in:
parent
092b7e39cd
commit
324a68949a
@ -6,6 +6,8 @@ import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -18,6 +20,7 @@ import com.gmail.nossr50.database.SQLDatabaseManager;
|
||||
import com.gmail.nossr50.datatypes.MobHealthbarType;
|
||||
import com.gmail.nossr50.datatypes.skills.AbilityType;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillXpGain;
|
||||
import com.gmail.nossr50.datatypes.spout.huds.HudType;
|
||||
import com.gmail.nossr50.datatypes.spout.huds.McMMOHud;
|
||||
import com.gmail.nossr50.skills.child.FamilyTree;
|
||||
@ -40,7 +43,8 @@ public class PlayerProfile {
|
||||
private final Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
|
||||
|
||||
// Store previous XP gains for deminished returns
|
||||
private Map<SkillType, Float> gainedSkillsXp = new HashMap<SkillType, Float>();
|
||||
private HashMap<SkillType, LinkedList<SkillXpGain>> gainedSkillsXp = new HashMap<SkillType, LinkedList<SkillXpGain>>();
|
||||
private HashMap<SkillType, Float> rollingSkillsXp = new HashMap<SkillType, Float>();
|
||||
|
||||
public PlayerProfile(String playerName, boolean addNew) {
|
||||
this.playerName = playerName;
|
||||
@ -258,27 +262,16 @@ public class PlayerProfile {
|
||||
public float getRegisteredXpGain(SkillType skillType) {
|
||||
float xp;
|
||||
|
||||
if (gainedSkillsXp.get(skillType) == null) {
|
||||
if (rollingSkillsXp.get(skillType) == null) {
|
||||
xp = 0F;
|
||||
}
|
||||
else {
|
||||
xp = gainedSkillsXp.get(skillType);
|
||||
xp = rollingSkillsXp.get(skillType);
|
||||
}
|
||||
|
||||
return xp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set registered experience gains
|
||||
* This is used for diminished XP returns
|
||||
*
|
||||
* @param skillType Skill being used
|
||||
* @param xp Experience amount to set
|
||||
*/
|
||||
public void setRegisteredXpGain(SkillType skillType, float xp) {
|
||||
gainedSkillsXp.put(skillType, xp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an experience gain
|
||||
* This is used for diminished XP returns
|
||||
@ -287,7 +280,42 @@ public class PlayerProfile {
|
||||
* @param xp Experience amount to add
|
||||
*/
|
||||
public void registeredXpGain(SkillType skillType, float xp) {
|
||||
gainedSkillsXp.put(skillType, getRegisteredXpGain(skillType) + xp);
|
||||
LinkedList<SkillXpGain> gains = gainedSkillsXp.get(skillType);
|
||||
|
||||
if(gains == null) {
|
||||
gains = new LinkedList<SkillXpGain>(); // Maybe add an initial capacity?
|
||||
}
|
||||
gains.addLast(new SkillXpGain(System.currentTimeMillis(), xp));
|
||||
|
||||
gainedSkillsXp.put(skillType, gains);
|
||||
rollingSkillsXp.put(skillType, getRegisteredXpGain(skillType) + xp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove experience gains older than a given time
|
||||
* This is used for diminished XP returns
|
||||
*
|
||||
* @param age Age in milliseconds that gains older than should be removed
|
||||
*/
|
||||
public void removeXpGainsOlderThan(long age) {
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
Iterator<LinkedList<SkillXpGain>> iterator = gainedSkillsXp.values().iterator();
|
||||
while(iterator.hasNext()) {
|
||||
LinkedList<SkillXpGain> skillGains = iterator.next();
|
||||
|
||||
// Because we are using a LinkedList and addLast ordering is guaranteed, so we loop through and remove things that are too old, and stop immediately once we find a young'n
|
||||
Iterator<SkillXpGain> gainsIterator = skillGains.iterator();
|
||||
while(gainsIterator.hasNext()) {
|
||||
SkillXpGain gain = gainsIterator.next();
|
||||
|
||||
if(now - gain.getTime() >= age) {
|
||||
gainsIterator.remove();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,19 @@
|
||||
package com.gmail.nossr50.datatypes.skills;
|
||||
|
||||
public class SkillXpGain {
|
||||
private final long time;
|
||||
private final float xp;
|
||||
|
||||
public SkillXpGain(long time, float xp) {
|
||||
this.time = time;
|
||||
this.xp = xp;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public float getXp() {
|
||||
return xp;
|
||||
}
|
||||
}
|
@ -64,4 +64,4 @@ public class SelfListener implements Listener {
|
||||
|
||||
mcMMOPlayer.getProfile().registeredXpGain(skillType, event.getRawXpGained());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -368,10 +368,8 @@ public class mcMMO extends JavaPlugin {
|
||||
}
|
||||
|
||||
// Clear the registered XP data so players can earn XP again
|
||||
long clearRegisteredXPGainInterval = Config.getInstance().getExperienceDeminishedReturnsTimeInterval() * 60 * 20;
|
||||
|
||||
if (kickIntervalTicks > 0 && Config.getInstance().getExperienceDeminishedReturnsThreshold() > 0) {
|
||||
new ClearRegisteredXPGainTask().runTaskTimer(this, clearRegisteredXPGainInterval, clearRegisteredXPGainInterval);
|
||||
if (Config.getInstance().getExperienceDeminishedReturnsThreshold() > 0) {
|
||||
new ClearRegisteredXPGainTask().runTaskTimer(this, 60 * 20, 60 * 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,20 +2,15 @@ package com.gmail.nossr50.runnables.player;
|
||||
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
|
||||
public class ClearRegisteredXPGainTask extends BukkitRunnable {
|
||||
@Override
|
||||
public void run() {
|
||||
for (McMMOPlayer mcMMOPlayer : UserManager.getPlayers().values()) {
|
||||
for (SkillType skillType : SkillType.values()) {
|
||||
if (skillType.isChildSkill()) {
|
||||
continue;
|
||||
}
|
||||
mcMMOPlayer.getProfile().setRegisteredXpGain(skillType, 0F);
|
||||
}
|
||||
mcMMOPlayer.getProfile().removeXpGainsOlderThan(Config.getInstance().getExperienceDeminishedReturnsTimeInterval() * 60 * 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user