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

Merged
merged 19 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 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,56 @@
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/Closed")
@Description("Check to see whether lidded blocks (chests, shulkers, etc.) are open or closed.")
@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[s]] of %blocks% (is|are) (open[ed]|:close[d])",
"[the] lid [state[s]] of %blocks% (isn't|is not|aren't|are not) (open[ed]|:close[d])",
"%blocks%'[s] lid [state[s]] (is|are) (open[ed]|:close[d])",
"%blocks%'[s] lid [state[s]] (isn't|is not|aren't|are not) (open[ed]|:close[d])"
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
);
}

private boolean checkOpen;

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

@Override
public boolean check(Block block) {
return (block.getState() instanceof Lidded lidded) ? lidded.isOpen() == checkOpen : false;
}

@Override
protected String getPropertyName() {
return (checkOpen ? "opened" : "closed") + " lid state";
}
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 the block(s).")
@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|:close) [the] lid (of|for) %blocks%",
"(open|:close) %blocks%'[s] lid"
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 = !parseResult.hasTag("close");
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);
}

}