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

EffBlockUpdate #7065

Open
wants to merge 18 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
26 changes: 26 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import ch.njol.yggdrasil.Fields;
import io.papermc.paper.world.MoonPhase;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

/**
* @author Peter Güttinger
Expand Down Expand Up @@ -1526,6 +1527,31 @@ public String toVariableNameString(EnchantmentOffer eo) {
.name("Transform Reason")
.description("Represents a transform reason of an <a href='events.html#entity transform'>entity transform event</a>.")
.since("2.8.0"));

if (Classes.getExactClassInfo(BlockState.class) == null) {
Classes.registerClass(new ClassInfo<>(BlockState.class, "blockstate")
.user("block ?states?")
.name("BlockState")
.description("Represents the block state of a block.")
.since("INSERT VERSION")
.parser(new Parser<>() {
@Override
public boolean canParse(ParseContext context) {
return false;
}

@Override
public @NotNull String toString(BlockState blockState, int flags) {
return String.format("BlockState{type=%s,location=%s}",
blockState.getType(), blockState.getLocation());
}

@Override
public @NotNull String toVariableNameString(BlockState blockState) {
return toString(blockState, 0);
}
}));
}
}

}
89 changes: 89 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffBlockUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("BlockState - Update")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Description({
"Updates the blockstate of a block to cached state or selected block",
"`force`: Will force the update of the block",
"`without physics`: Does not send updates to surrounding blocks"
})
@Examples({
"set {_state} to blockstate of event-block",
"set event-block to air",
"wait 1 minute",
"force update {_state} without physics updates",
"",
"force update event-block as stone without physics updates"
})
@Since("INSERT VERSION")
// Ported over from SkBee made by ShaneBee (Credits go to him)
public class EffBlockUpdate extends Effect {

static {
Skript.registerEffect(EffBlockUpdate.class,
"[:force] update %blockstates% [physics:without (neighbour|physics) updates]",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"[:force] update %blocks% as %blockdata% [physics:without (neighbour|physics) updates]");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean force, physics;
private Expression<BlockState> blockStates;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
private Expression<Block> blocks;
private Expression<BlockData> blockData;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.force = parseResult.hasTag("force");
this.physics = !parseResult.hasTag("physics");
if (matchedPattern == 0) {
this.blockStates = (Expression<BlockState>) exprs[0];
} else {
this.blocks = (Expression<Block>) exprs[0];
this.blockData = (Expression<BlockData>) exprs[1];
}
return true;
}

@Override
protected void execute(Event event) {
if (this.blockStates != null) {
for (BlockState blockState : this.blockStates.getArray(event)) {
blockState.update(this.force, this.physics);
}
} else {
for (Block block : this.blocks.getArray(event)) {
BlockState state = block.getState();
state.setBlockData(this.blockData.getSingle(event));
state.update(this.force, this.physics);
}
}
}

@Override
public @NotNull String toString(@Nullable Event event, boolean bool) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
String result = this.force ? "force " : "";
if (this.blockStates != null) {
result += this.blockStates.toString(event, bool);
} else {
result += this.blocks.toString(event, bool) + " " + this.blockData.toString(event, bool);
}
result += this.physics ? " without neighbour updates" : "";
return result;
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

}
46 changes: 46 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprBlockState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch.njol.skript.expressions;

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.SimplePropertyExpression;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("BlockState")
@Description({
"Gets the blockstate of a block.",
"Can be used to update block back to this state."
})
@Examples({
"set {_state} to blockstate of event-block",
"set event-block to air",
"wait 1 minute",
"force update {_state} without physics updates"
})
@Since("INSERT VERSION")
// Ported over from SkBee made by ShaneBee (Credits go to him)
Pikachu920 marked this conversation as resolved.
Show resolved Hide resolved
public class ExprBlockState extends SimplePropertyExpression<Block, BlockState> {
static {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
register(ExprBlockState.class, BlockState.class, "(captured|block)[ ]state[s]", "blocks");
}

@Override
public @Nullable BlockState convert(Block block) {
return block.getState();
}

@Override
public @NotNull Class<? extends BlockState> getReturnType() {
return BlockState.class;
}

@Override
protected @NotNull String getPropertyName() {
return "block state";
}

}
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,7 @@ types:
experience.pattern: (e?xp|experience( points?)?)
classinfo: type¦s @a
visualeffect: visual effect¦s @a
blockstate: blockstate¦s

# Hooks
money: money
Expand Down
25 changes: 25 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffBlockUpdate.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test "block update":
set {_block} to spawn of world "world" ~ vector(10,10,10)
set {_stateAir} to block state of block at {_block}
set block at {_block} to stone
set {_stateStone} to block state of block at {_block}
set block at {_block} to air
force update {_stateStone}
assert block at {_block} is stone with "State: Block state did not update to stone"
force update {_stateAir}
assert block at {_block} is air with "State: Block state did not update to air"

set {_blocks::*} to blocks in radius 2 of block at {_block}
set {_statesAir::*} to block state of {_blocks::*}
set blocks at {_blocks::*} to stone
set {_statesStone::*} to block state of blocks at {_blocks::*}
set blocks at {_blocks::*} to air
force update {_statesStone::*}
assert blocks at {_blocks::*} is stone with "States: 1 or more block state did not update to stone"
force update {_statesAir::*}
assert blocks at {_blocks::*} is air with "States: 1 or more block state did not update to air"

force update blocks at {_blocks::*} as sand without physics updates
assert blocks at {_blocks::*} is sand with "As: 1 or more block state did not update with or without physics"
force update blocks at {_blocks::*} as air without physics updates
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
assert blocks at {_blocks::*} is air with "As: 1 or more block state did not update to air"