Too much Explosions!

Discussion in 'Plugin Development' started by pkt, Dec 31, 2012.

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

    pkt

    ok, so I'm making it so when a player gets close to a giant, it will make an explosion, so that way its kinda like the giant is stomping the player, but when the explosion is called, it keeps repeating. How would I make it so it only explodes once every x ticks?

    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            Location location = player.getLocation();
     
            for (Entity entity : player.getNearbyEntities(5, 5, 5)) {
                if (API.isGiant(entity)) {
                    if (entity.getNearbyEntities(5, 5, 5).contains(player)) {
                        entity.getLocation().getWorld().createExplosion(location, 1F);
                    }
                }
            }
        }
     
  2. Offline

    youngbawss22

    You could try a new repeating task that runs every x amount of ticks if they are close enough.
     
  3. Offline

    pkt

    Can you give me an example, Never made a repeating task before. I looked up a tutorial, but it didn't work. This is what I got so far:
    Code:
        @EventHandler
        public void onPlayerMove(final PlayerMoveEvent event) {
            _giants.getServer().getScheduler().scheduleSyncDelayedTask(_giants, new Runnable() {
                public void run() {
                    Player player = event.getPlayer();
                    Location location = player.getLocation();
                    for (Entity entity : player.getNearbyEntities(5, 5, 5)) {
                        if (API.isGiant(entity)) {
                            if (entity.getNearbyEntities(5, 5, 5).contains(player)) {
                                entity.getLocation().getWorld().createExplosion(location, 1F);
                            }
                        }
                    }
                }
            }, 60L);
        }
     
  4. Offline

    chaseoes

    You wouldn't want it in the move event, rather in onEnable. Otherwise it'll create a new repeating task each time the player moves.
     
  5. Offline

    pkt

    So how would I delay the move event check?
     
  6. Offline

    xWatermelon

    chaseoes or after he checks if a giant is nearby.
     
  7. Offline

    pkt

    That kind of worked... it delayed the explosions, but it still creates more than 1 explosion because the event is checked every time a player moves

    How can I delay the check of a PlayerMoveEvent?
     
  8. Offline

    Jogy34

    Have a hashmap of Strings(player names) and booleans (for weather or not they were blown up) then in the player move event if the hashmap either doesn't contain the player's name or the boolean retrieved by it is set to true blow up the player and set the boolean in the hashmap for the player to false. Then schedule a delayed task and in that set the boolean value to true.
     
  9. Offline

    fireblast709

    Instead of HashMap<String, Boolean> use a HashSet<String>, which means:
    • when you would do .put(name, true);, you do .add(name);
    • when you would do .put(name, false);, you do .remove(name);
    • when you would do .get(name) == true, you do .contains(name);
     
  10. Offline

    pkt

    Oh great, another thing I'm inexperienced with xD Would you mind showing me an example or a tutorial?
     
  11. Offline

    fireblast709

    when they explode:
    Code:java
    1. yourHashSet.add(player.getName());
    2. when the task ends:[syntax=java]yourHashSet.remove(player.getName());
    3. if they should explode:[syntax=java]yourHashSet.contains(player.getName());[/syntax][/syntax]
     
Thread Status:
Not open for further replies.

Share This Page