Added intial support for placeholders

This commit is contained in:
BuildTools
2020-11-13 10:19:18 +00:00
parent c21cd8396e
commit b56c94cc47
12 changed files with 187 additions and 10 deletions

View File

@@ -5,6 +5,8 @@ import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.util.Watcher;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderEntry;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.util.StringUtils;
import com.willfp.ecoenchants.util.interfaces.Registerable;
import com.willfp.ecoenchants.util.optional.Prerequisite;
@@ -99,6 +101,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
target.addAll(config.getTargets());
target.forEach(enchantmentTarget -> targetMaterials.addAll(enchantmentTarget.getMaterials()));
enabled = config.getBool("enabled", true);
this.updatePlaceholders();
this.register();
}
@@ -143,6 +146,34 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
EcoEnchants.removeEcoEnchant(this);
}
private void updatePlaceholders() {
if(this.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "chance-per-level") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "chance_per_level", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level"));
})
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "chance_per_level_percentage", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level") * 100);
})
);
}
if(this.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "multiplier") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "multiplier", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier"));
})
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "multiplier_percentage", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier") * 100);
})
);
}
}
/**
* Get if enchantment can be removed in grindstone
* @return Whether the enchantment can be removed

View File

@@ -1,6 +0,0 @@
package com.willfp.ecoenchants.enchantments.util;
@FunctionalInterface
public interface Callback {
void call();
}

View File

@@ -1,6 +1,7 @@
package com.willfp.ecoenchants.enchantments.util;
import com.willfp.ecoenchants.enchantments.itemtypes.Spell;
import com.willfp.ecoenchants.util.Callable;
import org.bukkit.entity.Player;
import java.util.UUID;
@@ -9,7 +10,7 @@ public class SpellRunnable {
private final UUID player;
private final Spell spell;
private long endTime = 0;
private Callback callback = () -> {};
private Callable callable = () -> {};
public SpellRunnable(Player player, Spell spell) {
this.player = player.getUniqueId();
@@ -21,7 +22,7 @@ public class SpellRunnable {
}
public void run() {
callback.call();
callable.call();
updateEndTime();
}
@@ -33,7 +34,7 @@ public class SpellRunnable {
endTime = System.currentTimeMillis() + (spell.getCooldownTime()*1000);
}
public void setTask(Callback callback) {
this.callback = callback;
public void setTask(Callable callable) {
this.callable = callable;
}
}

View File

@@ -0,0 +1,22 @@
package com.willfp.ecoenchants.integrations.placeholder;
import com.willfp.ecoenchants.util.ObjectCallable;
import org.bukkit.entity.Player;
public class PlaceholderEntry {
private final String identifier;
private final ObjectCallable<String, Player> function;
public PlaceholderEntry(String identifier, ObjectCallable<String, Player> function) {
this.identifier = identifier;
this.function = function;
}
public String getIdentifier() {
return this.identifier;
}
public String getResult(Player player) {
return this.function.call(player);
}
}

View File

@@ -0,0 +1,7 @@
package com.willfp.ecoenchants.integrations.placeholder;
import com.willfp.ecoenchants.integrations.Integration;
public interface PlaceholderIntegration extends Integration {
void registerIntegration();
}

View File

@@ -0,0 +1,29 @@
package com.willfp.ecoenchants.integrations.placeholder;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
public class PlaceholderManager {
private static final Set<PlaceholderEntry> placeholders = new HashSet<>();
public static void addIntegration(PlaceholderIntegration integration) {
integration.registerIntegration();
}
public static void registerPlaceholder(PlaceholderEntry expansion) {
placeholders.removeIf(placeholderEntry -> placeholderEntry.getIdentifier().equalsIgnoreCase(expansion.getIdentifier()));
placeholders.add(expansion);
}
public static String getResult(Player player, String identifier) {
Optional<PlaceholderEntry> matching = placeholders.stream().filter(expansion -> expansion.getIdentifier().equalsIgnoreCase(identifier)).findFirst();
if(matching.isPresent()) {
return matching.get().getResult(player);
} else {
return null;
}
}
}

View File

@@ -0,0 +1,52 @@
package com.willfp.ecoenchants.integrations.placeholder.plugins;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderIntegration;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
public class PlaceholderIntegrationPAPI extends PlaceholderExpansion implements PlaceholderIntegration {
@Override
public boolean persist() {
return true;
}
@Override
public boolean canRegister() {
return true;
}
@Override
public String getAuthor() {
return "Auxilor";
}
@Override
public String getIdentifier() {
return "ecoenchants";
}
@Override
public String getVersion() {
return EcoEnchantsPlugin.getInstance().getDescription().getVersion();
}
@Override
public String onPlaceholderRequest(Player player, String identifier) {
if(player == null)
return "";
return PlaceholderManager.getResult(player, identifier);
}
@Override
public void registerIntegration() {
this.register();
}
@Override
public String getPluginName() {
return "PlaceholderAPI";
}
}

View File

@@ -42,6 +42,8 @@ import com.willfp.ecoenchants.integrations.antigrief.plugins.AntigriefTowny;
import com.willfp.ecoenchants.integrations.antigrief.plugins.AntigriefWorldGuard;
import com.willfp.ecoenchants.integrations.essentials.EssentialsManager;
import com.willfp.ecoenchants.integrations.essentials.plugins.IntegrationEssentials;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.integrations.placeholder.plugins.PlaceholderIntegrationPAPI;
import com.willfp.ecoenchants.listeners.ArrowListeners;
import com.willfp.ecoenchants.listeners.PlayerJoinListener;
import com.willfp.ecoenchants.nms.BlockBreak;
@@ -419,6 +421,13 @@ public class Loader {
Logger.info("Spartan: §9DISABLED");
}
if(Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
PlaceholderManager.addIntegration(new PlaceholderIntegrationPAPI());
Logger.info("PlaceholderAPI: §aENABLED");
} else {
Logger.info("PlaceholderAPI: §9DISABLED");
}
/*
Check for paper

View File

@@ -0,0 +1,6 @@
package com.willfp.ecoenchants.util;
@FunctionalInterface
public interface Callable {
void call();
}

View File

@@ -0,0 +1,6 @@
package com.willfp.ecoenchants.util;
@FunctionalInterface
public interface ObjectCallable<A, B> {
A call(B object);
}