Self Updating Items - landonjw/GooeyLibs GitHub Wiki
GooeyLibs 2.0 now allows for the ability to update contents on a page, and have those contents be updated for every player that has it open. This allows for tons of options, including entire games being created in a single user interface (see Asteroids in cookbook)
Updating
When PageBase#setTemplate
, PageBase#setTitle
are used, this will update the page and refresh all contents of every user with the page open.
When TemplateSlot#setButton
is used and it is linked to an open Page
, it will send that specific slot to every user with it open.
When ButtonBase#setDisplay
is used and it is linked to an open Page
, it will send that specific slot to every user with it open.
Examples
As a base, every method will have this as context before it.
GooeyButton button = GooeyButton.builder()
.display(new ItemStack(Items.DIAMOND))
.title("foo")
.build();
ChestTemplate template = ChestTemplate.builder(6)
.fill(button)
.build();
GooeyPage page = GooeyPage.builder()
.template(template)
.title("bar")
.build();
UIManager.openUIForcefully(player1, page);
UIManager.openUIForcefully(player2, page);
Changing a Page
's template:
Button button2 = GooeyButton.builder()
.display(new ItemStack(Items.EMERALD))
.title("bar")
.build();
Template template2 = DispenserTemplate.builder()
.fill(button2)
.build();
page.setTemplate(template2);
This will automatically change the UI to a dispensor full of emeralds for player1
and player2
.
Changing a button on a Page
:
GooeyButton button2 = GooeyButton.builder()
.display(new ItemStack(Items.EMERALD))
.title("bar")
.build();
page.getTemplate().getSlot(0).setButton(button2);
This will automatically change the first slot on the UI to an emerald button for player1
and player2
.
Forcing an update on a Page
:
You are able to force refresh a Page
for all viewers at any time with BasePage.update()
. This typically isn't necessary for Gooey's base implementations, but can be useful if you are manipulating view state in a subclass extending BasePage
.
Custom Implementations - Concepts
The primary concept that allows for pages to be updateable is the observer pattern. It is recommended you understand this pattern if you want to create sub-classes of Pages, Templates, and Buttons that are not static. The default implementations supplied in GooeyLibs already implement this pattern and as such will require no work to make pages updateable.
You can see the implementations used in GooeyLibs in Subject
, EventEmitter
, and UpdateEmitter
.
For examples of custom page implementations being used, see Asteroids and Snake Game in the cookbook.