Tubing Guis

Tubing GUIs is a framework for building Inventory GUIs easily.

It is build upon Tubing. So you need to build a Tubing plugin for this to work. Look into the Tubing Setup before continuing here.

Setup

Begin by adding the Tubing GUI dependency

<dependency>
    <groupId>be.garagepoort.mcioc</groupId>
    <artifactId>tubing-bukkit-gui</artifactId>
    <version>${tubing.version}</version>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

The basic GUI framework requires 3 main components. A GUI Controller, a TubingGui and the GuiActionService.

Resources

The below configuration is only needed if you are using tubing gui templates and the maven-resources-plugin.

It's best to exclude these files to prevent maven from injecting properties into the file.

<plugins>
    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
            <nonFilteredFileExtensions>
                <nonFilteredFileExtension>ftl</nonFilteredFileExtension>
            </nonFilteredFileExtensions>
        </configuration>
        ...
    </plugin>
</plugins>

Example

config.yml

tubing-example:
  # When enabled, the message broadcasted will be send across the bungee network
  broadcast-on-bungee: true
  broadcast-prefix: "&C[BROADCAST] &6"
  broadcast-messages:
    - Restart in 10 minutes
    - Restart in 5 minutes
    - Restart in 1 minutes
    - Network maintenance today

Gui Controller

The controller defines 2 actions. The message-select action will open the inventory which will show what message to broadcast.

@IocBean
@GuiController
public class BroadcastGuiController {

    @ConfigProperty("tubing-example.broadcast-messages")
    private List<String> predefinedMessages;

    private final BroadcastingService broadcastingService;

    public BroadcastGuiController(BroadcastingService broadcastingService) {
        this.broadcastingService = broadcastingService;
    }

    @GuiAction("broadcast/message-select")
    public GuiTemplate viewMessageSelect() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("messages", predefinedMessages);
        return template("gui/broadcast/message-select.ftl", params);
    }

    @GuiAction("broadcast/send-message")
    public void sendMessage(Player player, @GuiParam("message") String message) {
        broadcastingService.broadcast(player, message);
    }
}

Message select template

<#assign URLEncoder=statics['java.net.URLEncoder']>
<TubingGui size="54" id="broadcast-message-select">
    <title class="gui-title">Select message to broadcast</title>

    <#list messages as message >
        <GuiItem id="broadcast-message-${message?index}"
                 class="broadcast-message"
                 slot="${message?index}"
                 material="PAPER"
                 onLeftClick="broadcast/send-message?message=${URLEncoder.encode(message)}">
            <name class="item-name">${message}</name>
        </GuiItem>
    </#list>
</TubingGui>

Command

The broadcast command will open the select GUI if you do not provide it any argument. We can call any GUI action using the GuiActionService.

@IocBean
@IocCommandHandler("broadcast")
public class BroadcastCmd implements CommandExecutor {

    private final MessageService messageService;
    private final BroadcastingService broadcastingService;
    private final GuiActionService guiActionService;

    public BroadcastCmd(MessageService messageService, BroadcastingService broadcastingService, GuiActionService guiActionService) {
        this.messageService = messageService;
        this.broadcastingService = broadcastingService;
        this.guiActionService = guiActionService;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
        try {
            if (args.length == 0) {
                if (sender instanceof Player) {
                    guiActionService.executeAction((Player) sender, "broadcast/message-select");
                    return true;
                } else {
                    throw new BusinessException("Invalid arguments given for broadcast. Must provide a message");
                }
            }

            String message = JavaUtils.compileWords(args, 0);
            broadcastingService.broadcast(sender, message);
            return true;
        } catch (BusinessException e) {
            messageService.sendMessage(sender, "&6[Broadcasts] &C" + e.getMessage());
            return false;
        }
    }
}

Last updated