Skip to content

Commit

Permalink
refactor: replacer without papi
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Aug 17, 2021
1 parent 63498b4 commit 3d04b9d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 81 deletions.
9 changes: 0 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@
<scope>provided</scope>
</dependency>

<!-- Placeholder API --><!-- Placeholder API -->
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.10</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>

<!-- Mojang Authlib --><!-- Mojang Authlib -->
<dependency>
<groupId>com.mojang</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.github.arthurfiorette.sinklibrary.config.BaseConfig;
import com.github.arthurfiorette.sinklibrary.replacer.Replacer;
import com.github.arthurfiorette.sinklibrary.replacer.ReplacerFunction;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.bukkit.Color;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.Configuration;
Expand Down Expand Up @@ -158,7 +159,7 @@ default String getString(final P path, final String def) {
*
* @return Requested String.
*/
default String getString(final P path, final ReplacerFunction replacer) {
default String getString(final P path, final Replacer.Function replacer) {
return Replacer.replace(this.getString(path), replacer);
}

Expand All @@ -176,7 +177,7 @@ default String getString(final P path, final ReplacerFunction replacer) {
*
* @return Requested String.
*/
default String getString(final P path, final String def, final ReplacerFunction replacer) {
default String getString(final P path, final String def, final Replacer.Function replacer) {
return Replacer.replace(this.getString(path, def), replacer);
}

Expand Down Expand Up @@ -470,7 +471,7 @@ default List<String> getStringList(final P path) {
*
* @return Requested List of String.
*/
default List<String> getStringList(final P path, final ReplacerFunction replacer) {
default List<String> getStringList(final P path, final Replacer.Function replacer) {
return this.getStringList(path)
.stream()
.map(str -> Replacer.replace(str, replacer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,62 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;
import org.bukkit.ChatColor;

/**
* A better text replacer that supports PlaceholderAPI (if enabled)
*
* @author https://github.com/ArthurFiorette/sink-library/
*/
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@NoArgsConstructor
public class Replacer {

private final Map<String, Supplier<String>> placeholders = new HashMap<>();
@Getter
@NonNull
private final Map<String, Object> placeholders = new HashMap<>();

public Replacer merge(final Replacer other) {
placeholders.putAll(other.placeholders);
public Replacer add(@NonNull final String key, @NonNull final Object value) {
placeholders.put(key, value);
return this;
}

/**
* Add a placeholder to replace when called.
*
* @param placeholder the placeholder to be replaced
* @param value the value to replace
*
* @return the instance
*/
public Replacer add(final String placeholder, final String value) {
return this.add(placeholder, () -> value);
public Replacer merge(@NonNull final Replacer other) {
placeholders.putAll(other.placeholders);
return this;
}

/**
* Add a placeholder to replace when called.
*
* @param placeholder the placeholder to be replaced
* @param supplier the value to replace and only generated when called
*
* @return the instance
*/
public Replacer add(final String placeholder, final Supplier<String> supplier) {
placeholders.put(placeholder, supplier);
return this;
//

public String replace(@NonNull String text) {
for(final Entry<String, Object> entry: placeholders.entrySet()) {
text = text.replaceAll(entry.getKey(), entry.getValue().toString().toString());
}
return text;
}

/**
* Replace the text with this atual set of placeholders.
*
* @param str the raw text
*
* @return the replaced text
*/
public String replace(final String str) {
String replaced = str;
for (final Entry<String, Supplier<String>> entry : placeholders.entrySet()) {
replaced = replaced.replace(entry.getKey(), entry.getValue().get());
public String replace(@NonNull String text, final boolean alternateColorCodes) {
text = this.replace(text);

if (alternateColorCodes) {
text = ChatColor.translateAlternateColorCodes('&', text);
}
return ChatColor.translateAlternateColorCodes('&', replaced);

return text;
}

//

public static String replace(final String text, @NonNull final Function fn) {
final Replacer replacer = fn.apply(new Replacer());
return replacer.replace(text);
}

/**
* A static and compacted method that add the placeholder and replace.
*
* @param str the raw text
* @param replacer a function to be added the placeholders in a other way
*
* @return the replaced text
*/
public static String replace(final String str, final ReplacerFunction replacer) {
return replacer.apply(new Replacer()).replace(str);
@FunctionalInterface
public interface Function {

Replacer apply(Replacer replacer);

default Function add(final String placeholder, final Object value) {
return r -> r.add(placeholder, value);
}
}
}
}

This file was deleted.

0 comments on commit 3d04b9d

Please sign in to comment.