https://github.com/ultragrav/Fluid
Collaboration with Magnus Petz (https://github.com/MrMineO5)
Purpose
Fluid is a component-based framework for building Minecraft GUIs, written in Kotlin. MidnightSky (https://mattlack.com/project/1748549972604) has over 300 GUIs, and well over 100 of them are built with Fluid.
Problem
A Minecraft GUI is really a chest inventory: a grid of slots, 9 wide and up to 6 tall, where each slot holds one item. The Spigot API only lets you put items in slots by index, and gives you a click event with a slot index. That's fine for one button, but in a real menu you end up calculating slot numbers by hand, you can't reuse a piece of one menu in another, and moving a button means changing numbers all over the place.
Components
In Fluid, everything is a component with a width and a height. A component knows how to render itself into a grid of items, and receives clicks in its own coordinates, so it never needs to know where it is in the GUI. Containers hold other components, and a FluidGui is just a container the size of the inventory.
Here's a real GUI from MidnightSky, with two buttons placed manually:
class GuiKillEffects : FluidGui("Kill Effects".toComp(), 3) {
init {
val pvp = ButtonComponent(
MidnightItemBuilder(Material.PLAYER_HEAD)
.name("<a>PvP")
.addLoreComp("<gray>Click to configure PvP Kill effects!")
.build(),
clickHandler = { GuiSelectKillEffects(it.whoClicked.serverPlayer, KillEffectContext.PVP).open(it.whoClicked) }
)
val pve = ButtonComponent(
MidnightItemBuilder(Material.ZOMBIE_HEAD)
.name("<a>PvE")
.addLoreComp("<gray>Click to configure PvE Kill effects!")
.build(),
clickHandler = { GuiSelectKillEffects(it.whoClicked.serverPlayer, KillEffectContext.PVE).open(it.whoClicked) }
)
background = fillerItem(Material.BLACK_STAINED_GLASS_PANE)
addComponent(pvp, 2, 1)
addComponent(pve, 6, 1)
}
}
Some of the built-in components are:
ButtonComponentandRendererComponent(a button whose item is computed each time it's rendered)ContainerComponent, which holds other components and can lay them outListComponent, which is a KotlinMutableList. Adding, removing or replacing an element automatically re-renders just the slots that changed.
Flex Layout
Placing every component by hand gets old fast, so I wrote a layout system that works like a CSS flex box. If you add components without coordinates, the container lays them out for you, in rows that wrap, with the same justify and align options as CSS: start, center, end, space-between, space-around and space-evenly. There's an example of this in the FluidGUI section of the MidnightSky page.
The twist compared to CSS is that everything has to land on a whole slot. There's no such thing as a gap of 1.5 slots. So when a spacing mode can't divide the leftover space evenly, it falls back to the next closest one, from space-evenly to space-around to space-between, and finally to center:
Justify.SPACE_EVENLY -> {
val space = limit - group.size
if (space % (group.members.size + 1) != 0) return flex(group, Justify.SPACE_AROUND, limit)
val gap = space / (group.members.size + 1).coerceAtLeast(1)
var acc = gap
for (item in group.members) {
item.pos = acc
acc += item.size + gap
}
}
Containers can also be dynamic. A dynamic { } block rebuilds the container's children every time it renders, which is handy for things like a list of effects that can change while the GUI is open.
Updates
When something changes, you call update() on the component that changed, not on the whole GUI. The component renders itself and passes the result to its parent, which shifts it into its own coordinates and passes it up again, until it reaches the FluidGui at the top. Only the slots in that area are changed in the actual inventory. Small updates set slots one at a time, and anything bigger than 5 slots replaces the inventory contents all at once, so the client gets a single packet:
override fun update(area: Shape, solid: Solid) {
val numUpdates = area.iterator(dimensions).asSequence().count()
if (numUpdates <= 5) {
// Small update
for ((j, i) in area.iterator(dimensions).withIndex()) {
inv.setItem(i, solid.grid[j])
}
} else {
// Large update (send only one packet)
val contents = inv.contents
for ((j, i) in area.iterator(dimensions).withIndex()) {
contents[i] = solid.grid[j]
}
inv.contents = contents
}
}
Coroutines
Each GUI has its own coroutine scope. You can register tasks with task { } that start when the first player opens the GUI and are cancelled when the last player closes it, which is great for things like timers or values that update live. open() also returns a job that completes when the GUI is closed, so code can just suspend until the player is done with a menu.