SkillUtils cleanup, EventUtils creation

Move some functions in SkillUtils to more relevant locations.

Begin work on utility class to handle all event calls.
This commit is contained in:
GJ
2013-10-15 13:03:33 -04:00
parent 309dfd50cd
commit 468fbdab56
32 changed files with 409 additions and 504 deletions

View File

@ -0,0 +1,62 @@
package com.gmail.nossr50.util;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityActivateEvent;
public class EventUtils {
public static McMMOPlayerAbilityActivateEvent callPlayerAbilityActivateEvent(Player player, SkillType skill) {
McMMOPlayerAbilityActivateEvent event = new McMMOPlayerAbilityActivateEvent(player, skill);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return event;
}
public static FakePlayerAnimationEvent callFakeArmSwingEvent(Player player) {
FakePlayerAnimationEvent event = new FakePlayerAnimationEvent(player);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return event;
}
public static McMMOPlayerLevelUpEvent callLevelUpEvent(Player player, SkillType skill, int levelsGained) {
McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, skill, levelsGained);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return event;
}
/**
* Simulate a block break event.
*
* @param block The block to break
* @param player The player breaking the block
* @param shouldArmSwing true if an armswing event should be fired, false otherwise
* @return true if the event wasn't cancelled, false otherwise
*/
public static boolean simulateBlockBreak(Block block, Player player, boolean shouldArmSwing) {
PluginManager pluginManager = mcMMO.p.getServer().getPluginManager();
// Support for NoCheat
if (shouldArmSwing) {
callFakeArmSwingEvent(player);
}
FakeBlockDamageEvent damageEvent = new FakeBlockDamageEvent(player, block, player.getItemInHand(), true);
pluginManager.callEvent(damageEvent);
FakeBlockBreakEvent breakEvent = new FakeBlockBreakEvent(block, player);
pluginManager.callEvent(breakEvent);
return !damageEvent.isCancelled() && !breakEvent.isCancelled();
}
}

View File

