Tweaked some stuff with adding XP, added an ExperienceAPI class.

This commit is contained in:
GJ 2012-03-29 14:24:41 -04:00
parent 26ed6aa840
commit 6c1ee24101
18 changed files with 184 additions and 98 deletions

View File

@ -0,0 +1,118 @@
package com.gmail.nossr50.api;
import org.bukkit.entity.Player;
import com.gmail.nossr50.Users;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.skills.Skills;
public class ExperienceAPI {
/**
* Check the XP of a player. This should be called after giving XP to process level-ups.
*
* @param player The player to check
* @param skillType The skill to check
*/
private static void checkXP(Player player, SkillType skillType) {
if (skillType.equals(SkillType.ALL)) {
Skills.XpCheckAll(player);
}
else {
Skills.XpCheckSkill(skillType, player);
}
}
/**
* Adds XP to the player, doesn't calculate for XP Rate or other modifiers.
* </br>
* This function is designed for API usage.
*
* @param player The player to add XP to
* @param skillType The skill to add XP to
* @param XP The amount of XP to add
*/
public static void addRawXP(Player player, SkillType skillType, int XP) {
Users.getProfile(player).addXPOverride(skillType, XP);
checkXP(player, skillType);
}
/**
* Adds XP to the player, calculates for XP Rate but not skill modifiers.
* </br>
* This function is designed for API usage.
*
* @param player The player to add XP to
* @param skillType The skill to add XP to
* @param XP The amount of XP to add
*/
public static void addMultipliedXP(Player player, SkillType skillType, int XP) {
Users.getProfile(player).addXPOverrideBonus(skillType, XP);
checkXP(player, skillType);
}
/**
* Adds XP to the player, calculates for XP Rate and skill modifiers.
* </br>
* This function is designed for API usage.
*
* @param player The player to add XP to
* @param skillType The skill to add XP to
* @param XP The amount of XP to add
*/
public static void addXP(Player player, SkillType skillType, int XP) {
Users.getProfile(player).addXP(skillType, XP);
checkXP(player, skillType);
}
/**
* Get the amount of XP a player has in a specific skill.
* </br>
* This function is designed for API usage.
*
* @param player The player to get XP for
* @param skillType The skill to get XP for
* @return the amount of XP in a given skill
*/
public static int getXP(Player player, SkillType skillType) {
return Users.getProfile(player).getSkillXpLevel(skillType);
}
/**
* Get the amount of XP left before leveling up.
* </br>
* This function is designed for API usage.
*
* @param player The player to get the XP amount for
* @param skillType The skill to get the XP amount for
* @return the amount of XP left before leveling up a specifc skill
*/
public static int getXPToNextLevel(Player player, SkillType skillType) {
return Users.getProfile(player).getXpToLevel(skillType);
}
/**
* Add levels to a skill.
* </br>
* This function is designed for API usage.
*
* @param skillType Type of skill to add levels to
* @param levels Number of levels to add
*/
public static void addLevel(Player player, SkillType skillType, int levels) {
Users.getProfile(player).addLevels(skillType, levels);
}
/**
* Get the level a player has in a specific skill.
* </br>
* This function is designed for API usage.
*
* @param player The player to get the level for
* @param skillType The skill to get the level for
* @return the level of a given skill
*/
public static int getLevel(Player player, SkillType skillType) {
return Users.getProfile(player).getSkillLevel(skillType);
}
}

View File

