123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package com.gmail.nossr50;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.block.Block;
- import org.bukkit.entity.LivingEntity;
- import org.bukkit.entity.Player;
- import org.bukkit.event.entity.EntityDamageEvent;
- import org.bukkit.event.player.PlayerAnimationEvent;
- import org.bukkit.inventory.ItemStack;
- import com.gmail.nossr50.config.LoadProperties;
- import com.gmail.nossr50.datatypes.PlayerProfile;
- import com.gmail.nossr50.datatypes.SkillType;
- import com.gmail.nossr50.events.FakeBlockBreakEvent;
- import com.gmail.nossr50.events.McMMOItemSpawnEvent;
- import com.gmail.nossr50.runnables.SQLConversionTask;
- public class m {
- /**
- * Gets a capitalized version of the target string.
- *
- * @param target String to capitalize
- * @return the capitalized string
- */
- public static String getCapitalized(String target) {
- String firstLetter = target.substring(0,1);
- String remainder = target.substring(1);
- String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
- return capitalized;
- }
- /**
- * Gets the int represented by this string.
- *
- * @param string The string to parse
- * @return the int represented by this string
- */
- public static int getInt(String string) {
- if (isInt(string)) {
- return Integer.parseInt(string);
- }
- else {
- return 0;
- }
- }
- /**
- * Checks to see if an entity is currently invincible.
- *
- * @param le The LivingEntity to check
- * @param event The event the entity is involved in
- * @return true if the entity is invincible, false otherwise
- */
- public static boolean isInvincible(LivingEntity le, EntityDamageEvent event) {
- /*
- * So apparently if you do more damage to a LivingEntity than its last damage int you bypass the invincibility.
- * So yeah, this is for that.
- */
- if (le.getNoDamageTicks() > le.getMaximumNoDamageTicks() / 2.0F && event.getDamage() <= le.getLastDamage()) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * Gets the power level of a player.
- *
- * @param player The player to get the power level of
- * @param PP The profile of the player
- * @return the power level of the player
- */
- public static int getPowerLevel(Player player, PlayerProfile PP) {
- int powerLevel = 0;
- for (SkillType type : SkillType.values()) {
- if (type.getPermissions(player)) {
- powerLevel += PP.getSkillLevel(type);
- }
- }
- return powerLevel;
- }
- /**
- * 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) {
- //Support for NoCheat
- if (shouldArmSwing) {
- PlayerAnimationEvent armswing = new PlayerAnimationEvent(player);
- Bukkit.getPluginManager().callEvent(armswing);
- }
- FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
- Bukkit.getPluginManager().callEvent(event);
- if (!event.isCancelled()) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * Get the upgrade tier of the item in hand.
- *
- * @param inHand The item to check the tier of
- * @return the tier of the item
- */
- public static Integer getTier(ItemStack inHand) {
- int tier = 0;
- if (ItemChecks.isWoodTool(inHand)) {
- tier = 1;
- }
- else if (ItemChecks.isStoneTool(inHand)) {
- tier = 2;
- }
- else if (ItemChecks.isIronTool(inHand)) {
- tier = 3;
- }
- else if(ItemChecks.isGoldTool(inHand)) {
- tier = 1;
- }
- else if(ItemChecks.isDiamondTool(inHand))
- tier = 4;
- return tier;
- }
- /**
- * Determine if two locations are near each other.
- *
- * @param first The first location
- * @param second The second location
- * @param maxDistance The max distance apart
- * @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
- */
- public static boolean isNear(Location first, Location second, int maxDistance) {
- double relX = first.getX() - second.getX();
- double relY = first.getY() - second.getY();
- double relZ = first.getZ() - second.getZ();
- double dist = (relX * relX) + (relY * relY) + (relZ * relZ);
- if (dist < maxDistance * maxDistance) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * Determine if a string represents an Integer
- *
- * @param string String to check
- * @return true if the string is an Integer, false otherwise
- */
- public static boolean isInt(String string) {
- try {
- Integer.parseInt(string);
- return true;
- }
- catch (NumberFormatException nFE) {
- return false;
- }
- }
- /**
- * Drop items at a given location.
- *
- * @param location The location to drop the items at
- * @param is The items to drop
- * @param quantity The amount of items to drop
- */
- public static void mcDropItems(Location location, ItemStack is, int quantity) {
- for (int i = 0; i < quantity; i++) {
- mcDropItem(location, is);
- }
- }
- /**
- * Randomly drop an item at a given location.
- *
- * @param location The location to drop the items at
- * @param is The item to drop
- * @param chance The percentage chance for the item to drop
- */
- public static void mcRandomDropItem(Location location, ItemStack is, double chance) {
- if (Math.random() * 100 < chance) {
- mcDropItem(location, is);
- }
- }
- /**
- * Randomly drop items at a given location.
- *
- * @param location The location to drop the items at
- * @param is The item to drop
- * @param chance The percentage chance for the item to drop
- * @param quantity The amount of items to drop
- */
- public static void mcRandomDropItems(Location location, ItemStack is, int chance, int quantity) {
- for(int i = 0; i < quantity; i++) {
- mcRandomDropItem(location, is, chance);
- }
- }
- /**
- * Drop an item at a given location.
- *
- * @param location The location to drop the item at
- * @param itemStack The item to drop
- */
- public static void mcDropItem(Location location, ItemStack itemStack) {
- // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
- McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
- Bukkit.getPluginManager().callEvent(event);
- if (event.isCancelled()) {
- return;
- }
- else {
- location.getWorld().dropItemNaturally(location, itemStack);
- }
- }
- /**
- * Convert FlatFile data to MySQL data.
- */
- public static void convertToMySQL() {
- if (!LoadProperties.useMySQL) {
- return;
- }
- Bukkit.getScheduler().scheduleAsyncDelayedTask(Bukkit.getPluginManager().getPlugin("mcMMO"), new SQLConversionTask(), 1);
- }
- public static int skillCheck(int skillLevel, int maxLevel) {
- if (skillLevel > maxLevel) {
- return maxLevel;
- }
- else {
- return skillLevel;
- }
- }
- }
|