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

adds delay checks to input key syntaxes #7392

Open
wants to merge 3 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.effects.Delay;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class CondIsPressingKey extends Condition {
private Expression<Player> players;
private Expression<InputKey> inputKeys;
private boolean past;
private boolean delayed;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Expand All @@ -54,8 +56,14 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
//noinspection unchecked
inputKeys = (Expression<InputKey>) expressions[1];
past = matchedPattern > 1;
if (past && !getParser().isCurrentEvent(PlayerInputEvent.class))
Skript.warning("Checking the past state of a player's input outside the 'player input' event has no effect.");
delayed = !isDelayed.isFalse();
if (past) {
if (!getParser().isCurrentEvent(PlayerInputEvent.class)) {
Skript.warning("Checking the past state of a player's input outside the 'player input' event has no effect.");
} else if (delayed) {
Skript.warning("Checking the past state of a player's input after the event has passed has no effect.");
}
}
setNegated(matchedPattern == 1 || matchedPattern == 3);
return true;
}
Expand All @@ -65,10 +73,11 @@ public boolean check(Event event) {
Player eventPlayer = event instanceof PlayerInputEvent inputEvent ? inputEvent.getPlayer() : null;
InputKey[] inputKeys = this.inputKeys.getAll(event);
boolean and = this.inputKeys.getAnd();
boolean delayed = this.delayed || Delay.isDelayed(event);
return players.check(event, player -> {
Input input;
// If we want to get the new input of the event-player, we must get it from the event
if (!past && player.equals(eventPlayer)) {
if (!delayed && !past && player.equals(eventPlayer)) {
input = ((PlayerInputEvent) event).getInput();
} else { // Otherwise, we get the current (or past in case of an event-player) input
input = player.getCurrentInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.effects.Delay;
import ch.njol.skript.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
Expand Down Expand Up @@ -29,9 +30,12 @@ public class ExprCurrentInputKeys extends PropertyExpression<Player, InputKey> {
register(ExprCurrentInputKeys.class, InputKey.class, "[current] (inputs|input keys)", "players");
}

private boolean delayed;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<? extends Player>) expressions[0]);
delayed = !isDelayed.isFalse();
return true;
}

Expand All @@ -41,9 +45,11 @@ protected InputKey[] get(Event event, Player[] source) {
if (SUPPORTS_TIME_STATES && getTime() == EventValues.TIME_NOW && event instanceof PlayerInputEvent inputEvent)
eventPlayer = inputEvent.getPlayer();

boolean delayed = this.delayed || Delay.isDelayed(event);

List<InputKey> inputKeys = new ArrayList<>();
for (Player player : source) {
if (player.equals(eventPlayer)) {
if (!delayed && player.equals(eventPlayer)) {
inputKeys.addAll(InputKey.fromInput(((PlayerInputEvent) event).getInput()));
} else {
inputKeys.addAll(InputKey.fromInput(player.getCurrentInput()));
Expand Down
Loading