Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. package at.syntax.caseopening.utils;
  2.  
  3. import java.util.List;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.event.Event.Result;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.HandlerList;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.inventory.InventoryClickEvent;
  12. import org.bukkit.event.inventory.InventoryCloseEvent;
  13. import org.bukkit.inventory.Inventory;
  14. import org.bukkit.inventory.ItemStack;
  15. import org.bukkit.scheduler.BukkitTask;
  16.  
  17. import at.syntax.caseopening.Main;
  18.  
  19. public class Animator implements Listener {
  20.  
  21. private Player player;
  22. private IAnimator ianimator;
  23. private Animator animator;
  24.  
  25. private Inventory inventory;
  26. private boolean running;
  27. private BukkitTask task;
  28.  
  29. private int rolls;
  30. private int timeout;
  31.  
  32. public Animator(Player player, IAnimator ianimator) {
  33. this.player = player;
  34. this.ianimator = ianimator;
  35. this.animator = this;
  36. }
  37.  
  38. public void startAnimation(int rolls, List<ItemStack> items, long waitAtEnd) {
  39. this.inventory = this.ianimator.setupInventory(this.player);
  40. this.rolls = rolls;
  41. this.player.openInventory(this.inventory);
  42.  
  43. this.running = true;
  44. Bukkit.getPluginManager().registerEvents(this.animator, Main.getInstance());
  45.  
  46. this.task = Bukkit.getScheduler().runTaskTimer(Main.getInstance(), new Runnable() {
  47.  
  48. @Override
  49. public void run() {
  50.  
  51. if (Animator.this.timeout != 0) {
  52. Animator.this.timeout--;
  53. } else {
  54. Animator.this.ianimator.onTick(Animator.this.player, Animator.this.inventory, items, false);
  55.  
  56. Animator.this.rolls--;
  57. if (Animator.this.rolls >= 20) {
  58. Animator.this.timeout = 0;
  59. } else if (Animator.this.rolls <= 19 && Animator.this.rolls >= 10) {
  60. Animator.this.timeout = 1;
  61. } else if (Animator.this.rolls <= 9 && Animator.this.rolls >= 5) {
  62. Animator.this.timeout = 2;
  63. } else if (Animator.this.rolls <= 4 && Animator.this.rolls >= 0) {
  64. Animator.this.timeout = 3;
  65. }
  66.  
  67. if (Animator.this.rolls == 0) {
  68. Animator.this.task.cancel();
  69. Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
  70.  
  71. @Override
  72. public void run() {
  73. Animator.this.ianimator.onTick(Animator.this.player, Animator.this.inventory, items,
  74. true);
  75.  
  76. Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
  77.  
  78. @Override
  79. public void run() {
  80. HandlerList.unregisterAll(Animator.this.animator);
  81.  
  82. Animator.this.running = false;
  83. Animator.this.player.closeInventory();
  84. }
  85. }, waitAtEnd);
  86.  
  87. }
  88. }, 20);
  89. }
  90. }
  91. }
  92. }, 0L, 2L);
  93.  
  94. }
  95.  
  96. @EventHandler
  97. public void onClick(InventoryClickEvent e) {
  98. if (e.getInventory().equals(this.inventory) && this.running) {
  99. e.setCancelled(true);
  100. e.setResult(Result.DENY);
  101. }
  102. }
  103.  
  104. @EventHandler
  105. public void onClose(InventoryCloseEvent e) {
  106. Player p = (Player) e.getPlayer();
  107. if (e.getInventory().equals(this.inventory) && this.running) {
  108. Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
  109.  
  110. @Override
  111. public void run() {
  112. p.openInventory(Animator.this.inventory);
  113. }
  114. }, 3);
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement