I want this code to apply to a single block

Discussion in 'Plugin Development' started by lebg, Jul 7, 2021.

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

    lebg

    Hello!
    I made a launchpad code but it applies to all "LIGHT_WEIGHTED_PRESSURE_PLATE" and I want it to apply to one.

    Is this possible, please?
    Thank you for reading!
     

    Attached Files:

  2. Online

    timtower Administrator Administrator Moderator

    @lebg Save the location of the block.
     
  3. Offline

    Strahan

    Another possibility if you intend for it to apply to literally any of that type of material is to just first thing check if the interacted block is not of that type and return if that's the case.
     
  4. Offline

    lebg

    Thank you very much for your answer!
    I tested what you said but it doesn't work unless I did it wrong


    Location loc2 = new Location(Bukkit.getWorld("world"), 43.589 ,155 ,-8.700);
    if(player.getLocation() == loc2 && player.getLocation().getBlock().equals(Material.LIGHT_WEIGHTED_PRESSURE_PLATE)) {

    Thank you very much for your answer!

    Okay okay I see what you mean

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2021
  5. Online

    timtower Administrator Administrator Moderator

    @lebg A block will not equal a material, location is never 100% accurate like that.
    Need to compare the block locations.
     
  6. Offline

    KarimAKL

    That is what they are already doing, but just without a guard clause. :p

    A few points about this piece of code:
    • A block's location is just integers, so you cannot use your player's coordinates to get the block. The debug screen (F3) in Minecraft allows you to see the coordinates of the block you are looking at.
    • Comparing the player's location with your newly created location using the equality operator (==) will always be false. You should compare two objects with the #equals method instead of the equality operator unless they are constants.
    • You are comparing the block itself with the pressure plate material, not the block's material. Also, it is preferred to compare enums with the equality operator since they are constants.
     
  7. Offline

    lebg

    I've been trying several of the methods you've recommended for 5 hours, but I can't seem to do it.

    Pleeaase Help me :'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(
     
  8. Offline

    KarimAKL

    @lebg Can you post your current code?
     
  9. Offline

    lebg


    Code:
    @EventHandler
       
        public void onInteract(PlayerInteractEvent event) {
           
        Player player = event.getPlayer();
       
       
           
        if(event.getAction() == Action.PHYSICAL) {   
           
           
            
           
        if(event.getClickedBlock().getType() == Material.LIGHT_WEIGHTED_PRESSURE_PLATE){
           
           
        Vector velocity = new Vector(0, 0.5, -2.5);
        event.getPlayer().setVelocity(velocity);
       
       
        player.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, player.getLocation(), 50);
        player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_BELL,1, 1);
       
        }
        }
     
  10. Offline

    KarimAKL

    @lebg You never compare any locations in that code.
     
Thread Status:
Not open for further replies.

Share This Page