Core/src/eu/univento/core/api/Utils.java

179 lines
4.3 KiB
Java

package eu.univento.core.api;
import eu.univento.core.Core;
import org.bukkit.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector;
import java.io.File;
import java.util.List;
/**
* 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;
}
/**
* 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());
}
/**
* 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;
}
/**
* restarts server
*/
public static void restart() {
Bukkit.getScheduler().scheduleSyncDelayedTask(Core.getInstance(), new Runnable() {
@Override
public void run() {
Bukkit.getServer().spigot().restart();
}
}, 10 * 20L);
}
/**
* shots random firework at specified location
* @param loc Location
*/
public static void randomFirework(Location loc) {
FireworkEffect.Builder builder = FireworkEffect.builder();
FireworkEffect effect = builder.flicker(false).trail(false).with(FireworkEffect.Type.BALL_LARGE).withColor(Color.RED).withFade(Color.BLUE).build();
//TODO: make a random fireworks effect
//TODO: remove effect.toString(), its only to remove unused warnings
effect.toString();
}
}