Core/src/de/joethei/core/api/Utils.java

179 lines
5.4 KiB
Java

package de.joethei.core.api;
import java.io.File;
import java.util.List;
import java.util.Random;
import net.minecraft.server.v1_8_R3.EnumParticle;
import net.minecraft.server.v1_8_R3.PacketPlayOutWorldParticles;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Effect;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector;
public class Utils
{
public static void deleteFolder(File folder)
{
File[] files = folder.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory())
deleteFolder(f);
else {
f.delete();
}
}
}
folder.delete();
}
public static void PlaySoundToAll(Sound s) {
for (Player all : Bukkit.getOnlinePlayers())
all.playSound(all.getLocation(), s, 3.0F, 3.0F);
}
@SuppressWarnings("deprecation")
public static void PlayEffectToAll(Effect s)
{
for (Player all : Bukkit.getOnlinePlayers())
all.playEffect(all.getLocation(), s, 3);
}
public static boolean isSpigot() {
return Bukkit.getVersion().contains("Spigot");
}
public static void deleteDir(File file) {
if (file.isDirectory()) {
if (file.list().length == 0) {
file.delete();
} else {
String[] files = file.list();
for (String tmp : files) {
File fileDelete = new File(file, tmp);
deleteDir(fileDelete);
}
if (file.list().length == 0)
file.delete();
}
}
else
file.delete();
}
public static double random(int low, int high) {
return Math.random() * (high - low) + low;
}
@SuppressWarnings("deprecation")
public static ItemStack createItemStack(int id, int anzahl, int sh, String name) {
ItemStack item = new ItemStack(id, anzahl, (short)sh);
ItemMeta im = item.getItemMeta();
im.setDisplayName(name);
item.setItemMeta(im);
return item;
}
public static boolean emptyInventory(Player p) {
for (ItemStack item : p.getInventory().getContents()) {
if ((item != null) &&
(item.getType() != Material.AIR))
return false;
}
for (ItemStack item : p.getInventory().getArmorContents()) {
if ((item != null) &&
(item.getType() != Material.AIR))
return false;
}
return true;
}
public static void randomFirework(Location loc) {
Firework fw = (Firework)loc.getWorld().spawnEntity(loc, EntityType.FIREWORK);
FireworkMeta fwm = fw.getFireworkMeta();
Random r = new Random();
int rt = r.nextInt(5) + 1;
FireworkEffect.Type type = FireworkEffect.Type.BALL;
if (rt == 1) type = FireworkEffect.Type.BALL;
if (rt == 2) type = FireworkEffect.Type.BALL_LARGE;
if (rt == 3) type = FireworkEffect.Type.BURST;
if (rt == 4) type = FireworkEffect.Type.CREEPER;
if (rt == 5) type = FireworkEffect.Type.STAR;
int red = r.nextInt(256);
int b = r.nextInt(256);
int g = r.nextInt(256);
Color c1 = Color.fromRGB(red, g, b);
red = r.nextInt(256);
b = r.nextInt(256);
g = r.nextInt(256);
Color c2 = Color.fromRGB(red, g, b);
FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
fwm.addEffect(effect);
int rp = r.nextInt(2) + 1;
fwm.setPower(rp);
fw.setFireworkMeta(fwm);
}
public static int removeEntitys(List<Entity> e) {
int i = 0;
for (Entity en : e) {
en.remove();
i++;
}
return i;
}
public static void clearPotionEffects(Player player) {
for (PotionEffect effect : player.getActivePotionEffects())
player.removePotionEffect(effect.getType());
}
public static void playEffect(Location loc, EnumParticle ep, float f, int count) {
PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(ep, true, (float)loc.getX(), (float)loc.getY(), (float)loc.getZ(), f, f, f, 0.0F, count, new int[] { 0, 0 });
for (Player p : Bukkit.getOnlinePlayers())
((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
}
public static Vector calculateVector(Location from, Location to) {
Location a = from, b = to;
//calculate the distance between the locations (a => from || b => to)
double dX = a.getX() - b.getX();
double dY = a.getY() - b.getY();
double dZ = a.getZ() - b.getZ();
// -------------------------
//calculate the yaw
double yaw = Math.atan2(dZ, dX);
// -------------------------
//calculate the pitch
double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX), dY) + Math.PI;
// -------------------------
//calculate and create the new vector
double x = Math.sin(pitch) * Math.cos(yaw);
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);
Vector vector = new Vector(x, z, y);
// -------------------------
return vector;
}
}