Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.TJM4.ProAnimations.Animator;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.Timer;
- import java.util.TimerTask;
- import org.bukkit.Location;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.LivingEntity;
- import org.bukkit.event.Listener;
- import me.TJM4.ProAnimations.Main.Main;
- public class Animator implements Listener{
- public int fps = Main.plugin.getConfig().getInt("animationSpeed"); //FPS
- private double oneDpRoundF(float f) { //Used to round number to 1dp
- DecimalFormat df = new DecimalFormat("#.#");
- df.setRoundingMode(RoundingMode.CEILING);
- return Float.parseFloat(df.format(f));
- }
- private double oneDpRoundD(double d) { //Used to round number to 1dp
- DecimalFormat df = new DecimalFormat("#.#");
- df.setRoundingMode(RoundingMode.CEILING);
- return Double.parseDouble(df.format(d));
- }
- public void animateRotation(Entity e, Location from, Location to, int seconds, Runnable r) { //Animation rotate
- if (e instanceof LivingEntity) { //If they are a mob
- ((LivingEntity)e).setAI(false); //Remove there AI
- }
- e.setInvulnerable(true); //Stopping mob from tacking damage
- float changeAcross = to.getPitch() - from.getPitch(); //Amount of across rotation needed each time
- float changeUp = to.getYaw() - from.getYaw(); //Amount of up rotation needed each time
- int waitTime = Math.round(1000/fps); //Amount of time waiting between each frame update
- int totalFrames = fps*seconds; //Total amount of frames
- float changePerTimeAcross = changeAcross/totalFrames; //Amount of change per time moving across
- float changePerTimeUp = changeUp/totalFrames; //Amount of change per time moving up
- Timer timer = new Timer(); //Creating timer
- TimerTask runFrame = new TimerTask() { //Timer task + runnable
- @Override
- public void run() {
- Location newLoc = e.getLocation();
- newLoc.setPitch(newLoc.getPitch() + changePerTimeAcross);
- newLoc.setYaw(newLoc.getYaw() + changePerTimeUp);
- e.teleport(newLoc);
- if ((oneDpRoundF(newLoc.getPitch()) == to.getPitch()) && (oneDpRoundF(newLoc.getYaw()) == to.getYaw())) { //Round new loc roation to 1dp
- timer.cancel();
- if (r != null) {
- r.run();
- }
- }
- }
- };
- timer.schedule(runFrame, 0, waitTime); //Running task
- }
- public void transform(Entity e, Location from, Location to, int seconds, Runnable r) { //Animation transform
- if (e instanceof LivingEntity) { //If they are a mob
- ((LivingEntity)e).setAI(false); //Remove there AI
- }
- e.setInvulnerable(true); //Stopping mob from taking damage
- double changeX = to.getX() - from.getX(); //Amount of X change needed each time
- double changeY = to.getY() - from.getY(); //Amount of Y change needed each time
- double changeZ = to.getZ() - from.getZ(); //Amount of Z change needed each time
- int waitTime = Math.round(1000/fps); //Amount of time waiting between each frame update
- int totalFrames = fps*seconds; //Total amount of frames
- double changePerTimeX = changeX/totalFrames; //Amount of change per time moving across
- double changePerTimeY = changeY/totalFrames; //Amount of change per time moving up
- double changePerTimeZ = changeZ/totalFrames; //Amount of change per time moving up
- Timer timer = new Timer(); //Creating timer
- TimerTask runFrame = new TimerTask() { //Timer task + runnable
- @Override
- public void run() {
- Location newLoc = e.getLocation();
- newLoc.setX(newLoc.getX() + changePerTimeX);
- newLoc.setY(newLoc.getY() + changePerTimeY);
- newLoc.setZ(newLoc.getZ() + changePerTimeZ);
- e.teleport(newLoc);
- if (oneDpRoundD(newLoc.getX()) == oneDpRoundD(to.getX()) && oneDpRoundD(newLoc.getY()) == oneDpRoundD(to.getY()) && oneDpRoundD(newLoc.getZ()) == oneDpRoundD(to.getZ())) { //Round new loc roation to 1dp
- timer.cancel();
- if (r != null) {
- r.run();
- }
- }
- }
- };
- timer.schedule(runFrame, 0, waitTime); //Running task
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement