How can I place blocks with code?

Discussion in 'Plugin Development' started by Arencos, Jul 3, 2020.

  1. Offline

    Arencos

    I am trying to add something to my plugin so it will place 2 obsidian blocks behind you when you walk. How can I do this? I know I need to use it with the PlayerMove event, but I dont know how to put 2 blocks behind the player. Thanks for any answers!! :D
     
    Last edited: Jul 3, 2020
  2. Offline

    KarimAKL

    @Arencos You're looking for Block#setType(Material).
     
  3. Offline

    Arencos

    @KarimAKL OK, but how do I get the block behind the player?? To get the location i could use getLocation(); , but how do I get the block behind and the one above the block behind??
     
  4. Offline

    KarimAKL

    @Arencos You can get the player's previous location using PlayerMoveEvent#getFrom(), then use Block#getRelative(BlockFace) to get the block above.
     
  5. Offline

    Arencos

    @KarimAKL Thanks, but when i try this, it wont place the block behind me. Instead it will place it on the player, causing them to suffocate and slide?? (I dont know why it will make the player slide). I looked at another post and kinda merged my code with it, here is the code:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
    HashSet<Material> transparent = new HashSet<Material>();
    transparent.add(Material.ENDER_CHEST);
    Player player = event.getPlayer();
    Block block = player.getTargetBlock(transparent, 120);
    Location location = event.getFrom();
    location.getBlock().setType(Material.BEDROCK);
    }

    (Ender chest is just for testing purposes :D)

    Here is a video of what is happening:
     
    Last edited: Jul 3, 2020
  6. Offline

    KarimAKL

    @Arencos Only set the block if the new location's block isn't the same as the old location's block.
     
  7. Offline

    Arencos

    @KarimAKL How can i do that? I tried this:
    if(location < recentLocation) {
    location.getBlock().setType(Material.BEDROCK);
    }
    But it says this:
    The operator < is undefined for the argument type(s) org.bukkit.Location, org.bukkit.Location

    Sorry if im being dumb. Its quite late rn :D
     
    Last edited: Jul 3, 2020
  8. Offline

    KarimAKL

    @Arencos You're using "Location is less than Location", that doesn't make a lot of sense, you need to check if it's a different location by doing "Location does not equal Location".
     
  9. Offline

    Arencos

    @KarimAKL I tried this rn, doesnt change anything for some reason?
    Code:
    Location location = event.getFrom();
    Location recentLocation = player.getLocation();
    if(!(recentLocation == location)) {
    location.getBlock().setType(Material.BEDROCK);
    }
     
  10. Offline

    KarimAKL

    @Arencos Use .equals() to compare objects and == to compare primitives.
     
  11. Offline

    Arencos

    @KarimAKL I changed it to equals and now, it wont place it if im sprinting for some reason?
     
  12. Offline

    KarimAKL

  13. Offline

    Arencos

    Here is the code:
    Location location = event.getFrom();
    Location recentLocation = player.getLocation();
    if(!(recentLocation.equals(location))) {
    location.getBlock().setType(Material.BEDROCK);
    }
    }

    Thanks so much for replying :D
     
  14. Offline

    mAndAle

    I think you have found one of the many versions of fastbridge (hack)
     
  15. Offline

    Strahan

    Remember, events run BEFORE the event's action takes place. Otherwise, one couldn't cancel them. So PlayerObject.getLocation() will be the same as EventObject.getFrom(). You need to use EventObject.getTo() to reference where they are going.

    I usually filter my PME as such:
    Code:
    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        if (e.getFrom().getBlockX() == e.getTo().getBlockX() &&
            e.getFrom().getBlockY() == e.getTo().getBlockY() &&
            e.getFrom().getBlockZ() == e.getTo().getBlockZ()) return;
    
      // do whatever
    }
    Using Location divergence alone would mean the code fires if they even move their head.
     
    KarimAKL likes this.
  16. Offline

    Arencos

    thanks, i will try that :D OK i just tried it, it doesnt really change anything and I dont know why? Maybe I wrote it wrong? Code:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            if(e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) {
                Location location = e.getFrom();
                location.getBlock().setType(Material.BEDROCK);
            }
        }
    Maybe it will work if I put a little delay before placing the block so the player has time to walk?
     
    Last edited: Jul 4, 2020
  17. Offline

    KarimAKL

    @Arencos That code will only run when you don't move XYZ, you want to do the opposite; check the yaw and pitch instead.
     
  18. Offline

    Arencos

    I have no idea how to do that I will google it rn, if i cant find it i will come back :D

    EDIT: I couldnt find anything but just one forum post which says there is no yaw and pitch in blocks? Here is an image.

    https://prnt.sc/tbuoop

    Or are you saying I should check the players yaw and pitch?
    [​IMG]
     
    Last edited: Jul 4, 2020
  19. Offline

    KarimAKL

    Blocks are "facing" a specific way. They use BlockFace.

    I meant the player's new location compared to their old location.
     

Share This Page