Countdown

Discussion in 'Plugin Development' started by bronzzze, Mar 7, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    bronzzze

    First time I drop item its working. Second time its not start over again but its start at 0 at item explode instantly
    Class:
    Code:
    public int number = 4;
        @EventHandler
        public void onThrow(final PlayerDropItemEvent event) {
            final Player player = event.getPlayer();
            final World world = player.getWorld();
            Item drop = event.getItemDrop();
            if(drop.getItemStack() != null
                    && drop.getItemStack().hasItemMeta()
                    && drop.getItemStack().getItemMeta()
                            .hasDisplayName() && drop.getItemStack()
                    .getItemMeta()
                    .getDisplayName()
                    .contains("" + ChatColor.GOLD + ChatColor.BOLD + "Small Bomb")) {
    
              
                          
      
                player.getWorld().playSound(player.getLocation(), Sound.WITHER_SHOOT, 1, 2);
                Bukkit.getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    
                    @Override
                    public void run() {
                        Item drop = event.getItemDrop();
                        if (drop.isDead() || !drop.isValid()){
      
                      
                            return;
                        }
    
                  
                      
                        if(number!=-1){
                            if(number!=0){
                                Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "" + number + " seconds");
                                drop.getWorld().playSound(drop.getLocation(), Sound.CLICK, 1, 1);
                                number--;
                              
                            }else{
                                Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "KABOOM!");
                                Location loc = event.getItemDrop().getLocation();
                                world.createExplosion(loc, 4F);
                                event.getItemDrop().remove();
                              
                            }
                      
                          
                          
                        }
                      
                    }
    
                }, 0L, 20L);
          
                }
            }
            
    Should I make it with delayed task?
     
  2. Offline

    mine-care

    Why you need a repeating task in an event?
    Could you explain the problem a bit more?
     
  3. Offline

    bronzzze

    I need when i drop bomb it count down 3 2 1 Kaboom and droped item explode.
    Do you know What I mean?

    Anyone?

    bump.

    I want to make when I drop Smallbomb(Fireball) it start to countdown 4 3 2 1 and fireball explode.
    First time I drop it it works fine second time i drop it explode immediately and I dont know what is wrong there is not error in console.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    sgavster

    You could try my SimplyCountdown util, it's in my signature. It hasn't been tested in a while, but if you try it and find any problems I will help.

    Also, if you want just 1 dropped at a time, you could make a boolean;

    boolean hasDropped = false;

    then take that and make it true when they drop the bomb

    hasDropped = true;

    then check if the bomb has dropped before that code, and put it so that it'll only activate the code if it is false. When the timer is over you could make
    hasDropped = false;.

    But, I'm pretty sure that with SimplyCountdown you shouldn't get any errors using it more than 1 time.
     
  5. Offline

    bronzzze

    Ty gonna try it:)

    @sgavster
    Ok now i set this class
    Code:
    package me.bronzzze.bombs;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.ChatColor;
    
    public class SimplyCountdown {
    
    
          private final Main plugin;
            private int countdownTimer;
     
            public SimplyCountdown(Main i)
            {
                this.plugin = i;
            }
     
            public void startCountdown(final Player p, final boolean all, final int time, final String msg)
            {
                this.countdownTimer = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable()
                {
                    int i = time;
     
                    public void run()
                    {
                        if(all)
                        {
                            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', msg.replace("#", Integer.toString(i))));
                        }
                        else
                        {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', msg.replace("#", Integer.toString(i))));
                        }
                        this.i--;
                        if (this.i <= 0) SimplyCountdown.this.cancel();
                    }
                }
                , 0L, 20L);
            }
     
            public void cancel()
            {
                Bukkit.getScheduler().cancelTask(this.countdownTimer);
            }
        }
    
    
    Event Class:
    Code:
    @EventHandler
        public void onThrow(final PlayerDropItemEvent event) {
            final Player player = event.getPlayer();
            Item drop = event.getItemDrop();
    
            if (drop.getItemStack() != null
                    && drop.getItemStack().hasItemMeta()
                    && drop.getItemStack().getItemMeta().hasDisplayName()
                    && drop.getItemStack()
                            .getItemMeta()
                            .getDisplayName()
                            .contains(
                                    "" + ChatColor.GOLD + ChatColor.BOLD
                                            + "Small Bomb")) {
    
                player.getWorld().playSound(player.getLocation(),
                        Sound.WITHER_SHOOT, 1, 2);
                SimplyCountdown countdown = new SimplyCountdown(main);
                countdown.startCountdown(player, false, 10, "&5# ...");
            
            }
        }
    }
    How should i add in explosion at end of cooldown?
    And sound.Click when it counting
    Should i add explosion there
    Code:
      if (this.i <= 0) SimplyCountdown.this.cancel();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  6. Offline

    sgavster

    Hmm, you could probably do this:

    Code:
    if (this.i <= 0) {
    SimplyCountdown.this.cancel();
    //your code
    }
     
  7. Offline

    bronzzze

    Ok gonna try. I need to make instance So i can get drop item from other class. right?
     
  8. Offline

    sgavster

  9. Offline

    bronzzze

    I cant get drop from event... I just can get int.
    I can put this but I never doing with this before and idk what to in brackets
    [​IMG]
    I can use this but idk how to get drop.
    [​IMG]
     
    Last edited: Mar 8, 2015
  10. Offline

    sgavster

    Make the item outside of the event
     
  11. Offline

    bronzzze

    oh i am so dumb lol:)
    I just need to get location of dropped item but idk how to set it for ItemStack with dropEvent
    [​IMG]

    @sgavster
    SimplyCountdown class:
    Code:
    package me.bronzzze.bombs;
    
    
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.ChatColor;
    
    public class SimplyCountdown {
     
     
     
     
     
          private final Main plugin;
            private int countdownTimer;
            SmallbombEvent instance = SmallbombEvent.getInstance();
            Commands instance2 = Commands.getInstance();
      
            public SimplyCountdown(Main i)
            {
                this.plugin = i;
            }
      
            public void startCountdown(final Player p, final boolean all, final int time, final String msg)
            {
                this.countdownTimer = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable()
                {
                    int i = time;
      
                    public void run()
                    {
                        if(all)
                        {
                            Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', msg.replace("#", Integer.toString(i))));
                        }
                        else
                        {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', msg.replace("#", Integer.toString(i))));
                        }
                        this.i--;
                        if (this.i <= 0) {
                         
                    
                            SimplyCountdown.this.cancel();
                            Location loc = instance.small.getClass().
                            for (Location l : generateSphere(loc, instance.radius, instance.hollow))
                                l.getBlock().setTypeId(0);
                                event.getItemDrop().remove();
                                loc.getWorld().playSound(loc, Sound.EXPLODE, 1, 1);
                                loc.getWorld().playEffect(loc, Effect.MOBSPAWNER_FLAMES, 4);
                        }
                    }
                }
                , 0L, 20L);
            }
      
            public void cancel()
            {
                Bukkit.getScheduler().cancelTask(this.countdownTimer);
            }
    
    
    }
    
    
    EventClass:
    Code:
    @EventHandler
        public void onThrow(final PlayerDropItemEvent event) {
            final Player player = event.getPlayer();
            Item drop = event.getItemDrop();
    
            if (drop.getItemStack() != null
                    && drop.getItemStack().hasItemMeta()
                    && drop.getItemStack().getItemMeta().hasDisplayName()
                    && drop.getItemStack()
                            .getItemMeta()
                            .getDisplayName()
                            .contains(
                                    "" + ChatColor.GOLD + ChatColor.BOLD
                                            + "Small Bomb")) {
    
                player.getWorld().playSound(player.getLocation(),
                        Sound.WITHER_SHOOT, 1, 2);
                SimplyCountdown countdown = new SimplyCountdown(main);
                countdown.startCountdown(player, false, 10, "&5# ...");
             
            }
        }
    
     
        }
    
    And this is ItemStack class:
    Code:
    ItemStack small = new ItemStack(Material.FIREBALL);
                ItemMeta smallM = small.getItemMeta();
                smallM.setDisplayName("" + ChatColor.GOLD + ChatColor.BOLD + "Small Bomb");
                small.setItemMeta(smallM);
                small.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    How to get itemstack drop location.
    This is what i can use for itemstack:
    [​IMG]

    any ideas?
    Should i make it with Hashmap or what?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  12. Offline

    sgavster

    This is not tested but;


    Code:
    ArrayList<Location> location = new ArrayList<Location>();
    
    @EventHandler
    public void onDrop(PlayerDropItemEvent event) {
        if(event.getItemDrop().equals(/**Your Item*/)) {
        Location loc = event.getItemDrop().getLocation();
        location.add(loc);
       }
    }
    Then get the arraylist location
     
  13. Offline

    Signatured

  14. Offline

    bronzzze

    How to set location in other class instance.location.get()?
    How to acces this List location?

    @sgavster
    [​IMG]
    instance.location.get ? or what should i use.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  15. Offline

    bronzzze

  16. Offline

    dlange

    @bronzzze Please, PLEASE, stop double/triple/quadruple/whatever posting, and remember only to bump once every 24 hours, if a mod could merge his posts please, would be appreciated! :D

    EDIT: Also, to post code click the button next to the movie insert thing which is next to the photo insert thing you are using to posts ur code, then click code->Java and paste ur code in. Then hit done and hey presto, CODE!
     
Thread Status:
Not open for further replies.

Share This Page