Delay help

Discussion in 'Plugin Development' started by slater96, Mar 5, 2012.

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

    slater96

    Hi, need help setting a delay between the use of a spawner egg.
    I've created a config with Enabled: true/false and Delay: 5
    However its not working right and I need help checking if its enabled or disabled.
    Code:
        @EventHandler
        public void EggDelayEvent(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            HashMap<Player,Long> lastUseMap = new HashMap<Player,Long>();
            long millis = System.currentTimeMillis();
           
            if(lastUseMap.get(player) != null && (millis - lastUseMap.get(player))/1000 > plugin.getConfig().getInt("Delay"))
            {
                if(player.hasPermission("mobeggs.bypass.delay") || player.isOp())
                    event.setCancelled(false);
                lastUseMap.put(player,millis);
            }
            else
            {
                if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
                    if(event.getItem().getTypeId() == 383)
                        event.setCancelled(true);
                player.sendMessage(ChatColor.YELLOW + "You must wait" + ChatColor.AQUA + " X Seconds" + ChatColor.YELLOW + " before you can spawn another mob!");
            }
        }
    For the message, i want to change X seconds to make it realtime so it will count down when you right click, but have no clue how to do that :(
    Any help appreciated.
     
  2. Offline

    slater96

    bump anyone?
     
  3. Offline

    edocsyl

  4. Offline

    slater96

    I've had a look at that - thanks for linking me to it.
    Based on what i've read, would this be right?
    Code:
        @EventHandler
        public void onBlockPlace(final BlockPlaceEvent event) {
            if(plugin.getConfig().getBoolean("Enabled") == true);
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    plugin.getConfig().getInt("Interval");
                    if(event.getItemInHand().getTypeId() == 383);
                    event.setCancelled(true);
                }
            }, 20L);
        }
        
     
  5. Offline

    edocsyl

    You need to run the schedueler in onEnabled();
     
  6. Offline

    Zelnehlun

    Is the plugin variable final?

    So you want to make a countdown and only if the player clicks it should display how many seconds are left?
     
  7. Offline

    slater96

    Oh right ok, but then how does it set an interval between the use of a spawn egg? Do I link it to a block place listener like onBlockPlaceEvent(event);

    I want there to be a configurable time between using a spawn egg IF it is enabled. It would be good if i could get it to countdown in player.sendMessage too so every time you click it it will display the time you've got to wait before allowed to spawn again like You must wait X seconds. Also I need it to carry on with the player interact event i have if it is disabled..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  8. Offline

    nisovin

    Using the scheduler is not necessary for this, your first attempt had the right idea. Your problem is that you have some major logic errors. What you need to do is take a certain action (for example, a player trying to use an egg for the first time) and mentally step through your code. You'll quickly see that you have some problems.
     
  9. Offline

    slater96

    Is there... Sorry i'm new to this :L
     
  10. well, not really. you can create a new task everywhere in the code as long as you have access to the main class.

    -see nisovin's entry above
     
  11. Offline

    Zelnehlun

    I got an own class which acts just like a timer based on the Bukkit Scheduler, maybe it helps you.
    For example you could start the cooldown when a player clicks witch the egg, if he clicks again you can check if the countdown has already stopped if not just send the remaining seconds.

    Util.delay() is a function which shortens Bukkit.getScheduler().scheduleSyncDelayedTask etc...

    Code:
    public class Cooldown {
       
        int time;
        int seconds;
       
        public OrbCooldown(int seconds){
            time = seconds;
           
            stop();
        }
       
        public void start(){
            seconds = time;
           
            countdown();
        }
     
        public void stop(){
            seconds = 0;
        }
       
        public void countdown(){
            if(seconds > 0){
                Util.delay(new Runnable(){
                    public void run(){
                        seconds--;
                       
                        if(seconds > 0){
                            countdown();
                        }
                    }
                }, 20L);
            }
        }
       
        public boolean isStopped(){
            boolean b = seconds == 0;
           
            return b;
        }
     
        public int getSeconds(){
            return seconds;
        }
       
    }
     
    slater96 likes this.
  12. Offline

    nisovin

    Yes there are, many. Here's a small selection:

    1. Your map is recreated every time the event fires, this is a problem.
    2. It's currently impossible for anything to get into the map, because that first if statement can never be true.
    3. You're checking to make sure it's an egg much too late in the code.
    4. Your method for checking the bypass permission is not in the correct location, and is therefore useless.
     
    slater96 likes this.
  13. Offline

    slater96

    Lol i missed all four :oops:
     
Thread Status:
Not open for further replies.

Share This Page