Annotations

This page describes all annotations provided by the Tubing framework.

@IocBean

This is the most used annotation. It registers the class annotated with this bean inside the ioc container of Tubing. Making it a bean. The class can now be injected into other Tubing beans, or can inject other Tubing beans in its constructor.

Property

conditionalOnProperty

you can specify this optional property inside the annotation if you want to conditionally register this bean. The bean will only be created if the property specified can be found and has the specified value.

example simple bean:

@IocBean
public class MessageService {

    public void sendMessage(CommandSender player, String message) {
        player.sendMessage(translateAlternateColorCodes('&', message));
    }
}

example conditional bean

@IocBean(conditionalOnProperty = "tubing-example.broadcast-on-bungee=true")
@IocListener
public class BroadcastedMessageBungeeSender implements Listener {

    private final BungeeService bungeeService;

    public BroadcastedMessageBungeeSender(BungeeService bungeeService) {
        this.bungeeService = bungeeService;
    }

    @EventHandler
    public void onBroadcast(MessageBroadcastedEvent messageBroadcastedEvent) {
        Player player = Bukkit.getOnlinePlayers().iterator().next();
        bungeeService.sendMessage(player, Constants.BUNGEE_REPORT_MESSAGE_BROADCAST_CHANNEL, new BungeeBroadcastedMessage(messageBroadcastedEvent.getBroadcastedMessage()));
    }
}

@IocMultiProvider

Must be used on a class that will be a registered bean Must be used on a class implementing at least one interface

This annotation indicates that this bean can be used for list injection.

property

description

value

The interface that this bean extends and you want to list inject.

public interface SessionEnhancer {
    void enhance(PlayerSession playerSession);
}

@IocBean
@IocMultiProvider(SessionEnhancer.class)
public class VanishSessionEnhancer implements SessionEnhancer {
    @Override
    public void enhance(PlayerSession playerSession) {
        // do something
    }
}

@IocBean
@IocMultiProvider(SessionEnhancer.class)
public class FoodSessionEnhancer implements SessionEnhancer {
    @Override
    public void enhance(PlayerSession playerSession) {
        // do something
    }
}

@IocMulti

Used to inject a list of @IocMultiProvider beans.

property

description

value

The interface class you want to inject a list of

@IocBean
public class SessionManager {
    private static Map<UUID, PlayerSession> playerSessions = new HashMap<>();
    private final List<SessionEnhancer> sessionEnhancers;

    public SessionManager(@IocMulti(SessionEnhancer.class) List<SessionEnhancer> sessionEnhancers) {
        this.sessionEnhancers = sessionEnhancers;
        Bukkit.getOnlinePlayers().forEach(this::initialize);
    }

    public void initialize(Player player) {
        if (!has(player.getUniqueId())) {
            PlayerSession playerSession = new PlayerSession();
            sessionEnhancers.forEach(s -> s.enhance(playerSession));
            playerSessions.put(player.getUniqueId(), playerSession);
        }
    }
}

@TubingConfiguration

Notates a class as a tubing configuration class. This type of class can contain Tubing bean provider methods. Provider methods are used when you want to register a bean that is determined based on some logic that is more complex than can be handled by the @IocBean annotation

@TubingConfiguration
public class TubingConfiguration {

    @IocBeanProvider
    public static PermissionHandler instantiatePermissionHandler(Options options) {
        final PluginManager pluginManager = TubingExample.get().getServer().getPluginManager();
        Plugin gMplugin = pluginManager.getPlugin("GroupManager");
        if(gMplugin != null && gMplugin.isEnabled()) {
            TubingExample.get().getLogger().info("GroupManager found. Permissions will be handled by GroupManager");
            return new GroupManagerPermissionHandler(options);
        }

        RegisteredServiceProvider<Permission> registration = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
        if (registration != null) {
            TubingExample.get().getLogger().info("Vault found. Permissions will be handled by Vault");
            return new VaultPermissionHandler(options);
        }

        TubingExample.get().getLogger().info("Permissions handled by Bukkit");
        return new DefaultPermissionHandler(options);
    }
    
    @IocMultiProvider(ChatInterceptor.class)
    public static List<ChatInterceptor> loadChatInterceptors(StaffChatConfiguration staffChatConfiguration, SessionManagerImpl sessionManager, PermissionHandler permissionHandler, StaffChatServiceImpl staffChatService) {
        return staffChatConfiguration.getChannelConfigurations().stream()
            .map(c -> new StaffChatChatInterceptor(staffChatService, permissionHandler, sessionManager, c, staffChatConfiguration))
            .collect(Collectors.toList());
    }
}

@IocBeanProvider

Can only be used on methods inside a Tubing Configuration class. Inside the arguments of this method we can use tubing beans. Tubing will inject the beans when using this method when creating the bean.

@IocCommandHandler

Specify this bean as a command executor. More info

property

description

value

The name of the command as specified in the plugin.yml

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

    private final MessageService messageService;
    private final BroadcastingService broadcastingService;

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
     ...
    }
}

@IocListener

Specify this bean as a Bukkit event listener.

@IocListener
public class PlayerJoinListener implements Listener {

    @EventHandler(priority = EventPriority.NORMAL)
    public void onJoin(PlayerJoinEvent event) {
        ...
    }
}

@IocMessageListener

Specify this bean as a Bungee message listener.

@IocMessageListener(channel = "BungeeCord")
public class BroadcastedMessageBungeeReceiver implements PluginMessageListener {

    private final BungeeService bungeeService;

    public BroadcastedMessageBungeeReceiver(BungeeService bungeeService) {
        this.bungeeService = bungeeService;
    }

    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        Optional<BungeeBroadcastedMessage> bungeeMessage = bungeeService.handleReceived(channel, BUNGEE_REPORT_MESSAGE_BROADCAST_CHANNEL, message, BungeeBroadcastedMessage.class);
        bungeeMessage.map(BungeeBroadcastedMessage::getBroadcastedMessage).ifPresent(b -> Bukkit.getPluginManager().callEvent(new BroadcastedMessageReceivedBungeeEvent(b)));
    }
}

property

Description

channel

The channel on which the listener should listen.

@ConfigProperty

This annotation can only be used on fields of Tubing beans. Inject a property from the config file inside a bean.

Property

Description

value

The config yml path.

Config

tubing-example:
  broadcast-prefix: "&C[BROADCAST] &6"
@IocBean
public class Configuration {

    @ConfigProperty("tubing-example.broadcast-prefix")
    public String broadcastPrefix;
}

@ConfigTransformer

This annotation must be used on a field annotated with the @ConfigProperty annotation. The ConfigTransformer class must implement the IConfigTransformer interface. Specify a class which will be used to transform the property before it gets injected into the bean

Property

Description

value

The transformer class to use

@IocBean
public class Configuration {

    @ConfigProperty("multiline-property")
    @ConfigTransformer(MessageMultiLineTransformer.class)
    public List<String> broadcastPrefix;
}

Transformer

public class MessageMultiLineTransformer implements IConfigTransformer<List<String>, String> {
    @Override
    public List<String> mapConfig(String value) {
        return Arrays.asList(commas.split("\\s*,\\s*"));
    }
}

Last updated