Skip to content

Instantly share code, notes, and snippets.

@Sxtanna
Last active February 7, 2021 09:15
Show Gist options
  • Save Sxtanna/29244fd4fb02306292f6f982836b3a18 to your computer and use it in GitHub Desktop.
Save Sxtanna/29244fd4fb02306292f6f982836b3a18 to your computer and use it in GitHub Desktop.
Placeholder Plugin Wrapper
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Supplier;
/**
* Drag and Drop Placeholder Replacement Wrapper
*
* @see Replace#set(OfflinePlayer, String)
*/
public final class Replace implements BiFunction<@Nullable OfflinePlayer, @NotNull String, @NotNull String>
{
@NotNull
private static final String PAPI_PLUGIN_NAME = "PlaceholderAPI";
@NotNull
private static final String MVDW_PLUGIN_NAME = "MVdWPlaceholderAPI";
@Nullable
private static Replace instance;
/**
* @return The singleton instance of {@link Replace}
*
* @apiNote Lazily initializes a singleton instance of this wrapper,
* automatically registering the main two placeholder plugin integrations
*/
public static @NotNull Replace get()
{
// check if singleton is initialized
if (instance == null)
{
// create new instance
instance = new Replace();
// attempt to register placeholderapi
instance.attemptToRegister(PAPI_PLUGIN_NAME,
() -> me.clip.placeholderapi.PlaceholderAPI::setPlaceholders);
// attempt to register mvdwplaceholderapi
instance.attemptToRegister(MVDW_PLUGIN_NAME,
() -> be.maximvdw.placeholderapi.PlaceholderAPI::replacePlaceholders);
}
// return cached instance
return instance;
}
/**
* Convenience function for setting the placeholders using the singleton {@link Replace} instance
*
* @see Replace#apply(OfflinePlayer, String)
*/
@Contract(pure = true)
public static @NotNull String set(@Nullable final OfflinePlayer player, @NotNull final String string)
{
return get().apply(player, string);
}
/**
* A list of replacement functions, could technically be anything, but here we focus on these spigot plugins
*/
@NotNull
private final List<BiFunction<@Nullable OfflinePlayer, @NotNull String, @NotNull String>> replacers = new ArrayList<>();
/**
* Function for setting the placeholders using the stored replacer BiFunctions
*
* @param player The player to use as the context for the placeholders
* @param string The string containing placeholders
*
* @return A new string containing the replaced values for applicable placeholders
*/
@Contract(pure = true)
@Override
public @NotNull String apply(@Nullable final OfflinePlayer player, @NotNull final String string)
{
return this.replacers
// create stream of replacer functions
.stream()
// reduce the collection of replacers using the input string as our identity, and each function as the accumulator
.reduce(string,
(text, replacer) -> replacer.apply(player, text),
// this is kinda not what's supposed to happen as a combiner, this is not a parallel stream, so it doesn't matter anyway...
String::concat);
}
/**
* Checks if the source plugin is enabled, and attempts to register it's replacement function
*
* @param name The name of the source plugin
* @param supplier A supplier that returns the replacement function
*/
private void attemptToRegister(@NotNull final String name, @NotNull final Supplier<BiFunction<OfflinePlayer, String, String>> supplier)
{
// check if a plugin by this name is enabled
if (!Bukkit.getPluginManager().isPluginEnabled(name))
{
// if not, do nothing, fail gracefully
return;
}
try
{
// attempt to retrieve the replacer function from the supplier, and add it to our list
this.replacers.add(supplier.get());
}
catch (final Throwable ignored /* most likely would be a NCDFE */)
{
// maybe pass in a logger?
}
}
}
@Sxtanna
Copy link
Author

Sxtanna commented Feb 7, 2021

Plugin Description

// ....
softdepend:
  - "PlaceholderAPI"
  - "MVdWPlaceholderAPI"

Repository and Dependency

Gradle

repositories {
    // ....
    maven {
        url = uri("https://repo.mvdw-software.com/repository/public/")
    }
    maven {
        url = uri("https://repo.extendedclip.com/content/repositories/placeholderapi/")
    }
}

dependencies {
    // ....
    compileOnly("me.clip:placeholderapi:2.10.9") {
        transitive false
    }
    compileOnly("be.maximvdw:MVdWPlaceholderAPI:3.1.1-SNAPSHOT") {
        transitive false
    }
}

Maven

<repositories>
    // ....
    <repository>
        <id>mvdw-repo</id>
        <url>https://repo.mvdw-software.com/repository/public/</url>
    </repository>
    <repository>
        <id>papi-repo</id>
        <url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
    </repository>
</repositories>

<dependencies>
    // ....
    <dependency>
        <groupId>me.clip</groupId>
        <artifactId>placeholderapi</artifactId>
        <version>2.10.9</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>*</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>be.maximvdw</groupId>
        <artifactId>MVdWPlaceholderAPI</artifactId>
        <version>3.1.1-SNAPSHOT</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>*</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment