Skip to content

Commit

Permalink
refactor: recode and more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
darksaid98 committed Jul 20, 2023
1 parent 0674e8e commit 0858ee8
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 41 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ A simple utility for easily adding [MiniMessage](https://docs.advntr.dev/minimes

Basic usage example parsing a string into a component:
```java
Component message = new ColorParser("<#00ff00><hover:show_text:'<red>test'>R G B!").build();
Component message = ColorParser.of("<#00ff00><hover:show_text:'<red>test'>R G B!").build();
player.sendMessage(message);
```

This example has a custom placeholder (`<player>`) in the string that needs to be replaced:
```java
Component message = new ColorParser("<green><player> Teleported to you.").parseMinimessagePlaceholder("player", player.getName()).build();
Component message = ColorParser.of("<green><player> Teleported to you.").parseMinimessagePlaceholder("player", player.getName()).build();
player.sendMessage(message);
```

This example also parses legacy color codes in the string:
```java
Component message = new ColorParser("&6So<green>me &5String &4Here").parseLegacy().build();
Component message = ColorParser.of("&6So<green>me &5String &4Here").parseLegacy().build();
player.sendMessage(message);
```

This example parses all PlaceholderAPI placeholders in the string:
```java
Component message = new ColorParser("Your Displayname is: %player_displayname%").parsePAPIPlaceholders(player).build();
Component message = ColorParser.of("Your Displayname is: %player_displayname%").parsePAPIPlaceholders(player).build();
player.sendMessage(message);
```

Expand All @@ -72,7 +72,7 @@ repositories {
}

dependencies {
implementation("com.github.milkdrinkers:colorparser:1.0.7")
implementation("com.github.milkdrinkers:colorparser:2.0.0")
}
```

Expand All @@ -88,6 +88,6 @@ dependencies {
<dependency>
<groupId>com.github.milkdrinkers</groupId>
<artifactId>colorparser</artifactId>
<version>1.0.7</version>
<version>2.0.0</version>
</dependency>
```
33 changes: 24 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ plugins {
}

group = "com.github.milkdrinkers"
version = "1.0.7"
version = "2.0.0"
description = ""


java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17)) // Configure the java toolchain. This allows gradle to auto-provision JDK 17 on systems that only have JDK 8 installed for example.
withJavadocJar()
Expand All @@ -19,20 +18,36 @@ java {
repositories {
mavenCentral()

maven("https://papermc.io/repo/repository/maven-public/")
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") {
content {
includeGroup("org.bukkit")
includeGroup("org.spigotmc")
}
}
maven("https://oss.sonatype.org/content/repositories/snapshots") // Required for Spigots Bungeecord dependency
maven("https://oss.sonatype.org/content/repositories/central") // Required for Spigots Bungeecord dependency

// maven("https://papermc.io/repo/repository/maven-public/")

maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") {
content { includeGroup("me.clip") }
}

maven("https://jitpack.io/") {
content {}
}
// maven("https://jitpack.io/") { content {} }
}

dependencies {
implementation("org.jetbrains:annotations:24.0.1")
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("org.jetbrains:annotations:24.0.1")

api("net.kyori:adventure-api:4.14.0")
api("net.kyori:adventure-text-minimessage:4.14.0")
api("net.kyori:adventure-text-serializer-gson:4.14.0")
api("net.kyori:adventure-text-serializer-legacy:4.14.0")
api("net.kyori:adventure-text-serializer-plain:4.14.0")

compileOnly("org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT")
// compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")

compileOnly("me.clip:placeholderapi:2.11.3")
}

Expand All @@ -43,7 +58,7 @@ tasks {

compileJava {
options.encoding = Charsets.UTF_8.name() // We want UTF-8 for everything
options.release.set(17)
sourceCompatibility = "${JavaVersion.VERSION_1_8}"
}

/*shadowJar {
Expand Down
143 changes: 117 additions & 26 deletions src/main/java/com/github/milkdrinkers/colorparser/ColorParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
import java.util.regex.Pattern;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;

/**
* Builder Util for Adventure Messages.
* Builder Utility for easily turning strings into Adventure Components.
*/
public class ColorParser {

Expand Down Expand Up @@ -71,22 +74,44 @@ public class ColorParser {
private String text;

/**
* Instantiates a new Component parser.
* Instantiates a new color parser object.
*/
private ColorParser() {
}

/**
* Instantiates a new Color parser.
*
* @param text the text
* @param text the string to parse.
* @deprecated use {@link com.github.milkdrinkers.colorparser.ColorParser#of(String)}.
* Instantiates a new color parser object.
*/
@Deprecated
public ColorParser(String text) {
this.text = text;
setText(text);
}

/**
* Text component parser.
* Text color parser.
*
* @param text the text
* @return the component parser
* @param text the string to parse.
* @return the color parser object.
* @deprecated use {@link com.github.milkdrinkers.colorparser.ColorParser#of(String)}
* Instantiates a new color parser object.
*/
public static ColorParser text(String text) {
return new ColorParser(text);
@Deprecated
public static @NotNull ColorParser text(String text) {
return ColorParser.of(text);
}

/**
* Instantiates a new color parser object.
*
* @param text the string to parse.
* @return the color parser object.
*/
public static @NotNull ColorParser of(String text) {
return new ColorParser().setText(text);
}

/**
Expand All @@ -95,25 +120,68 @@ public static ColorParser text(String text) {
* @return the component
*/
public @NotNull Component build() {
return mm.deserialize(this.text, this.minimessagePlaceholders.toArray(new TagResolver[0]));
return mm.deserialize(getText(), this.minimessagePlaceholders.toArray(new TagResolver[0]));
}

/**
* Parse legacy color codes and formatting, including <code>{@literal &}</code> and
* <code>{@literal §}</code> into their minimessage equivalents.
*
* @return the component parser
* @return the color parser object
*/
public @NotNull ColorParser parseLegacy() {
String textParsed = this.text;
final Matcher matcher = legacyRegex.matcher(textParsed);
String textParsed = getText();
final @NotNull Matcher matcher = legacyRegex.matcher(textParsed);

while (matcher.find()) {
final String match = matcher.group();
textParsed = textParsed.replace(match, legacyToMiniMessage.getOrDefault(match, match));
}

this.text = textParsed;
setText(textParsed);

return this;
}

/**
* Parse all PAPI placeholders.
*
* @param p the player to parse for
* @return the color parser object
*/
public @NotNull ColorParser parsePAPIPlaceholders(@NotNull Player p) {
if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
setText(PlaceholderAPI.setPlaceholders(p, getText()));
}

return this;
}

/**
* Parse all PAPI placeholders.
*
* @param p the player to parse for
* @return the color parser object
*/
public @NotNull ColorParser parsePAPIPlaceholders(@NotNull OfflinePlayer p) {
if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
setText(PlaceholderAPI.setPlaceholders(p, getText()));
}

return this;
}

/**
* Parse all PAPI placeholders.
*
* @param p the player to parse for
* @param p2 the player to parse for
* @return the color parser object
*/
public @NotNull ColorParser parsePAPIPlaceholdersRelational(@NotNull Player p, @NotNull Player p2) {
if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
setText(PlaceholderAPI.setRelationalPlaceholders(p, p2, getText()));
}

return this;
}
Expand All @@ -122,30 +190,43 @@ public static ColorParser text(String text) {
* Parse all PAPI placeholders.
*
* @param p the player to parse for
* @return the component parser
* @return the color parser object
*/
public @NotNull ColorParser parsePAPIPlaceholders(Player p) {
public @NotNull ColorParser parsePAPIPlaceholdersBracket(@NotNull Player p) {
if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
this.text = PlaceholderAPI.setPlaceholders(p.getPlayer(), this.text);
setText(PlaceholderAPI.setBracketPlaceholders(p, getText()));
}

return this;
}

/**
* Parse all PAPI placeholders.
*
* @param p the player to parse for
* @return the color parser object
*/
public @NotNull ColorParser parsePAPIPlaceholdersBracket(@NotNull OfflinePlayer p) {
if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
setText(PlaceholderAPI.setBracketPlaceholders(p, getText()));
}

return this;
}

/**
* Parse a minimessage placeholder.
*
* @param placeholder the placeholder name like <code>player_name</code>, in config
* <code>{@literal <player_name>}</code>
* @param value the value like <code>{@literal "<gold><bold>darksaid98"}</code>
* @return the component parser
* @return the color parser object
*/
public @NotNull ColorParser parseMinimessagePlaceholder(String placeholder, String value) {
public @NotNull ColorParser parseMinimessagePlaceholder(@Subst("test_placeholder") @NotNull String placeholder, String value) {
this.minimessagePlaceholders.add(
Placeholder.component(
placeholder,
new ColorParser(value).parseLegacy().build()
ColorParser.of(value).parseLegacy().build()
)
);

Expand All @@ -158,9 +239,9 @@ public static ColorParser text(String text) {
* @param placeholder the placeholder name like <code>player_name</code>, in config
* <code>{@literal <player_name>}</code>
* @param value a component
* @return the component parser
* @return the color parser object
*/
public @NotNull ColorParser parseMinimessagePlaceholder(String placeholder, Component value) {
public @NotNull ColorParser parseMinimessagePlaceholder(@Subst("test_placeholder") @NotNull String placeholder, @NotNull ComponentLike value) {
this.minimessagePlaceholders.add(
Placeholder.component(
placeholder,
Expand All @@ -176,15 +257,25 @@ public static ColorParser text(String text) {
*
* @param placeholder the placeholder like <code>{@literal %player_name%}</code>
* @param value the value like <code>{@literal &6&ldarksaid98}</code>
* @return the component parser
* @return the color parser object
*/
public @NotNull ColorParser parseStringPlaceholder(String placeholder, String value) {
this.text = this.text.replaceAll(placeholder, value);
public @NotNull ColorParser parseStringPlaceholder(@NotNull String placeholder, @NotNull String value) {
setText(getText().replaceAll(placeholder, value));

return this;
}

public String toString() {
return this.text;
return getText();
}

private String getText() {
return text;
}

private @NotNull ColorParser setText(String text) {
this.text = text;

return this;
}
}

0 comments on commit 0858ee8

Please sign in to comment.