Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 51 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprExactItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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.Material;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.jetbrains.annotations.Nullable;

@Name("Exact Item")
@Description(
"Get an exact item representation of a block, carrying over any data. "
+ "For example, using this expression on a chest block with items stored inside will return a chest "
+ "item with the exact same items in its inventory as the chest block."
)
@Examples("set {_item} to exact item of block at location(0, 0, 0)")
@Since("INSERT VERSION")
public class ExprExactItem extends SimplePropertyExpression<Block, ItemStack> {

static {
register(ExprExactItem.class, ItemStack.class, "exact item[s]", "blocks");
}

@Override
public @Nullable ItemStack convert(Block block) {
Material blockMaterial = block.getType();
if (!blockMaterial.isItem())
return null;
ItemStack itemStack = new ItemStack(blockMaterial);
if (itemStack.getItemMeta() instanceof BlockStateMeta blockStateMeta) {
blockStateMeta.setBlockState(block.getState());
itemStack.setItemMeta(blockStateMeta);
}
return itemStack;
}

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

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

}
7 changes: 7 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprExactItem.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test "exact item chest":
set {_old} to block data of test-block
set test-block to a chest
add a diamond to inventory of test-block
set {_item} to exact item of test-block
assert items in inventory of {_item} is a diamond with "BlockState data of block should carry over to exact item"
set block data of test-block to {_old}