Fixed NoClassDefFoundError when loading PlaceholderAPI hook

#322
This commit is contained in:
Florian CUNY 2018-11-04 11:25:14 +01:00
parent ac56759883
commit 16329d9372
5 changed files with 103 additions and 80 deletions

View File

@ -147,10 +147,7 @@ public class BentoBox extends JavaPlugin {
// Load hooks
hooksManager = new HooksManager(this);
hooksManager.registerHook(new VaultHook());
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")
|| getServer().getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) {
hooksManager.registerHook(new PlaceholderAPIHook());
}
hooksManager.registerHook(new PlaceholderAPIHook());
// Setup the Placeholders manager
placeholdersManager = new PlaceholdersManager(this);

View File

@ -0,0 +1,26 @@
package world.bentobox.bentobox.api.placeholders.placeholderapi;
import world.bentobox.bentobox.api.addons.Addon;
public class AddonPlaceholderExpansion extends BasicPlaceholderExpansion {
private Addon addon;
public AddonPlaceholderExpansion(Addon addon) {
this.addon = addon;
}
@Override
public String getName() {
return addon.getDescription().getName();
}
@Override
public String getAuthor() {
return addon.getDescription().getAuthors().get(0);
}
@Override
public String getVersion() {
return addon.getDescription().getVersion();
}
}

View File

@ -0,0 +1,37 @@
package world.bentobox.bentobox.api.placeholders.placeholderapi;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.api.user.User;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
abstract class BasicPlaceholderExpansion extends PlaceholderExpansion {
private Map<String, PlaceholderReplacer> placeholders;
BasicPlaceholderExpansion() {
this.placeholders = new HashMap<>();
}
@Override
public String getIdentifier() {
return getName().toLowerCase(Locale.ENGLISH);
}
public void registerPlaceholder(String placeholder, PlaceholderReplacer replacer) {
placeholders.putIfAbsent(placeholder, replacer);
}
@Override
public String onPlaceholderRequest(Player p, String placeholder) {
User user = User.getInstance(p);
if (placeholders.containsKey(placeholder)) {
return placeholders.get(placeholder).onReplace(user);
}
return null;
}
}

View File

@ -0,0 +1,26 @@
package world.bentobox.bentobox.api.placeholders.placeholderapi;
import world.bentobox.bentobox.BentoBox;
public class BentoBoxPlaceholderExpansion extends BasicPlaceholderExpansion {
private BentoBox plugin;
public BentoBoxPlaceholderExpansion(BentoBox plugin) {
this.plugin = plugin;
}
@Override
public String getName() {
return plugin.getName();
}
@Override
public String getAuthor() {
return "Tastybento and Poslovitch";
}
@Override
public String getVersion() {
return plugin.getDescription().getVersion();
}
}

View File

@ -6,10 +6,10 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.api.placeholders.placeholderapi.AddonPlaceholderExpansion;
import world.bentobox.bentobox.api.placeholders.placeholderapi.BentoBoxPlaceholderExpansion;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
@ -24,12 +24,17 @@ public class PlaceholderAPIHook extends Hook {
public PlaceholderAPIHook() {
super("PlaceholderAPI");
this.bentoboxExpansion = new BentoBoxPlaceholderExpansion(BentoBox.getInstance());
this.addonsExpansions = new HashMap<>();
}
@Override
public boolean hook() {
try {
this.bentoboxExpansion = new BentoBoxPlaceholderExpansion(BentoBox.getInstance());
} catch (Exception | NoClassDefFoundError | NoSuchMethodError e) {
return false;
}
return bentoboxExpansion.canRegister() && bentoboxExpansion.register();
}
@ -43,6 +48,11 @@ public class PlaceholderAPIHook extends Hook {
}
public void registerAddonPlaceholder(Addon addon, String placeholder, PlaceholderReplacer replacer) {
// If addon is null, then register the placeholder in BentoBox's expansion.
if (addon == null) {
registerBentoBoxPlaceholder(placeholder, replacer);
}
// Check if the addon expansion does not exist
if (!addonsExpansions.containsKey(addon)) {
AddonPlaceholderExpansion addonPlaceholderExpansion = new AddonPlaceholderExpansion(addon);
@ -52,77 +62,4 @@ public class PlaceholderAPIHook extends Hook {
addonsExpansions.get(addon).registerPlaceholder(placeholder, replacer);
}
abstract class BasicPlaceholderExpansion extends PlaceholderExpansion {
private Map<String, PlaceholderReplacer> placeholders;
BasicPlaceholderExpansion() {
this.placeholders = new HashMap<>();
}
@Override
public String getIdentifier() {
return getName().toLowerCase(Locale.ENGLISH);
}
void registerPlaceholder(String placeholder, PlaceholderReplacer replacer) {
placeholders.putIfAbsent(placeholder, replacer);
}
@Override
public String onPlaceholderRequest(Player p, String placeholder) {
User user = User.getInstance(p);
if (placeholders.containsKey(placeholder)) {
return placeholders.get(placeholder).onReplace(user);
}
return null;
}
}
class BentoBoxPlaceholderExpansion extends BasicPlaceholderExpansion {
private BentoBox plugin;
BentoBoxPlaceholderExpansion(BentoBox plugin) {
this.plugin = plugin;
}
@Override
public String getName() {
return plugin.getName();
}
@Override
public String getAuthor() {
return "Tastybento and Poslovitch";
}
@Override
public String getVersion() {
return plugin.getDescription().getVersion();
}
}
class AddonPlaceholderExpansion extends BasicPlaceholderExpansion {
private Addon addon;
AddonPlaceholderExpansion(Addon addon) {
this.addon = addon;
}
@Override
public String getName() {
return addon.getDescription().getName();
}
@Override
public String getAuthor() {
return addon.getDescription().getAuthors().get(0);
}
@Override
public String getVersion() {
return addon.getDescription().getVersion();
}
}
}