Advertisement
TJM4

Untitled

Jan 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. package me.TJM4.ProAnimations.Animator;
  2.  
  3. import java.math.RoundingMode;
  4. import java.text.DecimalFormat;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import org.bukkit.Location;
  9. import org.bukkit.entity.Entity;
  10. import org.bukkit.entity.LivingEntity;
  11. import org.bukkit.event.Listener;
  12.  
  13. import me.TJM4.ProAnimations.Main.Main;
  14.  
  15. public class Animator implements Listener{
  16.  
  17. public int fps = Main.plugin.getConfig().getInt("animationSpeed"); //FPS
  18.  
  19. private double oneDpRoundF(float f) { //Used to round number to 1dp
  20. DecimalFormat df = new DecimalFormat("#.#");
  21. df.setRoundingMode(RoundingMode.CEILING);
  22. return Float.parseFloat(df.format(f));
  23. }
  24.  
  25. private double oneDpRoundD(double d) { //Used to round number to 1dp
  26. DecimalFormat df = new DecimalFormat("#.#");
  27. df.setRoundingMode(RoundingMode.CEILING);
  28. return Double.parseDouble(df.format(d));
  29. }
  30.  
  31. public void animateRotation(Entity e, Location from, Location to, int seconds, Runnable r) { //Animation rotate
  32.  
  33. if (e instanceof LivingEntity) { //If they are a mob
  34. ((LivingEntity)e).setAI(false); //Remove there AI
  35. }
  36.  
  37. e.setInvulnerable(true); //Stopping mob from tacking damage
  38.  
  39. float changeAcross = to.getPitch() - from.getPitch(); //Amount of across rotation needed each time
  40. float changeUp = to.getYaw() - from.getYaw(); //Amount of up rotation needed each time
  41.  
  42. int waitTime = Math.round(1000/fps); //Amount of time waiting between each frame update
  43.  
  44. int totalFrames = fps*seconds; //Total amount of frames
  45.  
  46.  
  47. float changePerTimeAcross = changeAcross/totalFrames; //Amount of change per time moving across
  48. float changePerTimeUp = changeUp/totalFrames; //Amount of change per time moving up
  49.  
  50. Timer timer = new Timer(); //Creating timer
  51.  
  52. TimerTask runFrame = new TimerTask() { //Timer task + runnable
  53.  
  54. @Override
  55. public void run() {
  56. Location newLoc = e.getLocation();
  57. newLoc.setPitch(newLoc.getPitch() + changePerTimeAcross);
  58. newLoc.setYaw(newLoc.getYaw() + changePerTimeUp);
  59. e.teleport(newLoc);
  60. if ((oneDpRoundF(newLoc.getPitch()) == to.getPitch()) && (oneDpRoundF(newLoc.getYaw()) == to.getYaw())) { //Round new loc roation to 1dp
  61. timer.cancel();
  62. if (r != null) {
  63. r.run();
  64. }
  65. }
  66. }
  67. };
  68.  
  69. timer.schedule(runFrame, 0, waitTime); //Running task
  70. }
  71.  
  72. public void transform(Entity e, Location from, Location to, int seconds, Runnable r) { //Animation transform
  73.  
  74. if (e instanceof LivingEntity) { //If they are a mob
  75. ((LivingEntity)e).setAI(false); //Remove there AI
  76. }
  77.  
  78. e.setInvulnerable(true); //Stopping mob from taking damage
  79.  
  80. double changeX = to.getX() - from.getX(); //Amount of X change needed each time
  81. double changeY = to.getY() - from.getY(); //Amount of Y change needed each time
  82. double changeZ = to.getZ() - from.getZ(); //Amount of Z change needed each time
  83.  
  84. int waitTime = Math.round(1000/fps); //Amount of time waiting between each frame update
  85.  
  86. int totalFrames = fps*seconds; //Total amount of frames
  87.  
  88.  
  89. double changePerTimeX = changeX/totalFrames; //Amount of change per time moving across
  90. double changePerTimeY = changeY/totalFrames; //Amount of change per time moving up
  91. double changePerTimeZ = changeZ/totalFrames; //Amount of change per time moving up
  92.  
  93. Timer timer = new Timer(); //Creating timer
  94.  
  95. TimerTask runFrame = new TimerTask() { //Timer task + runnable
  96.  
  97. @Override
  98. public void run() {
  99. Location newLoc = e.getLocation();
  100. newLoc.setX(newLoc.getX() + changePerTimeX);
  101. newLoc.setY(newLoc.getY() + changePerTimeY);
  102. newLoc.setZ(newLoc.getZ() + changePerTimeZ);
  103. e.teleport(newLoc);
  104. 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
  105. timer.cancel();
  106.  
  107. if (r != null) {
  108. r.run();
  109. }
  110. }
  111. }
  112. };
  113.  
  114. timer.schedule(runFrame, 0, waitTime); //Running task
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement