mcMMO/src/main/java/com/gmail/nossr50/util/Skills.java

504 lines
18 KiB
Java
Raw Normal View History

2012-05-01 19:58:47 +02:00
package com.gmail.nossr50.util;
2012-01-09 20:00:13 +01:00
2012-05-21 14:31:29 +02:00
import java.util.Random;
2012-01-09 20:00:13 +01:00
import org.bukkit.ChatColor;
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.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-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.SpoutConfig;
import com.gmail.nossr50.datatypes.AbilityType;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.PlayerStat;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.datatypes.ToolType;
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;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.spout.SpoutStuff;
2012-01-09 20:00:13 +01:00
2012-03-13 19:09:32 +01:00
public class Skills {
private final static int TIME_CONVERSION_FACTOR = 1000;
private final static double MAX_DISTANCE_AWAY = 10.0;
2012-03-13 19:09:32 +01:00
2012-05-21 14:31:29 +02:00
private final static Random random = new Random();
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
*/
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
if (player.hasPermission("mcmmo.perks.cooldowns.halved")) {
adjustedCooldown = (int) (adjustedCooldown * 0.5);
}
else if (player.hasPermission("mcmmo.perks.cooldowns.thirded")) {
adjustedCooldown = (int) (adjustedCooldown * 0.66);
}
else if (player.hasPermission("mcmmo.perks.cooldowns.quartered")) {
adjustedCooldown = (int) (adjustedCooldown * 0.75);
}
if (currentTime - oldTime >= (adjustedCooldown * TIME_CONVERSION_FACTOR)) {
2012-03-13 19:09:32 +01:00
return true;
}
else {
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) {
return (int) (((deactivatedTimeStamp + (cooldown * TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / 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
* @param PP The profile of the player
* @param ability The ability to watch cooldowns for
*/
2012-06-25 16:29:29 +02:00
public static void watchCooldown(Player player, PlayerProfile PP, AbilityType ability) {
if (!PP.getAbilityInformed(ability) && cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
PP.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 PP = Users.getProfile(player);
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 (!PP.getAbilityUse()) {
2012-03-13 19:09:32 +01:00
return;
}
for (AbilityType x : AbilityType.values()) {
if (PP.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-04-05 05:30:56 +02:00
if (ability.getPermissions(player) && tool.inHand(inHand) && !PP.getToolPreparationMode(tool)) {
if (skill != SkillType.WOODCUTTING && skill != SkillType.AXES) {
if (!PP.getAbilityMode(ability) && !cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
2012-04-27 11:47:11 +02:00
player.sendMessage(LocaleLoader.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
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());
}
PP.setToolPreparationATS(tool, System.currentTimeMillis());
PP.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
* @param PP The profile of the player
* @param curTime The current system time
* @param skill The skill being monitored
*/
public static void monitorSkill(Player player, PlayerProfile PP, long curTime, SkillType skill) {
final int FOUR_SECONDS = 4000;
ToolType tool = skill.getTool();
AbilityType ability = skill.getAbility();
if (PP.getToolPreparationMode(tool) && curTime - (PP.getToolPreparationATS(tool) * TIME_CONVERSION_FACTOR) >= FOUR_SECONDS) {
PP.setToolPreparationMode(tool, false);
2012-03-13 19:09:32 +01:00
player.sendMessage(tool.getLowerTool());
}
if (ability.getPermissions(player)) {
if (PP.getAbilityMode(ability) && (PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR) <= curTime) {
PP.setAbilityMode(ability, false);
PP.setAbilityInformed(ability, false);
2012-03-13 19:09:32 +01:00
player.sendMessage(ability.getAbilityOff());
for (Player y : player.getWorld().getPlayers()) {
2012-04-27 11:47:11 +02:00
if (y != player && Misc.isNear(player.getLocation(), y.getLocation(), MAX_DISTANCE_AWAY)) {
y.sendMessage(ability.getAbilityPlayerOff(player));
2012-03-13 19:09:32 +01:00
}
}
2012-03-13 19:09:32 +01:00
}
}
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Update the leaderboards.
*
* @param skillType The skill to update the leaderboards for
* @param player The player whose skill to update
*/
public static void processLeaderboardUpdate(SkillType skillType, Player player) {
2012-03-13 19:09:32 +01:00
PlayerProfile PP = Users.getProfile(player);
PlayerStat ps = new PlayerStat();
if (skillType != SkillType.ALL) {
ps.statVal = PP.getSkillLevel(skillType);
}
else {
ps.statVal = PP.getPowerLevel();
2012-03-13 19:09:32 +01:00
}
ps.name = player.getName();
Leaderboard.updateLeaderboard(ps, skillType);
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)) {
if ((skillType.getMaxLevel() >= profile.getSkillLevel(skillType) + 1) && (Misc.getPowerLevelCap() >= profile.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
}
if (!Config.getInstance().getUseMySQL()) {
processLeaderboardUpdate(skillType, player);
processLeaderboardUpdate(SkillType.ALL, player);
2012-03-13 19:09:32 +01:00
}
2012-04-27 11:47:11 +02:00
String capitalized = Misc.getCapitalized(skillType.toString());
2012-03-13 19:09:32 +01:00
/* Spout Stuff */
if (mcMMO.spoutEnabled && player instanceof SpoutPlayer) {
2012-03-13 19:09:32 +01:00
SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
if (sPlayer.isSpoutCraftEnabled()) {
if (SpoutConfig.getInstance().getXPBarEnabled()) {
SpoutStuff.updateXpBar(player);
2012-03-13 19:09:32 +01:00
}
2012-03-13 19:09:32 +01:00
SpoutStuff.levelUpNotification(skillType, sPlayer);
2012-04-03 16:08:37 +02:00
/* Update custom titles */
if (SpoutConfig.getInstance().getShowPowerLevel()) {
sPlayer.setTitle(sPlayer.getName()+ "\n" + ChatColor.YELLOW + "P" + ChatColor.GOLD + "lvl" + ChatColor.WHITE + "." + ChatColor.GREEN + String.valueOf(profile.getPowerLevel()));
2012-04-03 16:08:37 +02:00
}
2012-03-13 19:09:32 +01:00
}
else {
player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", new Object[] {String.valueOf(skillups), profile.getSkillLevel(skillType)}));
2012-03-13 19:09:32 +01:00
}
}
else {
player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", new Object[] {String.valueOf(skillups), profile.getSkillLevel(skillType)}));
2012-03-13 19:09:32 +01:00
}
}
2012-03-29 04:45:34 +02:00
/* Always update XP Bar (Check if no levels were gained first to remove redundancy) */
if (skillups == 0 && mcMMO.spoutEnabled && player instanceof SpoutPlayer) {
2012-03-29 04:45:34 +02:00
SpoutPlayer sPlayer = (SpoutPlayer) player;
if (sPlayer.isSpoutCraftEnabled()) {
if (SpoutConfig.getInstance().getXPBarEnabled()) {
SpoutStuff.updateXpBar(player);
2012-03-29 04:45:34 +02:00
}
}
}
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Check XP of all skills.
*
* @param player The player to check XP for.
* @param profile The profile of the player whose skill to check
2012-03-13 19:09:32 +01:00
*/
public static void xpCheckAll(Player player, PlayerProfile profile) {
for (SkillType skillType : SkillType.values()) {
2012-03-13 19:09:32 +01:00
//Don't want to do anything with this one
if (skillType == SkillType.ALL) {
2012-03-13 19:09:32 +01:00
continue;
}
xpCheckSkill(skillType, player, profile);
2012-03-13 19:09:32 +01:00
}
2012-01-09 20:00:13 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Get the skill represented by the given string
*
* @param skillName The name of the skill
* @return the SkillType if it exists, null otherwise
*/
public static SkillType getSkillType(String skillName) {
for (SkillType x : SkillType.values()) {
2012-05-21 14:39:19 +02:00
if (x.toString().equals(skillName.toUpperCase())) {
2012-03-13 19:09:32 +01:00
return x;
2012-05-21 14:39:19 +02:00
}
2012-03-13 19:09:32 +01:00
}
2012-05-21 14:39:19 +02:00
2012-03-13 19:09:32 +01:00
return null;
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
* @return true if this is a valid skill, false otherwise
*/
public static boolean isSkill(String skillName) {
if (getSkillType(skillName) != null) {
return true;
}
else {
return false;
}
2012-01-09 20:00:13 +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) {
2012-04-27 11:47:11 +02:00
if (Permissions.getInstance().axes(player)
|| Permissions.getInstance().archery(player)
|| Permissions.getInstance().swords(player)
|| Permissions.getInstance().taming(player)
|| Permissions.getInstance().unarmed(player)) {
2012-03-13 19:09:32 +01:00
return true;
}
else {
return false;
}
}
/**
* 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) {
2012-04-27 11:47:11 +02:00
if (Permissions.getInstance().excavation(player)
|| Permissions.getInstance().fishing(player)
|| Permissions.getInstance().herbalism(player)
|| Permissions.getInstance().mining(player)
|| Permissions.getInstance().woodcutting(player)) {
2012-03-13 19:09:32 +01:00
return true;
}
else {
return false;
}
}
/**
* 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) {
2012-04-27 11:47:11 +02:00
if (Permissions.getInstance().acrobatics(player) || Permissions.getInstance().repair(player)) {
2012-03-13 19:09:32 +01:00
return true;
}
else {
return false;
}
}
/**
* 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 (random.nextInt(level + 1) > 0) {
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 PP = Users.getProfile(player);
ToolType tool = type.getTool();
2012-03-13 19:09:32 +01:00
2012-06-22 20:20:28 +02:00
if (!PP.getToolPreparationMode(tool)) {
return;
}
2012-03-13 19:09:32 +01:00
2012-06-22 20:20:28 +02:00
PP.setToolPreparationMode(tool, false);
2012-03-13 19:09:32 +01:00
2012-06-22 20:20:28 +02:00
AbilityType ability = type.getAbility();
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 (!PP.getAbilityMode(ability) && !cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
player.sendMessage(LocaleLoader.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
return;
}
2012-06-22 20:20:28 +02:00
}
2012-06-22 20:20:28 +02:00
int ticks = 2 + (PP.getSkillLevel(type) / 50);
2012-06-15 16:58:38 +02:00
2012-06-22 20:20:28 +02:00
if (player.hasPermission("mcmmo.perks.activationtime.twelveseconds")) {
ticks = ticks + 12;
}
else if (player.hasPermission("mcmmo.perks.activationtime.eightseconds")) {
ticks = ticks + 8;
}
else if (player.hasPermission("mcmmo.perks.activationtime.fourseconds")) {
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-06-22 20:20:28 +02:00
if (!PP.getAbilityMode(ability) && cooldownOver(PP.getSkillDATS(ability), ability.getCooldown(), player)) {
player.sendMessage(ability.getAbilityOn());
for (Player y : player.getWorld().getPlayers()) {
if (y != player && Misc.isNear(player.getLocation(), y.getLocation(), MAX_DISTANCE_AWAY)) {
y.sendMessage(ability.getAbilityPlayer(player));
}
2012-03-13 19:09:32 +01:00
}
2012-06-22 20:20:28 +02:00
PP.setSkillDATS(ability, System.currentTimeMillis() + (ticks * TIME_CONVERSION_FACTOR));
PP.setAbilityMode(ability, true);
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;
if (!ability.getPermissions(player)) {
activate = false;
return activate;
}
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
}
/**
* Handle the processing of XP gain from individual skills.
*
* @param player The player that gained XP
* @param profile The profile of the player gaining XP
* @param type The type of skill to gain XP from
* @param xp the amount of XP to gain
*/
public static void xpProcessing(Player player, PlayerProfile profile, SkillType type, int xp) {
if (type.getPermissions(player)) {
profile.addXP(type, xp);
xpCheckSkill(type, player, profile);
}
}
2012-01-09 20:00:13 +01:00
}