Solved Door opening problems

Discussion in 'Spigot Plugin Help' started by Destro122, Feb 22, 2020.

  1. Hello, I don't understand why but the door is opening when i only use one condition but when I add the "else" it broke somehow.
    Code (Java):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            Action act = e.getAction();
            Player p = e.getPlayer();
            if(act.equals(Action.RIGHT_CLICK_BLOCK)) {
                Block clicked = e.getClickedBlock();
                BlockState bc = clicked.getState();
                if(bc.getType() == Material.IRON_DOOR_BLOCK) {
                    Door d = (Door) bc.getData();
                    if(d.isTopHalf() == true){
                        clicked = clicked.getRelative(BlockFace.DOWN);
                        bc = clicked.getState();
                        d = (Door) bc.getData();
                        Openable o = (Openable) bc.getData();
                        if(d.isOpen() == true){
                        o.setOpen(false);
                        } else {
                            o.setOpen(true);
                        }
                        bc.setData((MaterialData) o);
                        bc.update();
                        p.playSound(p.getLocation(), Sound.BLOCK_LADDER_PLACE, 10, 29);
                        System.out.println(d.isOpen());
                        return;
                        }
                    }
                   
                }
            }
        }
     
    thank you !
     
  2. use act == for enums instead of act.equals
    And where exactly is it breaking?
    Code (Text):
    if(d.isOpen() == true){
                        o.setOpen(false);
                        } else {
                            o.setOpen(true);
                        }
    here?
    btw you can simplify d.isOpen() == true with d.isOpen() and also you could definetily just do o.setOpen(!d.isOpen());
     
  3. I changed :
    Code (Text):
    if(act == Action.RIGHT_CLICK_BLOCK) {
    #And
    o.setOpen(!d.isOpen());
    But when I click on the door, i got this in console :
     

    Attached Files:

  4. PlayerInteractEvent is fired twice for each hand. Check hand first - if(event.getHand() != EquipmentSlot.hand) return;
     
  5. I add it to my code but it keep send me : upload_2020-2-22_11-30-59.png
    So i guess it cancel the door opening by happening twice but how can I correct it ?
     
  6. Ok so I tested it in another server and it worked perfectly ! Thx you !