Skip to content

Example Usage

Adrian edited this page Apr 24, 2023 · 3 revisions

Paper

package io.github.miniplaceholders.paper.exampleexpansion;

import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import io.github.miniplaceholders.api.Expansion;
import io.github.miniplaceholders.api.MiniPlaceholders;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.Tag;

public final class ExampleExpansion extends JavaPlugin {

    @Override
    public void onEnable(){
        this.getSLF4JLogger().info("Starting Example Expansion");
        
        Expansion expansion = Expansion.builder("bukkit_example")
          // This will filter that the only audience that can be received must be an instance of a Player
          .filter(Player.class)
          // Registers a placeholder that requires an audience to be provided to function
          // Placeholder final name = <bukkit_example_player_name>
          .audiencePlaceholder("player_name", (aud, queue, ctx) -> Tag.selfClosingInserting(Component.text(((Player)aud).getName()))
          // Register a relational placeholder that requires 2 audiences to be provided
          // Placeholder final name = <bukkit_example_is_enemy>
          .relationalPlaceholder("is_enemy", (aud, otheraud, queue, ctx) -> {
              if(this.isEnemy((Player)aud, (Player)otheraud)){
                  return Tag.selfClosingInserting(Component.text("Enemy", NamedTextColor.RED));
              }
              return Tag.selfClosingInserting(Component.text("Neutral", NamedTextColor.GRAY));
          })
          // Registers a placeholder that does not require any audience for it to return a value
          // Placeholder final name = <bukkit_example_online_players>
          .globalPlaceholder("online_players", (queue, ctx) -> Tag.selfClosingInserting(Component.text(this.getServer().getOnlinePlayers().size())))
          // This will create the expansion
          .build();

        // This will register the expansion so that it can be used with the static methods in MiniPlaceholders
        expansion.register();
    }

    private boolean isEnemy(Player one, Player two){
      return one.getName().equals("James") && two.getName().equals("Juan");
    }

}

Velocity

package io.github.miniplaceholders.velocity.exampleexpansion;

import com.google.inject.Inject;

import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;

import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;

import org.slf4j.Logger;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.Tag;

import io.github.miniplaceholders.api.Expansion;

@Plugin(
    name = "ExampleExpansion",
    id = "exampleexpansion",
    version = "1.0.0",
    authors = {"4drian3d"}
)
public final class ExampleExpansion {
    private final Logger logger;
    private final ProxyServer proxy;
    
    @Inject
    public ExampleExpansion(Logger logger, ProxyServer proxy){
        this.logger = logger;
        this.proxy = proxy;
    }

    @Subscribe
    public void onProxyInitialization(ProxyInitializeEvent event) {
        Expansion expansion = Expansion.builder("velocity_example")
            // This will filter that the only audience that can be received must be an instance of a Player
            .filter(Player.class)
            // Registers a placeholder that does not require any audience for it to return a value
            // Placeholder final name = <velocity_example_online_players>
            // This placeholder can optionally accept only one server name argument
            .globalPlaceholder("online_players", (queue, ctx) -> {
                if(queue.hasNext()){
                    String server = queue.pop().value();
                    return Tag.selfClosingInserting(Component.text(proxy.getServer(server).map(sv -> sv.getPlayersConnected().size()).orElse(0)));
                }
                return Tag.selfClosingInserting(Component.text(proxy.getPlayerCount()));
            })
            // Registers a placeholder that requires an audience to be provided to function
            // Placeholder final name = <velocity_example_player_name>
            .audiencePlaceholder("player_name", (aud, queue, ctx) -> Tag.selfClosingInserting(Component.text(((Player)aud).getUsername()))
            // Register a relational placeholder that requires 2 audiences to be provided
            // Placeholder final name = <velocity_example_in_same_server>
            .relationalPlaceholder("in_same_server", (aud, otheraud, queue, ctx) -> {
                Optional<RegisteredServer> playerServer = ((Player)aud).getCurrentServer().map(ServerConnection::getServer);
                Optional<RegisteredServer> otherPlayerServer = ((Player)otheraud).getCurrentServer().map(ServerConnection::getServer);
                return Tag.selfClosingInserting(playerServer.equals(otherPlayerServer) ? Component.text("Yes", NamedTextColor.GREEN) : Component.text("NO", NamedTextColor.RED);             
            })
        // This will create the expansion
        .build();
      
      // This will register the expansion so that it can be used with the methods in MiniPlaceholders
      expansion.register();
    }
}

Fabric

package io.github.miniplaceholders.fabric.exampleexpansion;

import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//TODO
public class FabricMod implements ModInitializer {
    public static final Logger LOGGER = LoggerFactory.getLogger("fabric-expansion");

    @Override
    public void onInitialize() {
       final Expansion expansion = Expansion.builder("fabric-example")
             .build();
       expansion.register();
    }
}