Click test

Discussion in 'Plugin Development' started by Clybzotik, Aug 20, 2018.

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

    Clybzotik

    Hello people!

    Of PlayerInteractEvent i take clicked block, how i can check the door is it?
     
  2. Offline

    The_Spaceman

    what do you want to check? if its a door?
     
  3. Offline

    Zombie_Striker

    @Clybzotik
    You need to check the material type of the Block. Either do a switch case or a bunch of if checks to find out which door the block is. Note that the material type does not store for whether it is the top block or bottom block, in case you were looking for that (you would need to get the block's state for that)
     
  4. Offline

    Clybzotik

    PHP:
    @EventHandler
        
    public void click(PlayerInteractEvent pie) {
            
    Player p pie.getPlayer();
            
    Action a pie.getAction();
            
    Block b pie.getClickedBlock();
            if(
    != Action.RIGHT_CLICK_BLOCK) return;
            if((
    b.getType().equals(Material.ACACIA_DOOR)    || (b.getType().equals(Material.BIRCH_DOOR))  ||
               (
    b.getType().equals(Material.DARK_OAK_DOOR)) || (b.getType().equals(Material.IRON_DOOR))   ||
               (
    b.getType().equals(Material.JUNGLE_DOOR))   || (b.getType().equals(Material.SPRUCE_DOOR)) ||
               (
    b.getType().equals(Material.WOODEN_DOOR)))){
                if((
    b.getState() instanceof Door)) {
                    
    Door d = (Doorb;
                    
    d.setOpen(true);
                }
            }
          
        }
    Im added this if-s, but doors don`t open and any errors on a Console
     
  5. Offline

    timtower Moderator Moderator

    @Clybzotik Did you try to print the Material?
     
  6. Offline

    Zombie_Striker

    @Clybzotik
    1. You should have variables with one character names. It will make reading the code and adding onto the code later more confusing than it needs to be.
    2. Use == when comparing enum values (i.e when comparing Material types)
    3. If you don't need to check for specific types, just check if the block has a BlockState and if the state is an instanceof Door. This will mean you won't need to check the material type.
     
  7. Offline

    Clybzotik

    @Zombie_Striker
    PHP:
    if(block.getState() == null) return;
                if((
    block.getState() instanceof Door)) {
                    
    Door d = (Doorblock;
                    
    d.setOpen(true);
                } 
    Im added this check, and delete check of the material type, its true?
    (Sorry for my English)
     
    Last edited: Aug 21, 2018
  8. Offline

    Zombie_Striker

    @Clybzotik
    The only issue is the "d". You're checking if the state is equal to the door in the first line (which is correct), but then you are casting the block, not the state, to a Door. You need to cast the block's state to a door.
     
  9. Offline

    Clybzotik

    @Zombie_Striker
    PHP:
    if(!(pie.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            
    Block block pie.getClickedBlock();
            if(
    block.getState() == null) return;
            if((
    block.getState() instanceof Door)) {
                
    Door d = (Doorblock.getState();
                
    d.setOpen(d.isOpen());
            } 
        }
    I did what you said but the door does not open
     
  10. Offline

    bennie3211

    It is not opening because you set the door state to 'closed' due d.isOpen() returns false when its closed. So change that to true like d.setOpen(true); to make it open and d.setOpen(false) to close it.
     
  11. Offline

    Clybzotik

    I set d.setOpen true, but only wooden door open.
     
  12. Offline

    Zombie_Striker

    @Clybzotik
    By setting the state equal to whether the door is open, you are not changing anything. If the door is open (isOpen==true), then you are setting it so the door stays open (since it would be the same as setOpen(true)). If it is closes, isOpen==false, so you would be setting it so it stays closed (setOpen(false)).

    What you want to do is Invert the boolean. Use d.setOpen( ! d.isOpen) to invert the boolean ( The ! symbol turns true values to false, and false to true.)
     
  13. Offline

    Clybzotik

    Here's my code, what I doing wrong?
    PHP:
    @EventHandler 
        
    public void click(PlayerInteractEvent pie) {
            if(!(
    pie.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            
    Block block pie.getClickedBlock();
            if(
    block.getState() == null) return;
            if((
    block.getState() instanceof Door)) {
                
    Door d = (Doorblock.getState();
                
    d.setOpen(!d.isOpen());
            } 
        }
    When I click on the iron door nothing happens.
     
  14. Offline

    Zombie_Striker

    @Clybzotik
    Try sending a player a message if the state is an instanceof door. Also try sending a message if the state is null. Doing this will show the following:
    1. If you only see the message when the state is null, then you know that iron doors somehow don't have states.
    2. If you only see the door message, you know that setOpen method does not work, and you may need to report the problem to spigot/update your server.
    3. If you don't see any messages, that means the state is not a door, but the state exists, and you will need to check what the state is actually an instanceof (do this by sending a player a message, but use the state.toString() to see what it is)
     
  15. Offline

    timtower Moderator Moderator

  16. Offline

    Clybzotik

    upload_2018-8-23_18-9-15.png
    I get a iron door state, what does it mean?
     
  17. Offline

    timtower Moderator Moderator

    Did you update the blockstate?
     
  18. Offline

    Clybzotik

    When i try to update i got this error.
    Type mismatch: cannot convert from boolean to BlockState
     
  19. Offline

    timtower Moderator Moderator

  20. Offline

    Clybzotik

    BlockState bs = block.getState().update();
     
  21. Offline

    timtower Moderator Moderator

    @Clybzotik It is purely a method call. You don't assign it to something.
     
Thread Status:
Not open for further replies.

Share This Page