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

Support "is connected" pattern. #6105

Merged
merged 9 commits into from
Apr 15, 2024
35 changes: 24 additions & 11 deletions src/main/java/ch/njol/skript/conditions/CondIsOnline.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,49 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Is Online")
@Description("Checks whether a player is online.")
@Examples({"player is online",
"player-argument is offline"})
@Description("Checks whether a player is online. The 'connected' pattern will return false once this player leaves the server, even if they rejoin. Be aware that using the 'connected' pattern with a variable will not have this special behavior. Use the direct event-player or other non-variable expression for best results.")
@Examples({
"player is online",
"player-argument is offline",
"while player is connected:",
"\twait 60 seconds",
"\tsend \"hello!\" to player",
"",
"# The following will act like `{_player} is online`.",
"# Using variables with `is connected` will not behave the same as with non-variables.",
"while {_player} is connected:",
"\tbroadcast \"online!\"",
"\twait 1 tick"
})
@Since("1.4")
public class CondIsOnline extends PropertyCondition<OfflinePlayer> {

static {
register(CondIsOnline.class, "(online|offline)", "offlineplayers");
register(CondIsOnline.class, "(online|:offline|:connected)", "offlineplayers");
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
}
Moderocky marked this conversation as resolved.
Show resolved Hide resolved

private boolean connected; // https://github.com/SkriptLang/Skript/issues/6100

AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
setExpr((Expression<OfflinePlayer>) exprs[0]);
setNegated(matchedPattern == 1 ^ parseResult.mark == 1);
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.setExpr((Expression<OfflinePlayer>) exprs[0]);
this.setNegated(matchedPattern == 1 ^ parseResult.hasTag("offline"));
this.connected = parseResult.hasTag("connected");
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

@Override
public boolean check(OfflinePlayer op) {
if (connected)
return op.isConnected();
return op.isOnline();
}

@Override
protected String getPropertyName() {
return "online";
return connected ? "connected" : "online";
}

}
Loading