Getting location of a placed block

Discussion in 'Plugin Development' started by TheAnswerIsMinecraft, Dec 30, 2013.

Thread Status:
Not open for further replies.
  1. So, I am coding an endermage kit and I want to get the location of the placed portal block, and teleport a player to it, how would i do this? Thanks!
     
  2. Offline

    EcMiner

    In BlockPlaceEvent you can get the location of the block: event.getBlock().getLocation()
     
  3. EcMiner
    if i did
    Code:java
    1. @EventHandler
    2. public void onPlace(BlockPlaceEvent e){
    3. if (e.getBlock().getType().equals(Material.PORTAL)){
    4. Location loc1 = e.getBlock().getLocation();{
    5. }
    6. }
    7. }

    how would I be able to access that in a different eventhandler?
     
  4. Offline

    GaaTavares

    What do you mean?
    You could just make a "global location", then changing it..
    Location block = null;
    then in event, just set it to the location.
     
  5. Offline

    EcMiner

    @TheAnsweIsMinecraft Store it in a list.

    GaaTavares that's not a solution. Let's say 1 guy places a portal and you save it to your "global location" and someone else does the same thing just after that, that would override the already existing location, which makes it impossible to access later on because when you try that you would get the location of the block of someone else...
     
  6. Offline

    GaaTavares

    Yes I know that's just for 1 guy, but he didn't explain really what he wants x_x
    That's what i was going to say.
     
  7. GaaTavares EcMiner
    Endermage is a kit where the endermage places down his portal and players near him, and him, are teleported to that location. here is my code:
    (First EventHandler)
    Code:
        @EventHandler
        public void onPlayerEndermage(PlayerInteractEvent e){
          Player p = e.getPlayer();
          if (endermage.contains(p.getName())){
          if (e.getPlayer().getItemInHand().getType().equals(Material.PORTAL) && (e.getItem().getTypeId() == Material.PORTAL.getId()) && (e.getAction() == Action.RIGHT_CLICK_BLOCK) && (e.getClickedBlock() != null)){
            e.setCancelled(true);
            Material item = e.getItem().getType();
            if (item == Material.PORTAL) {
              List<Entity> nearby = p.getNearbyEntities(3.0D, 256.0D, 3.0D);
              for (Entity ent : nearby){
                if (ent instanceof Player){
                  ((Player)ent).teleport(loc1);
                  p.teleport(loc1);
                }
              }
              if (nearby.size() >= 1) {
                p.sendMessage(ChatColor.RED + "Teleported " + ChatColor.GREEN + ((Player)nearby).getName() + " to you!");
                ((Player) nearby).sendMessage(ChatColor.RED + "You have been teleported to " + ChatColor.GREEN + p.getName());
                  }
              }
            }
          }
        }
    Second EventHandler:
    Code:java
    1. @EventHandler
    2. public void onPlace(BlockPlaceEvent e){
    3. if (endermage.contains(e.getPlayer())){
    4. if (e.getBlock().equals(Material.PORTAL)){
    5. Location loc1 = e.getBlock().getLocation();
    6. }
    7. }
    8. }

    How exactly should I approach this?

    ok so i tried doing: Location loc1 = (Location) e.getClickedBlock();
    and teleporting the players there but it didn't work. Help? GaaTavares EcMiner

    Ok I got it thanks everyone lol

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

    aredherring

    Post your solution?
     
  9. aredherring
    Sure: using block place event rather than interact
    Code:java
    1. @EventHandler
    2. public void onPlayerEndermage(BlockPlaceEvent e){
    3. Player p = e.getPlayer();
    4. if (endermage.contains(p.getName())){
    5. if (e.getBlockPlaced().getType() == Material.PORTAL){
    6. e.setCancelled(true);
    7. final Block b = e.getBlock();
    8. Location loc1 = b.getLocation();
    9. List<Entity> nearby = p.getNearbyEntities(3.0D, 256.0D, 3.0D);
    10. for (Entity ent : nearby){
    11. if (ent instanceof Player){
    12. ((Player)ent).teleport(loc1);
    13. p.teleport(loc1);
    14. }
    15. }
    16. if (nearby.size() >= 1) {
    17. p.sendMessage(ChatColor.RED + "Teleported " + ChatColor.GREEN + ((Player)nearby).getName() + " to you!");
    18. ((Player) nearby).sendMessage(ChatColor.RED + "You have been teleported to " + ChatColor.GREEN + p.getName());
    19. }
    20. }
    21. }
    22. }
     
Thread Status:
Not open for further replies.

Share This Page