mcMMO/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java

516 lines
19 KiB
Java
Raw Normal View History

package com.gmail.nossr50.skills.utilities;
2012-01-09 20:00:13 +01:00
2012-03-09 07:23:32 +01:00
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
2012-01-09 20:00:13 +01:00
import org.bukkit.entity.Player;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.inventory.ItemStack;
2012-01-09 20:00:13 +01:00
import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.player.SpoutPlayer;
import com.gmail.nossr50.mcMMO;
2012-11-21 21:49:54 +01:00
import com.gmail.nossr50.config.AdvancedConfig;
2012-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.datatypes.PlayerProfile;
2012-03-27 07:09:28 +02:00
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
2013-01-30 17:35:33 +01:00
import com.gmail.nossr50.mods.ModChecks;
import com.gmail.nossr50.spout.SpoutConfig;
import com.gmail.nossr50.spout.SpoutTools;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.Users;
2012-01-09 20:00:13 +01:00
2013-01-26 23:01:55 +01:00
public class SkillTools {
2012-12-24 22:56:25 +01:00
static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
public static int abilityLengthIncreaseLevel = advancedConfig.getAbilityLength();
2013-01-24 20:19:26 +01:00
public static boolean abilitiesEnabled = Config.getInstance().getAbilitiesEnabled();
2012-03-13 19:09:32 +01:00
public static final int LUCKY_SKILL_ACTIVATION_CHANCE = 75;
public static final int NORMAL_SKILL_ACTIVATION_CHANCE = 100;
public static void handleFoodSkills(Player player, SkillType skill, FoodLevelChangeEvent event, int baseLevel, int maxLevel, int rankChange) {
int skillLevel = Users.getPlayer(player).getProfile().getSkillLevel(skill);
int currentFoodLevel = player.getFoodLevel();
int newFoodLevel = event.getFoodLevel();
int foodChange = newFoodLevel - currentFoodLevel;
for (int i = baseLevel; i <= maxLevel; i+= rankChange) {
if (skillLevel >= i) {
foodChange++;
}
}
event.setFoodLevel(currentFoodLevel + foodChange);
}
2012-03-13 19:09:32 +01:00
/**
* Checks to see if the cooldown for an item or ability is expired.
*
* @param oldTime The time the ability or item was last used
* @param cooldown The amount of time that must pass between uses
* @param player The player whose cooldown is being checked
2012-03-13 19:09:32 +01:00
* @return true if the cooldown is over, false otherwise
*/
2013-01-10 05:03:17 +01:00
public static boolean cooldownOver(long oldTime, int cooldown, Player player) {
2012-03-13 19:09:32 +01:00
long currentTime = System.currentTimeMillis();
int adjustedCooldown = cooldown;
2012-03-13 19:09:32 +01:00
//Reduced Cooldown Donor Perks
2013-01-07 02:52:31 +01:00
if (Permissions.cooldownsHalved(player)) {
adjustedCooldown = (int) (adjustedCooldown * 0.5);
}
2013-01-07 02:52:31 +01:00
else if (Permissions.cooldownsThirded(player)) {
adjustedCooldown = (int) (adjustedCooldown * 0.66);
}
2013-01-07 02:52:31 +01:00
else if (Permissions.cooldownsQuartered(player)) {
adjustedCooldown = (int) (adjustedCooldown * 0.75);
}
if (currentTime - oldTime >= (adjustedCooldown * Misc.TIME_CONVERSION_FACTOR)) {
2012-03-13 19:09:32 +01:00
return true;
}
2013-01-10 04:43:21 +01:00
return false;
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Calculate the time remaining until the cooldown expires.
*
* @param deactivatedTimeStamp Time of deactivation
* @param cooldown The length of the cooldown
* @return the number of seconds remaining before the cooldown expires
*/
public static int calculateTimeLeft(long deactivatedTimeStamp, int cooldown, Player player) {
int adjustedCooldown = cooldown;
//Reduced Cooldown Donor Perks
2013-01-07 02:52:31 +01:00
if (Permissions.cooldownsHalved(player)) {
adjustedCooldown = (int) (adjustedCooldown * 0.5);
}
2013-01-07 02:52:31 +01:00
else if (Permissions.cooldownsThirded(player)) {
adjustedCooldown = (int) (adjustedCooldown * 0.66);
}
2013-01-07 02:52:31 +01:00
else if (Permissions.cooldownsQuartered(player)) {
adjustedCooldown = (int) (adjustedCooldown * 0.75);
}
return (int) (((deactivatedTimeStamp + (adjustedCooldown * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR);
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Sends a message to the player when the cooldown expires.
*
* @param player The player to send a message to
2012-07-03 16:04:04 +02:00
* @param profile The profile of the player
2012-03-13 19:09:32 +01:00
* @param ability The ability to watch cooldowns for
*/
2012-07-03 16:04:04 +02:00
public static void watchCooldown(Player player, PlayerProfile profile, AbilityType ability) {
2013-01-10 05:03:17 +01:00
if (player == null || profile == null || ability == null)
return;
if (!profile.getAbilityInformed(ability) && cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
2012-07-03 16:04:04 +02:00
profile.setAbilityInformed(ability, true);
2012-03-13 19:09:32 +01:00
player.sendMessage(ability.getAbilityRefresh());
}
}
/**
* Process activating abilities & readying the tool.
*
* @param player The player using the ability
* @param skill The skill the ability is tied to
*/
public static void activationCheck(Player player, SkillType skill) {
if (Config.getInstance().getAbilitiesOnlyActivateWhenSneaking() && !player.isSneaking()) {
2012-03-13 19:09:32 +01:00
return;
}
PlayerProfile profile = Users.getPlayer(player).getProfile();
2012-03-13 19:09:32 +01:00
AbilityType ability = skill.getAbility();
ToolType tool = skill.getTool();
ItemStack inHand = player.getItemInHand();
if (ModChecks.isCustomTool(inHand) && !ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) {
return;
}
2012-03-13 19:09:32 +01:00
/* Check if any abilities are active */
if (profile == null) {
return;
}
2012-07-03 16:04:04 +02:00
if (!profile.getAbilityUse()) {
2012-03-13 19:09:32 +01:00
return;
}
for (AbilityType x : AbilityType.values()) {
2012-07-03 16:04:04 +02:00
if (profile.getAbilityMode(x)) {
return;
}
}
2012-03-13 19:09:32 +01:00
/* Woodcutting & Axes need to be treated differently.
* Basically the tool always needs to ready and we check to see if the cooldown is over when the user takes action
*/
2012-07-03 16:04:04 +02:00
if (ability.getPermissions(player) && tool.inHand(inHand) && !profile.getToolPreparationMode(tool)) {
2012-04-05 05:30:56 +02:00
if (skill != SkillType.WOODCUTTING && skill != SkillType.AXES) {
if (!profile.getAbilityMode(ability) && !cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString("Skills.TooTired", calculateTimeLeft(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)));
2012-04-05 05:30:56 +02:00
return;
2012-03-13 19:09:32 +01:00
}
}
if (Config.getInstance().getAbilityMessagesEnabled()) {
2012-03-13 19:09:32 +01:00
player.sendMessage(tool.getRaiseTool());
}
2012-07-03 16:04:04 +02:00
profile.setToolPreparationATS(tool, System.currentTimeMillis());
profile.setToolPreparationMode(tool, true);
2012-03-13 19:09:32 +01:00
}
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Monitors various things relating to skill abilities.
*
* @param player The player using the skill
2012-07-03 16:04:04 +02:00
* @param profile The profile of the player
2012-03-13 19:09:32 +01:00
* @param curTime The current system time
* @param skill The skill being monitored
*/
2012-07-03 16:04:04 +02:00
public static void monitorSkill(Player player, PlayerProfile profile, long curTime, SkillType skill) {
2012-03-13 19:09:32 +01:00
final int FOUR_SECONDS = 4000;
ToolType tool = skill.getTool();
AbilityType ability = skill.getAbility();
if (profile == null) {
return;
}
if (profile.getToolPreparationMode(tool) && curTime - (profile.getToolPreparationATS(tool) * Misc.TIME_CONVERSION_FACTOR) >= FOUR_SECONDS) {
2012-07-03 16:04:04 +02:00
profile.setToolPreparationMode(tool, false);
if (Config.getInstance().getAbilityMessagesEnabled()) {
player.sendMessage(tool.getLowerTool());
}
2012-03-13 19:09:32 +01:00
}
if (ability.getPermissions(player)) {
if (profile.getAbilityMode(ability) && (profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR) <= curTime) {
if (ability == AbilityType.BERSERK) {
player.setCanPickupItems(true);
}
2012-07-03 16:04:04 +02:00
profile.setAbilityMode(ability, false);
profile.setAbilityInformed(ability, false);
2012-03-13 19:09:32 +01:00
player.sendMessage(ability.getAbilityOff());
sendSkillMessage(player, ability.getAbilityPlayerOff(player));
2012-03-13 19:09:32 +01:00
}
}
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Check the XP of a skill.
*
* @param skillType The skill to check
* @param player The player whose skill to check
* @param profile The profile of the player whose skill to check
2012-03-13 19:09:32 +01:00
*/
public static void xpCheckSkill(SkillType skillType, Player player, PlayerProfile profile) {
2012-03-29 04:45:34 +02:00
int skillups = 0;
if (profile.getSkillXpLevel(skillType) >= profile.getXpToLevel(skillType)) {
while (profile.getSkillXpLevel(skillType) >= profile.getXpToLevel(skillType)) {
2012-07-06 17:57:17 +02:00
if ((skillType.getMaxLevel() >= profile.getSkillLevel(skillType) + 1) && (Misc.getPowerLevelCap() >= Users.getPlayer(player).getPowerLevel() + 1)) {
profile.removeXp(skillType, profile.getXpToLevel(skillType));
2012-03-13 19:09:32 +01:00
skillups++;
profile.skillUp(skillType, 1);
2012-03-13 19:09:32 +01:00
McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType);
mcMMO.p.getServer().getPluginManager().callEvent(eventToFire);
2012-03-13 19:09:32 +01:00
}
else {
profile.addLevels(skillType, 0);
}
2012-03-13 19:09:32 +01:00
}
String capitalized = StringUtils.getCapitalized(skillType.toString());
2012-03-13 19:09:32 +01:00
/* Spout Stuff */
if (mcMMO.spoutEnabled) {
2012-07-03 16:14:01 +02:00
SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
2012-03-13 19:09:32 +01:00
if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) {
SpoutTools.levelUpNotification(skillType, spoutPlayer);
2012-04-03 16:08:37 +02:00
/* Update custom titles */
if (SpoutConfig.getInstance().getShowPowerLevel()) {
2013-02-02 08:55:49 +01:00
spoutPlayer.setTitle(LocaleLoader.getString("Spout.Title", spoutPlayer.getName(), Users.getPlayer(player).getPowerLevel()));
2012-04-03 16:08:37 +02:00
}
2012-03-13 19:09:32 +01:00
}
else {
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", String.valueOf(skillups), profile.getSkillLevel(skillType)));
2012-03-13 19:09:32 +01:00
}
}
else {
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", String.valueOf(skillups), profile.getSkillLevel(skillType)));
2012-03-13 19:09:32 +01:00
}
}
if (mcMMO.spoutEnabled) {
SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) {
if (SpoutConfig.getInstance().getXPBarEnabled()) {
profile.getSpoutHud().updateXpBar();
2012-03-29 04:45:34 +02:00
}
}
}
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Checks if the given string represents a valid skill
*
* @param skillName The name of the skill to check
2012-03-13 19:09:32 +01:00
* @return true if this is a valid skill, false otherwise
*/
public static boolean isSkill(String skillName) {
if (SkillType.getSkill(skillName) != null) {
2012-03-13 19:09:32 +01:00
return true;
}
2013-01-10 04:43:21 +01:00
return false;
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
public static boolean isLocalizedSkill(String skillName) {
for (SkillType skill : SkillType.values()) {
if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString() + ".SkillName")))) {
return true;
}
}
return false;
}
public static String translateLocalizedSkill(String skillName) {
for (SkillType skill : SkillType.values()) {
if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString() + ".SkillName")))) {
return skill.toString();
}
}
return null;
}
2013-01-16 20:03:07 +01:00
public static String localizeSkillName(SkillType skill) {
return StringUtils.getCapitalized(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"));
2013-01-16 20:03:07 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Check if the player has any combat skill permissions.
*
* @param player The player to check permissions for
* @return true if the player has combat skills, false otherwise
*/
public static boolean hasCombatSkills(Player player) {
2013-01-07 02:52:31 +01:00
if (Permissions.axes(player)
|| Permissions.archery(player)
|| Permissions.swords(player)
|| Permissions.taming(player)
|| Permissions.unarmed(player)) {
2012-03-13 19:09:32 +01:00
return true;
}
2013-01-10 04:43:21 +01:00
return false;
2012-03-13 19:09:32 +01:00
}
/**
* Check if the player has any gathering skill permissions.
*
* @param player The player to check permissions for
* @return true if the player has gathering skills, false otherwise
*/
public static boolean hasGatheringSkills(Player player) {
2013-01-07 02:52:31 +01:00
if (Permissions.excavation(player)
|| Permissions.fishing(player)
|| Permissions.herbalism(player)
|| Permissions.mining(player)
|| Permissions.woodcutting(player)) {
2012-03-13 19:09:32 +01:00
return true;
}
2013-01-10 04:43:21 +01:00
return false;
2012-03-13 19:09:32 +01:00
}
/**
* Check if the player has any misc skill permissions.
*
* @param player The player to check permissions for
* @return true if the player has misc skills, false otherwise
*/
public static boolean hasMiscSkills(Player player) {
2013-01-07 02:52:31 +01:00
if (Permissions.acrobatics(player) || Permissions.repair(player)) {
2012-03-13 19:09:32 +01:00
return true;
}
2013-01-10 04:43:21 +01:00
return false;
2012-03-13 19:09:32 +01:00
}
/**
* Handle tool durability loss from abilities.
*
2012-05-21 13:41:49 +02:00
* @param inHand The item to damage
2012-03-13 19:09:32 +01:00
* @param durabilityLoss The durability to remove from the item
*/
2012-05-21 13:41:49 +02:00
public static void abilityDurabilityLoss(ItemStack inHand, int durabilityLoss) {
if (Config.getInstance().getAbilitiesDamageTools()) {
2012-05-21 14:31:29 +02:00
if (inHand.containsEnchantment(Enchantment.DURABILITY)) {
int level = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
if (Misc.getRandom().nextInt(level + 1) > 0) {
2012-05-21 14:31:29 +02:00
return;
}
}
2012-05-21 14:31:29 +02:00
inHand.setDurability((short) (inHand.getDurability() + durabilityLoss));
}
}
2012-03-13 19:09:32 +01:00
/**
* Check to see if an ability can be activated.
2012-03-13 19:09:32 +01:00
*
* @param player The player activating the ability
* @param type The skill the ability is based on
*/
2012-03-13 19:09:32 +01:00
public static void abilityCheck(Player player, SkillType type) {
PlayerProfile profile = Users.getPlayer(player).getProfile();
ToolType tool = type.getTool();
AbilityType ability = type.getAbility();
2012-03-13 19:09:32 +01:00
2012-07-03 16:04:04 +02:00
profile.setToolPreparationMode(tool, false);
2012-03-13 19:09:32 +01:00
2012-06-22 20:20:28 +02:00
/* Axes and Woodcutting are odd because they share the same tool.
* We show them the too tired message when they take action.
*/
if (type == SkillType.WOODCUTTING || type == SkillType.AXES) {
if (!profile.getAbilityMode(ability) && !cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
2013-02-02 08:55:49 +01:00
player.sendMessage(LocaleLoader.getString("Skills.TooTired", calculateTimeLeft(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)));
2012-06-22 20:20:28 +02:00
return;
}
2012-06-22 20:20:28 +02:00
}
2012-11-21 21:49:54 +01:00
int ticks = 2 + (profile.getSkillLevel(type) / abilityLengthIncreaseLevel);
2012-06-15 16:58:38 +02:00
2013-01-07 02:52:31 +01:00
if (Permissions.activationTwelve(player)) {
2012-06-22 20:20:28 +02:00
ticks = ticks + 12;
}
2013-01-07 02:52:31 +01:00
else if (Permissions.activationEight(player)) {
2012-06-22 20:20:28 +02:00
ticks = ticks + 8;
}
2013-01-07 02:52:31 +01:00
else if (Permissions.activationFour(player)) {
2012-06-22 20:20:28 +02:00
ticks = ticks + 4;
}
2012-03-13 19:09:32 +01:00
2012-06-22 20:20:28 +02:00
int maxTicks = ability.getMaxTicks();
2012-03-13 19:09:32 +01:00
2012-06-22 20:20:28 +02:00
if (maxTicks != 0 && ticks > maxTicks) {
ticks = maxTicks;
}
2012-03-13 19:09:32 +01:00
2012-07-03 16:04:04 +02:00
if (!profile.getAbilityMode(ability) && cooldownOver(profile.getSkillDATS(ability), ability.getCooldown(), player)) {
2012-06-22 20:20:28 +02:00
player.sendMessage(ability.getAbilityOn());
SkillTools.sendSkillMessage(player, ability.getAbilityPlayer(player));
2012-06-22 20:20:28 +02:00
profile.setSkillDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR));
2012-07-03 16:04:04 +02:00
profile.setAbilityMode(ability, true);
if (ability == AbilityType.BERSERK) {
player.setCanPickupItems(false);
}
2012-03-13 19:09:32 +01:00
}
}
2012-03-13 19:09:32 +01:00
/**
* Check to see if ability should be triggered.
*
* @param player The player using the ability
* @param block The block modified by the ability
* @param ability The ability to check
* @return true if the ability should activate, false otherwise
*/
2012-03-09 07:23:32 +01:00
public static boolean triggerCheck(Player player, Block block, AbilityType ability) {
2012-03-13 19:09:32 +01:00
boolean activate = true;
switch (ability) {
case BERSERK:
case GIGA_DRILL_BREAKER:
case SUPER_BREAKER:
case LEAF_BLOWER:
if (!ability.blockCheck(block)) {
activate = false;
break;
}
2012-04-27 11:47:11 +02:00
if (!Misc.blockBreakSimulate(block, player, true)) {
2012-03-13 19:09:32 +01:00
activate = false;
break;
}
break;
2012-03-13 19:09:32 +01:00
case GREEN_TERRA:
if (!ability.blockCheck(block)) {
2012-03-13 19:09:32 +01:00
activate = false;
break;
}
break;
2012-03-13 19:09:32 +01:00
default:
activate = false;
break;
}
return activate;
2012-03-09 07:23:32 +01:00
}
/**
* Calculate activation chance for a skill.
*
* @param isLucky true if the player has the appropriate "lucky" perk, false otherwise
* @return the activation chance
*/
public static int calculateActivationChance(boolean isLucky) {
if (isLucky) {
return LUCKY_SKILL_ACTIVATION_CHANCE;
}
return NORMAL_SKILL_ACTIVATION_CHANCE;
}
public static void sendSkillMessage(Player player, String message) {
for (Player otherPlayer : player.getWorld().getPlayers()) {
if (otherPlayer != player && Misc.isNear(player.getLocation(), otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
otherPlayer.sendMessage(message);
}
}
}
/**
* Check if a skill level is higher than the max bonus level of the ability.
*
* @param skillLevel Skill level to check
* @param maxLevel Max level of the ability
* @return whichever value is lower
*/
public static int skillCheck(int skillLevel, int maxLevel) {
//TODO: Could we just use Math.min(skillLevel, maxLevel) here?
if (skillLevel > maxLevel) {
return maxLevel;
}
return skillLevel;
}
2012-01-09 20:00:13 +01:00
}