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

165 lines
5.2 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.entity.Entity;
import org.bukkit.entity.Item;
2012-04-27 11:47:11 +02:00
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
2013-01-30 17:35:33 +01:00
import com.gmail.nossr50.mods.ModChecks;
2012-04-27 11:47:11 +02:00
2013-01-26 23:01:55 +01:00
public final class Misc {
2012-04-27 11:47:11 +02:00
private static Random random = new Random();
public static boolean isSpawnerXPEnabled = Config.getInstance().getExperienceGainsMobspawnersEnabled();
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;
//Sound Pitches & Volumes from CB
public static final float ANVIL_USE_PITCH = 0.3F; // Not in CB directly, I went off the place sound values
public static final float ANVIL_USE_VOLUME = 1.0F; // Not in CB directly, I went off the place sound values
public static final float FIZZ_PITCH = 2.6F + (Misc.getRandom().nextFloat() - Misc.getRandom().nextFloat()) * 0.8F;
public static final float FIZZ_VOLUME = 0.5F;
public static final float POP_PITCH = ((getRandom().nextFloat() - getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F;
public static final float POP_VOLUME = 0.2F;
2013-01-26 23:01:55 +01:00
private Misc() {};
public static boolean isNPCEntity(Entity entity) {
if (entity == null || entity.hasMetadata("NPC")) {
return true;
}
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
}
/**
* 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
*/
2013-02-17 07:26:47 +01:00
public static void randomDropItems(Location location, ItemStack is, int quantity) {
int dropCount = random.nextInt(quantity + 1);
if (dropCount > 0) {
is.setAmount(dropCount);
dropItem(location, is);
}
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
}
public static Random getRandom() {
return random;
}
2012-04-27 11:47:11 +02:00
}