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
Changes from 2 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
92 changes: 92 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprLiddable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
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.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.block.BlockState;
import org.bukkit.block.Block;
import org.bukkit.block.Lidded;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

@Name("Lid State")
@Description("Set the lid state of a chest, ender chest, barrel and shulker box.")
@Examples({
"if the lid state of {_chest} is false:",
"\tset the lid state of {_chest} to true"
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
public class ExprLiddable extends PropertyExpression<Block, Boolean> {

static {
Skript.registerExpression(ExprLiddable.class, Boolean.class, ExpressionType.PROPERTY, "[the] lid [state] of %blocks%");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Override
protected Boolean @Nullable [] get(Event event, Block[] source) {
List<Boolean> booleanList = new ArrayList<>();
for (Block block : getExpr().getArray(event)) {
BlockState state = block.getState();
if (state instanceof Lidded lidBlock) {
booleanList.add(lidBlock.isOpen());
}
}
return booleanList.toArray(new Boolean[0]);
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.SET)
return CollectionUtils.array(Boolean.class);
return null;
};

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
boolean toOpen = (boolean) delta[0];
if (toOpen) {
for (Block block : getExpr().getArray(event)) {
BlockState state = block.getState();
if (state instanceof Lidded lidBlock) {
lidBlock.open();
}
}
} else {
for (Block block : getExpr().getArray(event)) {
BlockState state = block.getState();
if (state instanceof Lidded lidBlock) {
lidBlock.close();
}
}
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Class<Boolean> getReturnType() {
return Boolean.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "lid state of " + getExpr().toString(event, debug);
}

}