Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/fr/romitou/mongosk/skript/conditions/CondMongoDocHasKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package fr.romitou.mongosk.skript.conditions; | ||
|
||
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.Condition; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.SkriptParser; | ||
import ch.njol.util.Kleenean; | ||
import fr.romitou.mongosk.elements.MongoSKDocument; | ||
import org.bukkit.event.Event; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
@Name("Mongo document has key?") | ||
@Description("Checks if the documents contains the specified key.") | ||
@Examples({"set {_doc} to a new mongo document", | ||
"set mongo value \"foo\" of {_doc} to \"bar\"", | ||
"if mongo document {_doc} has key \"foo\":", | ||
"\tbroadcast \"The 'foo' key exists!\"", | ||
"else:", | ||
"\tbroadcast \"The 'foo' key doesn't exist :(\"" | ||
}) | ||
@Since("2.1.0") | ||
public class CondMongoDocHasKey extends Condition { | ||
|
||
static { | ||
Skript.registerCondition( | ||
CondMongoDocHasKey.class, | ||
"mongo[(sk|db)] document[s] %mongoskdocuments% (1¦(has|contains)|2¦doesn't (have|contain)) key [named] %string%" | ||
); | ||
} | ||
|
||
private Expression<MongoSKDocument> exprMongoSKDocument; | ||
private Expression<String> exprKey; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, @Nonnull Kleenean isDelayed, SkriptParser.ParseResult parseResult) { | ||
exprMongoSKDocument = (Expression<MongoSKDocument>) exprs[0]; | ||
exprKey = (Expression<String>) exprs[1]; | ||
setNegated(parseResult.mark == 2); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(@Nonnull Event e) { | ||
String key = exprKey.getSingle(e); | ||
if (key == null) | ||
return false; | ||
return exprMongoSKDocument.check(e, doc -> doc.getBsonDocument().containsKey(key), isNegated()); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String toString(Event e, boolean debug) { | ||
return exprMongoSKDocument.toString(e, debug) + (isNegated() ? " doesn't have" : " has") + " key " + exprKey.toString(e, debug); | ||
} | ||
} |