Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.yhl;
- import java.util.HashMap;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Material;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- public class AnimationTimer implements Runnable {
- HashMap<Inventory,Integer> instances = new HashMap<Inventory,Integer>(); //inventory is the specific inv, integer is the current slot number.
- HashMap<Inventory,Integer> adding = new HashMap<Inventory,Integer>();
- HashMap<Inventory, Boolean> t = new HashMap<Inventory, Boolean>();
- double nextUpdate = 0L;
- static Boolean toggle = false;
- int Progress = 0;
- int add = 1;
- public void onEnable(){
- Miner m = Miner.getInstance();
- }
- public AnimationTimer(Miner Miner) {
- //typically you would assing your plugin variable here to a local plugin variable
- //ie this.plugin = freePlugin it would be the name of your main class.
- }
- public void getItem(Material item, String itemname,Inventory inv, int slot){
- ItemStack itemm = new ItemStack(item);
- ItemMeta tmeta = itemm.getItemMeta();
- tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', itemname));
- itemm.setItemMeta(tmeta);
- inv.setItem(slot, itemm);
- }
- public void setName(Player p, int slot, Inventory inv, String name){
- inv.getItem(slot).getItemMeta().setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
- }
- public void setup(Player p) //this routine adds a player to the list and lets the timer know they need the effect to happen.
- {
- //this should be in your interact event in your listener that already exists.
- /*
- if(e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.OBSIDIAN){
- AnimationTimer.setup(e.getPlayer()); //adds a player to the timer.
- }
- */
- 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
- t.put(miner, false);
- getItem(Material.COAL, "&4&lCOAL", miner, 31);
- getItem(Material.REDSTONE_BLOCK, "&c&lOFF", miner, 29);
- getItem(Material.PAPER, "&cProgress: &4" + Progress + "&l%", miner, 33);
- miner.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(16, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- p.openInventory(miner);
- 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.
- }
- @Override
- public void run() {
- HashMap<Inventory,Integer> updatedMap = new HashMap<Inventory,Integer>();
- 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.
- return;
- double seconds = Miner.getInstance().getConfig().getInt("seconds"); //change this to how many seconds of delay per update.
- this.nextUpdate = System.currentTimeMillis() + (1000 * seconds); //update the timestamp
- //now check for inventories we need to process if there are none the loop wont run and nothing happens.
- for(Inventory inv:instances.keySet()) //gets the list of inventories then pulls them out 1 by 1
- {
- if(t.get(inv)){
- int currentSlot = instances.get(inv); //gets the current slot from the hash map.
- if(inv.getViewers().isEmpty()) //if this returns true nobody is viewing the inventory. or all slots have been changed.
- continue; //this one was invalid, skip it and do the next one.
- if (currentSlot > 16) {
- resetBar(inv);
- currentSlot = 10;
- add = 1;
- }
- Player p = (Player) inv.getViewers().get(0); //should only ever be one viewer using this method.
- inv.setItem(currentSlot, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)13)); //update the item
- p.updateInventory(); //update the player inventory
- updatedMap.put(inv, currentSlot+1);
- adding.put(inv, add+1);
- Progress = add*14;
- ItemStack itemm = new ItemStack(Material.PAPER);
- ItemMeta tmeta = itemm.getItemMeta();
- tmeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&cProgress: &4" + Progress + "&l%"));
- inv.getItem(33).setAmount(add);
- setName(p, 33, inv, "&cProgress: &4" + Progress + "&l%");
- inv.getItem(33).setItemMeta(tmeta);
- p.updateInventory();
- add++;
- if(currentSlot == 9){
- add = 1;
- }
- //write the new slot to a map and increment it +1.
- //*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.
- //NEVER edit a hash map while you are iterating over it.
- if(currentSlot == 16){
- p.getInventory().addItem(new ItemStack(inv.getItem(31).getType()));
- }
- //now we update the map
- this.instances.clear(); //wipe out the old list
- this.instances.putAll(updatedMap); //update the map, it will NOT include the closed inventories or the finished ones.
- }
- }
- }
- public void check(Inventory miner){
- if(!toggle){
- getItem(Material.REDSTONE_BLOCK, "&c&lOFF", miner, 29);
- }
- if(toggle){
- getItem(Material.DIAMOND_BLOCK, "&3&lON", miner, 29);
- }
- }
- public void resetBar(Inventory miner){
- miner.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- miner.setItem(16, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement