Skip to content

Commit

Permalink
Improve adventure support and fix colorizing method, use setDurabilit…
Browse files Browse the repository at this point in the history
…y method instead of unsupported damageable class
  • Loading branch information
Ravis96 committed Oct 5, 2024
1 parent f1af001 commit 45185d7
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import cc.dreamcode.utilities.StringUtil;
import cc.dreamcode.utilities.builder.MapBuilder;
import cc.dreamcode.utilities.bukkit.adventure.AdventureLegacy;
import eu.okaeri.placeholders.context.PlaceholderContext;
import eu.okaeri.placeholders.message.CompiledMessage;
import cc.dreamcode.utilities.bukkit.adventure.AdventureUtil;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatColor;
Expand All @@ -26,7 +24,8 @@ public class StringColorUtil {
private static final char COLOR_CHAR = '\u00A7';
private static final char ALT_COLOR_CHAR = '&';

private static final Pattern hexPattern = Pattern.compile(ALT_COLOR_CHAR + "#([0-9A-Fa-f]{6})");
private static final Pattern HEX_PATTERN = Pattern.compile(ALT_COLOR_CHAR + "#([0-9A-Fa-f]{6})");

private static final Map<Color, ChatColor> COLORS = new MapBuilder<Color, ChatColor>()
.put(new Color(0), ChatColor.getByChar('0'))
.put(new Color(170), ChatColor.getByChar('1'))
Expand Down Expand Up @@ -55,7 +54,7 @@ public static String legacyFixColor(@NonNull String text, @NonNull Map<String, O
}

public static String legacyFixColor(@NonNull String text, @NonNull Locale locale, @NonNull Map<String, Object> placeholders) {
return legacyFixColor(StringUtil.replace(locale, text, placeholders));
return legacyFixColor(StringUtil.replace(text, locale, placeholders));
}

public static List<String> legacyFixColor(@NonNull List<String> stringList) {
Expand All @@ -77,25 +76,17 @@ public static List<String> legacyFixColor(@NonNull String... strings) {
}

public static String fixColor(@NonNull String text) {
return AdventureLegacy.serialize(AdventureLegacy.deserialize(text));
return legacyFixColor(AdventureUtil.format(text));
}

public static String fixColor(@NonNull String text, @NonNull Map<String, Object> placeholders) {
CompiledMessage compiledMessage = CompiledMessage.of(Locale.forLanguageTag("pl"), text);
PlaceholderContext placeholderContext = StringUtil.getPlaceholders()
.contextOf(compiledMessage)
.with(placeholders);

return AdventureLegacy.serialize(AdventureLegacy.deserialize(text, AdventureLegacy.getPlaceholderConfig(placeholderContext)));
final String formattedText = AdventureUtil.format(text, placeholders);
return legacyFixColor(formattedText);
}

public static String fixColor(@NonNull String text, @NonNull Locale locale, @NonNull Map<String, Object> placeholders) {
CompiledMessage compiledMessage = CompiledMessage.of(locale, text);
PlaceholderContext placeholderContext = StringUtil.getPlaceholders()
.contextOf(compiledMessage)
.with(placeholders);

return AdventureLegacy.serialize(AdventureLegacy.deserialize(text, AdventureLegacy.getPlaceholderConfig(placeholderContext)));
final String formattedText = AdventureUtil.format(text, locale, placeholders);
return legacyFixColor(formattedText);
}

public static List<String> fixColor(@NonNull List<String> stringList) {
Expand Down Expand Up @@ -148,7 +139,7 @@ private static Color hexToRgb(@NonNull String hex) {
}

private static String processRgb(@NonNull String text) {
Matcher matcher = hexPattern.matcher(text);
Matcher matcher = HEX_PATTERN.matcher(text);

AtomicReference<String> atomicText = new AtomicReference<>(text);
while (matcher.find()) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cc.dreamcode.utilities.bukkit.adventure;

import cc.dreamcode.utilities.StringUtil;
import eu.okaeri.placeholders.context.PlaceholderContext;
import eu.okaeri.placeholders.message.CompiledMessage;
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public final class AdventureUtil {

private static final char COLOR_CHAR = '&';

private static final Pattern FIELD_PATTERN = Pattern.compile("\\{(?<content>[^}]+)}");
private static final Pattern SECTION_COLOR_PATTERN = Pattern.compile("(?i)§([0-9A-FK-OR])");

private static final LegacyComponentSerializer AMPERSAND_SERIALIZER = LegacyComponentSerializer.builder()
.character(COLOR_CHAR)
.hexColors()
.build();

private static final MiniMessage MINI_MESSAGE = MiniMessage.builder()
.preProcessor(text -> SECTION_COLOR_PATTERN.matcher(text).replaceAll("&$1"))
.build();
private static final MiniMessage PLACEHOLDER_MINI_MESSAGE = MiniMessage.builder()
.preProcessor(text -> SECTION_COLOR_PATTERN.matcher(text).replaceAll("&$1"))
.tags(TagResolver.builder()
.resolver(StandardTags.color())
.resolver(StandardTags.decorations())
.resolver(StandardTags.rainbow())
.resolver(StandardTags.gradient())
.resolver(StandardTags.transition())
.build())
.build();

public static String format(@NonNull String text) {
final Component component = MINI_MESSAGE.deserialize(text);
return AMPERSAND_SERIALIZER.serialize(component);
}

public static String format(@NonNull String text, @NonNull Map<String, Object> placeholders) {
return format(text, Locale.forLanguageTag("pl"), placeholders);
}

public static String format(@NonNull String text, @NonNull Locale locale, @NonNull Map<String, Object> placeholders) {
final CompiledMessage compiledMessage = CompiledMessage.of(locale, text);
final PlaceholderContext placeholderContext = StringUtil.getPlaceholders().contextOf(compiledMessage)
.with(placeholders);

return format(text, placeholderContext);
}

public static String format(@NonNull String text, @NonNull PlaceholderContext placeholderContext) {

Component component = MINI_MESSAGE.deserialize(text);

final Map<String, String> fields = renderFields(placeholderContext);
final TextReplacementConfig replacementConfig = replacementConfig(fields);
component = component.replaceText(replacementConfig);

return AMPERSAND_SERIALIZER.serialize(component);
}

private static Map<String, String> renderFields(@NonNull PlaceholderContext placeholderContext) {
return placeholderContext.renderFields()
.entrySet()
.stream()
.collect(Collectors.toMap(
entry -> entry.getKey().getRaw(),
entry -> {
final Component component = PLACEHOLDER_MINI_MESSAGE.deserialize(entry.getValue());
return AMPERSAND_SERIALIZER.serialize(component);
}
));
}

private static TextReplacementConfig replacementConfig(@NonNull Map<String, String> replaceMap) {
return TextReplacementConfig.builder()
.match(FIELD_PATTERN)
.replacement((result, input) -> {
final String value = replaceMap.get(result.group(1));
return AMPERSAND_SERIALIZER.deserialize(value);
})
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import cc.dreamcode.utilities.builder.ListBuilder;
import cc.dreamcode.utilities.bukkit.StringColorUtil;
import cc.dreamcode.utilities.bukkit.VersionUtil;
import cc.dreamcode.utilities.bukkit.nbt.ItemNbtUtil;
import lombok.NonNull;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

Expand Down Expand Up @@ -92,21 +90,7 @@ public ItemBuilder setType(@NonNull ItemStack itemStack, boolean clone) {
}

public ItemBuilder withDurability(int durability) {

ItemMeta itemMeta = this.itemStack.getItemMeta();
if (VersionUtil.isSupported(13) && itemMeta instanceof Damageable) {
Damageable damageable = (Damageable) itemMeta;

if (damageable.hasDamage()) {
damageable.setDamage(durability);
}

this.itemStack.setItemMeta(itemMeta);
}
else {
this.itemStack.setDurability((short) durability);
}

this.itemStack.setDurability((short) durability);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class StringColorUtil {
private static final char COLOR_CHAR = '\u00A7';
private static final char ALT_COLOR_CHAR = '&';

private static final Pattern hexPattern = Pattern.compile(ALT_COLOR_CHAR + "#([0-9A-Fa-f]{6})");
private static final Pattern HEX_PATTERN = Pattern.compile(ALT_COLOR_CHAR + "#([0-9A-Fa-f]{6})");
private static final Map<Color, ChatColor> COLORS = new MapBuilder<Color, ChatColor>()
.put(new Color(0), ChatColor.getByChar('0'))
.put(new Color(170), ChatColor.getByChar('1'))
Expand Down Expand Up @@ -52,7 +52,7 @@ public static String fixColor(@NonNull String text, @NonNull Map<String, Object>
}

public static String fixColor(@NonNull String text, @NonNull Locale locale, @NonNull Map<String, Object> placeholders) {
return fixColor(StringUtil.replace(locale, text, placeholders));
return fixColor(StringUtil.replace(text, locale, placeholders));
}

public static List<String> fixColor(@NonNull List<String> stringList) {
Expand Down Expand Up @@ -105,7 +105,7 @@ private static Color hexToRgb(@NonNull String hex) {
}

private static String processRgb(@NonNull String text) {
Matcher matcher = hexPattern.matcher(text);
Matcher matcher = HEX_PATTERN.matcher(text);

AtomicReference<String> atomicText = new AtomicReference<>(text);
while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import cc.dreamcode.utilities.builder.ListBuilder;
import cc.dreamcode.utilities.bukkit.StringColorUtil;
import cc.dreamcode.utilities.bukkit.VersionUtil;
import cc.dreamcode.utilities.bukkit.nbt.ItemNbtUtil;
import lombok.NonNull;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

Expand Down Expand Up @@ -92,21 +90,7 @@ public ItemBuilder setType(@NonNull ItemStack itemStack, boolean clone) {
}

public ItemBuilder withDurability(int durability) {

ItemMeta itemMeta = this.itemStack.getItemMeta();
if (VersionUtil.isSupported(13) && itemMeta instanceof Damageable) {
Damageable damageable = (Damageable) itemMeta;

if (damageable.hasDamage()) {
damageable.setDamage(durability);
}

this.itemStack.setItemMeta(itemMeta);
}
else {
this.itemStack.setDurability((short) durability);
}

this.itemStack.setDurability((short) durability);
return this;
}

Expand Down
1 change: 1 addition & 0 deletions utilities-bungee-adventure/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
repositories {
maven("https://repo.codemc.io/repository/nms")
maven("https://repo.codemc.io/repository/maven-public")
maven("https://oss.sonatype.org/content/repositories/snapshots")
}

Expand Down
Loading

0 comments on commit 45185d7

Please sign in to comment.