Guest User

Untitled

a guest
May 9th, 2017
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.yhl;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.DyeColor;
  10. import org.bukkit.Material;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.inventory.Inventory;
  13. import org.bukkit.inventory.ItemStack;
  14. import org.bukkit.inventory.meta.ItemMeta;
  15. import org.bukkit.scheduler.BukkitRunnable;
  16.  
  17.  
  18. public class AnimationTimer implements Runnable {
  19.  
  20. HashMap<Inventory,Integer> instances = new HashMap<Inventory,Integer>();
  21. HashMap<Inventory,Integer> adding = new HashMap<Inventory,Integer>();
  22. double nextUpdate = 0L;
  23. int Progress = 0;
  24. int add = 1;
  25. Player p;
  26. Miner m;
  27. ItemStack[] items;
  28. public boolean Animation = true;
  29.  
  30.  
  31. public void onEnable(){
  32.  
  33. items = new ItemStack[5];
  34.  
  35. items[0] = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14);
  36. items[1] = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)13);
  37. items[2] = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)12);
  38. items[3] = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)11);
  39. items[4] = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)10);
  40.  
  41. }
  42.  
  43. public static ItemStack getRandom(ItemStack[] array) {
  44. int rnd = new Random().nextInt(array.length);
  45. return array[rnd];
  46. }
  47.  
  48. public AnimationTimer(Miner Miner) {
  49. }
  50.  
  51.  
  52.  
  53.  
  54. // public void toggleAnimation(Player p, Inventory inv){
  55. // t.put(p, false);
  56. // if(inv.getViewers().contains(p)){
  57. // if(t.get(p) == false){
  58. // t.put((Player) p, true);
  59. // } else {
  60. // t.put((Player) p, false);
  61. //
  62. // }
  63. // }
  64. //}
  65. //
  66. // public Boolean getToggle(Player p){
  67. //
  68. // return t.get(p);
  69. //
  70. // }
  71.  
  72. public void getItem(Material item, String itemname,Inventory inv, int slot){
  73. ItemStack itemm = new ItemStack(item);
  74. ItemMeta tmeta = itemm.getItemMeta();
  75. tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', itemname));
  76. itemm.setItemMeta(tmeta);
  77. inv.setItem(slot, itemm);
  78. }
  79. public void setItem(ItemStack item, String itemname,Inventory inv, int slot){
  80. ItemStack itemm = new ItemStack(item);
  81. ItemMeta tmeta = itemm.getItemMeta();
  82. tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', itemname));
  83. itemm.setItemMeta(tmeta);
  84. inv.setItem(slot, itemm);
  85. }
  86.  
  87. public void setName(Player p, int slot, Inventory inv, String name){
  88.  
  89. inv.getItem(slot).getItemMeta().setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
  90.  
  91. }
  92.  
  93.  
  94.  
  95. @SuppressWarnings("deprecation")
  96. public void setup(Player p)
  97. {
  98.  
  99.  
  100.  
  101. Inventory miner = getMiner(p);
  102.  
  103.  
  104.  
  105.  
  106. getItem(Material.COAL, "&4Coal", miner, 31);
  107.  
  108. getItem(Material.COMMAND, "&cControl Panel", miner,29);
  109. getItem(Material.PAPER, "&cProgress: &4" + Progress + "&l%", miner, 33);
  110. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 10);
  111. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 11);
  112. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 12);
  113. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 13);
  114. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 14);
  115. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 15);
  116. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14), "&c", miner, 16);
  117. p.openInventory(miner);
  118. this.instances.put(miner,10);
  119.  
  120.  
  121.  
  122.  
  123. List<ItemStack> items = new ArrayList<>();
  124. items.add(make(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)8), colorize("")));
  125. items.add(make(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)4), colorize("")));
  126. items.add(make(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)3), colorize("")));
  127. items.add(make(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)11), colorize("")));
  128. if(Animation){
  129. new BukkitRunnable() {
  130. int currentIndex = 0;
  131.  
  132. @Override
  133. public void run() {
  134. if (currentIndex == items.size()) {
  135. currentIndex = 0;
  136. }
  137.  
  138. int ii;
  139. for (ii = 0; ii < 9; ii++) {
  140. miner.setItem(ii, items.get(currentIndex));
  141.  
  142. }
  143. int ie;
  144. for (ie = 18; ie < 27; ie++) {
  145. miner.setItem(ie, items.get(currentIndex));
  146.  
  147. }
  148. int iu;
  149. for (iu = 36; iu < 45; iu++) {
  150. miner.setItem(iu, items.get(currentIndex));
  151.  
  152. }
  153.  
  154. miner.setItem(35, items.get(currentIndex));
  155. miner.setItem(9, items.get(currentIndex));
  156. miner.setItem(17, items.get(currentIndex));
  157. miner.setItem(28, items.get(currentIndex));
  158. miner.setItem(27, items.get(currentIndex));
  159. miner.setItem(30, items.get(currentIndex));
  160. miner.setItem(32, items.get(currentIndex));
  161. miner.setItem(34, items.get(currentIndex++));
  162.  
  163. }
  164.  
  165. }.runTaskTimer(m.getPlugin(), 0, 5);
  166. } else{
  167. int i;
  168. for (i = 18; i < 27; i++) {
  169. miner.clear(i);
  170.  
  171. }
  172. int e;
  173. for (e = 36; e < 45; e++) {
  174. miner.clear(e);
  175.  
  176. }
  177. int u;
  178. for (u = 0; u < 9; u++) {
  179. miner.clear(u);
  180.  
  181. }
  182.  
  183. miner.clear(35);
  184. miner.clear(9);
  185. miner.clear(17);
  186. miner.clear(28);
  187. miner.clear(27);
  188. miner.clear(30);
  189. miner.clear(32);
  190. miner.clear(34);
  191.  
  192. }
  193. }
  194.  
  195.  
  196. public static Inventory getMiner(Player p) {
  197.  
  198. Inventory miner = Bukkit.createInventory(p, 45, ChatColor.GOLD + "" + ChatColor.BOLD + p.getName() + "'s Miner");
  199. return miner;
  200. }
  201.  
  202. private String colorize(String input) {
  203. return ChatColor.translateAlternateColorCodes('&', input);
  204. }
  205.  
  206. public ItemStack make(ItemStack item, String itemname){
  207. ItemStack itemm = new ItemStack(item);
  208. ItemMeta tmeta = itemm.getItemMeta();
  209. tmeta.setDisplayName(itemname);
  210. itemm.setItemMeta(tmeta);
  211. return itemm;
  212. }
  213.  
  214. @Override
  215. public void run() {
  216.  
  217.  
  218.  
  219. HashMap<Inventory,Integer> updatedMap = new HashMap<Inventory,Integer>();
  220.  
  221. if(System.currentTimeMillis() <= this.nextUpdate) //not time to update, do nothing. Note ALL players would be on the same time delay not a per player delay.
  222. return;
  223.  
  224. double seconds = Miner.getInstance().getConfig().getInt("seconds"); //change this to how many seconds of delay per update.
  225.  
  226.  
  227. this.nextUpdate = System.currentTimeMillis() + (1000 * seconds); //update the timestamp
  228.  
  229. //now check for inventories we need to process if there are none the loop wont run and nothing happens.
  230. for(Inventory inv:instances.keySet()) //gets the list of inventories then pulls them out 1 by 1
  231. {
  232.  
  233.  
  234.  
  235. int currentSlot = instances.get(inv); //gets the current slot from the hash map.
  236. if(inv.getViewers().isEmpty()) //if this returns true nobody is viewing the inventory. or all slots have been changed.
  237. continue; //this one was invalid, skip it and do the next one.
  238. if (currentSlot > 16) {
  239. resetBar(inv);
  240. currentSlot = 10;
  241. add = 1;
  242. }
  243.  
  244. Player p = (Player) inv.getViewers().get(0); //should only ever be one viewer using this method.
  245.  
  246. setItem(new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.PURPLE.getDyeData()), "&c", inv, currentSlot); //update the item
  247. p.updateInventory(); //update the player inventory
  248.  
  249. updatedMap.put(inv, currentSlot+1);
  250. adding.put(inv, add+1);
  251. Progress = add*14;
  252.  
  253.  
  254. ItemStack itemm = new ItemStack(Material.PAPER);
  255. ItemMeta tmeta = itemm.getItemMeta();
  256. tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&cProgress: &4" + Progress + "&l%"));
  257.  
  258.  
  259. inv.getItem(33).setAmount(add);
  260. setName(p, 33, inv, "&cProgress: &4" + Progress + "&l%");
  261. inv.getItem(33).setItemMeta(tmeta);
  262.  
  263. if(inv.getItem(33).getAmount() > 1 && currentSlot == 10){
  264. currentSlot = 10;
  265. Progress = 0;
  266. add = 1;
  267. }
  268. if(currentSlot == 10){
  269.  
  270. currentSlot = 10;
  271. add = 1;
  272. inv.getItem(33).setAmount(1);
  273. Progress = 14;
  274. }
  275. p.updateInventory();
  276. add++;
  277. if(currentSlot == 9){
  278. add = 1;
  279. }
  280. //write the new slot to a map and increment it +1.
  281. //*NOTE* it would be cleaner if we had an object to store the inv, player, slot etc but for simplictys sake i just used a hashmap.
  282. //NEVER edit a hash map while you are iterating over it.
  283. if(currentSlot == 16){
  284.  
  285. p.getInventory().addItem(new ItemStack(inv.getItem(31).getType()));
  286.  
  287. }
  288.  
  289. //now we update the map
  290. this.instances.clear(); //wipe out the old list
  291. this.instances.putAll(updatedMap); //update the map, it will NOT include the closed inventories or the finished ones.
  292. }
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299. public void resetBar(Inventory miner){
  300. miner.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  301. miner.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  302. miner.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  303. miner.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  304. miner.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  305. miner.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  306. miner.setItem(16, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  307. }
  308.  
  309.  
  310. }
RAW Paste Data