Lock Unlock & Events - LegendOnline/InventoryAPI GitHub Wiki

Lock/Unlock:

What is locking / unlocking? In fact, its really simple! Generally every GUIElement and GUIContainer is locked by default. This means no Element can be moved nor taken out. By calling the .unlock() method, which is implemented by any kind of element and container the specific element will be unlocked, thus it can be taken out. Calling .unlock() on an container will make all its contents able to be taken out.

What we want to achieve:

We want to be able to take out the diamond block of the inventory, created in the previous tutorial. But how we do that in particular?

Code:

//Let's take the previously used example:
ExactLayout layout = new ExactLayout("rrrgggrrr",
                                     "bbbgdgbbb",
                                     "rrrgggrrr");

layout.set('r', new GUILabel("red", Material.STAINED_GLASS_PANE,(byte) 14));
layout.set('g', new GUILabel("gold", Material.GOLD_BLOCK));
layout.set('b', new GUILabel("black", Material.STAINED_GLASS_PANE,(byte) 15));
//But now we are saving our Button in a variable to be able to manipulate its default settings
GUIButton diamond =  new GUIButton("diamond", Material.DIAMOND_BLOCK);
diamond.unlock(); // this is literally where all the magic happens... Yeah that all about it
//You noticed we are using a button instead of a label. The reason is consistency, a label should not
//be able to be moved (it is just an object to display text)

layout.set('d', diamond);
McGui gui = new McGui(Test.plugin,"First Steps", layout);
gui.draw(player);

Events:

The built-in event system is also similar to the one you know from swing. Every GuiComponent has the ability to register events with the addEvent() method In addition, events can be removed from components by calling removeEvent(). This is automatically happening by suspending a McGUI or a GUISubContainer to avoid unused event calls.

What we want to achieve:

We want to be able to click on one of the gold blocks and change it into an emerald block Furthermore a nice little chat message should be displayed to the player

Code:

ExactLayout layout = new ExactLayout("rrrgggrrr",
                                     "bbbgdgbbb",
                                     "rrrgggrrr");

layout.set('r', new GUILabel("red", Material.STAINED_GLASS_PANE,(byte) 14));
layout.set('b', new GUILabel("black", Material.STAINED_GLASS_PANE,(byte) 15));

//Again we safe the object to manipulate it later on
GUIButton gold = new GUIButton("gold", Material.GOLD_BLOCK);

gold.addEvent( new GUIEvent() { //Lets add an event by creating an anonymous inner class such as in swing
    public void onClick( ComponentClickEvent event ) { //here we have multiple events we can choose to override
        ((GUIButton)event.getComponent()).setIcon( Material.EMERALD_BLOCK );
        //We can safely cast the clicked component to a button since we know it has to be one of the gold blocks
        //We use a button because we want to interact with the component, since label are there to display text
        ( (GUIButton) event.getComponent() ).draw(); //We have to redraw the Component (label) to apply our changes
        //And of course, do not forget the message!
        event.getPlayer().sendMessage( "Hey you clicked the gold block!" ); 
    }
} );

layout.set('g', gold);

GUIButton diamond =  new GUIButton("diamond", Material.DIAMOND_BLOCK);
diamond.unlock();

layout.set('d', diamond);

McGui gui = new McGui(Test.plugin,"First Steps", layout);
gui.draw(player);

Result:

As you can see, we are able to take out the diamond block and click on the gold blocks to change them to emeralds and get a nice little message in the chat Next tutorial: Using different layouts

Articles you might be interested in: