Solved Get Interacted Block y -1

Discussion in 'Plugin Development' started by Samthelord1, Aug 23, 2013.

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

    Samthelord1

    From my current code, how would I go about checking the block under the pressure plate?

    @EventHandler
    public void onPlayerStep(PlayerInteractEvent e) {
    if(e.getAction().equals(Action.PHYSICAL)) {
    Player p = e.getPlayer();
    if(e.getClickedBlock().equals(Material.STONE_PLATE)) {
     
  2. Samthelord1
    Code:
    Block b = e.getClickedBlock().getLocation().getBlock().getRelative(BlockFace.DOWN);
    or
    Code:
    Block b = e.getClickedBlock().getLocation().subtract(0, 1, 0).getBlock();
     
  3. Offline

    Samthelord1

    Assist so should

    public void onPlayerStep(PlayerInteractEvent e) {
    if(e.getAction().equals(Action.PHYSICAL)) {
    Player p = e.getPlayer();
    if(e.getClickedBlock().equals(Material.STONE_PLATE)) {
    Block b = e.getClickedBlock().getLocation().subtract(0, 1, 0).getBlock();

    if(b.equals(Material.REDSTONE_BLOCK)) {
    //to come

    }

    work, or can I not check if a block is a material?
     
  4. Offline

    raGan.

    Or maybe just
    Code:
    Block b = e.getClickedBlock().getRelative(BlockFace.DOWN);
     
  5. raGan.
    Or that... :rolleyes:

    Samthelord1
    Check if b.getType().equals(Material.REDSTONE_BLOCK)
     
  6. Offline

    raGan.

    Samthelord1
    You can also use == with enums, like
    Code:
    b.getType() == Material.REDSTONE_BLOCK
     
  7. Offline

    Samthelord1

    Assist off topic, but I don't 100% understand vectors, I looked at LucasEmanuel s tutorial, but I'm still not quite sure how to shoot some forward https://forums.bukkit.org/threads/tutorial-how-to-calculate-vectors.138849/

    raGan. I feel like I should be tagging you

    Assist any idea?

    evilmidget38 I have come across an error, if you have any idea help is appreciated. Here is my code.

    @EventHandler
    public void onPlayerStep(PlayerInteractEvent e) {
    if(e.getAction().equals(Action.PHYSICAL)) {
    Player p = e.getPlayer();
    if(e.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
    Block b = e.getClickedBlock().getLocation().subtract(0, 1, 0).getBlock();

    if(b.getType().equals(Material.REDSTONE_BLOCK)) {
    p.sendMessage("Debugging..");
    p.setVelocity(p.getVelocity().setY(5));
    p.setVelocity(p.getVelocity().setZ(15));
    I don't get the debugging message

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

    Chinwe

    Samthelord1
    That works fine for me, though your vectors are way too fast :>

    Setting Y to 1 will shoot up ~20 blocks, so setting Z to 15: "moved too quickly" spam :(

    Suggestion:
    p.setVelocity(p.getVelocity().add(new Vector(0, 0.5, 1.5)));
     
  9. Offline

    Samthelord1

    Chinwe still not working :/

    Chinwe whole code:
    HTML:
    package me.samthelord1.launchpads;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
     
    /**
    *
    * @author Sam
    */
    public class Main extends JavaPlugin {
        public void onEnable() {
           
        }
    @EventHandler
    public void onPlayerStep(PlayerInteractEvent e) {
        if(e.getAction().equals(Action.PHYSICAL)) {
        Player p = e.getPlayer();
        if(e.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
            Block b = e.getClickedBlock().getLocation().subtract(0, 1, 0).getBlock();
           
            if(b.getType().equals(Material.REDSTONE_BLOCK)) {
                p.sendMessage("Debugging..");
    p.setVelocity(p.getVelocity().add(new Vector(0, 0.5, 1.5)));
     
                            }
                        } 
                    }                     
                }
            }
     
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  10. Offline

    Chinwe

    Theres your problem, you ain't registering your listener :(
    Put this into your onEnable():

    getServer().getPluginManager().registerEvents(this, this);
     
  11. Offline

    Samthelord1

    Chinwe oh dear xD

    Chinwe I feel disguisted with myself, didnt even implement Listener

    Chinwe it's working :D but how would I make it shoot them in the direction that they're facing?
    EDIT: Should I use a switch and return x and z for where the players facing?

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

    Chinwe


    Nah, you can use their direction:
    Code:
    p.setVelocity(p.getVelocity().add(p.getLocation().getDirection().multiply(3)).setY(1));
    Trying to resemble HiveMC by any chance? ;)
     
  13. Offline

    Samthelord1

    Chinwe yeah, I've seen it requested a few times, as HiveMC is closed source

    Chinwe how would I cancel damage whilst this is happening?

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

    Retherz_

    easy way: add to a list, make a delayed task to remove them from it, on damage event check if they are in the list then cancel it
     
  15. Offline

    Samthelord1

    MercilessPvP I was thinking for a shorter way, but i guess I'll use it and make a pull request for setInvincible(true/false) on the github

    Chinwe if i do this:

    PHP:
    undamageable.add(p);
                            }
     
                        }
       
        }
    }
     
                   
    @
    EventHandler
    public void anotherEvent(EntityDamageEvent event) {
            
    Entity e event.getEntity();
    if(
    instanceof Player) {
        if(
    undamageable.contains(e)) {
    then schedule my delayed task will it work?

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

    raGan.

    Samthelord1
    Hi again, could you show more code ? I mean when do you schedule the task ?
     
  17. Offline

    Chinwe

    Samthelord1
    Nupe, because the list will throw huge amounts of NPEs - check undamageable contains the players names before trying to remove too.

    Try checking the cause is DamageCause.FALL too (and cancel it if so) :>

    And although using a scheduler might make it exploit proof, it would be much simpler just to listen to the damage event: in the chance that the player doesn't take any fall damage, they have only 1 damage event where they are invulnerable.
     
  18. Offline

    Samthelord1

    raGan. Chinwe I have
    PHP:
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package me.samthelord1.launchpads;
     
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    /**
    *
    * @author Sam
    */
    public class Main extends JavaPlugin implements Listener {
        public 
    void onEnable() {
        
    getServer().getPluginManager().registerEvents(thisthis);
     
        }
       
    public static List<
    Playerundamageable = new ArrayList<>();
     
    @
    EventHandler
    public void onPlayerStep(PlayerInteractEvent e) {
        if(
    e.getAction().equals(Action.PHYSICAL)) {
        
    Player p e.getPlayer();
        if(
    e.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
            
    Block b e.getClickedBlock().getLocation().subtract(010).getBlock();
           
            if(
    b.getType().equals(Material.REDSTONE_BLOCK)) {
                
    p.sendMessage("Debugging..");
    p.setVelocity(p.getVelocity().add(p.getLocation().getDirection().multiply(3)).setY(1));
    undamageable.add(p);
                            }
     
                        }
       
        }
    }
     
                   
    @
    EventHandler
    public void anotherEvent(EntityDamageEvent event) {
            final 
    Entity e event.getEntity();
    if(
    instanceof Player) {
        if(
    undamageable.contains(e)) {
          
    event.setCancelled(true);
        
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
      public 
    void run() {
          
    undamageable.remove(e);
      }
    }, 
    5*20L);
        }
    }
     
               
    }
            }
     
  19. Offline

    raGan.

    There's no need to use scheduler at all. Just store each player's start time in the map, and then look how long passed since and if it's within the limit, cancel the event. If the difference is higher then the limit, remove it from the map.

    NEVER store player objects, use player names instead.
    Code:
    static final long LIMIT = 3000; // 3 seconds
    final Map<String, Long> launchTimes = new HashMap<String, Long>();
     
    //put players you need when they are launched
    launchTimes.put(e.getPlayer().getName(), System.currentTimeMillis());
     
    //then check in damage event
    Long time = launchTimes.get(e.getPlayer().getName());
    if(time != null) {
        if(System.currentTimeMillis() - time < LIMIT) {
            e.setCancelled(true);
        }
        else {
            launchTimes.remove(e.getPlayer().getName());
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  20. Offline

    Samthelord1

    Chinwe raGan. should /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package me.samthelord1.launchpads;

    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    /**
    *
    * @author Sam
    */
    public class Main extends JavaPlugin implements Listener {
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);

    }



    @EventHandler
    public void onPlayerStep(PlayerInteractEvent e) {
    if(e.getAction().equals(Action.PHYSICAL)) {
    Player p = e.getPlayer();
    if(e.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
    Block b = e.getClickedBlock().getLocation().subtract(0, 1, 0).getBlock();

    if(b.getType().equals(Material.REDSTONE_BLOCK)) {
    p.sendMessage("Debugging..");
    p.setVelocity(p.getVelocity().add(p.getLocation().getDirection().multiply(3)).setY(1));
    }

    }

    }}


    @EventHandler
    public void anotherEvent(EntityDamageEvent event) {
    final Entity e = event.getEntity();
    if(e instanceof Player) {
    if(event.getCause().equals(DamageCause.FALL)) {
    event.setCancelled(true);
    }

    }
    }

    }
    work?
     
  21. Offline

    raGan.

    Samthelord1
    Sorry but I don't feel like reading not formatted code now.

    btw no, it will cancel all fall damage for all player all the time
     
  22. Offline

    Samthelord1

    raGan. with your code, its the exact thing that I'm annoyed about, from the event, you can't getPlayer() because its a entity, and you cant getName from a entity.
     
  23. Offline

    raGan.

    Samthelord1
    Cast it to a player then
    Code:
    if(e.getEntity() instanceof Player) {
        Player player = (Player) e.getEntity();
        // do stuff here
    }
     
  24. Offline

    Samthelord1

    raGan. I did but it still didn't work :/
     
  25. Offline

    raGan.

    Samthelord1
    You can't just say that it does not work. To help you, I need to see what doesn't work, and how it doesn't work.
     
  26. Offline

    Samthelord1

    raGan. I meant I tried it earlier, I can't remember but I think it was an error on line 50 or something, I'm not on my computer but ill tell you in about 8 hours

    Assist raGan. Chinwe heres my code, not sure why but the player's still taking damage
    PHP:
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package me.samthelord1.launchpads;
     
    import java.util.HashMap;
    import java.util.Map;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    /**
    *
    * @author Sam
    */
    public class Main extends JavaPlugin implements Listener {
        public 
    void onEnable() {
        
    getServer().getPluginManager().registerEvents(thisthis);
     
        }
        static 
    long LIMIT 3000// 3 seconds
    Map<StringLonglaunchTimes = new HashMap<>();
     
     
    @
    EventHandler
    public void onPlayerStep(PlayerInteractEvent e) {
        if(
    e.getAction().equals(Action.PHYSICAL)) {
        
    Player p e.getPlayer();
        if(
    e.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
            
    Block b e.getClickedBlock().getLocation().subtract(010).getBlock();
           
            if(
    b.getType().equals(Material.REDSTONE_BLOCK)) {
                
    p.sendMessage("Debugging..");
    p.setVelocity(p.getVelocity().add(p.getLocation().getDirection().multiply(3)).setY(1));
          
    launchTimes.put(p.getName(), LIMIT);
           
            }
     
                       
        }
       
        }}
     
                   
    @
    EventHandler
    public void anotherEvent(EntityDamageEvent event) {
    if(
    event.getEntity() instanceof Player) {
        
    Player player = (Playerevent.getEntity();
        
    Long time launchTimes.get(player.getName());
    if(
    time != null) {
        if(
    System.currentTimeMillis() - time LIMIT) {
            
    event.setCancelled(true);
        }
        else {
            
    launchTimes.remove(player.getName());
        }
    }
    }
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  27. Offline

    raGan.

    You need to put current time in lauchTime, not limit.
    Change
    Code:
    launchTimes.put(p.getName(), LIMIT);
    to
    Code:
    launchTimes.put(p.getName(), System.currentTimeMillis());
    I even gave you that code.
     
  28. Offline

    Samthelord1

Thread Status:
Not open for further replies.

Share This Page