Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExprPrefixSuffix Cleanup #6970

Merged
merged 9 commits into from
Oct 13, 2024
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript is 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.hooks.chat.expressions;

import ch.njol.skript.classes.Changer.ChangeMode;
Expand All @@ -34,66 +16,80 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

/**
* @author Peter Güttinger
*/
import java.util.concurrent.CompletableFuture;

@Name("Prefix/Suffix")
@Description("The prefix or suffix as defined in the server's chat plugin.")
@Examples({
"on chat:",
"\tcancel event",
"\tbroadcast \"%player's prefix%%player's display name%%player's suffix%: %message%\" to the player's world",
"",
"set the player's prefix to \"[&lt;red&gt;Admin<reset>] \""
"set the player's prefix to \"[&lt;red&gt;Admin<reset>] \"",
"",
"clear player's prefix"
})
@Since("2.0")
@Since("2.0, INSERT VERSION (delete)")
@RequiredPlugins({"Vault", "a chat plugin that supports Vault"})
public class ExprPrefixSuffix extends SimplePropertyExpression<Player, String> {
static {
register(ExprPrefixSuffix.class, String.class, "[chat] (1¦prefix|2¦suffix)", "players");
register(ExprPrefixSuffix.class, String.class, "[chat] (1:prefix|2:suffix)", "players");
}

private boolean prefix;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
prefix = parseResult.mark == 1;
return super.init(exprs, matchedPattern, isDelayed, parseResult);
}

@Override
public String convert(final Player p) {
return Utils.replaceChatStyles(prefix ? "" + VaultHook.chat.getPlayerPrefix(p) : "" + VaultHook.chat.getPlayerSuffix(p));
public String convert(Player player) {
return Utils.replaceChatStyles(prefix ? VaultHook.chat.getPlayerPrefix(player) : VaultHook.chat.getPlayerSuffix(player));
}

@Override
protected String getPropertyName() {
return prefix ? "prefix" : "suffix";
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
return switch (mode) {
case SET -> new Class[] {String.class};
case RESET, DELETE -> new Class<?>[0];
default -> null;
};
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
CompletableFuture.runAsync(() -> {
for (Player player : getExpr().getArray(event)) {
switch (mode) {
case SET -> {
if (prefix) {
VaultHook.chat.setPlayerPrefix(player, (String) delta[0]);
} else {
VaultHook.chat.setPlayerSuffix(player, (String) delta[0]);
}
}
case RESET, DELETE -> {
if (prefix) {
VaultHook.chat.setPlayerPrefix(player, null);
} else {
VaultHook.chat.setPlayerSuffix(player, null);
}
}
}
}
}).join();
}

@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
if (mode == ChangeMode.SET)
return new Class[] {String.class};
return null;
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) {
assert mode == ChangeMode.SET;
assert delta != null;
for (final Player p : getExpr().getArray(e)) {
if (prefix)
VaultHook.chat.setPlayerPrefix(p, (String) delta[0]);
else
VaultHook.chat.setPlayerSuffix(p, (String) delta[0]);
}
protected String getPropertyName() {
return prefix ? "prefix" : "suffix";
}

}