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

Add InventoryHolder -> Location converter #6747

Merged
merged 2 commits into from
Jun 1, 2024
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
15 changes: 14 additions & 1 deletion src/main/java/ch/njol/skript/classes/data/DefaultConverters.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public DefaultConverters() {}
return (InventoryHolder) s;
return null;
}, Converter.NO_RIGHT_CHAINING | Commands.CONVERTER_NO_COMMAND_ARGUMENTS);

Converters.registerConverter(InventoryHolder.class, Block.class, holder -> {
if (holder instanceof BlockState)
return new BlockInventoryHolder((BlockState) holder);
Expand All @@ -165,7 +166,19 @@ public DefaultConverters() {}
return (Entity) holder;
return null;
}, Converter.NO_CHAINING);


// InventoryHolder - Location
// since the individual ones can't be trusted to chain.
Converters.registerConverter(InventoryHolder.class, Location.class, holder -> {
if (holder instanceof Entity)
return ((Entity) holder).getLocation();
if (holder instanceof Block)
return ((Block) holder).getLocation();
if (holder instanceof BlockState)
return BlockUtils.getLocation(((BlockState) holder).getBlock());
return null;
});

// Enchantment - EnchantmentType
Converters.registerConverter(Enchantment.class, EnchantmentType.class, e -> new EnchantmentType(e, -1));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test "inventory holder location":
set {_b} to the block at spawn of world "world"
set {_prev} to type of block at {_b}
broadcast {_prev}

set block at {_b} to a chest
set {_inv} to inventory of {_b}
set {_holder} to holder of {_inv}

set {_a-loc} to location of {_holder}
set {_b-loc} to location of {_b}

# clean up first in case assert fails
set block at {_b} to {_prev}

assert {_a-loc} is {_b-loc} with "holder location differs from block location"