mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-17 00:05:16 +01:00
Added basic Placeholders support
This commit is contained in:
parent
3c86c3e99a
commit
6092217b46
@ -44,7 +44,7 @@ public class BSkyBlock extends JavaPlugin{
|
||||
return;
|
||||
}
|
||||
|
||||
// Load configuration and locales if the config don't have errors.
|
||||
// Load configuration and locales. If there are no errors, load the plugin.
|
||||
if(PluginConfig.loadPluginConfig(this)){
|
||||
playersManager = new PlayersManager(this);
|
||||
islandsManager = new IslandsManager(this);
|
||||
|
@ -0,0 +1,102 @@
|
||||
package us.tastybento.bskyblock.util.placeholders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
/**
|
||||
* Handles hooks with other Placeholder APIs.
|
||||
*
|
||||
* @author Poslovitch
|
||||
* @author Tastybento
|
||||
*/
|
||||
public class PlaceholderHandler {
|
||||
private static final String PACKAGE = "us.tastybento.bskyblock.util.placeholders.hooks.";
|
||||
/**
|
||||
* List of API classes in the package specified above (except the Internal one)
|
||||
*/
|
||||
private static final String[] HOOKS = {
|
||||
//TODO
|
||||
};
|
||||
|
||||
private static List<PlaceholderInterface> apis = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Register placeholders and hooks
|
||||
* @param plugin
|
||||
*/
|
||||
public static void register(BSkyBlock plugin){
|
||||
// Register placeholders
|
||||
new Placeholders(plugin);
|
||||
|
||||
// Load Internal Placeholder API
|
||||
try{
|
||||
Class<?> clazz = Class.forName(PACKAGE + "InternalPlaceholderImpl");
|
||||
PlaceholderInterface internal = (PlaceholderInterface)clazz.newInstance();
|
||||
apis.add(internal);
|
||||
} catch (Exception e){
|
||||
// Should never happen.
|
||||
plugin.getLogger().severe("Failed to load default placeholder API");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Load hooks
|
||||
for(String hook : HOOKS){
|
||||
if(plugin.getServer().getPluginManager().isPluginEnabled(hook)){
|
||||
try{
|
||||
Class<?> clazz = Class.forName(PACKAGE + hook + "PlaceholderImpl");
|
||||
PlaceholderInterface api = (PlaceholderInterface)clazz.newInstance();
|
||||
if(api.register(plugin)){
|
||||
plugin.getLogger().info("Hooked placeholders into " + hook);
|
||||
apis.add(api);
|
||||
} else {
|
||||
plugin.getLogger().info("Failed to hook placeholders into " + hook);
|
||||
}
|
||||
} catch (Exception e){
|
||||
plugin.getLogger().info("Failed to hook placeholders into " + hook);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister placeholder hooks
|
||||
* @param plugin
|
||||
*/
|
||||
public static void unregister(BSkyBlock plugin){
|
||||
Iterator<PlaceholderInterface> it = apis.iterator();
|
||||
while (it.hasNext()) {
|
||||
PlaceholderInterface api = it.next();
|
||||
api.unregister(plugin);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace placeholders in the message according to the receiver
|
||||
* @param receiver
|
||||
* @param message
|
||||
* @return updated message
|
||||
*/
|
||||
public static String replacePlaceholders(CommandSender receiver, String message){
|
||||
if(message == null || message.isEmpty()) return "";
|
||||
|
||||
for(PlaceholderInterface api : apis){
|
||||
message = api.replacePlaceholders(receiver, message);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return tre if APIs are registered (including Internal), otherwise false
|
||||
*/
|
||||
public static boolean hasHooks(){
|
||||
return apis != null ? true : false;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package us.tastybento.bskyblock.util.placeholders;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
/**
|
||||
* Simple interface for every Placeholder API.
|
||||
*
|
||||
* @author Poslovitch
|
||||
*/
|
||||
public interface PlaceholderInterface {
|
||||
|
||||
/**
|
||||
* Get the name of the Placeholder API
|
||||
* @return name of the placeholder plugin
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Register the placeholder API
|
||||
* @param plugin
|
||||
* @return true if registered
|
||||
*/
|
||||
boolean register(BSkyBlock plugin);
|
||||
|
||||
/**
|
||||
* Unregister the placeholder API
|
||||
* @param plugin
|
||||
*/
|
||||
void unregister(BSkyBlock plugin);
|
||||
|
||||
/**
|
||||
* Replace placeholders in the message according to the receiver
|
||||
* @param sender
|
||||
* @param message
|
||||
* @return updated message
|
||||
*/
|
||||
String replacePlaceholders(CommandSender receiver, String message);
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package us.tastybento.bskyblock.util.placeholders;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
|
||||
/**
|
||||
* Register placeholders
|
||||
*
|
||||
* @author Poslovitch
|
||||
*/
|
||||
public class Placeholders {
|
||||
private static Set<Placeholder> placeholders = new HashSet<Placeholder>();
|
||||
|
||||
private BSkyBlock plugin;
|
||||
|
||||
protected Placeholders(BSkyBlock plugin){
|
||||
this.plugin = plugin;
|
||||
register();
|
||||
}
|
||||
|
||||
private void register(){
|
||||
/* PLUGIN */
|
||||
new Placeholder("bsb_name"){
|
||||
@Override
|
||||
public String onRequest(CommandSender receiver) {
|
||||
return plugin.getDescription().getName();
|
||||
}
|
||||
};
|
||||
|
||||
new Placeholder("bsb_version") {
|
||||
|
||||
@Override
|
||||
public String onRequest(CommandSender receiver) {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
};
|
||||
|
||||
//TODO: add more placeholders
|
||||
}
|
||||
|
||||
public static Set<Placeholder> getPlaceholders(){
|
||||
return placeholders;
|
||||
}
|
||||
|
||||
public abstract class Placeholder{
|
||||
private String identifier;
|
||||
|
||||
protected Placeholder(String identifier){
|
||||
this.identifier = identifier;
|
||||
placeholders.add(this);
|
||||
}
|
||||
|
||||
public String getIdentifier(){
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public abstract String onRequest(CommandSender receiver);
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package us.tastybento.bskyblock.util.placeholders.hooks;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.util.placeholders.PlaceholderInterface;
|
||||
import us.tastybento.bskyblock.util.placeholders.Placeholders;
|
||||
import us.tastybento.bskyblock.util.placeholders.Placeholders.Placeholder;
|
||||
|
||||
/**
|
||||
* Built-in placeholder API
|
||||
*
|
||||
* @author Poslovitch
|
||||
*/
|
||||
public class InternalPlaceholderImpl implements PlaceholderInterface{
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Internal";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean register(BSkyBlock plugin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregister(BSkyBlock plugin) {
|
||||
// Useless : it would disable the placeholders.
|
||||
}
|
||||
|
||||
@Override
|
||||
public String replacePlaceholders(CommandSender receiver, String message) {
|
||||
if(message == null || message.isEmpty()) return "";
|
||||
|
||||
for(Placeholder placeholder : Placeholders.getPlaceholders()){
|
||||
String identifier = "{" + placeholder.getIdentifier() + "}";
|
||||
message = message.replaceAll(Pattern.quote(identifier), placeholder.onRequest(receiver));
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user