123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- package com.gmail.nossr50.util;
- import java.util.Random;
- import org.bukkit.ChatColor;
- import org.bukkit.block.Block;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.getspout.spoutapi.SpoutManager;
- import org.getspout.spoutapi.player.SpoutPlayer;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.config.Config;
- import com.gmail.nossr50.config.SpoutConfig;
- import com.gmail.nossr50.datatypes.AbilityType;
- import com.gmail.nossr50.datatypes.McMMOPlayer;
- import com.gmail.nossr50.datatypes.PlayerProfile;
- import com.gmail.nossr50.datatypes.PlayerStat;
- import com.gmail.nossr50.datatypes.SkillType;
- import com.gmail.nossr50.datatypes.ToolType;
- import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
- import com.gmail.nossr50.locale.LocaleLoader;
- import com.gmail.nossr50.spout.SpoutStuff;
- public class Skills {
- private final static int TIME_CONVERSION_FACTOR = 1000;
- private final static double MAX_DISTANCE_AWAY = 10.0;
- private final static Random random = new Random();
- /**
- * 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
- * @return true if the cooldown is over, false otherwise
- */
- public static boolean cooldownOver(long oldTime, int cooldown, Player player){
- long currentTime = System.currentTimeMillis();
- int adjustedCooldown = cooldown;
- //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)) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * 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);
- }
- /**
- * Sends a message to the player when the cooldown expires.
- *
- * @param player The player to send a message to
- * @param profile The profile of the player
- * @param ability The ability to watch cooldowns for
- */
- public static void watchCooldown(Player player, PlayerProfile profile, AbilityType ability) {
- if (!profile.getAbilityInformed(ability) && cooldownOver(profile.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
- profile.setAbilityInformed(ability, true);
- 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()) {
- return;
- }
- PlayerProfile profile = Users.getProfile(player);
- AbilityType ability = skill.getAbility();
- ToolType tool = skill.getTool();
- ItemStack inHand = player.getItemInHand();
- if (ModChecks.isCustomTool(inHand) && !ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) {
- return;
- }
- /* Check if any abilities are active */
- if (!profile.getAbilityUse()) {
- return;
- }
- for (AbilityType x : AbilityType.values()) {
- if (profile.getAbilityMode(x)) {
- return;
- }
- }
- /* 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
- */
- if (ability.getPermissions(player) && tool.inHand(inHand) && !profile.getToolPreparationMode(tool)) {
- if (skill != SkillType.WOODCUTTING && skill != SkillType.AXES) {
- if (!profile.getAbilityMode(ability) && !cooldownOver(profile.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
- player.sendMessage(LocaleLoader.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(profile.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
- return;
- }
- }
- if (Config.getInstance().getAbilityMessagesEnabled()) {
- player.sendMessage(tool.getRaiseTool());
- }
- profile.setToolPreparationATS(tool, System.currentTimeMillis());
- profile.setToolPreparationMode(tool, true);
- }
- }
- /**
- * Monitors various things relating to skill abilities.
- *
- * @param player The player using the skill
- * @param profile The profile of the player
- * @param curTime The current system time
- * @param skill The skill being monitored
- */
- public static void monitorSkill(Player player, PlayerProfile profile, long curTime, SkillType skill) {
- final int FOUR_SECONDS = 4000;
- ToolType tool = skill.getTool();
- AbilityType ability = skill.getAbility();
- if (profile.getToolPreparationMode(tool) && curTime - (profile.getToolPreparationATS(tool) * TIME_CONVERSION_FACTOR) >= FOUR_SECONDS) {
- profile.setToolPreparationMode(tool, false);
- if (Config.getInstance().getAbilityMessagesEnabled()) {
- player.sendMessage(tool.getLowerTool());
- }
- }
- if (ability.getPermissions(player)) {
- if (profile.getAbilityMode(ability) && (profile.getSkillDATS(ability) * TIME_CONVERSION_FACTOR) <= curTime) {
- profile.setAbilityMode(ability, false);
- profile.setAbilityInformed(ability, false);
- player.sendMessage(ability.getAbilityOff());
- for (Player nearbyPlayer : player.getWorld().getPlayers()) {
- if (nearbyPlayer != player && Misc.isNear(player.getLocation(), nearbyPlayer.getLocation(), MAX_DISTANCE_AWAY)) {
- nearbyPlayer.sendMessage(ability.getAbilityPlayerOff(player));
- }
- }
- }
- }
- }
- /**
- * 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) {
- McMMOPlayer mcMMOPlayer = Users.getPlayer(player);
- PlayerProfile profile = mcMMOPlayer.getProfile();
- PlayerStat ps = new PlayerStat();
- if (skillType != SkillType.ALL) {
- ps.statVal = profile.getSkillLevel(skillType);
- }
- else {
- ps.statVal = mcMMOPlayer.getPowerLevel();
- }
- ps.name = player.getName();
- Leaderboard.updateLeaderboard(ps, skillType);
- }
- /**
- * 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
- */
- public static void xpCheckSkill(SkillType skillType, Player player, PlayerProfile profile) {
- 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() >= Users.getPlayer(player).getPowerLevel() + 1)) {
- profile.removeXP(skillType, profile.getXpToLevel(skillType));
- skillups++;
- profile.skillUp(skillType, 1);
- McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType);
- mcMMO.p.getServer().getPluginManager().callEvent(eventToFire);
- }
- else {
- profile.addLevels(skillType, 0);
- }
- }
- if (!Config.getInstance().getUseMySQL()) {
- processLeaderboardUpdate(skillType, player);
- processLeaderboardUpdate(SkillType.ALL, player);
- }
- String capitalized = Misc.getCapitalized(skillType.toString());
- /* Spout Stuff */
- if (mcMMO.spoutEnabled && player instanceof SpoutPlayer) {
- SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
- if (spoutPlayer.isSpoutCraftEnabled()) {
- if (SpoutConfig.getInstance().getXPBarEnabled()) {
- profile.updateXpBar();
- }
- SpoutStuff.levelUpNotification(skillType, spoutPlayer);
- /* Update custom titles */
- if (SpoutConfig.getInstance().getShowPowerLevel()) {
- spoutPlayer.setTitle(spoutPlayer.getName()+ "\n" + ChatColor.YELLOW + "P" + ChatColor.GOLD + "lvl" + ChatColor.WHITE + "." + ChatColor.GREEN + String.valueOf(Users.getPlayer(player).getPowerLevel()));
- }
- }
- else {
- player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", new Object[] {String.valueOf(skillups), profile.getSkillLevel(skillType)}));
- }
- }
- else {
- player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", new Object[] {String.valueOf(skillups), profile.getSkillLevel(skillType)}));
- }
- }
- /* Always update XP Bar (Check if no levels were gained first to remove redundancy) */
- if (skillups == 0 && mcMMO.spoutEnabled && player instanceof SpoutPlayer) {
- SpoutPlayer spoutPlayer = (SpoutPlayer) player;
- if (spoutPlayer.isSpoutCraftEnabled()) {
- if (SpoutConfig.getInstance().getXPBarEnabled()) {
- profile.updateXpBar();
- }
- }
- }
- }
- /**
- * Check XP of all skills.
- *
- * @param player The player to check XP for.
- * @param profile The profile of the player whose skill to check
- */
- public static void xpCheckAll(Player player, PlayerProfile profile) {
- for (SkillType skillType : SkillType.values()) {
- //Don't want to do anything with this one
- if (skillType == SkillType.ALL) {
- continue;
- }
- xpCheckSkill(skillType, player, profile);
- }
- }
- /**
- * 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()) {
- if (x.toString().equals(skillName.toUpperCase())) {
- return x;
- }
- }
- return null;
- }
- /**
- * 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;
- }
- }
- /**
- * 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) {
- if (Permissions.getInstance().axes(player)
- || Permissions.getInstance().archery(player)
- || Permissions.getInstance().swords(player)
- || Permissions.getInstance().taming(player)
- || Permissions.getInstance().unarmed(player)) {
- 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) {
- if (Permissions.getInstance().excavation(player)
- || Permissions.getInstance().fishing(player)
- || Permissions.getInstance().herbalism(player)
- || Permissions.getInstance().mining(player)
- || Permissions.getInstance().woodcutting(player)) {
- 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) {
- if (Permissions.getInstance().acrobatics(player) || Permissions.getInstance().repair(player)) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * Handle tool durability loss from abilities.
- *
- * @param inHand The item to damage
- * @param durabilityLoss The durability to remove from the item
- */
- public static void abilityDurabilityLoss(ItemStack inHand, int durabilityLoss) {
- if (Config.getInstance().getAbilitiesDamageTools()) {
- if (inHand.containsEnchantment(Enchantment.DURABILITY)) {
- int level = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
- if (random.nextInt(level + 1) > 0) {
- return;
- }
- }
- inHand.setDurability((short) (inHand.getDurability() + durabilityLoss));
- }
- }
- /**
- * Check to see if an ability can be activated.
- *
- * @param player The player activating the ability
- * @param type The skill the ability is based on
- */
- public static void abilityCheck(Player player, SkillType type) {
- PlayerProfile profile = Users.getProfile(player);
- ToolType tool = type.getTool();
- if (!profile.getToolPreparationMode(tool)) {
- return;
- }
- profile.setToolPreparationMode(tool, false);
- AbilityType ability = type.getAbility();
- /* 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) * TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
- player.sendMessage(LocaleLoader.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(profile.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
- return;
- }
- }
- int ticks = 2 + (profile.getSkillLevel(type) / 50);
- 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;
- }
- int maxTicks = ability.getMaxTicks();
- if (maxTicks != 0 && ticks > maxTicks) {
- ticks = maxTicks;
- }
- if (!profile.getAbilityMode(ability) && cooldownOver(profile.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));
- }
- }
- profile.setSkillDATS(ability, System.currentTimeMillis() + (ticks * TIME_CONVERSION_FACTOR));
- profile.setAbilityMode(ability, true);
- }
- }
- /**
- * 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
- */
- public static boolean triggerCheck(Player player, Block block, AbilityType ability) {
- 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;
- }
- if (!Misc.blockBreakSimulate(block, player, true)) {
- activate = false;
- break;
- }
- break;
- case GREEN_TERRA:
- if (!ability.blockCheck(block)) {
- activate = false;
- break;
- }
- break;
- default:
- activate = false;
- break;
- }
- return activate;
- }
- /**
- * 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)) {
- Users.getPlayer(player).addXP(type, xp);
- xpCheckSkill(type, player, profile);
- }
- }
- }
|