Skip to content

Commit

Permalink
Add InventoryHolder -> Location converter (#6747)
Browse files Browse the repository at this point in the history
* add holder -> location converter

* clean up despite failure
------

Co-authored-by: Moderocky <admin@moderocky.com>
  • Loading branch information
sovdeeth and Moderocky authored Jun 1, 2024
1 parent 4f3ace9 commit cf80c77
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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"

0 comments on commit cf80c77

Please sign in to comment.