mcMMO/src/main/java/com/gmail/nossr50/m.java

259 lines
7.8 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50;
2012-03-26 17:04:17 +02:00
import java.util.Random;
2012-01-09 20:00:13 +01:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
2012-01-09 20:00:13 +01:00
import org.bukkit.block.Block;
2012-03-12 22:57:44 +01:00
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
2012-01-09 20:00:13 +01:00
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
2012-03-27 07:09:28 +02:00
import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
2012-01-09 20:00:13 +01:00
public class m {
2012-03-26 17:04:17 +02:00
private static Random random = new Random();
/**
* 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(m.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);
}
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;
}
}
2012-01-09 20:00:13 +01: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);
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 int getTier(ItemStack inHand) {
2012-03-08 22:17:57 +01:00
int tier = 0;
if (ItemChecks.isWoodTool(inHand)) {
2012-03-08 22:17:57 +01:00
tier = 1;
}
else if (ItemChecks.isStoneTool(inHand)) {
2012-03-08 22:17:57 +01:00
tier = 2;
}
else if (ItemChecks.isIronTool(inHand)) {
2012-03-08 22:17:57 +01:00
tier = 3;
}
else if(ItemChecks.isGoldTool(inHand)) {
2012-03-08 22:17:57 +01:00
tier = 1;
}
else if(ItemChecks.isDiamondTool(inHand))
2012-03-08 22:17:57 +01:00
tier = 4;
return tier;
}
2012-03-08 22:17:57 +01:00
/**
* 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.distance(second) < maxDistance) {
return true;
}
2012-03-08 22:17:57 +01:00
else {
return false;
2012-03-08 22:17:57 +01:00
}
}
2012-03-08 22:17:57 +01: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);
2012-03-08 22:17:57 +01:00
return true;
}
2012-03-08 22:17:57 +01:00
catch (NumberFormatException nFE) {
return false;
}
}
2012-03-08 22:17:57 +01:00
/**
* 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);
2012-03-08 22:17:57 +01:00
}
}
2012-03-08 22:17:57 +01: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 mcRandomDropItem(Location location, ItemStack is, double chance) {
2012-03-26 17:04:17 +02:00
if (random.nextInt(100) < chance) {
mcDropItem(location, is);
2012-03-08 22:17:57 +01:00
}
}
2012-03-08 22:17:57 +01: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
*/
public static void mcRandomDropItems(Location location, ItemStack is, int chance, int quantity) {
for(int i = 0; i < quantity; i++) {
mcRandomDropItem(location, is, chance);
2012-03-08 22:17:57 +01:00
}
}
2012-03-08 22:17:57 +01:00
/**
* 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) {
2012-03-08 22:17:57 +01: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);
Bukkit.getPluginManager().callEvent(event);
2012-01-09 20:00:13 +01:00
2012-03-08 22:17:57 +01:00
if (event.isCancelled()) {
return;
}
location.getWorld().dropItemNaturally(location, itemStack);
}
2012-03-08 22:17:57 +01: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) {
if (skillLevel > maxLevel) {
return maxLevel;
}
else {
return skillLevel;
}
}
2012-03-08 22:17:57 +01:00
}