2012-04-27 11:47:11 +02:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
2013-08-19 10:37:04 +02:00
|
|
|
import java.util.ArrayList;
|
2013-05-15 16:23:09 +02:00
|
|
|
import java.util.Collection;
|
2013-06-12 21:41:26 +02:00
|
|
|
import java.util.List;
|
2012-04-27 11:47:11 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2013-05-16 15:31:30 +02:00
|
|
|
import org.bukkit.Chunk;
|
2012-04-27 11:47:11 +02:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
2013-06-12 21:10:43 +02:00
|
|
|
import org.bukkit.block.Block;
|
2013-06-12 21:56:42 +02:00
|
|
|
import org.bukkit.block.BlockState;
|
2013-06-12 21:10:43 +02:00
|
|
|
import org.bukkit.block.Furnace;
|
2013-01-24 21:46:29 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2013-04-28 22:50:41 +02:00
|
|
|
import org.bukkit.entity.HumanEntity;
|
2013-08-22 20:45:30 +02:00
|
|
|
import org.bukkit.entity.NPC;
|
2013-03-12 21:25:42 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2013-06-12 21:10:43 +02:00
|
|
|
import org.bukkit.event.inventory.InventoryEvent;
|
|
|
|
import org.bukkit.inventory.FurnaceInventory;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
2012-04-27 11:47:11 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-06-12 21:41:26 +02:00
|
|
|
import org.bukkit.metadata.MetadataValue;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2012-06-07 00:02:22 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2012-04-27 11:47:11 +02:00
|
|
|
import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
|
2013-03-12 21:25:42 +01:00
|
|
|
import com.gmail.nossr50.util.player.UserManager;
|
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();
|
2013-06-13 01:26:09 +02:00
|
|
|
|
2013-01-10 03:44:53 +01:00
|
|
|
public static final int TIME_CONVERSION_FACTOR = 1000;
|
2013-06-13 01:26:09 +02:00
|
|
|
public static final int TICK_CONVERSION_FACTOR = 20;
|
|
|
|
|
|
|
|
public static final int PLAYER_RESPAWN_COOLDOWN_SECONDS = 5;
|
2013-01-10 03:44:53 +01:00
|
|
|
public static final double SKILL_MESSAGE_MAX_SENDING_DISTANCE = 10.0;
|
2013-03-01 06:52:01 +01:00
|
|
|
|
|
|
|
// Sound Pitches & Volumes from CB
|
2013-09-16 15:52:09 +02:00
|
|
|
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
|
2013-03-01 06:52:01 +01:00
|
|
|
public static final float FIZZ_VOLUME = 0.5F;
|
|
|
|
public static final float POP_VOLUME = 0.2F;
|
2013-03-14 22:26:02 +01:00
|
|
|
public static final float BAT_VOLUME = 1.0F;
|
|
|
|
public static final float BAT_PITCH = 0.6F;
|
2013-05-02 03:57:42 +02:00
|
|
|
public static final float GHAST_VOLUME = 1.0F;
|
2013-09-16 15:52:09 +02:00
|
|
|
public static final float LEVELUP_PITCH = 0.5F; // Reduced to differentiate between vanilla level-up
|
|
|
|
public static final float LEVELUP_VOLUME = 0.75F; // Use max volume always
|
2013-01-30 00:36:16 +01:00
|
|
|
|
2013-01-26 23:01:55 +01:00
|
|
|
private Misc() {};
|
|
|
|
|
2013-04-24 17:05:40 +02:00
|
|
|
public static float getFizzPitch() {
|
|
|
|
return 2.6F + (getRandom().nextFloat() - getRandom().nextFloat()) * 0.8F;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float getPopPitch() {
|
|
|
|
return ((getRandom().nextFloat() - getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F;
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:57:42 +02:00
|
|
|
public static float getGhastPitch() {
|
|
|
|
return (getRandom().nextFloat() - getRandom().nextFloat()) * 0.2F + 1.0F;
|
|
|
|
}
|
|
|
|
|
2013-01-24 21:46:29 +01:00
|
|
|
public static boolean isNPCEntity(Entity entity) {
|
2013-08-22 20:45:30 +02:00
|
|
|
return (entity == null || entity.hasMetadata("NPC") || entity instanceof NPC || (mcMMO.isCombatTagEnabled() && entity instanceof HumanEntity && ((HumanEntity) entity).getName().contains("PvpLogger")));
|
2013-01-24 21:46:29 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (ItemUtils.isWoodTool(inHand)) {
|
2012-04-27 11:47:11 +02:00
|
|
|
tier = 1;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isStoneTool(inHand)) {
|
2012-04-27 11:47:11 +02:00
|
|
|
tier = 2;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isIronTool(inHand)) {
|
2012-04-27 11:47:11 +02:00
|
|
|
tier = 3;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isGoldTool(inHand)) {
|
2012-04-27 11:47:11 +02:00
|
|
|
tier = 1;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isDiamondTool(inHand)) {
|
2012-04-27 11:47:11 +02:00
|
|
|
tier = 4;
|
2012-05-18 03:19:17 +02:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ModUtils.isCustomTool(inHand)) {
|
|
|
|
tier = ModUtils.getToolFromItemStack(inHand).getTier();
|
2012-05-18 03:19:17 +02:00
|
|
|
}
|
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
|
2013-08-10 20:10:45 +02:00
|
|
|
* @return true if the distance between {@code first} and {@code second} is less than {@code maxDistance}, false otherwise
|
2012-04-27 11:47:11 +02:00
|
|
|
*/
|
|
|
|
public static boolean isNear(Location first, Location second, double maxDistance) {
|
2013-03-10 22:07:07 +01:00
|
|
|
if (first.getWorld() != second.getWorld()) {
|
2012-04-27 11:47:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-17 01:53:58 +02:00
|
|
|
return first.distanceSquared(second) < (maxDistance * maxDistance) || maxDistance == 0;
|
2013-05-16 15:23:35 +02:00
|
|
|
}
|
2013-01-10 04:43:21 +01:00
|
|
|
|
2013-05-16 15:23:35 +02:00
|
|
|
public static void dropItems(Location location, Collection<ItemStack> drops) {
|
|
|
|
for (ItemStack drop : drops) {
|
|
|
|
dropItem(location, drop);
|
|
|
|
}
|
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
|
|
|
|
*/
|
2012-11-13 04:10:14 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-10 20:10:45 +02:00
|
|
|
* Drop items with random quantity at a given location.
|
2012-04-27 11:47:11 +02:00
|
|
|
*
|
|
|
|
* @param location The location to drop the items at
|
|
|
|
* @param is 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) {
|
2013-02-03 09:12:34 +01:00
|
|
|
if (itemStack.getType() == Material.AIR) {
|
2013-01-14 20:31:11 +01:00
|
|
|
return;
|
2013-02-01 20:58:03 +01:00
|
|
|
}
|
2013-01-14 20:31:11 +01:00
|
|
|
|
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);
|
2012-06-07 00:02:22 +02:00
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (event.isCancelled()) {
|
2012-04-27 11:47:11 +02:00
|
|
|
return;
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-05-16 15:23:35 +02:00
|
|
|
location.getWorld().dropItemNaturally(location, itemStack);
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2013-03-12 21:25:42 +01:00
|
|
|
public static void profileCleanup(String playerName) {
|
2013-08-20 20:10:49 +02:00
|
|
|
UserManager.remove(playerName);
|
2013-03-12 21:25:42 +01:00
|
|
|
|
2013-09-16 18:12:31 +02:00
|
|
|
Player player = mcMMO.p.getServer().getPlayerExact(playerName);
|
|
|
|
|
|
|
|
if (player != null) {
|
2013-08-20 20:10:49 +02:00
|
|
|
UserManager.addUser(player);
|
2013-03-12 21:25:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 23:29:31 +01:00
|
|
|
public static void resendChunkRadiusAt(Player player, int radius) {
|
2013-05-16 15:31:30 +02:00
|
|
|
Chunk chunk = player.getLocation().getChunk();
|
|
|
|
int chunkX = chunk.getX();
|
|
|
|
int chunkZ = chunk.getZ();
|
|
|
|
|
|
|
|
for (int x = chunkX - radius; x < chunkX + radius; x++) {
|
|
|
|
for (int z = chunkZ - radius; z < chunkZ + radius; z++) {
|
2013-03-29 23:29:31 +01:00
|
|
|
player.getWorld().refreshChunk(x, z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 21:10:43 +02:00
|
|
|
public static Block processInventoryOpenorCloseEvent(InventoryEvent event) {
|
|
|
|
Inventory inventory = event.getInventory();
|
|
|
|
|
|
|
|
if (!(inventory instanceof FurnaceInventory)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Furnace furnace = (Furnace) inventory.getHolder();
|
|
|
|
|
|
|
|
if (furnace == null || furnace.getBurnTime() != 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return furnace.getBlock();
|
|
|
|
}
|
|
|
|
|
2013-06-12 21:41:26 +02:00
|
|
|
public static Player getPlayerFromFurnace(Block furnaceBlock) {
|
|
|
|
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
|
|
|
|
2013-06-12 21:56:42 +02:00
|
|
|
if (metadata.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getSmeltingFromFurnace(Block furnaceBlock) {
|
|
|
|
BlockState furnaceState = furnaceBlock.getState();
|
|
|
|
|
|
|
|
if (!(furnaceState instanceof Furnace)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((Furnace) furnaceState).getInventory().getSmelting();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getResultFromFurnace(Block furnaceBlock) {
|
|
|
|
BlockState furnaceState = furnaceBlock.getState();
|
|
|
|
|
|
|
|
if (!(furnaceState instanceof Furnace)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((Furnace) furnaceState).getInventory().getResult();
|
2013-06-12 21:41:26 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 10:37:04 +02:00
|
|
|
/**
|
|
|
|
* Attempts to match any player names with the given name, and returns a list of all possibly matches.
|
|
|
|
*
|
|
|
|
* This list is not sorted in any particular order.
|
|
|
|
* If an exact match is found, the returned list will only contain a single result.
|
|
|
|
*
|
|
|
|
* @param partialName Name to match
|
|
|
|
* @return List of all possible names
|
|
|
|
*/
|
|
|
|
public static List<String> matchPlayer(String partialName) {
|
|
|
|
List<String> matchedPlayers = new ArrayList<String>();
|
|
|
|
|
2013-08-20 20:10:49 +02:00
|
|
|
for (String playerName : mcMMO.getDatabaseManager().getStoredUsers()) {
|
|
|
|
if (partialName.equalsIgnoreCase(playerName)) {
|
2013-08-19 10:37:04 +02:00
|
|
|
// Exact match
|
|
|
|
matchedPlayers.clear();
|
2013-08-20 20:10:49 +02:00
|
|
|
matchedPlayers.add(playerName);
|
2013-08-19 10:37:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-08-20 20:10:49 +02:00
|
|
|
if (playerName.toLowerCase().contains(partialName.toLowerCase())) {
|
2013-08-19 10:37:04 +02:00
|
|
|
// Partial match
|
2013-08-20 20:10:49 +02:00
|
|
|
matchedPlayers.add(playerName);
|
2013-08-19 10:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchedPlayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a matched player name if one was found in the database.
|
|
|
|
*
|
|
|
|
* @param partialName Name to match
|
|
|
|
* @return Matched name or {@code partialName} if no match was found
|
|
|
|
*/
|
|
|
|
public static String getMatchedPlayerName(String partialName) {
|
|
|
|
List<String> matches = matchPlayer(partialName);
|
|
|
|
|
|
|
|
if (matches.size() == 1) {
|
|
|
|
partialName = matches.get(0);
|
|
|
|
}
|
2013-08-20 20:10:49 +02:00
|
|
|
|
2013-08-19 10:37:04 +02:00
|
|
|
return partialName;
|
|
|
|
}
|
|
|
|
|
2013-01-10 05:46:35 +01:00
|
|
|
public static Random getRandom() {
|
|
|
|
return random;
|
|
|
|
}
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|