Help with Bukkit API - I dont know what to use in order to accomplish a task

Discussion in 'Plugin Development' started by alta189, Apr 10, 2011.

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

    alta189

    I am relitively new to Java Programming, and I need help with a problem I am having.

    I do not know how to wait and then open a door. I currently have code that can open and close a door, but I do not know how to schedule an event. Help is appreciated. If you need more info, let me know what to tell you.

    Thanks
     
  2. Offline

    ssell

    @alta189

    As in delay it with a timer?

    There are four different types of scheduling: syncrhonous delayed/repeating, and asynchronous delayed/repeating. Delayed occurs only once, while repeating, well, repeats. For your purposes you want this (going off of memory so may be slighly off):

    Code:
    getServer( ).getScheduler( ).scheduleAsyncDelayedTask( yourPlugin, new Runnable( )
    {
        void run( )
        {
            //Your open/close code. Or call the method that does it.
        }
    }, ( long )#ofTicksUntilEventFires );
    
    #ofTicksUntilEventFires is how long to wait. There are 20 server ticks a second.

    http://javadoc.lukegb.com/BukkitJD/...kkit.plugin.Plugin, java.lang.Runnable, long)
     
  3. Offline

    alta189


    How do I pass a block to that run()
     
  4. Offline

    aranian

    Code:
    final Block block = yourEvent.getBlock(); // Declare a final variable that holds your block
    getServer( ).getScheduler( ).scheduleAsyncDelayedTask( yourPlugin,
    new Runnable( ) {
        void run( )
        {
            //Your open/close code. Or call the method that does it.
            // You can use that final variable here
        }
    }, ( long )#ofTicksUntilEventFires );
    
    There are other ways as well, for example implementing Runnable in an own class and using a constructor that accepts a Block object.
     
  5. Offline

    alta189

    @ssell
    Code:
        public void onPlayerInteract(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
            this.block2 = block;
            int type = block.getTypeId();
            int sec = plugin.getClosingTime();
            BukkitScheduler scheduler = plugin.getServer().getScheduler();
            scheduler.scheduleSyncDelayedTask(plugin, new Runnable()
            {
    
                          public void run() {
                            Block upperblock = this.block2.getFace(BlockFace.UP);
                            if (this.block2.getData() == 4)
                              this.block2.setData(0);
                            else if (this.block2.getData() == 3) {
                              this.block2.setData(7);
                            }
                            if (upperblock.getType() != Material.WOODEN_DOOR) return;
                            if (upperblock.getData() == 12)
                              upperblock.setData(8);
                            else if (upperblock.getData() == 11)
                              upperblock.setData(15);
                          }
                        }
                }
            , sec * 20);
        }
    This is my code and I get an error in eclipse saying
     
  6. Offline

    ssell

    @alta189

    Where are you creating the block2 object? Looks like it is out of scope. Also you have some typos like:

    Code:
    this.blcokk
    You would want to do something like this :

    Code:
        @Override
        public void onPlayerInteract(PlayerInteractEvent event) 
        {
            //Was a door clicked?
            if( event.getClickedBlock( ).getType( ).equals( Material.WOODEN_DOOR ) )
            {
                final Block block2 = event.getClickedBlock( );
                
                plugin.getServer( ).getScheduler( ).scheduleSyncDelayedTask( plugin, new Runnable( )
                {
                    public void run( )
                    {
                        //Make sure it is still a door
                        if( !block2.getType( ).equals( Material.WOODEN_DOOR ) )
                        {
                            return;
                        }
                        
                        Block upperBlock = block2.getFace( BlockFace.UP );
                        
                        if( block2.getData( ) == 4 )
                        {
                            block2.setData( ( byte )0 );
                        }
                        else if( block2.getData( ) == 3 )
                        {
                            block2.setData( ( byte )7 );
                        }
                        
                        if( upperBlock.getData( ) == 12 )
                        {
                            upperBlock.setData( ( byte )8 );
                        }
                        else if ( upperBlock.getData( ) == 11 )
                        {
                            upperBlock.setData( ( byte )15 );
                        }    
                    }
                }, plugin.getClosingTime( ) * 20 );    
            }
    
    If you need to update a block outside the scope of run() ( such as one owned by the playerListener ), you could always make and call a method such as updateBlocks( block2, upperBlock ).
     
  7. Offline

    alta189

    @ssell
    @aranian

    Thanks for all your help!!! I got it working now!
     
Thread Status:
Not open for further replies.

Share This Page