QUOTE: Life is tough, but so are you.

poppy

A feature-rich Minecraft plugin which enhances gaming experience

Utils.java (5648B)


      1package poppy;
      2
      3import org.bukkit.Bukkit;
      4import org.bukkit.Location;
      5import org.bukkit.Material;
      6import org.bukkit.block.Block;
      7import org.bukkit.block.BlockFace;
      8import org.bukkit.entity.Player;
      9import org.bukkit.entity.Entity;
     10import org.bukkit.inventory.Inventory;
     11import org.bukkit.inventory.ItemStack;
     12import org.bukkit.plugin.Plugin;
     13
     14import net.md_5.bungee.api.ChatColor;
     15import net.md_5.bungee.api.ChatMessageType;
     16import net.md_5.bungee.api.chat.TextComponent;
     17
     18
     19public class Utils
     20{
     21	public static boolean isEmpty(ItemStack item)
     22	{
     23		return item == null || item.getType() == Material.AIR;
     24	}
     25
     26	public static boolean isType(ItemStack item, Material type)
     27	{
     28		return !isEmpty(item) && item.getType() == type;
     29	}
     30
     31	public static boolean compare(ItemStack item, ItemStack item2)
     32	{
     33		if(item == null || item2 == null)
     34		{
     35			return false;
     36		}
     37
     38		if(item.getType() != item2.getType())
     39		{
     40			return false;
     41		}
     42
     43		return true;
     44	}
     45
     46	public static boolean isInventoryFull(Inventory inventory, ItemStack result)
     47	{
     48		for(ItemStack item : inventory.getContents())
     49		{
     50			if(Utils.isEmpty(item))
     51			{
     52				return false;
     53			}
     54
     55			if(result.isSimilar(item))
     56			{
     57				if(result.getAmount() + item.getAmount() <= item.getMaxStackSize())
     58				{
     59					return false;
     60				}
     61			}
     62		}
     63
     64		return true;
     65	}
     66
     67	public static boolean containsAtLeast(Inventory inventory, ItemStack item, int amount)
     68	{
     69		for(int i = 0; i < inventory.getSize(); i++)
     70		{
     71			ItemStack current = inventory.getItem(i);
     72
     73			if(Utils.compare(item, current))
     74			{
     75				amount -= current.getAmount();
     76
     77				if(amount <= 0)
     78				{
     79					return true;
     80				}
     81			}
     82		}
     83
     84		return false;
     85	}
     86
     87	public static void sendActionMessage(Player player, String message)
     88	{
     89		player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacy(message));
     90	}
     91
     92	public static void sendCountdown(Plugin plugin, final String message, final int time, Runnable runnable)
     93	{
     94		Bukkit.getScheduler().runTaskTimer(plugin, new Runnable()
     95		{
     96			int count = time;
     97			
     98			@Override
     99			public void run()
    100			{
    101				if(count == 0)
    102				{
    103					runnable.run();
    104					return;
    105				}
    106				
    107				Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + message + " " + count + " sec");
    108				
    109				count--;
    110			}
    111		}, 0L, Constants.TICK);
    112	}
    113
    114	public static Float convertFacingToYaw(BlockFace face)
    115	{
    116		switch(face)
    117		{
    118			case NORTH:
    119				return 0F;
    120			case EAST:
    121				return 90F;
    122			case SOUTH:
    123				return 180F;
    124			case WEST:
    125				return -90F;
    126			default:
    127				break;
    128		}
    129
    130		return null;
    131	}
    132
    133	public static boolean isAir(Material material)
    134	{
    135		return material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR;
    136	}
    137
    138	public static boolean isFarmable(Material material)
    139	{
    140		return material == Material.WHEAT || material == Material.CARROTS || material == Material.POTATOES || material == Material.BEETROOTS || material == Material.NETHER_WART || material == Material.COCOA;
    141	}
    142
    143	public static boolean isStem(Material material)
    144	{
    145		return material == Material.ATTACHED_PUMPKIN_STEM || material == Material.ATTACHED_MELON_STEM;
    146	}
    147
    148	public static boolean hasStem(Block block)
    149	{
    150		Location blockLocation = block.getLocation();
    151		Material blockNorth = blockLocation.clone().add(0, 0, -1).getBlock().getType();
    152		Material blockEast = blockLocation.clone().add(1, 0, 0).getBlock().getType();
    153		Material blockSouth = blockLocation.clone().add(0, 0, 1).getBlock().getType();
    154		Material blockWest = blockLocation.clone().add(-1, 0, 0).getBlock().getType();
    155
    156		return isStem(blockNorth) || isStem(blockEast) || isStem(blockSouth) || isStem(blockWest);
    157	}
    158	
    159	public static Location calcBlockCenter(final Location location)
    160	{
    161		Location centered = location.clone();
    162		centered.setX(Location.locToBlock(location.getX()) + 0.5);
    163		centered.setY(Location.locToBlock(location.getY()));
    164		centered.setZ(Location.locToBlock(location.getZ()) + 0.5);
    165
    166		return centered;
    167	}
    168
    169	public static int calcTravelFee(final Location from, final Location to)
    170	{
    171		if(!isLocationInSameWorld(from, to))
    172		{
    173			return 0;
    174		}
    175	
    176		final Double distance = from.distance(to);
    177		int fee = (int) Math.floor(distance / (Constants.BLOCKS_PER_CHUNK * Constants.CHUNKS_PER_EMERALD));
    178
    179		if(fee < Constants.MIN_FEE)
    180		{
    181			fee = Constants.MIN_FEE;
    182		}
    183
    184		if(fee > Constants.MAX_FEE)
    185		{
    186			fee = Constants.MAX_FEE;
    187		}
    188
    189		return fee;
    190	}
    191
    192	public static boolean travelTo(final Plugin plugin, final Player player, final Location location)
    193	{
    194		if(!isLocationInSameWorld(player.getLocation(), location))
    195		{
    196			player.sendMessage(ChatColor.RED + "The warppoint cannot be located in this world!");
    197			return false;
    198		}
    199
    200		if(player.getVehicle() != null)
    201		{
    202			Entity vehicle = player.getVehicle();
    203			vehicle.eject();
    204			player.teleport(location);
    205
    206			Bukkit.getScheduler().runTaskLater(plugin, () -> {
    207				vehicle.teleport(location);
    208			}, 3);
    209
    210			Bukkit.getScheduler().runTaskLater(plugin, () -> {
    211				vehicle.addPassenger(player);
    212			}, 6);
    213		}
    214		else
    215		{
    216			player.teleport(location);
    217		}
    218
    219		return true;
    220	}
    221
    222	public static boolean payFee(final Player player, final Material unit, final int amount)
    223	{
    224		final ItemStack item = player.getInventory().getItemInMainHand();
    225
    226		if(item.getType() != unit || item.getAmount() < amount)
    227		{
    228			return false;
    229		}
    230
    231		item.setAmount(item.getAmount() - amount);
    232		
    233		return true;
    234	}
    235
    236	public static boolean isLocationInSameWorld(final Location from, final Location to)
    237	{
    238		return from.getWorld().getName().equals(to.getWorld().getName());
    239	}
    240
    241	public static boolean randomlyReduceDurability(final int level)
    242	{
    243		final float chance = 100.0f / (level + 1);
    244		return Math.random() * 100 <= chance;
    245	}
    246}