mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-09-09 20:21:18 +02:00
Format to Daddy code style
This commit is contained in:
@@ -20,181 +20,155 @@
|
||||
|
||||
package me.clip.placeholderapi.replacer;
|
||||
|
||||
import java.util.function.Function;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
public final class CharsReplacer implements Replacer {
|
||||
|
||||
public final class CharsReplacer implements Replacer
|
||||
{
|
||||
@NotNull
|
||||
private final Closure closure;
|
||||
|
||||
@NotNull
|
||||
private final Closure closure;
|
||||
|
||||
public CharsReplacer(@NotNull final Closure closure)
|
||||
{
|
||||
this.closure = closure;
|
||||
}
|
||||
public CharsReplacer(@NotNull final Closure closure) {
|
||||
this.closure = closure;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup)
|
||||
{
|
||||
final char[] chars = text.toCharArray();
|
||||
final StringBuilder builder = new StringBuilder(text.length());
|
||||
@NotNull
|
||||
@Override
|
||||
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player,
|
||||
@NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) {
|
||||
final char[] chars = text.toCharArray();
|
||||
final StringBuilder builder = new StringBuilder(text.length());
|
||||
|
||||
final StringBuilder identifier = new StringBuilder();
|
||||
final StringBuilder parameters = new StringBuilder();
|
||||
final StringBuilder identifier = new StringBuilder();
|
||||
final StringBuilder parameters = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < chars.length; i++)
|
||||
{
|
||||
final char l = chars[i];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
final char l = chars[i];
|
||||
|
||||
if (l == '&' && ++i < chars.length)
|
||||
{
|
||||
final char c = Character.toLowerCase(chars[i]);
|
||||
if (l == '&' && ++i < chars.length) {
|
||||
final char c = Character.toLowerCase(chars[i]);
|
||||
|
||||
if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != 'a' && c != 'b' && c != 'c' && c != 'd' && c != 'e' && c != 'f' && c != 'k' && c != 'l' && c != 'm' && c != 'o' && c != 'r' && c != 'x')
|
||||
{
|
||||
builder.append(l).append(chars[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.append(ChatColor.COLOR_CHAR);
|
||||
if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6'
|
||||
&& c != '7' && c != '8' && c != '9' && c != 'a' && c != 'b' && c != 'c' && c != 'd'
|
||||
&& c != 'e' && c != 'f' && c != 'k' && c != 'l' && c != 'm' && c != 'o' && c != 'r'
|
||||
&& c != 'x') {
|
||||
builder.append(l).append(chars[i]);
|
||||
} else {
|
||||
builder.append(ChatColor.COLOR_CHAR);
|
||||
|
||||
if (c != 'x')
|
||||
{
|
||||
builder.append(chars[i]);
|
||||
continue;
|
||||
}
|
||||
if (c != 'x') {
|
||||
builder.append(chars[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((i > 1 && chars[i - 2] == '\\') /*allow escaping &x*/)
|
||||
{
|
||||
builder.setLength(builder.length() - 2);
|
||||
builder.append('&').append(chars[i]);
|
||||
continue;
|
||||
}
|
||||
if ((i > 1 && chars[i - 2] == '\\') /*allow escaping &x*/) {
|
||||
builder.setLength(builder.length() - 2);
|
||||
builder.append('&').append(chars[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
builder.append(c);
|
||||
builder.append(c);
|
||||
|
||||
int j = 0;
|
||||
while (++j <= 6)
|
||||
{
|
||||
if (i + j >= chars.length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
int j = 0;
|
||||
while (++j <= 6) {
|
||||
if (i + j >= chars.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
final char x = chars[i + j];
|
||||
builder.append(ChatColor.COLOR_CHAR).append(x);
|
||||
}
|
||||
final char x = chars[i + j];
|
||||
builder.append(ChatColor.COLOR_CHAR).append(x);
|
||||
}
|
||||
|
||||
if (j == 7)
|
||||
{
|
||||
i += 6;
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.setLength(builder.length() - (j * 2)); // undo &x parsing
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (j == 7) {
|
||||
i += 6;
|
||||
} else {
|
||||
builder.setLength(builder.length() - (j * 2)); // undo &x parsing
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (l != closure.head || i + 1 >= chars.length)
|
||||
{
|
||||
builder.append(l);
|
||||
continue;
|
||||
}
|
||||
if (l != closure.head || i + 1 >= chars.length) {
|
||||
builder.append(l);
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean identified = false;
|
||||
boolean oopsitsbad = true;
|
||||
boolean identified = false;
|
||||
boolean oopsitsbad = true;
|
||||
|
||||
while (++i < chars.length)
|
||||
{
|
||||
final char p = chars[i];
|
||||
while (++i < chars.length) {
|
||||
final char p = chars[i];
|
||||
|
||||
if (p == ' ' && !identified)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (p == closure.tail)
|
||||
{
|
||||
oopsitsbad = false;
|
||||
break;
|
||||
}
|
||||
if (p == ' ' && !identified) {
|
||||
break;
|
||||
}
|
||||
if (p == closure.tail) {
|
||||
oopsitsbad = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == '_' && !identified)
|
||||
{
|
||||
identified = true;
|
||||
continue;
|
||||
}
|
||||
if (p == '_' && !identified) {
|
||||
identified = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identified)
|
||||
{
|
||||
parameters.append(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
identifier.append(p);
|
||||
}
|
||||
}
|
||||
if (identified) {
|
||||
parameters.append(p);
|
||||
} else {
|
||||
identifier.append(p);
|
||||
}
|
||||
}
|
||||
|
||||
final String identifierString = identifier.toString().toLowerCase();
|
||||
final String parametersString = parameters.toString();
|
||||
final String identifierString = identifier.toString().toLowerCase();
|
||||
final String parametersString = parameters.toString();
|
||||
|
||||
identifier.setLength(0);
|
||||
parameters.setLength(0);
|
||||
identifier.setLength(0);
|
||||
parameters.setLength(0);
|
||||
|
||||
if (oopsitsbad)
|
||||
{
|
||||
builder.append(closure.head).append(identifierString);
|
||||
if (oopsitsbad) {
|
||||
builder.append(closure.head).append(identifierString);
|
||||
|
||||
if (identified)
|
||||
{
|
||||
builder.append('_').append(parametersString);
|
||||
}
|
||||
if (identified) {
|
||||
builder.append('_').append(parametersString);
|
||||
}
|
||||
|
||||
builder.append(' ');
|
||||
continue;
|
||||
}
|
||||
builder.append(' ');
|
||||
continue;
|
||||
}
|
||||
|
||||
final PlaceholderExpansion placeholder = lookup.apply(identifierString);
|
||||
if (placeholder == null)
|
||||
{
|
||||
builder.append(closure.head).append(identifierString);
|
||||
final PlaceholderExpansion placeholder = lookup.apply(identifierString);
|
||||
if (placeholder == null) {
|
||||
builder.append(closure.head).append(identifierString);
|
||||
|
||||
if (identified)
|
||||
{
|
||||
builder.append('_');
|
||||
}
|
||||
if (identified) {
|
||||
builder.append('_');
|
||||
}
|
||||
|
||||
builder.append(parametersString).append(closure.tail);
|
||||
continue;
|
||||
}
|
||||
builder.append(parametersString).append(closure.tail);
|
||||
continue;
|
||||
}
|
||||
|
||||
final String replacement = placeholder.onRequest(player, parametersString);
|
||||
if (replacement == null)
|
||||
{
|
||||
builder.append(closure.head).append(identifierString);
|
||||
final String replacement = placeholder.onRequest(player, parametersString);
|
||||
if (replacement == null) {
|
||||
builder.append(closure.head).append(identifierString);
|
||||
|
||||
if (identified)
|
||||
{
|
||||
builder.append('_');
|
||||
}
|
||||
if (identified) {
|
||||
builder.append('_');
|
||||
}
|
||||
|
||||
builder.append(parametersString).append(closure.tail);
|
||||
continue;
|
||||
}
|
||||
builder.append(parametersString).append(closure.tail);
|
||||
continue;
|
||||
}
|
||||
|
||||
builder.append(ChatColor.translateAlternateColorCodes('&', replacement));
|
||||
}
|
||||
builder.append(ChatColor.translateAlternateColorCodes('&', replacement));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,57 +20,53 @@
|
||||
|
||||
package me.clip.placeholderapi.replacer;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
public final class RegexReplacer implements Replacer {
|
||||
|
||||
public final class RegexReplacer implements Replacer
|
||||
{
|
||||
@NotNull
|
||||
private final Pattern pattern;
|
||||
|
||||
@NotNull
|
||||
private final Pattern pattern;
|
||||
|
||||
public RegexReplacer(@NotNull final Closure closure)
|
||||
{
|
||||
this.pattern = Pattern.compile(String.format("\\%s((?<identifier>[a-zA-Z0-9]+)_)(?<parameters>[^%s%s]+)\\%s", closure.head, closure.head, closure.tail, closure.tail));
|
||||
}
|
||||
public RegexReplacer(@NotNull final Closure closure) {
|
||||
this.pattern = Pattern.compile(String
|
||||
.format("\\%s((?<identifier>[a-zA-Z0-9]+)_)(?<parameters>[^%s%s]+)\\%s", closure.head,
|
||||
closure.head, closure.tail, closure.tail));
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup)
|
||||
{
|
||||
final Matcher matcher = pattern.matcher(text);
|
||||
if (!matcher.find())
|
||||
{
|
||||
return text;
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player,
|
||||
@NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) {
|
||||
final Matcher matcher = pattern.matcher(text);
|
||||
if (!matcher.find()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
final StringBuffer builder = new StringBuffer();
|
||||
final StringBuffer builder = new StringBuffer();
|
||||
|
||||
do
|
||||
{
|
||||
final String identifier = matcher.group("identifier");
|
||||
final String parameters = matcher.group("parameters");
|
||||
do {
|
||||
final String identifier = matcher.group("identifier");
|
||||
final String parameters = matcher.group("parameters");
|
||||
|
||||
final PlaceholderExpansion expansion = lookup.apply(identifier);
|
||||
if (expansion == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
final PlaceholderExpansion expansion = lookup.apply(identifier);
|
||||
if (expansion == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final String requested = expansion.onRequest(player, parameters);
|
||||
matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0));
|
||||
}
|
||||
while (matcher.find());
|
||||
final String requested = expansion.onRequest(player, parameters);
|
||||
matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0));
|
||||
}
|
||||
while (matcher.find());
|
||||
|
||||
return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(builder).toString());
|
||||
}
|
||||
return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(builder).toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,33 +20,30 @@
|
||||
|
||||
package me.clip.placeholderapi.replacer;
|
||||
|
||||
import java.util.function.Function;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
public interface Replacer {
|
||||
|
||||
public interface Replacer
|
||||
{
|
||||
|
||||
@NotNull
|
||||
String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup);
|
||||
@NotNull
|
||||
String apply(@NotNull final String text, @Nullable final OfflinePlayer player,
|
||||
@NotNull final Function<String, @Nullable PlaceholderExpansion> lookup);
|
||||
|
||||
|
||||
enum Closure
|
||||
{
|
||||
BRACKET('{', '}'),
|
||||
PERCENT('%', '%');
|
||||
enum Closure {
|
||||
BRACKET('{', '}'),
|
||||
PERCENT('%', '%');
|
||||
|
||||
|
||||
public final char head, tail;
|
||||
public final char head, tail;
|
||||
|
||||
Closure(final char head, final char tail)
|
||||
{
|
||||
this.head = head;
|
||||
this.tail = tail;
|
||||
}
|
||||
}
|
||||
Closure(final char head, final char tail) {
|
||||
this.head = head;
|
||||
this.tail = tail;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user