Check how long a player stands on a block?

Discussion in 'Plugin Development' started by OfficerDeveloper, Mar 29, 2015.

Thread Status:
Not open for further replies.
  1. Hello, I want to check how long a player stands on a block, and I have no clue how to go about doing this.
    (For a KingOfTheHill Faction-Minigame plugin)
     
  2. Offline

    Signatured

    Since there's not an event to check if a player's moving, you'd have to use a scheduler to check every second (or however fast you want to check) the players velocity.

    Player p=something;
    int time = 0;
    if(time = 5){
    //do stuff
    }
    Velocity v=p.getVelocity();
    if((v.getX()==0)&&(v.getY()==0)&&(v.getZ()==0)){
    time++;
    }

    (Not 100% sure the code is correct, but you get the idea)

    Have this run in a repeating scheduler that repeats every second, and add your extra code that you want it to do.
     
  3. Offline

    teej107

  4. Offline

    teej107

  5. @teej107
    so this should work?
    @nverdier
    Code:
    package com.officeremerald.events;
    
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.Inventory;
    
    import com.officeremerald.utils.KOTH;
    
    public class capCapture implements Listener {
    
        static Inventory inv = Bukkit.createInventory(null, 54, ChatColor.translateAlternateColorCodes('&', "&6&l--> &bCap Rewards &6&l<--"));
        static Random r = new Random();
    
        @EventHandler
        public void onMove(final PlayerMoveEvent e) {
            final Player p = e.getPlayer();
    
            Bukkit.getScheduler().scheduleSyncRepeatingTask(KOTH.instance, new Runnable() { //Checks every couple of minutes(config) wether a person is standing on it for that amount of time.
    
                @SuppressWarnings("deprecation")
                public void run() {
    
                    if (e.getTo().equals(null)) {
    
                        Location bLoc = p.getLocation().subtract(0, 1, 0);
    
                        if (bLoc.getBlock().equals(Material.IRON_BLOCK)) {
    
                            for (Player pl : Bukkit.getOnlinePlayers()) {
                                KOTH.sendMessage(pl, "&c" + p.getName() + " &8has captued a hill!"); //Boadcasts that the person captures
                                KOTH.sendMessage(pl, "&cLocation: X-" + p.getLocation().getX() + ", Y-" + p.getLocation().getY() + ", Z-" + p.getLocation().getZ() + "!");
    
                                for (Integer s : KOTH.config.getIntegerList("Options.consoleCapRewards")) {
    
                                    GUI.createItem(inv, Material.getMaterial(s), "&a&k;-&6" + Material.getMaterial(s).getData().getName() + "&a&k;-", 1, (short) 0, r.nextInt(54)); //Sets the item GUI from the config
                                    p.openInventory(inv); //Opens the reward GUI
    
                                }
    
                            }
                            p.getLocation().getDirection().multiply(3); //Bounces them forward
    
                        } else {
                            return;
                        }
    
                    } else {
                        return;
                    }
    
                }
    
            }, 0, KOTH.config.getInt("Options.amountCaptureSec") * 20);
    
        }
    
    }
    
     
    Last edited: Mar 29, 2015
  6. Offline

    teej107

    @OfficerDeveloper The best way to find out is to try it!

    But creating a scheduled task in a player move event is a seriously inefficient way to go.
     
  7. Well it has to be in a method, it doesn't work, so I am drained :(
     
  8. Tried scheduler, didn't work. BUMP

    I got this, and now where do I put it? Not asking for spoodfeed code, just I have no clue where to put it to check if the player stands on a block.

    Code:
    package com.officeremerald.events;
    
    import java.util.Random;
    
    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.scheduler.BukkitRunnable;
    
    import com.officeremerald.utils.KOTH;
    
    public class capCapture extends BukkitRunnable {
    
        static Inventory inv = Bukkit.createInventory(null, 54, ChatColor.translateAlternateColorCodes('&', "&6&l--> &bCap Rewards &6&l<--"));
        static Random r = new Random();
        private Player p;
        private String name;
    
        public void ExampleTask(Player player, String s) {
            this.p = player;
            this.name = s;
        }
    
        @SuppressWarnings("deprecation")
        @Override
        public void run() {
    
            if (p.getLocation().subtract(0, 1, 0).getBlock().equals(new ItemStack(Material.WOOL, 1, (short) 14))) {
    
                if (p.getLocation().getX() + 100 <= KOTH.getArenaData(name).getInt("Spawns.locX") || p.getLocation().getY() + 100 <= KOTH.getArenaData(name).getInt("Spawns.locY") || p.getLocation().getZ() + 100 <= KOTH.getArenaData(name).getInt("Spawns.locZ")) {
    
                    for (Player pl : Bukkit.getOnlinePlayers()) {
                        KOTH.sendMessage(pl, "&c" + p.getName() + " &8has captued a hill!");
                        KOTH.sendMessage(pl, "&cLocation: X-" + p.getLocation().getX() + ", Y-" + p.getLocation().getY() + ", Z-" + p.getLocation().getZ() + "!");
    
                        for (Integer s : KOTH.config.getIntegerList("Options.consoleCapRewards")) {
    
                            GUI.createItem(inv, Material.getMaterial(s), "&a&k;-&6" + Material.getMaterial(s).getData().getName() + "&a&k;-", 1, (short) 0, r.nextInt(54));
                            p.openInventory(inv);
    
                        }
    
                    }
                    p.getLocation().getDirection().multiply(3);
    
                } else {
                    return;
                }
    
            } else {
                return;
            }
    
        }
    }
    
    No clue to where I would put it to check how long a player stands on a block.

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Apr 2, 2015
  9. Offline

    SuperOriginal

    If you want the task checking all the time you can start it onEnable, or start it in whatever method you call when you want it to start.
     
  10. Ok, but with this scheduler, I am afraid that if the player steps on it last second it will still count, instead of standing on it.

    Please HELP ME!!! I only have 2 days :/
     
    Last edited: Apr 3, 2015
  11. Offline

    WeeSkilz

    @OfficerDeveloper

    A better way to do this would be to have a task in your onEnable that runs every second.

    You check the PlayerMoveEvent and if the player is standing on the correct block you add them to a HashMap<String, Integer> where the Integer value is 0.

    In your scheduler you have a foreach loop ticking the values in the hashmap and incrementing them by one. If on that iteration the integer value is equal to the capture time, open the GUI.

    Remove the player from the hashmap if they move off the block.
     
  12. That didn't work :/. Please help guys @teej107
     
    Last edited: Apr 4, 2015
Thread Status:
Not open for further replies.

Share This Page