package me.aurelien;
import java.util.HashMap;
import java.util.Map.Entry;
import org.bukkit.entity.Player;
/**
* Copyright (C) 2013-2014 Datter Alon <[email protected]>
*
* This file is part of the MyXpBar Libary
*
* See the GNU General Public License at <http://www.gnu.org/licenses/>.
*/
public class MyXpBar {
private HashMap<Integer, Integer> defaultXpLevels = new HashMap<Integer, Integer>();
private HashMap<Integer, Integer> customXpLevels = new HashMap<Integer, Integer>();
/**
* Constructor
* @param cl HashMap with levels and its required experience (f.e. 1,10 - Level 1 requires 10 experience)
*/
public MyXpBar(HashMap<Integer, Integer> cl) {
loadDefaultXpLevels();
customXpLevels = defaultXpLevels;
loadCustomXpLevels(cl);
}
/**
* Adds xp to a player
* @param p the Player
* @param amount the amount of xp
*/
public void addXp(Player p, int amount) {
int level = p.getLevel();
float oldXpBar = p.getExp();
float oldTotalXp = p.getTotalExperience();
if(customXpLevels.size() < (level+1)) {
System.out.println("[MyXpBar] Not enough custom levels set.");
return;
}
float needCustomXp = customXpLevels.get(level+1);
float newTotalXp = oldTotalXp+amount;
p.setTotalExperience((int) newTotalXp);
float newXpBar = oldXpBar + ((1/needCustomXp)*amount);
if(newXpBar >= 1) {
float overXpBar = newXpBar-1;
p.setLevel(level+1);
p.setExp(overXpBar);
} else {
p.setExp(newXpBar);
}
}
/**
* Removes xp from a player
* @param p the player
* @param amount the amount of xp
*/
public void removeXp(Player p, int amount) {
int level = p.getLevel();
float oldXpBar = p.getExp();
float oldTotalXp = p.getTotalExperience();
if(customXpLevels.size() < (level)) {
System.out.println("[MyXpBar] Not enough custom levels set.");
return;
}
float needCustomXp = customXpLevels.get(level);
float newTotalXp = oldTotalXp-amount;
p.setTotalExperience((int) newTotalXp);
float newXpBar = oldXpBar - ((1/needCustomXp)*amount);
if(newXpBar <= 0) {
float underXpBar = 1F-newXpBar;
p.setLevel(level-1);
p.setExp(underXpBar);
} else {
p.setExp(newXpBar);
}
}
private void loadCustomXpLevels(HashMap<Integer, Integer> cl) {
for(Entry<Integer, Integer> e : cl.entrySet()) {
customXpLevels.put(e.getKey(), e.getValue());
}
}
private void loadDefaultXpLevels() {
defaultXpLevels.put(1, 200);
defaultXpLevels.put(2, 200);
defaultXpLevels.put(3, 200);
defaultXpLevels.put(4, 200);
defaultXpLevels.put(5, 200);
defaultXpLevels.put(6, 500);
defaultXpLevels.put(7, 500);
defaultXpLevels.put(8, 500);
defaultXpLevels.put(9, 500);
defaultXpLevels.put(10, 500);
defaultXpLevels.put(11, 1000);
defaultXpLevels.put(12, 1000);
defaultXpLevels.put(13, 1000);
defaultXpLevels.put(14, 1000);
defaultXpLevels.put(15, 1000);
defaultXpLevels.put(16, 2000);
defaultXpLevels.put(17, 2000);
defaultXpLevels.put(18, 2000);
defaultXpLevels.put(19, 2000);
defaultXpLevels.put(20, 2000);
defaultXpLevels.put(21, 3000);
defaultXpLevels.put(22, 3000);
defaultXpLevels.put(23, 3000);
defaultXpLevels.put(24, 3000);
defaultXpLevels.put(25, 3000);
defaultXpLevels.put(26, 4000);
defaultXpLevels.put(27, 4000);
defaultXpLevels.put(28, 4000);
defaultXpLevels.put(29, 4000);
defaultXpLevels.put(30, 4000);
defaultXpLevels.put(31, 5000);
defaultXpLevels.put(32, 5000);
defaultXpLevels.put(33, 5000);
defaultXpLevels.put(34, 5000);
defaultXpLevels.put(35, 5000);
defaultXpLevels.put(36, 6000);
defaultXpLevels.put(37, 6000);
defaultXpLevels.put(38, 6000);
defaultXpLevels.put(39, 6000);
defaultXpLevels.put(40, 6000);
defaultXpLevels.put(41, 7000);
defaultXpLevels.put(42, 7000);
defaultXpLevels.put(43, 7000);
defaultXpLevels.put(44, 7000);
defaultXpLevels.put(45, 7000);
defaultXpLevels.put(46, 8000);
defaultXpLevels.put(47, 8000);
defaultXpLevels.put(48, 8000);
defaultXpLevels.put(49, 8000);
defaultXpLevels.put(50, 8000);
}
}