Added MVdWPlaceholderAPI support and improved placeholders integration

#651
This commit is contained in:
Florian CUNY 2019-05-31 10:56:03 +02:00
parent a87ef8bff6
commit eebb2a982d
7 changed files with 216 additions and 34 deletions

10
pom.xml
View File

@ -52,6 +52,7 @@
<bstats.version>1.5</bstats.version>
<vault.version>1.7</vault.version>
<placeholderapi.version>2.10.1</placeholderapi.version>
<mvdwplaceholderapi.version>2.5.1-SNAPSHOT</mvdwplaceholderapi.version>
<githubapi.version>1.0</githubapi.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
@ -206,6 +207,15 @@
<version>${placeholderapi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<!-- We have to use a forked version of this api
because Maxim had the brilliant idea to make one
of the dependencies rely on an illegal artifact
thus making its repository unusable. -->
<groupId>com.github.Prouser123-forks</groupId>
<artifactId>MVdWPlaceholderAPI</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
<!-- Hooks -->
<dependency>
<groupId>us.dynmap</groupId>

View File

@ -17,8 +17,9 @@ import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.commands.BentoBoxCommand;
import world.bentobox.bentobox.hooks.DynmapHook;
import world.bentobox.bentobox.hooks.placeholders.MVdWPlaceholderAPIHook;
import world.bentobox.bentobox.hooks.MultiverseCoreHook;
import world.bentobox.bentobox.hooks.PlaceholderAPIHook;
import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook;
import world.bentobox.bentobox.hooks.VaultHook;
import world.bentobox.bentobox.listeners.BannedVisitorCommands;
import world.bentobox.bentobox.listeners.BlockEndDragon;
@ -154,6 +155,7 @@ public class BentoBox extends JavaPlugin {
getServer().getScheduler().runTask(instance, () -> {
final long enableStart = System.currentTimeMillis();
hooksManager.registerHook(new PlaceholderAPIHook());
hooksManager.registerHook(new MVdWPlaceholderAPIHook());
// Setup the Placeholders manager
placeholdersManager = new PlaceholdersManager(this);

View File

@ -0,0 +1,86 @@
package world.bentobox.bentobox.hooks.placeholders;
import be.maximvdw.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.api.user.User;
/**
* Provides interfacing needed to register and get placeholders from MVdWPlaceholderAPI.
* @author Poslovitch
* @since 1.5.0
*/
public class MVdWPlaceholderAPIHook extends PlaceholderHook {
public MVdWPlaceholderAPIHook() {
super("MVdWPlaceholderAPI");
}
@Override
public boolean hook() {
return true; // There are no special checks to run when hooking into MVdWPlaceholderAPI.
}
@Override
public String getFailureCause() {
return "the version of MVdWPlaceholderAPI you're using is incompatible with this hook";
}
/**
* {@inheritDoc}
*/
@Override
public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
PlaceholderAPI.registerPlaceholder(BentoBox.getInstance(), "bentobox_" + placeholder,
event -> replacer.onReplace(User.getInstance(event.getPlayer())));
}
/**
* {@inheritDoc}
*/
@Override
public void registerPlaceholder(@NonNull Addon addon, @NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
PlaceholderAPI.registerPlaceholder(BentoBox.getInstance(), addon.getDescription().getName().toLowerCase() + "_" + placeholder,
event -> replacer.onReplace(User.getInstance(event.getPlayer())));
}
/**
* {@inheritDoc}
* <b>This is not supported by MVdWPlaceholderAPI. #HighQualityContent.</b>
*/
@Override
public void unregisterPlaceholder(@NonNull String placeholder) {
// Do nothing: not supported by MVdW. #HighQualityContent
}
/**
* {@inheritDoc}
* <b>This is not supported by MVdWPlaceholderAPI. #HighQualityContent.</b>
*/
@Override
public void unregisterPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
// Do nothing: not supported by MVdW. #HighQualityContent
}
/**
* {@inheritDoc}
* <b>This is not supported by MVdWPlaceholderAPI. #HighQualityContent.</b>
* As a result, this will <b>always return {@code false}</b>.
*/
@Override
public boolean isPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
return false; // Do nothing: not supported by MVdW. #HighQualityContent
}
/**
* {@inheritDoc}
*/
@Override
@NonNull
public String replacePlaceholders(@NonNull Player player, @NonNull String string) {
return PlaceholderAPI.replacePlaceholders(player, string);
}
}

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.hooks;
package world.bentobox.bentobox.hooks.placeholders;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Material;
@ -19,13 +19,13 @@ import java.util.Map;
*
* @author Poslovitch
*/
public class PlaceholderAPIHook extends Hook {
public class PlaceholderAPIHook extends PlaceholderHook {
private BentoBoxPlaceholderExpansion bentoboxExpansion;
private Map<Addon, AddonPlaceholderExpansion> addonsExpansions;
public PlaceholderAPIHook() {
super("PlaceholderAPI", Material.NAME_TAG);
super("PlaceholderAPI");
this.addonsExpansions = new HashMap<>();
}
@ -46,23 +46,17 @@ public class PlaceholderAPIHook extends Hook {
}
/**
* Registers this placeholder into BentoBox's PlaceholderAPI expansion.
* @param placeholder the placeholder to register, not null
* @param replacer its replacement, not null
* @since 1.4.0
* {@inheritDoc}
*/
@Override
public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
bentoboxExpansion.registerPlaceholder(placeholder, replacer);
}
/**
* Registers this placeholder into this addon's PlaceholderAPI expansion.
* It will register the expansion if it previously did not exist.
* @param addon the addon, not null
* @param placeholder the placeholder to register, not null
* @param replacer its replacement, not null
* @since 1.4.0
* {@inheritDoc}
*/
@Override
public void registerPlaceholder(@NonNull Addon addon, @NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
// Check if the addon expansion does not exist
if (!addonsExpansions.containsKey(addon)) {
@ -75,20 +69,17 @@ public class PlaceholderAPIHook extends Hook {
}
/**
* Unregisters this placeholder from the BentoBox PlaceholderAPI expansion.
* @param placeholder the placeholder to unregister, not null
* @since 1.4.0
* {@inheritDoc}
*/
@Override
public void unregisterPlaceholder(@NonNull String placeholder) {
bentoboxExpansion.unregisterPlaceholder(placeholder);
}
/**
* Unregister this placeholder from this addon's PlaceholderAPI expansion.
* @param addon the addon, not null
* @param placeholder the placeholder to unregister, not null
* @since 1.4.0
* {@inheritDoc}
*/
@Override
public void unregisterPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
if (addonsExpansions.containsKey(addon)) {
addonsExpansions.get(addon).unregisterPlaceholder(placeholder);
@ -96,23 +87,17 @@ public class PlaceholderAPIHook extends Hook {
}
/**
* Checks if a placeholder with this name is already registered
* @param addon the addon, not null
* @param placeholder - name of placeholder
* @return {@code true} if a placeholder with this name is already registered
* @since 1.4.0
* {@inheritDoc}
*/
@Override
public boolean isPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
return addonsExpansions.containsKey(addon) && addonsExpansions.get(addon).isPlaceholder(placeholder);
}
/**
* Replaces the placeholders in this String and returns it.
* @param player the Player to get the placeholders for.
* @param string the String to replace the placeholders in.
* @return the String with placeholders replaced, or the identical String if no placeholders were available.
* @since 1.5.0
* {@inheritDoc}
*/
@Override
@NonNull
public String replacePlaceholders(@NonNull Player player, @NonNull String string) {
return PlaceholderAPI.setPlaceholders(player, string);

View File

@ -0,0 +1,70 @@
package world.bentobox.bentobox.hooks.placeholders;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
/**
* @author Poslovitch
* @since 1.5.0
*/
public abstract class PlaceholderHook extends Hook {
public PlaceholderHook(@NonNull String pluginName) {
super(pluginName, Material.NAME_TAG);
}
/**
* Registers this placeholder on the behalf of BentoBox.
* @param placeholder the placeholder to register, not null
* @param replacer its replacement, not null
* @since 1.5.0
*/
public abstract void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer);
/**
* Registers this placeholder on the behalf of this addon.
* @param addon the addon, not null.
* @param placeholder the placeholder to register, not null.
* @param replacer its replacement, not null.
* @since 1.5.0
*/
public abstract void registerPlaceholder(@NonNull Addon addon, @NonNull String placeholder, @NonNull PlaceholderReplacer replacer);
/**
* Unregisters this placeholder on the behalf of BentoBox.
* @param placeholder the placeholder to unregister, not null.
* @since 1.5.0
*/
public abstract void unregisterPlaceholder(@NonNull String placeholder);
/**
* Unregister this placeholder on the behalf of this addon.
* @param addon the addon, not null.
* @param placeholder the placeholder to unregister, not null.
* @since 1.5.0
*/
public abstract void unregisterPlaceholder(@NonNull Addon addon, @NonNull String placeholder);
/**
* Checks if a placeholder with this name is already registered
* @param addon the addon, not null
* @param placeholder this placeholder
* @return {@code true} if a placeholder with this name is already registered
* @since 1.5.0
*/
public abstract boolean isPlaceholder(@NonNull Addon addon, @NonNull String placeholder);
/**
* Replaces the placeholders in this String and returns it.
* @param player the Player to get the placeholders for.
* @param string the String to replace the placeholders in.
* @return the String with placeholders replaced, or the identical String if no placeholders were available.
* @since 1.5.0
*/
@NonNull
public abstract String replacePlaceholders(@NonNull Player player, @NonNull String string);
}

View File

@ -7,7 +7,8 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.hooks.PlaceholderAPIHook;
import world.bentobox.bentobox.hooks.placeholders.MVdWPlaceholderAPIHook;
import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook;
import world.bentobox.bentobox.lists.GameModePlaceholder;
import java.util.Arrays;
@ -37,6 +38,8 @@ public class PlaceholdersManager {
public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
// Register it in PlaceholderAPI
getPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(placeholder, replacer));
// Register it in MVdWPlaceholderAPI
getMVdWPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(placeholder, replacer));
}
/**
@ -54,6 +57,8 @@ public class PlaceholdersManager {
}
// Register it in PlaceholderAPI
getPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(addon, placeholder, replacer));
// Register it in MVdWPlaceholderAPI
getMVdWPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(addon, placeholder, replacer));
}
/**
@ -85,6 +90,7 @@ public class PlaceholdersManager {
public void unregisterPlaceholder(@NonNull String placeholder) {
// Unregister it from PlaceholderAPI
getPlaceholderAPIHook().ifPresent(hook -> hook.unregisterPlaceholder(placeholder));
// Not supported by MVdW
}
/**
@ -101,6 +107,7 @@ public class PlaceholdersManager {
}
// Unregister it from PlaceholderAPI
getPlaceholderAPIHook().ifPresent(hook -> hook.unregisterPlaceholder(addon, placeholder));
// Not supported by MVdW
}
/**
@ -108,10 +115,21 @@ public class PlaceholdersManager {
* @return Optional containing the PlaceholderAPIHook instance or an empty Optional otherwise.
* @since 1.4.0
*/
@NonNull
private Optional<PlaceholderAPIHook> getPlaceholderAPIHook() {
return plugin.getHooks().getHook("PlaceholderAPI").map(hook -> (PlaceholderAPIHook) hook);
}
/**
* Returns an Optional containing the MVdWPlaceholderAPIHook instance, or an empty Optional otherwise.
* @return Optional containing the MVdWPlaceholderAPIHook instance or an empty Optional otherwise.
* @since 1.5.0
*/
@NonNull
private Optional<MVdWPlaceholderAPIHook> getMVdWPlaceholderAPIHook() {
return plugin.getHooks().getHook("MVdWPlaceholderAPI").map(hook -> (MVdWPlaceholderAPIHook) hook);
}
/**
* Checks if a placeholder with this name is already registered
* @param addon the addon, not null
@ -120,6 +138,7 @@ public class PlaceholdersManager {
* @since 1.4.0
*/
public boolean isPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
// MVdW will always return false
return getPlaceholderAPIHook().map(h -> h.isPlaceholder(addon, placeholder)).orElse(false);
}
@ -131,6 +150,16 @@ public class PlaceholdersManager {
* @since 1.5.0
*/
public String replacePlaceholders(@NonNull Player player, @NonNull String string) {
return getPlaceholderAPIHook().map(h -> h.replacePlaceholders(player, string)).orElse(string);
Optional<PlaceholderAPIHook> papi = getPlaceholderAPIHook();
if (papi.isPresent()) {
string = papi.get().replacePlaceholders(player, string);
}
Optional<MVdWPlaceholderAPIHook> mvdw = getMVdWPlaceholderAPIHook();
if (mvdw.isPresent()) {
string = mvdw.get().replacePlaceholders(player, string);
}
return string;
}
}

View File

@ -11,7 +11,7 @@ load: STARTUP
loadbefore: [Multiverse-Core, Residence]
softdepend: [Vault, PlaceholderAPI, dynmap]
softdepend: [Vault, PlaceholderAPI, MVdWPlaceholderAPI, dynmap]
permissions:
bentobox.admin: