Help with Events

Discussion in 'Plugin Development' started by arkolly, Apr 21, 2014.

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

    arkolly

    Hi, i have just started to code and i have just attempted an event. Here is my code so far:

    Code:
    package com.arkolly.events;
     
     
    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    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.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
     
        public void onEnable() {
            System.out.println("Events TEST has been ENABLED!!");
           
        }
       
       
        @EventHandler
        public void eventsTest(PlayerMoveEvent moveEvent){
            Player player = moveEvent.getPlayer();
            if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.GRASS)) {   
                ItemStack items = new ItemStack(org.bukkit.Material.IRON_CHESTPLATE);
                player.getInventory().addItem(items);
     
               
            }
               
        }
    }
    
    The listener is in the Main.java and the plugin.yml looks like this:
    Code:
    main: com.arkolly.events.Main
    name: Events
    version: 1.0
    description: >
                Event Testing
    It loads up ok, but nothing happends when i move on stone? Thanks!
     
  2. Offline

    Krotass

    Register the event? :D
     
    lukewizzy likes this.
  3. Offline

    arkolly

    Phahaha sorry XD Thanks! lemme try and see if it works

    How do i make it so the user has to stand on the block for 5 seconds? Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Cancel PlayerMoveEvent for 5 sec.
     
  5. Offline

    arkolly

    Thats it? Is this correct? :

    Code:
        @EventHandler
        public void eventsTest(PlayerMoveEvent moveEvent){
            Player player = moveEvent.getPlayer();
            if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.GRASS)) {
                Cancel PlayerMoveEvent(5);
                ItemStack items = new ItemStack(org.bukkit.Material.IRON_CHESTPLATE);
                player.getInventory().addItem(items);
     
             
            }
             
        }
     
  6. Code:java
    1. HashMap<String, Long> move = new HashMap<String, Long>();
    2.  
    3. @EventHandler
    4. public void stop(PlayerMoveEvent e) {
    5. if (move.containsKey(e.getPlayer().getName())) {
    6. if (move.get(e.getPlayer().getName()) < System.currentTimeMillis() - 5*1000) {
    7. move.remove(e.getPlayer().getName());
    8. } else {
    9. e.setCancelled(true);
    10. }
    11.  
    12. }
    13. }



    if you want to freeze someone do:
    Code:java
    1. move.put(p.getName(), System.currentTimeMillis());
     
  7. Offline

    thisguy128512

    If you're just waiting to see if someone stands still for five seconds...

    Code:java
    1. @EventHandler
    2. onPlayerMove(PlayerMoveEvent event) {
    3. //Good stuff
    4. getServer().getScheduler().scheduleSyncDelayedTask(this,
    5. new Runnable() {
    6. public void run() {
    7. //Are you still there?
    8. }
    9. }, 100L);
    10. }


    Wherein 100L is 20 ticks (one second) times 5, in data type long, and this is a JavaPlugin.
     
Thread Status:
Not open for further replies.

Share This Page