1
mirror of https://github.com/CarmJos/MineConfiguration.git synced 2024-09-19 20:05:49 +00:00

[1.2.2] 令物品配置原生支持PlaceholderAPI

This commit is contained in:
Carm Jos 2022-05-01 17:38:04 +08:00
parent 29526223e4
commit cf80291e51
8 changed files with 39 additions and 15 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>

View File

@ -20,14 +20,12 @@ public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, Mes
}
@Override
public @NotNull
<M> CraftMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
public @NotNull <M> CraftMessageValueBuilder<M> asValue(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new CraftMessageValueBuilder<>(parser);
}
@Override
public @NotNull
<M> CraftMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
public @NotNull <M> CraftMessageListBuilder<M> asList(@NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable M> parser) {
return new CraftMessageListBuilder<>(parser);
}
@ -41,8 +39,7 @@ public class CraftMessageBuilder extends MessageConfigBuilder<CommandSender, Mes
return asList(defaultParser()).whenSend((r, m) -> m.forEach(r::sendMessage));
}
protected static @NotNull
BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
protected static @NotNull BiFunction<@Nullable CommandSender, @NotNull String, @Nullable String> defaultParser() {
return (receiver, message) -> {
if (receiver instanceof Player && hasPlaceholderAPI()) {
message = PAPIHelper.parseMessages((Player) receiver, message);

View File

@ -1,7 +1,11 @@
package cc.carm.lib.configuration.craft.data;
import cc.carm.lib.configuration.common.utils.ColorParser;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.craft.utils.PAPIHelper;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
@ -49,16 +53,23 @@ public class ItemConfig {
}
public @NotNull ItemStack getItemStack(int amount) {
return getItemStack(null, amount);
}
public @NotNull ItemStack getItemStack(@Nullable Player player) {
return getItemStack(player, 1);
}
public @NotNull ItemStack getItemStack(@Nullable Player player, int amount) {
ItemStack item = new ItemStack(type, amount, data);
ItemMeta meta = item.getItemMeta();
if (meta == null) return item;
if (getName() != null) meta.setDisplayName(getName());
if (!getLore().isEmpty()) meta.setLore(getLore());
if (getName() != null) meta.setDisplayName(parseName(player, getName()));
if (!getLore().isEmpty()) meta.setLore(parseLore(player, getLore()));
item.setItemMeta(meta);
return item;
}
public @NotNull Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("type", type.name());
@ -89,4 +100,20 @@ public class ItemConfig {
.collect(Collectors.toList());
}
protected static @NotNull String parseName(@Nullable Player player, String message) {
if (player != null && hasPlaceholderAPI()) message = PAPIHelper.parseMessages(player, message);
return ColorParser.parse(message);
}
protected static @NotNull List<String> parseLore(@Nullable Player player, List<String> messages) {
if (player != null && hasPlaceholderAPI()) messages = PAPIHelper.parseMessages(player, messages);
return ColorParser.parse(messages);
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
}

View File

@ -22,7 +22,7 @@
</properties>
<groupId>cc.carm.lib</groupId>
<artifactId>mineconfiguration-parent</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.2</version>
<packaging>pom</packaging>
<name>MineConfiguration</name>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>mineconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>