|
20 | 20 | package ch.njol.skript.conditions;
|
21 | 21 |
|
22 | 22 | 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; |
23 | 27 | import ch.njol.skript.conditions.base.PropertyCondition;
|
24 | 28 | import ch.njol.skript.doc.Description;
|
25 | 29 | import ch.njol.skript.doc.Examples;
|
26 | 30 | import ch.njol.skript.doc.Name;
|
27 | 31 | 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; |
28 | 36 |
|
29 | 37 | @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") |
31 | 39 | @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.\""}) |
33 | 43 | @Since("2.5")
|
34 |
| -public class CondIsBlockRedstonePowered extends PropertyCondition<Block> { |
| 44 | +public class CondIsBlockRedstonePowered extends Condition { |
35 | 45 |
|
36 | 46 | 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; |
38 | 65 | }
|
39 | 66 |
|
40 | 67 | @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()); |
43 | 72 | }
|
44 | 73 |
|
45 | 74 | @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"); |
48 | 77 | }
|
| 78 | + |
49 | 79 | }
|
0 commit comments