2020-07-27 09:21:35 +02:00
|
|
|
/*
|
|
|
|
* This file is part of PlaceholderAPI
|
|
|
|
*
|
|
|
|
* PlaceholderAPI
|
|
|
|
* Copyright (c) 2015 - 2020 PlaceholderAPI Team
|
|
|
|
*
|
|
|
|
* PlaceholderAPI free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlaceholderAPI is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-07-20 22:59:25 +02:00
|
|
|
package me.clip.placeholderapi.replacer;
|
|
|
|
|
2020-07-27 00:03:31 +02:00
|
|
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
2020-07-23 05:59:39 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2020-07-20 22:59:25 +02:00
|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
private final Closure closure;
|
|
|
|
|
|
|
|
public CharsReplacer(@NotNull final Closure closure)
|
|
|
|
{
|
|
|
|
this.closure = closure;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-27 00:03:31 +02:00
|
|
|
@NotNull
|
2020-07-20 22:59:25 +02:00
|
|
|
@Override
|
2020-07-27 00:03:31 +02:00
|
|
|
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup)
|
2020-07-20 22:59:25 +02:00
|
|
|
{
|
|
|
|
final char[] chars = text.toCharArray();
|
|
|
|
final StringBuilder builder = new StringBuilder(text.length());
|
|
|
|
|
|
|
|
final StringBuilder identifier = new StringBuilder();
|
|
|
|
final StringBuilder parameters = new StringBuilder();
|
|
|
|
|
|
|
|
for (int i = 0; i < chars.length; i++)
|
|
|
|
{
|
|
|
|
final char l = chars[i];
|
|
|
|
|
|
|
|
if (l == '&' && ++i < chars.length)
|
|
|
|
{
|
2020-07-23 05:59:39 +02:00
|
|
|
final char c = Character.toLowerCase(chars[i]);
|
2020-07-20 22:59:25 +02:00
|
|
|
|
|
|
|
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')
|
|
|
|
{
|
2020-07-23 05:59:39 +02:00
|
|
|
builder.append(l).append(chars[i]);
|
2020-07-20 22:59:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-23 05:59:39 +02:00
|
|
|
builder.append(ChatColor.COLOR_CHAR);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(c);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == 7)
|
|
|
|
{
|
|
|
|
i += 6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
builder.setLength(builder.length() - (j * 2)); // undo &x parsing
|
|
|
|
}
|
2020-07-20 22:59:25 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l != closure.head || i + 1 >= chars.length)
|
|
|
|
{
|
|
|
|
builder.append(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean identified = false;
|
2020-07-21 07:48:59 +02:00
|
|
|
boolean oopsitsbad = true;
|
2020-07-20 22:59:25 +02:00
|
|
|
|
|
|
|
while (++i < chars.length)
|
|
|
|
{
|
|
|
|
final char p = chars[i];
|
|
|
|
|
2020-07-21 07:48:59 +02:00
|
|
|
if (p == ' ')
|
2020-07-20 22:59:25 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2020-07-21 07:48:59 +02:00
|
|
|
if (p == closure.tail)
|
2020-07-20 22:59:25 +02:00
|
|
|
{
|
2020-07-21 07:48:59 +02:00
|
|
|
oopsitsbad = false;
|
2020-07-20 22:59:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == '_' && !identified)
|
|
|
|
{
|
|
|
|
identified = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identified)
|
|
|
|
{
|
|
|
|
parameters.append(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
identifier.append(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final String identifierString = identifier.toString();
|
|
|
|
final String parametersString = parameters.toString();
|
|
|
|
|
|
|
|
identifier.setLength(0);
|
|
|
|
parameters.setLength(0);
|
|
|
|
|
|
|
|
if (oopsitsbad)
|
|
|
|
{
|
|
|
|
builder.append(closure.head).append(identifierString);
|
|
|
|
|
|
|
|
if (identified)
|
|
|
|
{
|
|
|
|
builder.append('_').append(parametersString);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(' ');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-27 00:03:31 +02:00
|
|
|
final PlaceholderExpansion placeholder = lookup.apply(identifierString);
|
2020-07-20 22:59:25 +02:00
|
|
|
if (placeholder == null)
|
|
|
|
{
|
2020-07-21 07:48:59 +02:00
|
|
|
builder.append(closure.head).append(identifierString);
|
|
|
|
|
|
|
|
if (identified)
|
|
|
|
{
|
|
|
|
builder.append('_');
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(parametersString).append(closure.tail);
|
2020-07-20 22:59:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String replacement = placeholder.onRequest(player, parametersString);
|
|
|
|
if (replacement == null)
|
|
|
|
{
|
2020-07-21 07:48:59 +02:00
|
|
|
builder.append(closure.head).append(identifierString);
|
|
|
|
|
|
|
|
if (identified)
|
|
|
|
{
|
|
|
|
builder.append('_');
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(parametersString).append(closure.tail);
|
2020-07-20 22:59:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(replacement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|