Made xp sharing less ugly by reworking McMMOPlayer xp methods

Notable consequence: checkXP and addLevel in ExperienceAPI are no longer
needed and became deprecated
This commit is contained in:
bm01 2013-02-03 12:45:19 +01:00
parent 6d871c9bdb
commit a1ab6f286b
17 changed files with 110 additions and 113 deletions

View File

@ -2,6 +2,7 @@ package com.gmail.nossr50.api;
import org.bukkit.entity.Player;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.Users;
@ -14,6 +15,7 @@ public final class ExperienceAPI {
*
* @param player The player to check
* @param skillType The skill to check
* @deprecated Calling this function is no longer needed and should be avoided
*/
private static void checkXP(Player player, SkillType skillType) {
if (skillType.equals(SkillType.ALL)) {
@ -25,7 +27,7 @@ public final class ExperienceAPI {
}
/**
* Adds XP to the player, doesn't calculate for XP Rate or other modifiers.
* Adds raw XP to the player.
* </br>
* This function is designed for API usage.
*
@ -34,12 +36,11 @@ public final class ExperienceAPI {
* @param XP The amount of XP to add
*/
public static void addRawXP(Player player, SkillType skillType, int XP) {
Users.getPlayer(player).addXpOverride(skillType, XP);
checkXP(player, skillType);
Users.getPlayer(player).applyXpGain(skillType, XP);
}
/**
* Adds XP to the player, calculates for XP Rate but not skill modifiers.
* Adds XP to the player, calculates for XP Rate only.
* </br>
* This function is designed for API usage.
*
@ -48,12 +49,11 @@ public final class ExperienceAPI {
* @param XP The amount of XP to add
*/
public static void addMultipliedXP(Player player, SkillType skillType, int XP) {
Users.getPlayer(player).addXpOverrideBonus(skillType, XP);
checkXP(player, skillType);
Users.getPlayer(player).applyXpGain(skillType, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
}
/**
* Adds XP to the player, calculates for XP Rate and skill modifiers.
* Adds XP to the player, calculates for XP Rate, skill modifiers and perks. May be shared with the party.
* </br>
* This function is designed for API usage.
*
@ -62,8 +62,7 @@ public final class ExperienceAPI {
* @param XP The amount of XP to add
*/
public static void addXP(Player player, SkillType skillType, int XP) {
Users.getPlayer(player).addXp(skillType, XP);
checkXP(player, skillType);
Users.getPlayer(player).beginXpGain(skillType, XP);
}
/**
@ -100,7 +99,8 @@ public final class ExperienceAPI {
* @param player The player to add levels to
* @param skillType Type of skill to add levels to
* @param levels Number of levels to add
* @param notify True if this should fire a level up notification, false otherwise.
* @param notify Unused argument
* @deprecated Use addLevel(Player, SKillType, int) instead
*/
public static void addLevel(Player player, SkillType skillType, int levels, boolean notify) {
Users.getProfile(player).addLevels(skillType, levels);
@ -110,6 +110,20 @@ public final class ExperienceAPI {
}
}
/**
* Add levels to a skill.
* </br>
* This function is designed for API usage.
*
* @param player The player to add levels to
* @param skillType Type of skill to add levels to
* @param levels Number of levels to add
* @param notify True if this should fire a level up notification, false otherwise.
*/
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>

View File

@ -44,14 +44,17 @@ public class AddxpCommand implements CommandExecutor {
mcMMOPlayer = Users.getPlayer(modifiedPlayer);
profile = mcMMOPlayer.getProfile();
mcMMOPlayer.addXpOverride(skill, xp);
if (skill.equals(SkillType.ALL)) {
SkillTools.xpCheckAll(modifiedPlayer, profile);
for (SkillType skillType : SkillType.values()) {
if (!skillType.isChildSkill()) {
mcMMOPlayer.applyXpGain(skill, xp);
}
}
sender.sendMessage(LocaleLoader.getString("Commands.addxp.AwardAll", xp));
}
else {
SkillTools.xpCheckSkill(skill, modifiedPlayer, profile);
mcMMOPlayer.applyXpGain(skill, xp);
sender.sendMessage(LocaleLoader.getString("Commands.addxp.AwardSkill", xp, Misc.getCapitalized(skill.toString())));
}
@ -85,7 +88,7 @@ public class AddxpCommand implements CommandExecutor {
return true;
}
// This is basically a copy of McMMOPlayer.addXpOverride(), this method should probably be moved to PlayerProfile to avoid that
// TODO: Currently the offline player doesn't level up automatically
if (skill.equals(SkillType.ALL)) {
for (SkillType type : SkillType.values()) {
if (type.equals(SkillType.ALL) || type.isChildSkill()) {
@ -94,19 +97,15 @@ public class AddxpCommand implements CommandExecutor {
profile.setSkillXpLevel(type, profile.getSkillXpLevel(type) + xp);
}
// TODO: Find a way to make it work, it currently requires a valid Player
// SkillTools.xpCheckAll(modifiedPlayer, profile);
}
else {
profile.setSkillXpLevel(skill, profile.getSkillXpLevel(skill) + xp);
// TODO: Find a way to make it work, it currently requires a valid Player
// SkillTools.xpCheckSkill(skill, modifiedPlayer, profile);
}
profile.save(); // Since this is a temporary profile, we save it here.
}
else {
mcMMOPlayer.addXpOverride(skill, xp);
mcMMOPlayer.applyXpGain(skill, xp);
modifiedPlayer = mcMMOPlayer.getPlayer();
profile = mcMMOPlayer.getProfile();
@ -114,11 +113,9 @@ public class AddxpCommand implements CommandExecutor {
if (modifiedPlayer.isOnline()) {
if (skill.equals(SkillType.ALL)) {
modifiedPlayer.sendMessage(LocaleLoader.getString("Commands.addxp.AwardAll", xp));
SkillTools.xpCheckAll(modifiedPlayer, profile);
}
else {
modifiedPlayer.sendMessage(LocaleLoader.getString("Commands.addxp.AwardSkill", xp, Misc.getCapitalized(skill.toString())));
SkillTools.xpCheckSkill(skill, modifiedPlayer, profile);
}
}
}

View File

@ -53,23 +53,43 @@ public class McMMOPlayer {
}
/**
* Adds Xp to the player, doesn't calculate for Xp Rate
* Begins an experience gain. The amount will be affected by skill modifiers, global rate, perks, and may be shared with the party
*
* @param skillType The skill to add Xp to
* @param xp The amount of Xp to add
* @param skillType Skill being used
* @param xp Experience amount to process
*/
public void addXpOverride(SkillType skillType, int xp) {
if (skillType.equals(SkillType.ALL)) {
for (SkillType type : SkillType.values()) {
if (type.equals(SkillType.ALL) || type.isChildSkill()) {
continue;
public void beginXpGain(SkillType skillType, int xp) {
// Return if the experience has been shared
if (party != null && ShareHandler.handleXpShare(xp, this, skillType)) {
return;
}
mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, type, xp));
profile.setSkillXpLevel(type, profile.getSkillXpLevel(type) + xp);
beginUnsharedXpGain(skillType, xp);
}
/**
* Begins an experience gain. The amount will be affected by skill modifiers, global rate and perks
*
* @param skillType Skill being used
* @param xp Experience amount to process
*/
public void beginUnsharedXpGain(SkillType skillType, int xp) {
xp = modifyXpGain(skillType, xp);
if (xp == 0) {
return;
}
else {
applyXpGain(skillType, xp);
}
/**
* Applies an experience gain
*
* @param skillType Skill being used
* @param xp Experience amount to add
*/
public void applyXpGain(SkillType skillType, int xp) {
mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, skillType, xp));
profile.setSkillXpLevel(skillType, profile.getSkillXpLevel(skillType) + xp);
@ -78,40 +98,24 @@ public class McMMOPlayer {
if (spoutHud != null) {
spoutHud.setLastGained(skillType);
}
}
SkillTools.xpCheckSkill(skillType, player, profile);
}
/**
* Adds Xp to the player, this ignores skill modifiers.
* Modifies an experience gain using skill modifiers, global rate and perks
*
* @param skillType The skill to add Xp to
* @param xp The amount of Xp to add
* @param skillType Skill being used
* @param xp Experience amount to process
* @return Modified experience
*/
public void addXpOverrideBonus(SkillType skillType, int xp) {
int modifiedXp = (int) Math.floor(xp * Config.getInstance().getExperienceGainsGlobalMultiplier());
addXpOverride(skillType, modifiedXp);
}
/**
* Adds experience to the player, this is affected by skill modifiers, rate and permissions
*
* @param skillType The skill to add Xp to
* @param xp The amount of Xp to add
*/
public void addXp(SkillType skillType, int xp) {
private int modifyXpGain(SkillType skillType, int xp) {
if (player.getGameMode() == GameMode.CREATIVE) {
return;
}
if (party != null && !ShareHandler.isRunning()) {
// Return if the Xp has been shared
if (ShareHandler.handleEqualXpShare(xp, this, skillType)) {
return;
}
return 0;
}
if ((skillType.getMaxLevel() < profile.getSkillLevel(skillType) + 1) || (Misc.getPowerLevelCap() < getPowerLevel() + 1)) {
return;
return 0;
}
xp = (int) (xp / skillType.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier());
@ -121,11 +125,11 @@ public class McMMOPlayer {
CustomTool tool = ModChecks.getToolFromItemStack(item);
if (tool != null) {
xp = (int) (xp * tool.getXpMultiplier());
xp *= tool.getXpMultiplier();
}
}
// TODO: find a better way to do this, if possible
// TODO: Too many permission checks here, is there no way to avoid that?
if (Permissions.xpQuadruple(player)) {
xp *= 4;
}
@ -142,16 +146,7 @@ public class McMMOPlayer {
xp *= 1.5;
}
mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, skillType, xp));
profile.setSkillXpLevel(skillType, profile.getSkillXpLevel(skillType) + xp);
SpoutHud spoutHud = profile.getSpoutHud();
if (spoutHud != null) {
spoutHud.setLastGained(skillType);
}
SkillTools.xpCheckSkill(skillType, player, profile);
return xp;
}
// Players & Profiles

View File

@ -27,8 +27,6 @@ public final class ShareHandler {
}
};
private static boolean running; // Used to prevent permanent sharing, McMMOPlayer.addXp() uses it
private ShareHandler() {}
/**
@ -39,8 +37,7 @@ public final class ShareHandler {
* @param skillType Skill being used
* @return True is the xp has been shared
*/
public static boolean handleEqualXpShare(int xp, McMMOPlayer mcMMOPlayer, SkillType skillType) {
running = true;
public static boolean handleXpShare(int xp, McMMOPlayer mcMMOPlayer, SkillType skillType) {
Party party = mcMMOPlayer.getParty();
switch (party.getXpShareMode()) {
@ -49,7 +46,6 @@ public final class ShareHandler {
List<Player> nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange());
if (nearMembers.isEmpty()) {
running = false;
return false;
}
@ -58,21 +54,16 @@ public final class ShareHandler {
int roundedXp = (int) Math.ceil(splitXp);
for (Player member : nearMembers) {
Users.getPlayer(member).addXp(skillType, roundedXp);
Users.getPlayer(member).beginUnsharedXpGain(skillType, roundedXp);
}
mcMMOPlayer.addXp(skillType, roundedXp);
mcMMOPlayer.beginUnsharedXpGain(skillType, roundedXp);
running = false;
return true;
case NONE:
default:
running = false;
return false;
}
}
}
public static boolean isRunning() {
return running;
}
}

View File

@ -46,7 +46,7 @@ public class DodgeEventHandler extends AcrobaticsEventHandler {
McMMOPlayer mcMMOPlayer = manager.getMcMMOPlayer();
if (System.currentTimeMillis() >= mcMMOPlayer.getProfile().getRespawnATS() + Misc.PLAYER_RESPAWN_COOLDOWN_SECONDS) {
manager.getMcMMOPlayer().addXp(SkillType.ACROBATICS, xp);
manager.getMcMMOPlayer().beginXpGain(SkillType.ACROBATICS, xp);
}
}
}

View File

@ -69,7 +69,7 @@ public class RollEventHandler extends AcrobaticsEventHandler {
@Override
protected void processXpGain(int xp) {
manager.getMcMMOPlayer().addXp(SkillType.ACROBATICS, xp);
manager.getMcMMOPlayer().beginXpGain(SkillType.ACROBATICS, xp);
}
/**

View File

@ -34,7 +34,7 @@ public class ArcheryManager extends SkillManager {
}
int bonusXp = (int) (squaredDistance * Archery.distanceXpModifer);
mcMMOPlayer.addXp(SkillType.ARCHERY, bonusXp);
mcMMOPlayer.beginXpGain(SkillType.ARCHERY, bonusXp);
}
/**

View File

@ -119,7 +119,7 @@ public class Excavation {
}
}
mcMMOPlayer.addXp(SkillType.EXCAVATION, xp);
mcMMOPlayer.beginXpGain(SkillType.EXCAVATION, xp);
}
/**

View File

@ -128,7 +128,7 @@ public final class Fishing {
caught.setItemStack(treasureDrop);
}
mcMMOPlayer.addXp(SkillType.FISHING, Config.getInstance().getFishingBaseXP() + treasureXp);
mcMMOPlayer.beginXpGain(SkillType.FISHING, Config.getInstance().getFishingBaseXP() + treasureXp);
event.setExpToDrop(event.getExpToDrop() * getVanillaXpMultiplier(skillLevel));
}

View File

@ -212,7 +212,7 @@ public class Herbalism {
}
}
mcMMOPlayer.addXp(SkillType.HERBALISM, xp);
mcMMOPlayer.beginXpGain(SkillType.HERBALISM, xp);
}
/**

View File

@ -104,7 +104,7 @@ public class Mining {
break;
}
mcMMOPlayer.addXp(SkillType.MINING, xp);
mcMMOPlayer.beginXpGain(SkillType.MINING, xp);
}
/**

View File

@ -48,7 +48,7 @@ public class Repair {
Player player = mcMMOPlayer.getPlayer();
player.playSound(player.getLocation(), Sound.ANVIL_USE, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH);
mcMMOPlayer.addXp(SkillType.REPAIR, dif * 10);
mcMMOPlayer.beginXpGain(SkillType.REPAIR, dif * 10);
}
/**

View File

@ -35,6 +35,6 @@ public class CombatXpGiver implements Runnable {
damage += health;
}
mcMMOPlayer.addXp(skillType, (int) (damage * baseXp));
mcMMOPlayer.beginXpGain(skillType, (int) (damage * baseXp));
}
}

View File

@ -68,11 +68,11 @@ public class SmeltResourceEventHandler {
Player player = mcMMOPlayer.getPlayer();
if (Permissions.mining(player)) {
mcMMOPlayer.addXp(SkillType.MINING, xp / 2);
mcMMOPlayer.beginXpGain(SkillType.MINING, xp / 2);
}
if (Permissions.repair(player)) {
mcMMOPlayer.addXp(SkillType.REPAIR, xp / 2);
mcMMOPlayer.beginXpGain(SkillType.REPAIR, xp / 2);
}
}

View File

@ -33,11 +33,11 @@ public class TamingManager extends SkillManager {
switch (event.getEntityType()) {
case WOLF:
mcMMOPlayer.addXp(SkillType.TAMING, Taming.wolfXp);
mcMMOPlayer.beginXpGain(SkillType.TAMING, Taming.wolfXp);
break;
case OCELOT:
mcMMOPlayer.addXp(SkillType.TAMING, Taming.ocelotXp);
mcMMOPlayer.beginXpGain(SkillType.TAMING, Taming.ocelotXp);
break;
default:

View File

@ -220,6 +220,6 @@ public final class TreeFeller {
block.setType(Material.AIR);
}
mcMMOPlayer.addXp(SkillType.WOODCUTTING, xp);
mcMMOPlayer.beginXpGain(SkillType.WOODCUTTING, xp);
}
}

View File

@ -80,7 +80,7 @@ public final class Woodcutting {
checkForDoubleDrop(mcMMOPlayer, block);
}
mcMMOPlayer.addXp(SkillType.WOODCUTTING, xp);
mcMMOPlayer.beginXpGain(SkillType.WOODCUTTING, xp);
}
/**