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

357 lines
11 KiB
Java
Raw Normal View History

2012-04-27 11:47:11 +02:00
package com.gmail.nossr50.util;
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Item;
2012-04-27 11:47:11 +02:00
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
2012-04-27 11:47:11 +02:00
import org.bukkit.event.entity.EntityDamageEvent;
2013-01-15 22:16:46 +01:00
import org.bukkit.inventory.EntityEquipment;
2012-04-27 11:47:11 +02:00
import org.bukkit.inventory.ItemStack;
2012-06-04 14:51:10 +02:00
import org.bukkit.plugin.PluginManager;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.mcMMO;
2013-01-15 22:16:46 +01:00
import com.gmail.nossr50.config.AdvancedConfig;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
import com.gmail.nossr50.party.PartyManager;
2012-04-27 11:47:11 +02:00
public class Misc {
private static Random random = new Random();
2013-01-15 22:16:46 +01:00
public static int toolDurabilityLoss = Config.getInstance().getAbilityToolDamage();
public static int abilityLengthIncreaseLevel = AdvancedConfig.getInstance().getAbilityLength();
public static final int PLAYER_RESPAWN_COOLDOWN_SECONDS = 5;
2013-01-10 03:44:53 +01:00
public static final int TIME_CONVERSION_FACTOR = 1000;
public static final double SKILL_MESSAGE_MAX_SENDING_DISTANCE = 10.0;
2013-01-15 22:16:46 +01:00
/**
* Check if a player has armor.
*
* @param player Player whose armor to check
* @return true if the player has armor, false otherwise
*/
public static boolean hasArmor(LivingEntity entity) {
EntityEquipment equipment = entity.getEquipment();
if (equipment.getBoots() != null || equipment.getChestplate() != null || equipment.getHelmet() != null || equipment.getLeggings() != null) {
return true;
}
return false;
}
public static boolean isFriendlyPet(Player attacker, Tameable pet) {
if (pet.isTamed()) {
AnimalTamer tamer = pet.getOwner();
if (tamer instanceof Player) {
Player owner = (Player) tamer;
if (owner == attacker || PartyManager.getInstance().inSameParty(attacker, owner)) {
return true;
}
}
}
return false;
}
public static boolean isNPC(Player player) {
2013-01-08 23:26:11 +01:00
if (player == null || Users.getProfile(player) == null || player.hasMetadata("NPC")) {
2013-01-08 23:24:09 +01:00
return true;
}
return false;
}
2013-01-08 18:52:16 +01:00
2013-01-10 03:44:53 +01:00
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);
}
}
}
2012-04-27 11:47:11 +02:00
/**
* 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 a nicely formatted string version of an item name from a given item ID.
*
* @param itemID The ID of the item to convert to string.
* @return the nicely formatting string
*/
public static String prettyItemString(int itemID) {
String baseString = Material.getMaterial(itemID).toString();
String[] substrings = baseString.split("_");
String prettyString = "";
int size = 1;
for (String s : substrings) {
prettyString = prettyString.concat(Misc.getCapitalized(s));
if (size < substrings.length) {
prettyString = prettyString.concat(" ");
}
size++;
}
return prettyString;
}
/**
* 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);
}
2013-01-10 04:43:21 +01:00
return 0;
2012-04-27 11:47:11 +02:00
}
/**
* 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;
}
2013-01-10 04:43:21 +01:00
return false;
2012-04-27 11:47:11 +02:00
}
2012-06-05 15:57:10 +02:00
2012-04-27 11:47:11 +02:00
/**
* 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) {
FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player);
mcMMO.p.getServer().getPluginManager().callEvent(armswing);
2012-04-27 11:47:11 +02:00
}
2012-06-04 14:51:10 +02:00
PluginManager pluginManger = mcMMO.p.getServer().getPluginManager();
2012-06-04 14:51:10 +02:00
FakeBlockDamageEvent damageEvent = new FakeBlockDamageEvent(player, block, player.getItemInHand(), true);
2012-06-04 14:51:10 +02:00
pluginManger.callEvent(damageEvent);
FakeBlockBreakEvent breakEvent = new FakeBlockBreakEvent(block, player);
2012-06-04 14:51:10 +02:00
pluginManger.callEvent(breakEvent);
2012-04-27 11:47:11 +02:00
if (!damageEvent.isCancelled() && !breakEvent.isCancelled()) {
2012-04-27 11:47:11 +02:00
return true;
2012-06-04 14:51:10 +02:00
}
2013-01-10 04:43:21 +01:00
return false;
2012-04-27 11:47:11 +02:00
}
/**
* 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 int 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)) {
2012-04-27 11:47:11 +02:00
tier = 1;
}
else if (ItemChecks.isDiamondTool(inHand)) {
2012-04-27 11:47:11 +02:00
tier = 4;
}
2012-05-18 20:29:53 +02:00
else if (ModChecks.isCustomTool(inHand)) {
tier = ModChecks.getToolFromItemStack(inHand).getTier();
}
2012-04-27 11:47:11 +02:00
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, double maxDistance) {
if (!first.getWorld().equals(second.getWorld())) {
return false;
}
if (first.distanceSquared(second) < (maxDistance * maxDistance)) {
return true;
}
2013-01-10 04:43:21 +01:00
return false;
2012-04-27 11:47:11 +02:00
}
/**
* 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
*/
2012-05-18 19:40:21 +02:00
public static void dropItems(Location location, ItemStack is, int quantity) {
2012-04-27 11:47:11 +02:00
for (int i = 0; i < quantity; i++) {
2012-05-18 19:40:21 +02:00
dropItem(location, is);
2012-04-27 11:47:11 +02:00
}
}
/**
* 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 randomDropItem(Location location, ItemStack is, int chance) {
2012-04-27 11:47:11 +02:00
if (random.nextInt(100) < chance) {
2012-05-18 19:40:21 +02:00
dropItem(location, is);
2012-04-27 11:47:11 +02:00
}
}
/**
* 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
*/
2012-05-18 19:40:21 +02:00
public static void randomDropItems(Location location, ItemStack is, int chance, int quantity) {
2013-01-10 05:07:32 +01:00
for (int i = 0; i < quantity; i++) {
2012-05-18 19:40:21 +02:00
randomDropItem(location, is, chance);
2012-04-27 11:47:11 +02:00
}
}
/**
* Drop an item at a given location.
*
* @param location The location to drop the item at
* @param itemStack The item to drop
*/
2012-05-18 19:40:21 +02:00
public static void dropItem(Location location, ItemStack itemStack) {
2012-04-27 11:47:11 +02:00
if(itemStack.getType() == Material.AIR)
return;
2012-04-27 11:47:11 +02:00
// 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);
mcMMO.p.getServer().getPluginManager().callEvent(event);
2012-04-27 11:47:11 +02:00
if (event.isCancelled())
2012-04-27 11:47:11 +02:00
return;
Item newItem = location.getWorld().dropItemNaturally(location, itemStack);
ItemStack cloned = itemStack.clone();
cloned.setAmount(newItem.getItemStack().getAmount());
newItem.setItemStack(cloned);
2012-04-27 11:47:11 +02:00
}
/**
* 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) {
2012-06-15 03:10:47 +02:00
//TODO: Could we just use Math.min(skillLevel, maxLevel) here?
2012-04-27 11:47:11 +02:00
if (skillLevel > maxLevel) {
return maxLevel;
}
2013-01-10 04:43:21 +01:00
return skillLevel;
2012-04-27 11:47:11 +02:00
}
2012-06-04 15:03:25 +02:00
/**
* Get the max power level for a player.
*
* @return the maximum power level for a player
*/
2012-04-27 11:47:11 +02:00
public static int getPowerLevelCap() {
2012-05-21 13:53:52 +02:00
int levelCap = Config.getInstance().getPowerLevelCap();
if (levelCap > 0) {
return levelCap;
2012-04-27 11:47:11 +02:00
}
2013-01-10 04:43:21 +01:00
return Integer.MAX_VALUE;
2012-04-27 11:47:11 +02:00
}
public static Random getRandom() {
return random;
}
2012-04-27 11:47:11 +02:00
}