Skip to content

Commit

Permalink
Add player values for relational placeholder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed May 24, 2024
1 parent 1801dcb commit cc4c882
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.apickledwalrus.skriptplaceholders.skript.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import io.github.apickledwalrus.skriptplaceholders.skript.RelationalPlaceholderEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("Relational Placeholder Players")
@Description("The two players involved in a relational placeholder request.")
@Examples({
"on placeholderapi placeholder request for the relational prefix \"skriptplaceholders\":",
"\tif the identifier is \"rel_longer_name\": # Placeholder is \"%skriptplaceholders_author%\"",
"\t\tif the length of the name of the first player > the length of the name of the second player:",
"\t\t\tset the result to the name of the first player",
"\t\telse:",
"\t\t\tset the result to the name of the second player"
})
@Since("1.7.0")
public class ExprRelationalPlayers extends SimpleExpression<Player> {

static {
Skript.registerExpression(ExprRelationalPlayers.class, Player.class, ExpressionType.SIMPLE,
"[the] first player",
"[the] second player"
);
}

private boolean first;

@Override
public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull Kleenean kleenean, @NotNull ParseResult parseResult) {
if (!getParser().isCurrentEvent(RelationalPlaceholderEvent.class)) {
Skript.error("'the " + (first ? "first" : "second") + " player' can only be used in custom relational placeholders.");
}
first = matchedPattern == 0;
return true;
}

@Override
protected Player @NotNull [] get(@NotNull Event e) {
if (!(e instanceof RelationalPlaceholderEvent)) {
return new Player[0];
}
RelationalPlaceholderEvent event = (RelationalPlaceholderEvent) e;
return new Player[]{first ? event.getPlayer() : event.getOther()};
}

@Override
public boolean isSingle() {
return true;
}

@Override
public @NotNull Class<? extends Player> getReturnType() {
return Player.class;
}

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "the " + (first ? "first" : "second") + " player";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
"on placeholderapi placeholder request for the prefix \"skriptplaceholders\":",
"\tif the identifier is \"author\": # Placeholder is \"%skriptplaceholders_author%\"",
"\t\tset the result to \"APickledWalrus\"",
"on placeholderapi placeholder request for the relational prefix \"skriptplaceholders\":",
"\tif the identifier is \"rel_longer_name\": # Placeholder is \"%skriptplaceholders_author%\"",
"\t\tif the length of the name of the first player > the length of the name of the second player:",
"\t\t\tset the result to the name of the first player",
"\t\telse:",
"\t\t\tset the result to the name of the second player",
"on mvdw placeholder request for the placeholder \"skriptplaceholders_author\":",
"\t# Placeholder is \"{skriptplaceholders_author}\"",
"\tset the result to \"APickledWalrus\""
Expand Down

0 comments on commit cc4c882

Please sign in to comment.