Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ dependencies {

implementation 'org.bstats:bstats-bukkit:3.0.0'

implementation 'net.kyori:adventure-text-minimessage:4.17.0'
implementation 'net.kyori:adventure-text-serializer-legacy:4.17.0'

// Spigot jars
compileOnly "org.spigotmc:v1_7_R3:latest"
compileOnly 'org.spigotmc:v1_16_R3-Tuinity:latest'
Expand All @@ -89,6 +92,7 @@ subprojects.each { evaluationDependsOn(it.path) }

shadowJar {
relocate 'org.bstats', 'com.bgsoftware.wildtools.libs.org.bstats'
relocate 'net.kyori', 'com.bgsoftware.wildtools.libs.net.kyori'

archiveClassifier.set('')

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bgsoftware/wildtools/Locale.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bgsoftware.wildtools;

import com.bgsoftware.common.config.CommentedConfiguration;
import org.bukkit.ChatColor;
import com.bgsoftware.wildtools.utils.TextFormatter;
import org.bukkit.command.CommandSender;

import java.io.File;
Expand Down Expand Up @@ -122,7 +122,7 @@ public static void reload() {
}

for (String identifier : localeMap.keySet())
localeMap.get(identifier).setMessage(ChatColor.translateAlternateColorCodes('&', cfg.getString(identifier, "")));
localeMap.get(identifier).setMessage(TextFormatter.format(cfg.getString(identifier, "")));

WildToolsPlugin.log(" - Found " + messagesAmount + " messages in lang.yml.");
WildToolsPlugin.log("Loading messages done (Took " + (System.currentTimeMillis() - startTime) + "ms)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import com.bgsoftware.wildtools.scheduler.Scheduler;
import com.bgsoftware.wildtools.tools.ToolBreaksTracker;
import com.bgsoftware.wildtools.tools.WCannonTool;
import com.bgsoftware.wildtools.utils.TextFormatter;
import com.bgsoftware.wildtools.utils.WSelection;
import com.bgsoftware.wildtools.utils.items.ItemUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -108,7 +107,7 @@ public void onPlayerRespawn(PlayerRespawnEvent e) {
}

private void sendMessage(Player player, String message) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
player.sendMessage(TextFormatter.format(message));
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/bgsoftware/wildtools/tools/WTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildtools.WildToolsPlugin;
import com.bgsoftware.wildtools.utils.TextFormatter;
import com.bgsoftware.wildtools.api.objects.ToolMode;
import com.bgsoftware.wildtools.api.objects.tools.Tool;
import com.bgsoftware.wildtools.utils.Materials;
import com.bgsoftware.wildtools.utils.items.ItemUtils;
import com.bgsoftware.wildtools.utils.items.ToolItemStack;
import com.bgsoftware.wildtools.world.BlockMaterial;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -84,7 +84,7 @@ public WTool(Material type, String name, ToolMode toolMode) {
@Override
public void setDisplayName(String name) {
ItemMeta im = toolItemStack.getItemMeta();
im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
im.setDisplayName(TextFormatter.format(name));
toolItemStack.setItemMeta(im);
}

Expand All @@ -94,7 +94,7 @@ public void setLore(List<String> lore) {
List<String> _lore = new ArrayList<>();

for (String line : lore)
_lore.add(ChatColor.translateAlternateColorCodes('&', line));
_lore.add(TextFormatter.format(line));

im.setLore(_lore);
toolItemStack.setItemMeta(im);
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/com/bgsoftware/wildtools/utils/TextFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.bgsoftware.wildtools.utils;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

public final class TextFormatter {

private static final MiniMessage MINI = MiniMessage.miniMessage();
private static final LegacyComponentSerializer TO_LEGACY = LegacyComponentSerializer.builder()
.character(LegacyComponentSerializer.SECTION_CHAR)
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.build();

private TextFormatter() {
}

public static String format(String text) {
if (text == null || text.isEmpty()) return text;
return TO_LEGACY.serialize(MINI.deserialize(convertLegacyToMiniMessage(text)));
}

private static String convertLegacyToMiniMessage(String text) {
if (!text.contains("&")) return text;
StringBuilder sb = new StringBuilder(text.length());
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '&' && i + 1 < text.length()) {
String tag = legacyCodeToTag(text.charAt(i + 1));
if (tag != null) {
sb.append(tag);
i++;
continue;
}
}
sb.append(c);
}
return sb.toString();
}

private static String legacyCodeToTag(char code) {
switch (Character.toLowerCase(code)) {
case '0':
return "<black>";
case '1':
return "<dark_blue>";
case '2':
return "<dark_green>";
case '3':
return "<dark_aqua>";
case '4':
return "<dark_red>";
case '5':
return "<dark_purple>";
case '6':
return "<gold>";
case '7':
return "<gray>";
case '8':
return "<dark_gray>";
case '9':
return "<blue>";
case 'a':
return "<green>";
case 'b':
return "<aqua>";
case 'c':
return "<red>";
case 'd':
return "<light_purple>";
case 'e':
return "<yellow>";
case 'f':
return "<white>";
case 'k':
return "<obfuscated>";
case 'l':
return "<bold>";
case 'm':
return "<strikethrough>";
case 'n':
return "<underlined>";
case 'o':
return "<italic>";
case 'r':
return "<reset>";
default:
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bgsoftware.wildtools.WildToolsPlugin;
import com.bgsoftware.wildtools.utils.Materials;
import com.bgsoftware.wildtools.utils.TextFormatter;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -75,18 +76,18 @@ public ItemBuilder(ConfigurationSection section) {

public ItemBuilder withName(String name) {
if (!name.isEmpty())
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
itemMeta.setDisplayName(TextFormatter.format(name));
return this;
}

public ItemBuilder withLore(String firstLine, List<String> listLine) {
List<String> loreList = new ArrayList<>();

firstLine = ChatColor.translateAlternateColorCodes('&', firstLine);
firstLine = TextFormatter.format(firstLine);
loreList.add(firstLine);

for (String line : listLine) {
loreList.add(ChatColor.getLastColors(firstLine) + ChatColor.translateAlternateColorCodes('&', line));
loreList.add(ChatColor.getLastColors(firstLine) + TextFormatter.format(line));
}

if (loreList.size() > 10) {
Expand All @@ -104,7 +105,7 @@ public ItemBuilder withLore(List<String> listLine) {
List<String> loreList = new ArrayList<>();

for (String line : listLine) {
loreList.add(ChatColor.translateAlternateColorCodes('&', line));
loreList.add(TextFormatter.format(line));
}

itemMeta.setLore(loreList);
Expand All @@ -115,7 +116,7 @@ public ItemBuilder withLore(String... lore) {
List<String> loreList = new ArrayList<>();

for (String line : lore) {
loreList.add(ChatColor.translateAlternateColorCodes('&', line));
loreList.add(TextFormatter.format(line));
}

itemMeta.setLore(loreList);
Expand Down