Skip to content

Commit

Permalink
Support "is connected" pattern. (#6105)
Browse files Browse the repository at this point in the history
* Support "is connected" pattern.

* Move example down one line in case the world ends

* Update src/main/java/ch/njol/skript/conditions/CondIsOnline.java

Co-authored-by: Ayham Al Ali <[email protected]>

* Update src/main/java/ch/njol/skript/conditions/CondIsOnline.java

Co-authored-by: Ayham Al Ali <[email protected]>

* Update src/main/java/ch/njol/skript/conditions/CondIsOnline.java

Co-authored-by: sovdee <[email protected]>

* Apply suggestions from code review

Co-authored-by: sovdee <[email protected]>

---------

Co-authored-by: Ayham Al Ali <[email protected]>
Co-authored-by: sovdee <[email protected]>
  • Loading branch information
3 people authored Apr 15, 2024
1 parent 7759802 commit 9bba481
Showing 1 changed file with 24 additions and 11 deletions.
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");
}

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

@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");
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";
}

}

0 comments on commit 9bba481

Please sign in to comment.