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

Adds some command block syntax #6859

Merged
merged 24 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b19a17a
Adds command block syntax
cheeezburga Jul 4, 2024
0214404
Adds Skript annotations
cheeezburga Jul 4, 2024
14452c6
Apply suggestion from review
cheeezburga Jul 17, 2024
a114752
Suggestions
cheeezburga Jul 17, 2024
234a91c
Merge remote-tracking branch 'origin/command-block-syntax' into comma…
cheeezburga Jul 17, 2024
a61770a
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Jul 17, 2024
d5c75da
Hopefully fixes tests
cheeezburga Jul 17, 2024
c9dc493
Merge remote-tracking branch 'origin/command-block-syntax' into comma…
cheeezburga Jul 17, 2024
8c16754
Hopefully fixes tests
cheeezburga Jul 17, 2024
3b26cef
Uses setNegated and hopefully fixes tests
cheeezburga Jul 17, 2024
784250a
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Jul 21, 2024
e14da98
Suggestions
cheeezburga Jul 21, 2024
31490df
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Sep 1, 2024
0783fb3
Suggestions and stuff
cheeezburga Sep 1, 2024
3bf5ec3
Changes effect pattern to be more clear
cheeezburga Sep 2, 2024
d71a6ab
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Sep 2, 2024
36d3416
Fixes test syntax
cheeezburga Sep 2, 2024
bb55224
Merge remote-tracking branch 'origin/command-block-syntax' into comma…
cheeezburga Sep 2, 2024
1f24b16
Merge branch 'dev/feature' into command-block-syntax
Moderocky Sep 6, 2024
b773c9c
Pattern matching in CondIsCommandBlockConditional
cheeezburga Sep 13, 2024
67b2116
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Sep 13, 2024
d32007e
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Oct 2, 2024
ea4e687
Merge branch 'dev/feature' into command-block-syntax
cheeezburga Oct 5, 2024
e40c687
Merge branch 'dev/feature' into command-block-syntax
sovdeeth Oct 13, 2024
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
49 changes: 49 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsConditional.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ch.njol.skript.conditions;

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.data.type.CommandBlock;

@Name("Is Conditional")
@Description(
"Checks whether a command block is conditional or not."
)
@Examples({
"if {_block} is conditional:",
"\tmake {_block} unconditional"
})
@Since("INSERT VERSION")
public class CondIsConditional extends PropertyCondition<Block> {

static {
register(CondIsConditional.class, "[:un]conditional", "blocks");
}

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<Block>) exprs[0]);
setNegated(parseResult.hasTag("un") ^ matchedPattern == 1);
return true;
}

@Override
public boolean check(Block block) {
if (block.getBlockData() instanceof CommandBlock)
return ((CommandBlock) block.getBlockData()).isConditional();
return false;
}

@Override
protected String getPropertyName() {
return "conditional";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.data.type.CommandBlock;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

@Name("Conditional / Unconditional")
@Description(
"Sets whether the provided command blocks are conditional or not."
)
@Examples({
"make {_block} conditional",
"make {_block} unconditional if {_block} is conditional"
})
@Since("INSERT VERSION")
public class EffCommandBlockConditional extends Effect {

static {
Skript.registerEffect(EffCommandBlockConditional.class, "make %blocks% [not:(un|not )]conditional");
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<Block> blocks;
private boolean conditional;

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

@Override
protected void execute(Event event) {
for (Block block : blocks.getArray(event)) {
if (block.getBlockData() instanceof CommandBlock) {
CommandBlock data = (CommandBlock) block.getBlockData();
data.setConditional(conditional);
block.setBlockData(data);
}
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "make " + blocks.toString(event, debug) + (conditional ? " " : " un") + "conditional";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ch.njol.skript.expressions;

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.SimplePropertyExpression;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.block.Block;
import org.bukkit.block.CommandBlock;
import org.bukkit.entity.minecart.CommandMinecart;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

@Name("Command Block Command")
@Description(
"Gets or sets the command associated with a command block or minecart with command block."
)
@Examples({
"send command of {_block}",
"set command of {_cmdMinecart} to \"say asdf\""
})
@Since("INSERT VERSION")
public class ExprCommandBlockCommand extends SimplePropertyExpression<Object, String> {

static {
register(ExprCommandBlockCommand.class, String.class, "[command[ ]block] command", "blocks/entities");
}

@Override
public @Nullable String convert(Object holder) {
String command = "";
if (holder instanceof Block && ((Block) holder).getState() instanceof CommandBlock) {
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
command = ((CommandBlock) ((Block) holder).getState()).getCommand();
} else if (holder instanceof CommandMinecart) {
command = ((CommandMinecart) holder).getCommand();
}
return (command.isEmpty()) ? null : command;
}

@Override
@SuppressWarnings("ConstantConditions")
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
if (mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET)
return CollectionUtils.array(String.class);
return null;
}

@Override
@SuppressWarnings("ConstantConditions")
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
String newCommand = delta == null ? null : ((String) delta[0]);
for (Object holder : getExpr().getArray(event)) {
switch (mode) {
case RESET:
case DELETE:
case SET:
if (holder instanceof Block && ((Block) holder).getState() instanceof CommandBlock) {
CommandBlock state = ((CommandBlock) ((Block) holder).getState());
state.setCommand(newCommand);
state.update();
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
} else if (holder instanceof CommandMinecart) {
((CommandMinecart) holder).setCommand(newCommand);
}
break;
default:
assert false;
}
}
}

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

@Override
public Class<? extends String> getReturnType() {
return String.class;
}
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

}
17 changes: 17 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsConditional.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "is conditional":
set block at location(0,0,0,world) to command block
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
set {_b} to block at location(0,0,0,world)

assert {_b} is not conditional with "a new command block shouldn't be conditional"

make {_b} conditional
assert {_b} is conditional with "making a command block conditional should do exactly that"

make {_b} unconditional
assert {_b} is unconditional with "making a command block unconditional should do exactly that"

set block at {_b} to stone
assert {_b} is not conditional with "a non-command block should not be conditional"

make {_b} conditional
assert {_b} is not conditional with "making a non-command block conditional shouldn't do anything"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
test "command block command":
set block at location(0,1,0,world) to command block
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved
spawn a minecart with command block at (spawn of world "world"):
set {_e} to entity
spawn a zombie at (spawn of world "world"):
set {_z} to entity
set {_b} to block at location(0,1,0,world)

assert command block command of {_b} is not set with "a new command block shouldn't have an associated command"
assert command block command of {_e} is not set with "a new minecart with command block shouldn't have an associated command"

set command block command of {_b} to "say asdf"
set command block command of {_e} to "say asdf"
assert command block command of {_b} is "say asdf" with "setting the command of a command block should do exactly that"
assert command block command of {_e} is "say asdf" with "setting the command of a minecart with command block should do exactly that"

clear command block command of {_b}
clear command block command of {_e}
assert command block command of {_b} is not set with "clearing/deleting/resetting the command of a command block should return null"
assert command block command of {_e} is not set with "clearing/deleting/resetting the command of a minecart with command block should return null"

assert command of {_b} is not set with "testing for possible future conflicts (block)"
assert command of {_e} is not set with "testing for possible future conflicts (entity)"
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved

set block at {_b} to stone
assert command block command of {_b} is not set with "the command of a non-command block should return null"
assert command block command of {_z} is not set with "the command of a non-minecart with command block should return null"

set command block command of {_b} to "say asdf"
set command block command of {_z} to "say asdf"
assert command block command of {_b} is not set with "setting the command of a non-command block shouldn't do anything"
assert command block command of {_z} is not set with "setting the command of a non-minecart with command block shouldn't do anything"
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

delete entity within {_e}
delete entity within {_z}
Loading