@ -26,7 +26,7 @@ public final class HardcoreManager {
PlayerProfile playerProfile = UserManager.getPlayer(player).getProfile();
int totalLost = 0;
for (SkillType skillType : SkillType.nonChildSkills()) {
for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
int playerSkillLevel = playerProfile.getSkillLevel(skillType);
if (playerSkillLevel <= 0) {
@ -56,7 +56,7 @@ public final class HardcoreManager {
PlayerProfile victimProfile = UserManager.getPlayer(victim).getProfile();
int totalStolen = 0;
for (SkillType skillType : SkillType.nonChildSkills()) {
for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
int killerSkillLevel = killerProfile.getSkillLevel(skillType);
int victimSkillLevel = victimProfile.getSkillLevel(skillType);
@ -89,7 +89,7 @@ public final class HardcoreManager {
public static boolean isStatLossEnabled() {
boolean enabled = false;
for (SkillType skillType : SkillType.nonChildSkills()) {
for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
if (skillType.getHardcoreStatLossEnabled()) {
enabled = true;
break;
@ -107,7 +107,7 @@ public final class HardcoreManager {
public static boolean isVampirismEnabled() {
boolean enabled = false;
for (SkillType skillType : SkillType.nonChildSkills()) {
for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
if (skillType.getHardcoreVampirismEnabled()) {
enabled = true;
break;

View File

@ -50,7 +50,6 @@ import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
public final class CommandRegistrationManager {
private CommandRegistrationManager() {};
@ -60,7 +59,7 @@ public final class CommandRegistrationManager {
private static void registerSkillCommands() {
for (SkillType skill : SkillType.values()) {
String commandName = skill.toString().toLowerCase();
String localizedName = SkillUtils.getSkillName(skill).toLowerCase();
String localizedName = skill.getSkillName().toLowerCase();
PluginCommand command;

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.util.commands;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.OfflinePlayer;
@ -157,16 +158,7 @@ public final class CommandUtils {
* @param display The sender to display stats to
*/
public static void printGatheringSkills(Player inspect, CommandSender display) {
if (SkillUtils.hasGatheringSkills(inspect)) {
PlayerProfile profile = UserManager.getPlayer(inspect).getProfile();
display.sendMessage(LocaleLoader.getString("Stats.Header.Gathering"));
displaySkill(inspect, profile, SkillType.EXCAVATION, display);
displaySkill(inspect, profile, SkillType.FISHING, display);
displaySkill(inspect, profile, SkillType.HERBALISM, display);
displaySkill(inspect, profile, SkillType.MINING, display);
displaySkill(inspect, profile, SkillType.WOODCUTTING, display);
}
printGroupedSkillData(inspect, display, LocaleLoader.getString("Stats.Header.Gathering"), SkillType.GATHERING_SKILLS);
}
public static void printGatheringSkills(Player player) {
@ -180,16 +172,7 @@ public final class CommandUtils {
* @param display The sender to display stats to
*/
public static void printCombatSkills(Player inspect, CommandSender display) {
if (SkillUtils.hasCombatSkills(inspect)) {
PlayerProfile profile = UserManager.getPlayer(inspect).getProfile();
display.sendMessage(LocaleLoader.getString("Stats.Header.Combat"));
displaySkill(inspect, profile, SkillType.AXES, display);
displaySkill(inspect, profile, SkillType.ARCHERY, display);
displaySkill(inspect, profile, SkillType.SWORDS, display);
displaySkill(inspect, profile, SkillType.TAMING, display);
displaySkill(inspect, profile, SkillType.UNARMED, display);
}
printGroupedSkillData(inspect, display, LocaleLoader.getString("Stats.Header.Combat"), SkillType.COMBAT_SKILLS);
}
public static void printCombatSkills(Player player) {
@ -203,26 +186,38 @@ public final class CommandUtils {
* @param display The sender to display stats to
*/
public static void printMiscSkills(Player inspect, CommandSender display) {
if (SkillUtils.hasMiscSkills(inspect)) {
PlayerProfile profile = UserManager.getPlayer(inspect).getProfile();
display.sendMessage(LocaleLoader.getString("Stats.Header.Misc"));
displaySkill(inspect, profile, SkillType.ACROBATICS, display);
displaySkill(inspect, profile, SkillType.REPAIR, display);
}
printGroupedSkillData(inspect, display, LocaleLoader.getString("Stats.Header.Misc"), SkillType.MISC_SKILLS);
}
public static void printMiscSkills(Player player) {
printMiscSkills(player, player);
}
private static void displaySkill(Player player, PlayerProfile profile, SkillType skill, CommandSender display) {
if (Permissions.skillEnabled(player, skill)) {
displaySkill(display, profile, skill);
public static String displaySkill(PlayerProfile profile, SkillType skill) {
if (skill.isChildSkill()) {
return LocaleLoader.getString("Skills.ChildStats", LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".Listener"), profile.getSkillLevel(skill));
}
return LocaleLoader.getString("Skills.Stats", LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".Listener"), profile.getSkillLevel(skill), profile.getSkillXpLevel(skill), profile.getXpToLevel(skill));
}
private static void printGroupedSkillData(Player inspect, CommandSender display, String header, List<SkillType> skillGroup) {
PlayerProfile profile = UserManager.getPlayer(inspect).getProfile();
List<String> displayData = new ArrayList<String>();
displayData.add(header);
for (SkillType skill : skillGroup) {
if (Permissions.skillEnabled(inspect, skill)) {
displayData.add(displaySkill(profile, skill));
}
}
int size = displayData.size();
if (size > 1) {
display.sendMessage(displayData.toArray(new String[size]));
}
}
public static void displaySkill(CommandSender sender, PlayerProfile profile, SkillType skill) {
sender.sendMessage(LocaleLoader.getString("Skills.Stats", LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".Listener"), profile.getSkillLevel(skill), profile.getSkillXpLevel(skill), profile.getXpToLevel(skill)));
}
}

View File

@ -23,7 +23,6 @@ import com.gmail.nossr50.runnables.scoreboards.ScoreboardChangeTask;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillUtils;
public class ScoreboardManager {
private static final Map<String, Scoreboard> PLAYER_SCOREBOARDS = new HashMap<String, Scoreboard>();
@ -80,10 +79,10 @@ public class ScoreboardManager {
Player player = mcMMOPlayer.getPlayer();
Scoreboard oldScoreboard = player.getScoreboard();
Scoreboard newScoreboard = PLAYER_SCOREBOARDS.get(player.getName());
Objective objective = newScoreboard.getObjective(SkillUtils.getSkillName(skill));
Objective objective = newScoreboard.getObjective(skill.getSkillName());
if (objective == null) {
objective = newScoreboard.registerNewObjective(SkillUtils.getSkillName(skill), "dummy");
objective = newScoreboard.registerNewObjective(skill.getSkillName(), "dummy");
}
updatePlayerSkillScores(mcMMOPlayer.getProfile(), skill, objective);
@ -165,7 +164,7 @@ public class ScoreboardManager {
}
Objective newObjective = GLOBAL_STATS_SCOREBOARD.registerNewObjective(skillName, "dummy");
newObjective.setDisplayName(ChatColor.GOLD + (skillName.equalsIgnoreCase("all") ? POWER_LEVEL : SkillUtils.getSkillName(SkillType.getSkill(skillName))));
newObjective.setDisplayName(ChatColor.GOLD + (skillName.equalsIgnoreCase("all") ? POWER_LEVEL : SkillType.getSkill(skillName).getSkillName()));
updateGlobalStatsScores(player, newObjective, skillName, pageNumber);
changeScoreboard(player, oldScoreboard, GLOBAL_STATS_SCOREBOARD, Config.getInstance().getMctopScoreboardTime());
@ -187,12 +186,12 @@ public class ScoreboardManager {
PlayerProfile profile = mcMMOPlayer.getProfile();
Server server = mcMMO.p.getServer();
for (SkillType skill : SkillType.nonChildSkills()) {
for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
if (!Permissions.skillEnabled(player, skill)) {
continue;
}
objective.getScore(server.getOfflinePlayer(SkillUtils.getSkillName(skill))).setScore(profile.getSkillLevel(skill));
objective.getScore(server.getOfflinePlayer(skill.getSkillName())).setScore(profile.getSkillLevel(skill));
}
objective.getScore(server.getOfflinePlayer(ChatColor.GOLD + POWER_LEVEL)).setScore(mcMMOPlayer.getPowerLevel());
@ -206,7 +205,7 @@ public class ScoreboardManager {
Map<String, Integer> skills = mcMMO.getDatabaseManager().readRank(playerName);
for (SkillType skill : SkillType.nonChildSkills()) {
for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
if (!Permissions.skillEnabled(player, skill)) {
continue;
}
@ -214,7 +213,7 @@ public class ScoreboardManager {
rank = skills.get(skill.name());
if (rank != null) {
objective.getScore(server.getOfflinePlayer(SkillUtils.getSkillName(skill))).setScore(rank);
objective.getScore(server.getOfflinePlayer(skill.getSkillName())).setScore(rank);
}
}
@ -233,11 +232,11 @@ public class ScoreboardManager {
Map<String, Integer> skills = mcMMO.getDatabaseManager().readRank(targetName);
for (SkillType skill : SkillType.nonChildSkills()) {
for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
rank = skills.get(skill.name());
if (rank != null) {
objective.getScore(server.getOfflinePlayer(SkillUtils.getSkillName(skill))).setScore(rank);
objective.getScore(server.getOfflinePlayer(skill.getSkillName())).setScore(rank);
}
}
@ -258,13 +257,13 @@ public class ScoreboardManager {
int powerLevel = 0;
int skillLevel;
for (SkillType skill : SkillType.nonChildSkills()) {
for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
if (!Permissions.skillEnabled(target, skill)) {
continue;
}
skillLevel = profile.getSkillLevel(skill);
objective.getScore(server.getOfflinePlayer(SkillUtils.getSkillName(skill))).setScore(skillLevel);
objective.getScore(server.getOfflinePlayer(skill.getSkillName())).setScore(skillLevel);
powerLevel += skillLevel;
}
@ -278,9 +277,9 @@ public class ScoreboardManager {
int powerLevel = 0;
int skillLevel;
for (SkillType skill : SkillType.nonChildSkills()) {
for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
skillLevel = targetProfile.getSkillLevel(skill);
objective.getScore(server.getOfflinePlayer(SkillUtils.getSkillName(skill))).setScore(skillLevel);
objective.getScore(server.getOfflinePlayer(skill.getSkillName())).setScore(skillLevel);
powerLevel += skillLevel;
}

View File

@ -51,7 +51,7 @@ public final class CombatUtils {
SwordsManager swordsManager = mcMMOPlayer.getSwordsManager();
if (swordsManager.canActivateAbility()) {
SkillUtils.abilityCheck(mcMMOPlayer, SkillType.SWORDS);
mcMMOPlayer.checkAbilityActivation(SkillType.SWORDS);
}
if (swordsManager.canUseBleed()) {
@ -73,7 +73,7 @@ public final class CombatUtils {
AxesManager axesManager = mcMMOPlayer.getAxesManager();
if (axesManager.canActivateAbility()) {
SkillUtils.abilityCheck(mcMMOPlayer, SkillType.AXES);
mcMMOPlayer.checkAbilityActivation(SkillType.AXES);
}
if (axesManager.canUseAxeMastery()) {
@ -107,7 +107,7 @@ public final class CombatUtils {
UnarmedManager unarmedManager = mcMMOPlayer.getUnarmedManager();
if (unarmedManager.canActivateAbility()) {
SkillUtils.abilityCheck(mcMMOPlayer, SkillType.UNARMED);
mcMMOPlayer.checkAbilityActivation(SkillType.UNARMED);
}
if (unarmedManager.canUseIronArm()) {

View File

@ -3,39 +3,24 @@ package com.gmail.nossr50.util.skills;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.HiddenConfig;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.skills.ToolType;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityActivateEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.skills.AbilityDisableTask;
import com.gmail.nossr50.runnables.skills.ToolLowerTask;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.ModUtils;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.player.UserManager;
@ -78,108 +63,7 @@ public class SkillUtils {
* @return true if the cooldown is expired
*/
public static boolean cooldownExpired(long deactivatedTimeStamp, int cooldown) {
return (System.currentTimeMillis() >= (deactivatedTimeStamp + cooldown) * Misc.TIME_CONVERSION_FACTOR);
}
/**
* 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;
}
ItemStack inHand = player.getItemInHand();
if (ModUtils.isCustomTool(inHand) && !ModUtils.getToolFromItemStack(inHand).isAbilityEnabled()) {
return;
}
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
if (!mcMMOPlayer.getAbilityUse()) {
return;
}
for (AbilityType abilityType : AbilityType.values()) {
if (mcMMOPlayer.getAbilityMode(abilityType)) {
return;
}
}
PlayerProfile playerProfile = mcMMOPlayer.getProfile();
AbilityType ability = skill.getAbility();
ToolType tool = skill.getTool();
/*
* 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) && !mcMMOPlayer.getToolPreparationMode(tool)) {
if (skill != SkillType.WOODCUTTING && skill != SkillType.AXES) {
int timeRemaining = calculateTimeLeft(playerProfile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player);
if (!mcMMOPlayer.getAbilityMode(ability) && timeRemaining > 0) {
player.sendMessage(LocaleLoader.getString("Skills.TooTired", timeRemaining));
return;
}
}
if (Config.getInstance().getAbilityMessagesEnabled()) {
player.sendMessage(tool.getRaiseTool());
}
mcMMOPlayer.setToolPreparationATS(tool, System.currentTimeMillis());
mcMMOPlayer.setToolPreparationMode(tool, true);
new ToolLowerTask(mcMMOPlayer, tool).runTaskLaterAsynchronously(mcMMO.p, 4 * Misc.TICK_CONVERSION_FACTOR);
}
}
/**
* 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 levelsGained = 0;
float xpRemoved = 0;
if (profile.getSkillXpLevelRaw(skillType) >= profile.getXpToLevel(skillType)) {
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
while (profile.getSkillXpLevelRaw(skillType) >= profile.getXpToLevel(skillType)) {
if ((skillType.getMaxLevel() >= profile.getSkillLevel(skillType) + 1) && (Config.getInstance().getPowerLevelCap() >= mcMMOPlayer.getPowerLevel() + 1)) {
int xp = profile.getXpToLevel(skillType);
xpRemoved += xp;
profile.removeXp(skillType, xp);
levelsGained++;
profile.skillUp(skillType, 1);
}
else {
profile.addLevels(skillType, 0);
}
}
McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType, levelsGained);
mcMMO.p.getServer().getPluginManager().callEvent(eventToFire);
if (eventToFire.isCancelled()) {
profile.modifySkill(skillType, profile.getSkillLevel(skillType) - levelsGained);
profile.setSkillXpLevel(skillType, profile.getSkillXpLevelRaw(skillType) + xpRemoved);
return;
}
String capitalized = StringUtils.getCapitalized(skillType.toString());
player.playSound(player.getLocation(), Sound.LEVEL_UP, Misc.LEVELUP_VOLUME, Misc.LEVELUP_PITCH);
player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", levelsGained, profile.getSkillLevel(skillType)));
}
return System.currentTimeMillis() >= (deactivatedTimeStamp + cooldown) * Misc.TIME_CONVERSION_FACTOR;
}
/**
@ -189,174 +73,14 @@ public class SkillUtils {
* @return true if this is a valid skill, false otherwise
*/
public static boolean isSkill(String skillName) {
if (!Config.getInstance().getLocale().equalsIgnoreCase("en_US")) {
return isLocalizedSkill(skillName);
}
return SkillType.getSkill(skillName) != null;
}
private 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 getSkillName(SkillType skill) {
if (!Config.getInstance().getLocale().equalsIgnoreCase("en_US")) {
return StringUtils.getCapitalized(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"));
}
return StringUtils.getCapitalized(skill.toString());
}
/**
* 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) {
return Permissions.skillEnabled(player, SkillType.AXES)
|| Permissions.skillEnabled(player, SkillType.ARCHERY)
|| Permissions.skillEnabled(player, SkillType.SWORDS)
|| Permissions.skillEnabled(player, SkillType.TAMING)
|| Permissions.skillEnabled(player, SkillType.UNARMED);
}
/**
* 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) {
return Permissions.skillEnabled(player, SkillType.EXCAVATION)
|| Permissions.skillEnabled(player, SkillType.FISHING)
|| Permissions.skillEnabled(player, SkillType.HERBALISM)
|| Permissions.skillEnabled(player, SkillType.MINING)
|| Permissions.skillEnabled(player, SkillType.WOODCUTTING);
}
/**
* 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) {
return Permissions.skillEnabled(player, SkillType.ACROBATICS)
|| Permissions.skillEnabled(player, SkillType.SMELTING)
|| Permissions.skillEnabled(player, SkillType.REPAIR);
}
/**
* Check to see if an ability can be activated.
*
* @param mcMMOPlayer The player activating the ability
* @param type The skill the ability is based on
*/
public static void abilityCheck(McMMOPlayer mcMMOPlayer, SkillType type) {
ToolType tool = type.getTool();
AbilityType ability = type.getAbility();
mcMMOPlayer.setToolPreparationMode(tool, false);
Player player = mcMMOPlayer.getPlayer();
PlayerProfile playerProfile = mcMMOPlayer.getProfile();
int timeRemaining = calculateTimeLeft(playerProfile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player);
/*
* 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 (!mcMMOPlayer.getAbilityMode(ability) && timeRemaining > 0) {
player.sendMessage(LocaleLoader.getString("Skills.TooTired", timeRemaining));
return;
}
}
if (!mcMMOPlayer.getAbilityMode(ability) && timeRemaining <= 0) {
McMMOPlayerAbilityActivateEvent event = new McMMOPlayerAbilityActivateEvent(player, type);
mcMMO.p.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
int ticks = PerksUtils.handleActivationPerks(player, 2 + (playerProfile.getSkillLevel(type) / AdvancedConfig.getInstance().getAbilityLength()), ability.getMaxLength());
ParticleEffectUtils.playAbilityEnabledEffect(player);
if (mcMMOPlayer.useChatNotifications()) {
player.sendMessage(ability.getAbilityOn());
}
SkillUtils.sendSkillMessage(player, ability.getAbilityPlayer(player));
playerProfile.setSkillDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR));
mcMMOPlayer.setAbilityMode(ability, true);
if (ability == AbilityType.SUPER_BREAKER || ability == AbilityType.GIGA_DRILL_BREAKER) {
handleAbilitySpeedIncrease(player);
}
new AbilityDisableTask(mcMMOPlayer, ability).runTaskLater(mcMMO.p, ticks * Misc.TICK_CONVERSION_FACTOR);
}
}
/**
* 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;
switch (ability) {
case BERSERK:
case BLOCK_CRACKER:
case LEAF_BLOWER:
if (!ability.blockCheck(block.getState())) {
activate = false;
break;
}
if (!blockBreakSimulate(block, player, true)) {
activate = false;
break;
}
break;
case GIGA_DRILL_BREAKER:
case SUPER_BREAKER:
case GREEN_TERRA:
if (!ability.blockCheck(block.getState())) {
activate = false;
break;
}
break;
default:
activate = false;
break;
}
return activate;
return Config.getInstance().getLocale().equalsIgnoreCase("en_US") ? SkillType.getSkill(skillName) != null : isLocalizedSkill(skillName);
}
public static void sendSkillMessage(Player player, String message) {
Location location = player.getLocation();
for (Player otherPlayer : player.getWorld().getPlayers()) {
if (otherPlayer != player && Misc.isNear(player.getLocation(), otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
if (otherPlayer != player && Misc.isNear(location, otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
otherPlayer.sendMessage(message);
}
}
@ -408,48 +132,39 @@ public class SkillUtils {
}
public static void handleAbilitySpeedDecrease(Player player) {
if (HiddenConfig.getInstance().useEnchantmentBuffs()) {
PlayerInventory playerInventory = player.getInventory();
if (!HiddenConfig.getInstance().useEnchantmentBuffs()) {
return;
}
for (int i = 0; i < playerInventory.getContents().length; i++) {
ItemStack item = playerInventory.getItem(i);
playerInventory.setItem(i, removeAbilityBuff(item));
}
for (ItemStack item : player.getInventory().getContents()) {
removeAbilityBuff(item);
}
}
public static ItemStack removeAbilityBuff(ItemStack item) {
if (item == null || item.getType() == Material.AIR) {
return item;
public static void removeAbilityBuff(ItemStack item) {
if (item == null || item.getType() == Material.AIR || (!ItemUtils.isPickaxe(item) && !ItemUtils.isShovel(item)) || !item.containsEnchantment(Enchantment.DIG_SPEED)) {
return;
}
if (!ItemUtils.isPickaxe(item) && !ItemUtils.isShovel(item)) {
return item;
}
ItemMeta itemMeta = item.getItemMeta();
if (item.containsEnchantment(Enchantment.DIG_SPEED)) {
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta.hasLore()) {
List<String> itemLore = itemMeta.getLore();
if (itemMeta.hasLore()) {
List<String> itemLore = itemMeta.getLore();
if (itemLore.remove("mcMMO Ability Tool")) {
int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
if (itemLore.remove("mcMMO Ability Tool")) {
int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
if (efficiencyLevel <= AdvancedConfig.getInstance().getEnchantBuff()) {
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
}
else {
itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - AdvancedConfig.getInstance().getEnchantBuff(), true);
}
itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta);
if (efficiencyLevel <= AdvancedConfig.getInstance().getEnchantBuff()) {
itemMeta.removeEnchant(Enchantment.DIG_SPEED);
}
else {
itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - AdvancedConfig.getInstance().getEnchantBuff(), true);
}
itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta);
}
}
return item;
}
/**
@ -458,53 +173,29 @@ public class SkillUtils {
* @param itemStack The ItemStack which durability should be modified
* @return the itemStack with modified durability
*/
public static ItemStack handleDurabilityChange(ItemStack itemStack, int durabilityModifier) {
short finalDurability = (short) (itemStack.getDurability() + durabilityModifier);
short maxDurability = itemStack.getType().getMaxDurability();
boolean overMax = (finalDurability >= maxDurability);
itemStack.setDurability(overMax ? maxDurability : finalDurability);
return itemStack;
}
/**
* Simulate a block break event.
*
* @param block The block to break
* @param player The player breaking the block
* @param shouldArmSwing true if an armswing event should be fired, false otherwise
* @return true if the event wasn't cancelled, false otherwise
*/
public static boolean blockBreakSimulate(Block block, Player player, boolean shouldArmSwing) {
PluginManager pluginManger = mcMMO.p.getServer().getPluginManager();
// Support for NoCheat
if (shouldArmSwing) {
pluginManger.callEvent(new FakePlayerAnimationEvent(player));
}
FakeBlockDamageEvent damageEvent = new FakeBlockDamageEvent(player, block, player.getItemInHand(), true);
pluginManger.callEvent(damageEvent);
FakeBlockBreakEvent breakEvent = new FakeBlockBreakEvent(block, player);
pluginManger.callEvent(breakEvent);
return !damageEvent.isCancelled() && !breakEvent.isCancelled();
public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) {
itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, itemStack.getType().getMaxDurability()));
}
public static boolean activationSuccessful(Player player, SkillType skill, double maxChance, int maxLevel) {
int skillLevel = UserManager.getPlayer(player).getProfile().getSkillLevel(skill);
int activationChance = PerksUtils.handleLuckyPerks(player, skill);
double chance = (maxChance / maxLevel) * Math.min(skillLevel, maxLevel);
return chance > Misc.getRandom().nextInt(activationChance);
return activationSuccessful(UserManager.getPlayer(player).getProfile().getSkillLevel(skill), PerksUtils.handleLuckyPerks(player, skill), maxChance, maxLevel);
}
public static boolean activationSuccessful(int skillLevel, int activationChance, double maxChance, int maxLevel) {
return ((maxChance / maxLevel) * Math.min(skillLevel, maxLevel)) > Misc.getRandom().nextInt(activationChance);
return (maxChance / maxLevel) * Math.min(skillLevel, maxLevel) > Misc.getRandom().nextInt(activationChance);
}
public static boolean treasureDropSuccessful(double dropChance, int activationChance) {
return dropChance > Misc.getRandom().nextDouble() * activationChance;
}
private static boolean isLocalizedSkill(String skillName) {
for (SkillType skill : SkillType.values()) {
if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"))) {
return true;
}
}
return false;
}
}