fix(replacer): Stop lowercasing replacements

This commit changes two lines but fixes the replacement of cased
    placeholders with non-cased ones that was accidentally happening.
    This was partially fixed in a few previous commits, but not fully as
    it was still replacing failed placeholder matches.
This commit is contained in:
Starmism 2022-03-12 12:42:56 -07:00
parent bb149811d4
commit 1388a5278f
1 changed files with 137 additions and 137 deletions

View File

@ -1,137 +1,137 @@
/* /*
* This file is part of PlaceholderAPI * This file is part of PlaceholderAPI
* *
* PlaceholderAPI * PlaceholderAPI
* Copyright (c) 2015 - 2021 PlaceholderAPI Team * Copyright (c) 2015 - 2021 PlaceholderAPI Team
* *
* PlaceholderAPI free software: you can redistribute it and/or modify * PlaceholderAPI free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* PlaceholderAPI is distributed in the hope that it will be useful, * PlaceholderAPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package me.clip.placeholderapi.replacer; package me.clip.placeholderapi.replacer;
import java.util.Locale; import java.util.Locale;
import java.util.function.Function; import java.util.function.Function;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public final class CharsReplacer implements Replacer { public final class CharsReplacer implements Replacer {
@NotNull @NotNull
private final Closure closure; private final Closure closure;
public CharsReplacer(@NotNull final Closure closure) { public CharsReplacer(@NotNull final Closure closure) {
this.closure = closure; this.closure = closure;
} }
@NotNull @NotNull
@Override @Override
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, public String apply(@NotNull final String text, @Nullable final OfflinePlayer player,
@NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) { @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) {
final char[] chars = text.toCharArray(); final char[] chars = text.toCharArray();
final StringBuilder builder = new StringBuilder(text.length()); final StringBuilder builder = new StringBuilder(text.length());
final StringBuilder identifier = new StringBuilder(); final StringBuilder identifier = new StringBuilder();
final StringBuilder parameters = new StringBuilder(); final StringBuilder parameters = new StringBuilder();
for (int i = 0; i < chars.length; i++) { for (int i = 0; i < chars.length; i++) {
final char l = chars[i]; final char l = chars[i];
if (l != closure.head || i + 1 >= chars.length) { if (l != closure.head || i + 1 >= chars.length) {
builder.append(l); builder.append(l);
continue; continue;
} }
boolean identified = false; boolean identified = false;
boolean invalid = true; boolean invalid = true;
boolean hadSpace = false; boolean hadSpace = false;
while (++i < chars.length) { while (++i < chars.length) {
final char p = chars[i]; final char p = chars[i];
if (p == ' ' && !identified) { if (p == ' ' && !identified) {
hadSpace = true; hadSpace = true;
break; break;
} }
if (p == closure.tail) { if (p == closure.tail) {
invalid = false; invalid = false;
break; break;
} }
if (p == '_' && !identified) { if (p == '_' && !identified) {
identified = true; identified = true;
continue; continue;
} }
if (identified) { if (identified) {
parameters.append(p); parameters.append(p);
} else { } else {
identifier.append(p); identifier.append(p);
} }
} }
final String identifierString = identifier.toString(); final String identifierString = identifier.toString();
final String lowercaseIdentifierString = identifierString.toLowerCase(Locale.ROOT); final String lowercaseIdentifierString = identifierString.toLowerCase(Locale.ROOT);
final String parametersString = parameters.toString(); final String parametersString = parameters.toString();
identifier.setLength(0); identifier.setLength(0);
parameters.setLength(0); parameters.setLength(0);
if (invalid) { if (invalid) {
builder.append(closure.head).append(identifierString); builder.append(closure.head).append(identifierString);
if (identified) { if (identified) {
builder.append('_').append(parametersString); builder.append('_').append(parametersString);
} }
if (hadSpace) { if (hadSpace) {
builder.append(' '); builder.append(' ');
} }
continue; continue;
} }
final PlaceholderExpansion placeholder = lookup.apply(lowercaseIdentifierString); final PlaceholderExpansion placeholder = lookup.apply(lowercaseIdentifierString);
if (placeholder == null) { if (placeholder == null) {
builder.append(closure.head).append(lowercaseIdentifierString); builder.append(closure.head).append(identifierString);
if (identified) { if (identified) {
builder.append('_'); builder.append('_');
} }
builder.append(parametersString).append(closure.tail); builder.append(parametersString).append(closure.tail);
continue; continue;
} }
final String replacement = placeholder.onRequest(player, parametersString); final String replacement = placeholder.onRequest(player, parametersString);
if (replacement == null) { if (replacement == null) {
builder.append(closure.head).append(lowercaseIdentifierString); builder.append(closure.head).append(identifierString);
if (identified) { if (identified) {
builder.append('_'); builder.append('_');
} }
builder.append(parametersString).append(closure.tail); builder.append(parametersString).append(closure.tail);
continue; continue;
} }
builder.append(ChatColor.translateAlternateColorCodes('&', replacement)); builder.append(ChatColor.translateAlternateColorCodes('&', replacement));
} }
return builder.toString(); return builder.toString();
} }
} }