Skip to content

Commit dcd89af

Browse files
authored
CondIsBlockRedstonePowered - Adding "indirectly redstone powered" (#3297)
1 parent 1de865e commit dcd89af

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

src/main/java/ch/njol/skript/conditions/CondIsBlockRedstonePowered.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,60 @@
2020
package ch.njol.skript.conditions;
2121

2222
import org.bukkit.block.Block;
23+
import org.bukkit.event.Event;
24+
import org.eclipse.jdt.annotation.Nullable;
25+
26+
import ch.njol.skript.Skript;
2327
import ch.njol.skript.conditions.base.PropertyCondition;
2428
import ch.njol.skript.doc.Description;
2529
import ch.njol.skript.doc.Examples;
2630
import ch.njol.skript.doc.Name;
2731
import ch.njol.skript.doc.Since;
32+
import ch.njol.skript.lang.Condition;
33+
import ch.njol.skript.lang.Expression;
34+
import ch.njol.skript.lang.SkriptParser.ParseResult;
35+
import ch.njol.util.Kleenean;
2836

2937
@Name("Is Block Redstone Powered")
30-
@Description("Checks if a block is powered by redstone")
38+
@Description("Checks if a block is indirectly or directly powered by redstone")
3139
@Examples({"if clicked block is redstone powered:",
32-
"\tsend \"This block is well-powered by redstone!\""})
40+
"\tsend \"This block is well-powered by redstone!\"",
41+
"if clicked block is indirectly redstone powered:",
42+
"\tsend \"This block is indirectly redstone powered.\""})
3343
@Since("2.5")
34-
public class CondIsBlockRedstonePowered extends PropertyCondition<Block> {
44+
public class CondIsBlockRedstonePowered extends Condition {
3545

3646
static {
37-
register(CondIsBlockRedstonePowered.class, "redstone powered", "blocks");
47+
Skript.registerCondition(CondIsBlockRedstonePowered.class,
48+
"%blocks% (is|are) redstone powered",
49+
"%blocks% (is|are) indirectly redstone powered",
50+
"%blocks% (is|are)(n't| not) redstone powered",
51+
"%blocks% (is|are)(n't| not) indirectly redstone powered");
52+
}
53+
54+
@SuppressWarnings("null")
55+
private Expression<Block> blocks;
56+
private boolean isIndirectlyPowered;
57+
58+
@SuppressWarnings({"unchecked", "null"})
59+
@Override
60+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
61+
blocks = (Expression<Block>) exprs[0];
62+
isIndirectlyPowered = matchedPattern % 2 == 1;
63+
setNegated(matchedPattern > 1);
64+
return true;
3865
}
3966

4067
@Override
41-
public boolean check(Block b) {
42-
return b.isBlockPowered();
68+
public boolean check(Event e) {
69+
return isIndirectlyPowered
70+
? blocks.check(e, Block::isBlockIndirectlyPowered, isNegated())
71+
: blocks.check(e, Block::isBlockPowered, isNegated());
4372
}
4473

4574
@Override
46-
protected String getPropertyName() {
47-
return "redstone powered";
75+
public String toString(@Nullable Event e, boolean debug) {
76+
return PropertyCondition.toString(this, PropertyCondition.PropertyType.BE, e, debug, blocks, (isIndirectlyPowered ? "indirectly " : "") + "powered");
4877
}
78+
4979
}

0 commit comments

Comments
 (0)