217 lines
5.8 KiB
Java
217 lines
5.8 KiB
Java
package eu.univento.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.potion.PotionEffect;
|
|
import org.bukkit.util.Vector;
|
|
|
|
/**
|
|
* some utils you may need
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
public class Utils {
|
|
|
|
/**
|
|
* plays sound for all players
|
|
* @param s Sound
|
|
*/
|
|
public static void playSoundToAll(Sound s) {
|
|
for (Player all : Bukkit.getOnlinePlayers())
|
|
all.playSound(all.getLocation(), s, 3.0F, 3.0F);
|
|
}
|
|
|
|
/**
|
|
* plays effect for all players
|
|
* @param e Effect
|
|
*/
|
|
@SuppressWarnings("deprecation")
|
|
public static void playEffectToAll(Effect e) {
|
|
for (Player all : Bukkit.getOnlinePlayers())
|
|
all.playEffect(all.getLocation(), e, 3);
|
|
}
|
|
|
|
/**
|
|
* checks if server version is from spigot
|
|
* @return true/false
|
|
*/
|
|
public static boolean isSpigot() {
|
|
return Bukkit.getVersion().contains("Spigot");
|
|
}
|
|
|
|
/**
|
|
* deletes all folders and files in directory
|
|
* @param file File
|
|
*/
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* creates a random number
|
|
* @param low lowest possible value
|
|
* @param high highest possible value
|
|
* @return double
|
|
*/
|
|
public static double random(int low, int high) {
|
|
return Math.random() * (high - low) + low;
|
|
}
|
|
|
|
/**
|
|
* checks if player has empty inventory
|
|
* @param p Player
|
|
* @return true/false
|
|
*/
|
|
public static boolean hasEmptyInventory(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;
|
|
}
|
|
|
|
/**
|
|
* shoots random fireworks at defined location
|
|
* @param loc Location
|
|
*/
|
|
public static void randomFireworks(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);
|
|
}
|
|
|
|
/**
|
|
* removes list of entity and counts them
|
|
* @param e List<Entity>
|
|
* @return Integer
|
|
*/
|
|
public static int removeEntitys(List<Entity> e) {
|
|
int i = 0;
|
|
for (Entity en : e) {
|
|
en.remove();
|
|
i++;
|
|
}
|
|
return i;
|
|
}
|
|
|
|
/**
|
|
* clears all potion effects from player
|
|
* @param player Player
|
|
*/
|
|
public static void clearPotionEffects(Player player) {
|
|
for (PotionEffect effect : player.getActivePotionEffects())
|
|
player.removePotionEffect(effect.getType());
|
|
}
|
|
|
|
/**
|
|
* plays effect at defined location for all players
|
|
* @param loc Location
|
|
* @param ep EnumParticle
|
|
* @param f random placement of particles
|
|
* @param count count of particles
|
|
*/
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* calculates vector from one location to another
|
|
* @param from Location
|
|
* @param to Location
|
|
* @return Vector
|
|
*/
|
|
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;
|
|
}
|
|
} |