@ -8,7 +8,6 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.gmail.nossr50.Users;
import com.gmail.nossr50.datatypes.PlayerProfile;
public class PartyAPI {
@ -21,8 +20,7 @@ public class PartyAPI {
* @return the name of the player's party
*/
public static String getPartyName(Player player) {
PlayerProfile PP = Users.getProfile(player);
return PP.getParty();
return Users.getProfile(player).getParty();
}
/**
@ -34,8 +32,7 @@ public class PartyAPI {
* @return true if the player is in a party, false otherwise
*/
public static boolean inParty(Player player) {
PlayerProfile PP = Users.getProfile(player);
return PP.inParty();
return Users.getProfile(player).inParty();
}
/**
@ -48,13 +45,8 @@ public class PartyAPI {
* @return true if the two players are in the same party, false otherwise
*/
public static boolean inSameParty(Player playera, Player playerb) {
if (Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty()) {
if (Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty())) {
return true;
}
else {
return false;
}
if (inParty(playera) && inParty(playerb) && getPartyName(playera).equals(getPartyName(playerb))) {
return true;
}
else {
return false;

View File

@ -41,7 +41,7 @@ public class AddxpCommand implements CommandExecutor {
} else if (args.length == 3) {
if ((plugin.getServer().getPlayer(args[0]) != null) && m.isInt(args[2]) && Skills.isSkill(args[1])) {
int newvalue = Integer.valueOf(args[2]);
Users.getProfile(plugin.getServer().getPlayer(args[0])).addXPOverrideNoBonus(Skills.getSkillType(args[1]), newvalue);
Users.getProfile(plugin.getServer().getPlayer(args[0])).addXPOverride(Skills.getSkillType(args[1]), newvalue);
plugin.getServer().getPlayer(args[0]).sendMessage(ChatColor.GREEN + "Experience granted!"); //TODO: Needs more locale.
System.out.println(args[1] + " has been modified for " + plugin.getServer().getPlayer(args[0]).getName() + ".");
Skills.XpCheckAll(plugin.getServer().getPlayer(args[0]));
@ -65,14 +65,14 @@ public class AddxpCommand implements CommandExecutor {
if (args.length == 3) {
if ((plugin.getServer().getPlayer(args[0]) != null) && m.isInt(args[2]) && Skills.isSkill(args[1])) {
int newvalue = Integer.valueOf(args[2]);
Users.getProfile(plugin.getServer().getPlayer(args[0])).addXP(Skills.getSkillType(args[1]), newvalue, plugin.getServer().getPlayer(args[0]));
Users.getProfile(plugin.getServer().getPlayer(args[0])).addXP(Skills.getSkillType(args[1]), newvalue);
plugin.getServer().getPlayer(args[0]).sendMessage(ChatColor.GREEN + "Experience granted!"); //TODO: Needs more locale.
player.sendMessage(ChatColor.RED + args[1] + " has been modified."); //TODO: Needs more locale.
Skills.XpCheckAll(plugin.getServer().getPlayer(args[0]));
}
} else if (args.length == 2 && m.isInt(args[1]) && Skills.isSkill(args[0])) {
int newvalue = Integer.valueOf(args[1]);
Users.getProfile(player).addXP(Skills.getSkillType(args[0]), newvalue, player);
Users.getProfile(player).addXP(Skills.getSkillType(args[0]), newvalue);
player.sendMessage(ChatColor.GREEN + "Experience granted!"); //TODO: Needs more locale.
player.sendMessage(ChatColor.RED + args[0] + " has been modified."); //TODO: Needs more locale.
Skills.XpCheckAll(plugin.getServer().getPlayer(args[0]));

View File

@ -47,7 +47,7 @@ public class InspectCommand implements CommandExecutor {
PlayerProfile PPt = Users.getProfile(target);
//If they are not an Op they have to be close
if(sender instanceof Player && !player.isOp() && !m.isNear(player.getLocation(), target.getLocation(), 5))
if(sender instanceof Player && !player.isOp() && !m.isNear(player.getLocation(), target.getLocation(), 5.0))
{
sender.sendMessage("You are too far away to inspect that player!"); //TODO: Needs more locale.
}

View File

@ -125,7 +125,7 @@ public class PlayerProfile {
}
public boolean loadMySQL() {
Integer id = 0;
int id = 0;
id = mcMMO.database.getInt("SELECT id FROM "+LoadProperties.MySQLtablePrefix+"users WHERE user = '" + playerName + "'");
if(id == 0)
return false;
@ -207,7 +207,7 @@ public class PlayerProfile {
}
public void addMySQLPlayer() {
Integer id = 0;
int id = 0;
mcMMO.database.write("INSERT INTO "+LoadProperties.MySQLtablePrefix+"users (user, lastlogin) VALUES ('" + playerName + "'," + System.currentTimeMillis() / 1000 +")");
id = mcMMO.database.getInt("SELECT id FROM "+LoadProperties.MySQLtablePrefix+"users WHERE user = '" + playerName + "'");
mcMMO.database.write("INSERT INTO "+LoadProperties.MySQLtablePrefix+"cooldowns (user_id) VALUES ("+id+")");
@ -989,11 +989,11 @@ public class PlayerProfile {
* XP Functions
*/
public Integer getSkillLevel(SkillType skillType) {
public int getSkillLevel(SkillType skillType) {
return skills.get(skillType);
}
public Integer getSkillXpLevel(SkillType skillType) {
public int getSkillXpLevel(SkillType skillType) {
return skillsXp.get(skillType);
}
@ -1003,7 +1003,7 @@ public class PlayerProfile {
* @param skillType The skill to add XP to
* @param newValue The amount of XP to add
*/
public void addXPOverrideNoBonus(SkillType skillType, int newValue) {
public void addXPOverride(SkillType skillType, int newValue) {
Player player = Bukkit.getPlayer(playerName);
if (skillType.equals(SkillType.ALL)) {
@ -1029,26 +1029,9 @@ public class PlayerProfile {
* @param skillType The skill to add XP to
* @param newValue The amount of XP to add
*/
public void addXPOverride(SkillType skillType, int newValue) {
Player player = Bukkit.getPlayer(playerName);
if (skillType.equals(SkillType.ALL)) {
for (SkillType x : SkillType.values()) {
if (x.equals(SkillType.ALL)) {
continue;
}
Bukkit.getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, x, newValue));
skillsXp.put(x, skillsXp.get(x) + newValue);
}
}
else {
int xp = newValue * LoadProperties.xpGainMultiplier;
Bukkit.getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, skillType, xp));
skillsXp.put(skillType, skillsXp.get(skillType) + xp);
lastgained = skillType;
}
public void addXPOverrideBonus(SkillType skillType, int newValue) {
int xp = newValue * LoadProperties.xpGainMultiplier;
addXPOverride(skillType, xp);
}
/**
@ -1058,7 +1041,9 @@ public class PlayerProfile {
* @param newvalue The amount of XP to add
* @param player The player to add XP to
*/
public void addXP(SkillType skillType, int newValue, Player player) {
public void addXP(SkillType skillType, int newValue) {
Player player = Bukkit.getPlayer(playerName);
if (System.currentTimeMillis() < ((xpGainATS * 1000) + 250) || player.getGameMode().equals(GameMode.CREATIVE)) {
return;
}
@ -1067,20 +1052,7 @@ public class PlayerProfile {
double bonusModifier = 0;
if (inParty()) {
for (Player x : Party.getInstance().getPartyMembers(player)) {
if (x.isOnline() && !x.getName().equals(player.getName()) && Party.getInstance().isPartyLeader(x.getName(), this.getParty())) {
if (m.isNear(player.getLocation(), x.getLocation(), 25)) {
PlayerProfile PartyLeader = Users.getProfile(x);
if (PartyLeader.getSkillLevel(skillType) >= this.getSkillLevel(skillType)) {
int leaderLevel = PartyLeader.getSkillLevel(skillType);
int difference = leaderLevel - this.getSkillLevel(skillType);
bonusModifier = (difference * 0.75D) / 100D;
}
}
}
}
bonusModifier = partyModifier(skillType);
}
int xp = (int) (newValue / skillType.getXpModifier()) * LoadProperties.xpGainMultiplier;
@ -1155,10 +1127,38 @@ public class PlayerProfile {
* @param skillType Type of skill to check
* @return the XP remaining until next level
*/
public Integer getXpToLevel(SkillType skillType) {
public int getXpToLevel(SkillType skillType) {
return (int) (1020 + (skills.get(skillType) * 20)); //Do we REALLY need to cast to int here?
}
/**
* Calculate the party XP modifier.
*
* @param skillType Type of skill to check
* @return the party bonus multiplier
*/
private double partyModifier(SkillType skillType) {
Player player = Bukkit.getPlayer(playerName);
double bonusModifier = 0.0;
for (Player x : Party.getInstance().getPartyMembers(player)) {
if (x.isOnline() && !x.getName().equals(player.getName()) && Party.getInstance().isPartyLeader(x.getName(), this.getParty())) {
if (m.isNear(player.getLocation(), x.getLocation(), 25.0)) {
PlayerProfile PartyLeader = Users.getProfile(x);
if (PartyLeader.getSkillLevel(skillType) >= this.getSkillLevel(skillType)) {
int leaderLevel = PartyLeader.getSkillLevel(skillType);
int difference = leaderLevel - this.getSkillLevel(skillType);
bonusModifier = (difference * 0.75) / 100.0;
}
}
}
}
return bonusModifier;
}
/*
* Party Stuff
*/

View File

@ -307,7 +307,7 @@ public class mcEntityListener implements Listener {
break;
}
PP.addXP(SkillType.TAMING, xp, player);
PP.addXP(SkillType.TAMING, xp);
Skills.XpCheckSkill(SkillType.TAMING, player);
}
}

View File

@ -177,13 +177,8 @@ public class m {
* @param maxDistance The max distance apart
* @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
*/
public static boolean isNear(Location first, Location second, int maxDistance) {
double relX = first.getX() - second.getX();
double relY = first.getY() - second.getY();
double relZ = first.getZ() - second.getZ();
double dist = (relX * relX) + (relY * relY) + (relZ * relZ);
if (dist < maxDistance * maxDistance) {
public static boolean isNear(Location first, Location second, double maxDistance) {
if (first.distance(second) < maxDistance) {
return true;
}
else {

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.commands.skills.*;
import com.gmail.nossr50.commands.spout.*;
import com.gmail.nossr50.commands.mc.*;
@ -9,7 +8,6 @@ import com.gmail.nossr50.commands.party.*;
import com.gmail.nossr50.commands.general.*;
import com.gmail.nossr50.config.*;
import com.gmail.nossr50.runnables.*;
import com.gmail.nossr50.skills.Skills;
import com.gmail.nossr50.spout.SpoutStuff;
import com.gmail.nossr50.listeners.mcBlockListener;
import com.gmail.nossr50.listeners.mcEntityListener;
@ -187,23 +185,6 @@ public class mcMMO extends JavaPlugin {
return Users.getProfile(player);
}
/**
* Check the XP of a player.
* </br>
* This function is designed for API usage.
*
* @param player
* @param skillType
*/
public static void checkXp(Player player, SkillType skillType) {
if (skillType == SkillType.ALL) {
Skills.XpCheckAll(player);
}
else {
Skills.XpCheckSkill(skillType, player);
}
}
/**
* Things to be run when the plugin is disabled.
*/

View File

@ -39,7 +39,7 @@ public class GainXp implements Runnable {
damage += health;
}
PP.addXP(skillType, (int) (damage * baseXp), player);
PP.addXP(skillType, (int) (damage * baseXp));
Skills.XpCheckSkill(skillType, player);
}
}

View File

@ -58,7 +58,7 @@ public class Acrobatics {
/* Check for death */
if (health - damage >= 1) {
PP.addXP(SkillType.ACROBATICS, damage * ROLL_XP_MODIFIER, player);
PP.addXP(SkillType.ACROBATICS, damage * ROLL_XP_MODIFIER);
Skills.XpCheckSkill(SkillType.ACROBATICS, player);
event.setDamage(newDamage);
@ -76,7 +76,7 @@ public class Acrobatics {
}
}
else if (health - damage >= 1) {
PP.addXP(SkillType.ACROBATICS, event.getDamage() * FALL_XP_MODIFIER, player);
PP.addXP(SkillType.ACROBATICS, event.getDamage() * FALL_XP_MODIFIER);
Skills.XpCheckSkill(SkillType.ACROBATICS, player);
}
}
@ -111,7 +111,7 @@ public class Acrobatics {
defender.sendMessage(mcLocale.getString("Acrobatics.Dodge"));
if (System.currentTimeMillis() >= (5000 + PPd.getRespawnATS()) && defender.getHealth() >= 1) {
PPd.addXP(SkillType.ACROBATICS, damage * DODGE_MODIFIER, defender);
PPd.addXP(SkillType.ACROBATICS, damage * DODGE_MODIFIER);
Skills.XpCheckSkill(SkillType.ACROBATICS, defender);
}

View File

@ -257,7 +257,7 @@ public class BlastMining {
Block block = player.getTargetBlock(transparent, BLOCKS_AWAY);
if (block.getType().equals(Material.TNT) && m.blockBreakSimulate(block, player, true) && PP.getSkillLevel(SkillType.MINING) >= 125) {
final int MAX_DISTANCE_AWAY = 10;
final double MAX_DISTANCE_AWAY = 10.0;
AbilityType ability = AbilityType.BLAST_MINING;
/* Check Cooldown */

View File

@ -120,7 +120,7 @@ public class Excavation {
}
//Handle XP related tasks
PP.addXP(SkillType.EXCAVATION, xp, player);
PP.addXP(SkillType.EXCAVATION, xp);
Skills.XpCheckSkill(SkillType.EXCAVATION, player);
}

View File

@ -101,7 +101,7 @@ public class Fishing {
FishingTreasure treasure = rewards.get(random.nextInt(rewards.size()));
if (random.nextInt(100) <= treasure.getDropChance()) {
Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp(), player);
Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp());
theCatch.setItemStack(treasure.getDrop());
}
}
@ -116,7 +116,7 @@ public class Fishing {
}
m.mcDropItem(player.getLocation(), new ItemStack(Material.RAW_FISH)); //Always drop a fish
PP.addXP(SkillType.FISHING, LoadProperties.mfishing, player);
PP.addXP(SkillType.FISHING, LoadProperties.mfishing);
Skills.XpCheckSkill(SkillType.FISHING, player);
}

View File

@ -251,7 +251,7 @@ public class Herbalism {
}
}
PP.addXP(SkillType.HERBALISM, xp, player);
PP.addXP(SkillType.HERBALISM, xp);
Skills.XpCheckSkill(SkillType.HERBALISM, player);
}
}

View File

@ -145,7 +145,7 @@ public class Mining {
break;
}
PP.addXP(SkillType.MINING, xp, player);
PP.addXP(SkillType.MINING, xp);
Skills.XpCheckSkill(SkillType.MINING, player);
}

View File

@ -138,7 +138,7 @@ public class Repair {
dif = (short) (dif / 2);
}
PP.addXP(SkillType.REPAIR, dif * 10, player);
PP.addXP(SkillType.REPAIR, dif * 10);
Skills.XpCheckSkill(SkillType.REPAIR, player);
//CLANG CLANG

View File

@ -26,7 +26,7 @@ import com.gmail.nossr50.locale.mcLocale;
public class Skills {
private final static int TIME_CONVERSION_FACTOR = 1000;
private final static int MAX_DISTANCE_AWAY = 10;
private final static double MAX_DISTANCE_AWAY = 10.0;
/**
* Checks to see if the cooldown for an item or ability is expired.

View File

@ -156,7 +156,7 @@ public class WoodCutting {
}
}
PP.addXP(SkillType.WOODCUTTING, xp, player); //Tree Feller gives nerf'd XP
PP.addXP(SkillType.WOODCUTTING, xp); //Tree Feller gives nerf'd XP
Skills.XpCheckSkill(SkillType.WOODCUTTING, player);
}
@ -297,7 +297,7 @@ public class WoodCutting {
}
WoodCutting.woodCuttingProcCheck(player, block);
PP.addXP(SkillType.WOODCUTTING, xp, player);
PP.addXP(SkillType.WOODCUTTING, xp);
Skills.XpCheckSkill(SkillType.WOODCUTTING, player);
}