Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.yhl;
  2. import java.util.HashMap;
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Material;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.inventory.Inventory;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.inventory.meta.ItemMeta;
  10.  
  11.  
  12. public class AnimationTimer implements Runnable {
  13.  
  14. HashMap<Inventory,Integer> instances = new HashMap<Inventory,Integer>(); //inventory is the specific inv, integer is the current slot number.
  15. HashMap<Inventory,Integer> adding = new HashMap<Inventory,Integer>();
  16. HashMap<Inventory, Boolean> t = new HashMap<Inventory, Boolean>();
  17. double nextUpdate = 0L;
  18. static Boolean toggle = false;
  19. int Progress = 0;
  20. int add = 1;
  21.  
  22. public void onEnable(){
  23. Miner m = Miner.getInstance();
  24.  
  25.  
  26. }
  27.  
  28. public AnimationTimer(Miner Miner) {
  29. //typically you would assing your plugin variable here to a local plugin variable
  30. //ie this.plugin = freePlugin it would be the name of your main class.
  31. }
  32.  
  33. public void getItem(Material item, String itemname,Inventory inv, int slot){
  34. ItemStack itemm = new ItemStack(item);
  35. ItemMeta tmeta = itemm.getItemMeta();
  36. tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', itemname));
  37. itemm.setItemMeta(tmeta);
  38. inv.setItem(slot, itemm);
  39. }
  40.  
  41. public void setName(Player p, int slot, Inventory inv, String name){
  42.  
  43. inv.getItem(slot).getItemMeta().setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
  44.  
  45. }
  46.  
  47.  
  48.  
  49. public void setup(Player p) //this routine adds a player to the list and lets the timer know they need the effect to happen.
  50. {
  51. //this should be in your interact event in your listener that already exists.
  52. /*
  53. if(e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.OBSIDIAN){
  54. AnimationTimer.setup(e.getPlayer()); //adds a player to the timer.
  55. }
  56.  
  57. */
  58. Inventory miner = Bukkit.createInventory(p, 45, ChatColor.GOLD + "" + ChatColor.BOLD + p.getName() + "'s Miner"); //add their name to it so its not able to be opened by others
  59.  
  60.  
  61.  
  62. t.put(miner, false);
  63. getItem(Material.COAL, "&4&lCOAL", miner, 31);
  64.  
  65. getItem(Material.REDSTONE_BLOCK, "&c&lOFF", miner, 29);
  66. getItem(Material.PAPER, "&cProgress: &4" + Progress + "&l%", miner, 33);
  67. miner.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  68. miner.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  69. miner.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  70. miner.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  71. miner.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  72. miner.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  73. miner.setItem(16, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  74. p.openInventory(miner);
  75. this.instances.put(miner,10); //this stores the inventory in a set so we can access it later. we set 10 here because thats where you want to start.
  76.  
  77.  
  78. }
  79.  
  80.  
  81. @Override
  82. public void run() {
  83.  
  84.  
  85.  
  86. HashMap<Inventory,Integer> updatedMap = new HashMap<Inventory,Integer>();
  87.  
  88. 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.
  89. return;
  90.  
  91. double seconds = Miner.getInstance().getConfig().getInt("seconds"); //change this to how many seconds of delay per update.
  92.  
  93.  
  94. this.nextUpdate = System.currentTimeMillis() + (1000 * seconds); //update the timestamp
  95.  
  96. //now check for inventories we need to process if there are none the loop wont run and nothing happens.
  97. for(Inventory inv:instances.keySet()) //gets the list of inventories then pulls them out 1 by 1
  98. {
  99. if(t.get(inv)){
  100.  
  101.  
  102. int currentSlot = instances.get(inv); //gets the current slot from the hash map.
  103. if(inv.getViewers().isEmpty()) //if this returns true nobody is viewing the inventory. or all slots have been changed.
  104. continue; //this one was invalid, skip it and do the next one.
  105. if (currentSlot > 16) {
  106. resetBar(inv);
  107. currentSlot = 10;
  108. add = 1;
  109. }
  110.  
  111. Player p = (Player) inv.getViewers().get(0); //should only ever be one viewer using this method.
  112.  
  113. inv.setItem(currentSlot, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)13)); //update the item
  114. p.updateInventory(); //update the player inventory
  115.  
  116. updatedMap.put(inv, currentSlot+1);
  117. adding.put(inv, add+1);
  118. Progress = add*14;
  119.  
  120.  
  121. ItemStack itemm = new ItemStack(Material.PAPER);
  122. ItemMeta tmeta = itemm.getItemMeta();
  123. tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&cProgress: &4" + Progress + "&l%"));
  124.  
  125.  
  126. inv.getItem(33).setAmount(add);
  127. setName(p, 33, inv, "&cProgress: &4" + Progress + "&l%");
  128. inv.getItem(33).setItemMeta(tmeta);
  129.  
  130.  
  131. p.updateInventory();
  132. add++;
  133. if(currentSlot == 9){
  134. add = 1;
  135. }
  136. //write the new slot to a map and increment it +1.
  137. //*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.
  138. //NEVER edit a hash map while you are iterating over it.
  139. if(currentSlot == 16){
  140.  
  141. p.getInventory().addItem(new ItemStack(inv.getItem(31).getType()));
  142.  
  143. }
  144.  
  145. //now we update the map
  146. this.instances.clear(); //wipe out the old list
  147. this.instances.putAll(updatedMap); //update the map, it will NOT include the closed inventories or the finished ones.
  148. }
  149. }
  150.  
  151. }
  152.  
  153. public void check(Inventory miner){
  154. if(!toggle){
  155. getItem(Material.REDSTONE_BLOCK, "&c&lOFF", miner, 29);
  156. }
  157.  
  158. if(toggle){
  159. getItem(Material.DIAMOND_BLOCK, "&3&lON", miner, 29);
  160. }
  161. }
  162.  
  163. public void resetBar(Inventory miner){
  164. miner.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  165. miner.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  166. miner.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  167. miner.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  168. miner.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  169. miner.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  170. miner.setItem(16, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
  171. }
  172.  
  173.  
  174. }
Advertisement
RAW Paste Data Copied
Advertisement