"Save" events

Discussion in 'Plugin Development' started by Permeer, Feb 2, 2015.

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

    Permeer

    Hey guys, i want to know how to "save" a event to happen when a metod is called i saw it before , but i dont found it , its something like this :

    Code:
    Craft c = new Craft(String name , List<ItemStack> items);
                   
    c.setResult(new Result(){
                    
        public void result(){
        c.getLocation().getBlock().setType(Material.FLOWER_POT);
                
       }
    
    });
     
    Last edited: Feb 2, 2015
  2. Offline

    Burnett

    @Permeer Please elaborate on what you want. Save how?
     
  3. Offline

    ChipDev

    grammer ples? ;-;
     
    ReadySetPawn likes this.
  4. Offline

    Permeer

    @Burnett , eg:
    * I create a new variable that the type is Craft ( Made by me ) *
    I want to save a result( (Event) ) that fires when something happen
    i saw in another plugin that use a metod like this >

    Code:
    c.setResult(new Result(){
                 
        public void result(){
        c.getLocation().getBlock().setType(Material.FLOWER_POT);
             
       }
    
    });
    But i dont know how to do methods like this, to save what will happen after the * requirement to enable that ( like a player click a item and i get that variable that have the result(Event) saved and it will execute that result) *.
     
  5. Offline

    Burnett

    @Permeer Afraid I am still confused. Your grammar doesn't help along with it being 23:45. You want to save it and execute the results? Why not just execute the results? Why do you need to save it?
     
  6. Offline

    1Rogue

    If you want to save code that should be executed later then you would do so with a Runnable, or a class that mimics the function of it (a functional interface). Then your "Craft" class would call upon the method of that Runnable/interface.
     
  7. Offline

    Permeer

    @1Rogue, @Burnett I will show by code.

    Code:
    public void onEnable() {
          
            // First of all , in the onEnable i will register all the possible recipes.
          
            // Its a craft system like survival games that you drop the items and check if you can do anything with they
          
            Craft c1 = new Craft("Campfire", Arrays.asList(new ItemStack(Material.FLINT_AND_STEEL), new ItemStack(Material.STICK)));
          
            // Craft will save the name, and the list of items to craft the campfire.
          
            c1.setResult(new ResultManager(){
              
                @Override
                public void onCraft(){
                    Location loc = c1.getLocation(); // Location of the craft
                          
                    loc.clone().add(0, -1, 0).getBlock().setType(Material.NETHERRACK);
                    loc.getBlock().setType(Material.FIRE);
                }
              
            });
          
            // Now it saved what will happen when all the needed items is dropped - The onCraft method.
            // to be automatic , the result (onCraft method) will be fired when any player drop all the items that he need to craft the campfire.
        }
     
  8. Offline

    1Rogue

    What's the confusion from my post? It's precisely that concept.
     
  9. Offline

    Permeer

    @1Rogue , sorry i didnt see your post. But a runnable can be a good ideia ? if i have 5 crafts , so , i will need 5 runnables running.
     
  10. Offline

    1Rogue

    They don't need to be running, you can simply pass a Runnable to your Craft class, and then save it for later and call run() when appropriate on the object.
     
Thread Status:
Not open for further replies.

Share This Page