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

ExprExplodedBlocks - fix not being able to remove #6655

Merged
merged 10 commits into from
Jun 1, 2024
59 changes: 54 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.List;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityExplodeEvent;
Expand All @@ -38,12 +40,23 @@
import ch.njol.util.Kleenean;

@Name("Exploded Blocks")
@Description("Get all the blocks that were destroyed in an explode event")
@Examples({"on explode:",
"\tloop exploded blocks:",
"\t\tadd loop-block to {exploded::blocks::*}"})
@Description("Get all the blocks that were destroyed in an explode event. Supports add/remove/set/clear/delete blocks.")
@Examples({
"on explode:",
"\tloop exploded blocks:",
"\t\tadd loop-block to {exploded::blocks::*}",
"on explode:",
"\tloop exploded blocks:",
"\t\tif loop-block is grass:",
"\t\t\tremove loop-block from exploded blocks",
"on explode:",
"\tclear exploded blocks",
"on explode:",
"\tset exploded blocks to blocks in radius 10 around event-entity",
"on explode:",
"\tadd blocks above event-entity to exploded blocks"})
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
@Events("explode")
@Since("2.5")
@Since("2.5, INSERT VERSION (modify list)")
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
public class ExprExplodedBlocks extends SimpleExpression<Block> {

static {
Expand All @@ -68,6 +81,42 @@ protected Block[] get(Event e) {
List<Block> blockList = ((EntityExplodeEvent) e).blockList();
return blockList.toArray(new Block[blockList.size()]);
}

@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case ADD:
case REMOVE:
case SET:
return CollectionUtils.array(Block[].class);
case DELETE:
return CollectionUtils.array();
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
default:
return null;
}
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
if (!(event instanceof EntityExplodeEvent))
return;

List<Block> blocks = ((EntityExplodeEvent) event).blockList();
if (mode == ChangeMode.DELETE) {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
blocks.clear();
return;
}
if (mode == ChangeMode.SET)
blocks.clear();
for (Object object : delta) {
if (object instanceof Block) {
if (mode == ChangeMode.REMOVE)
blocks.remove((Block) object);
else if (mode == ChangeMode.ADD || mode == ChangeMode.SET)
blocks.add((Block) object);
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

@Override
public boolean isSingle() {
Expand Down