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

Eff + Cond Lidded #7094

Open
wants to merge 13 commits into
base: dev/feature
Choose a base branch
from
67 changes: 67 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondLidState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ch.njol.skript.conditions;

import ch.njol.skript.Skript;
import ch.njol.skript.conditions.base.PropertyCondition;
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.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.block.Block;
import org.bukkit.block.Lidded;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Lid is Open")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Description("Check to see if the lid of blocks are open or closed.")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"if the lid of {_chest} is closed:",
"\topen the lid of {_block}"
})
@Since("INSERT VERSION")
public class CondLidState extends PropertyCondition<Block> {

static {
Skript.registerCondition(CondLidState.class, ConditionType.PROPERTY,
"[the] lid [state] of %blocks% (is|are) open[ed]",
"[the] lid [state] of %blocks% (isn't|aren't) open[ed]",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"[the] lid [state] of %blocks% (is|are) close[d]",
"[the] lid [state] of %blocks% (isn't|aren't) close[d]"
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved


TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

private boolean checkOpen;
private Expression<Block> blocks;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
checkOpen = matchedPattern <= 1;
blocks = (Expression<Block>) exprs[0];
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
setExpr((Expression<Block>) exprs[0]);
if (matchedPattern == 1 || matchedPattern == 3)
setNegated(true);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

@Override
public boolean check(Block block) {
if (!(block.getState() instanceof Lidded lidBlock))
return false;
return lidBlock.isOpen() == checkOpen;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected String getPropertyName() {
return (checkOpen ? "opened" : "closed") + "lid state";
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return (checkOpen ? "opened" : "closed") + "lid of " + blocks.toString(event, debug);
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
62 changes: 62 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffLidState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ch.njol.skript.effects;

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.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.block.Block;
import org.bukkit.block.Lidded;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Open/Close Lid")
@Description("Open or close the lid of blocks.")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"open the lid of {_chest}",
"close the lid of {_blocks::*}"
})
@Since("INSERT VERSION")
public class EffLidState extends Effect {

static {
Skript.registerEffect(EffLidState.class,
"open [the] lid (of|for) %blocks%",
"close [the] lid (of|for) %blocks%"
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean setOpen;
private Expression<Block> blocks;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setOpen = matchedPattern == 0;
blocks = (Expression<Block>) exprs[0];
return true;
}

@Override
protected void execute(Event event) {
for (Block block : blocks.getArray(event)) {
if (block.getState() instanceof Lidded lidBlock) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
if (setOpen) {
lidBlock.open();
} else {
lidBlock.close();
}
}
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return (setOpen ? "open" : "close") + "lid of " + blocks.toString(event, debug);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

}