2121import ch .njol .yggdrasil .Fields ;
2222import ch .njol .yggdrasil .Fields .FieldContext ;
2323import ch .njol .yggdrasil .YggdrasilSerializable .YggdrasilExtendedSerializable ;
24- import org .bukkit .Location ;
25- import org .bukkit .Material ;
26- import org .bukkit .OfflinePlayer ;
27- import org .bukkit .Tag ;
28- import org .bukkit .Bukkit ;
24+ import org .bukkit .*;
2925import org .bukkit .block .Block ;
3026import org .bukkit .block .BlockState ;
3127import org .bukkit .block .Skull ;
3531import org .bukkit .inventory .Inventory ;
3632import org .bukkit .inventory .ItemStack ;
3733import org .bukkit .inventory .PlayerInventory ;
34+ import org .bukkit .inventory .meta .BlockStateMeta ;
3835import org .bukkit .inventory .meta .ItemMeta ;
3936import org .bukkit .inventory .meta .SkullMeta ;
4037import org .jetbrains .annotations .NotNull ;
4340import java .io .NotSerializableException ;
4441import java .io .StreamCorruptedException ;
4542import java .lang .reflect .Field ;
46- import java .util .ArrayList ;
47- import java .util .Arrays ;
48- import java .util .Collection ;
49- import java .util .Collections ;
50- import java .util .HashSet ;
51- import java .util .Iterator ;
52- import java .util .List ;
53- import java .util .Map ;
43+ import java .util .*;
5444import java .util .Map .Entry ;
55- import java .util .NoSuchElementException ;
56- import java .util .Random ;
57- import java .util .RandomAccess ;
58- import java .util .Set ;
5945import java .util .stream .Collectors ;
6046
6147@ ContainerType (ItemStack .class )
@@ -390,8 +376,8 @@ public boolean setBlock(Block block, boolean applyPhysics) {
390376
391377 ItemMeta itemMeta = getItemMeta ();
392378
393- if (itemMeta instanceof SkullMeta ) {
394- OfflinePlayer offlinePlayer = (( SkullMeta ) itemMeta ) .getOwningPlayer ();
379+ if (itemMeta instanceof SkullMeta skullMeta ) {
380+ OfflinePlayer offlinePlayer = skullMeta .getOwningPlayer ();
395381 if (offlinePlayer == null )
396382 continue ;
397383 Skull skull = (Skull ) block .getState ();
@@ -407,11 +393,49 @@ public boolean setBlock(Block block, boolean applyPhysics) {
407393 skull .update (false , applyPhysics );
408394 }
409395
396+ // https://github.com/SkriptLang/Skript/issues/7735
397+ // No method exists to copy general BlockStateMeta data to a block, so we have to do it manually for now
398+ copyContainerState (block , itemMeta );
399+
410400 return true ;
411401 }
412402 return false ;
413403 }
414404
405+ /**
406+ * Copies the container state from the item meta to the block state
407+ * @param block The block to copy the state to
408+ * @param itemMeta The item meta to copy the state from
409+ */
410+ private void copyContainerState (@ NotNull Block block , @ NotNull ItemMeta itemMeta ) {
411+ // ensure the item has a block state
412+ if (!(itemMeta instanceof BlockStateMeta blockStateMeta ) || !blockStateMeta .hasBlockState ())
413+ return ;
414+
415+ // only care about container -> container copying
416+ if (!(blockStateMeta .getBlockState () instanceof org .bukkit .block .Container itemContainer )
417+ || !(block .getState () instanceof org .bukkit .block .Container blockContainer ))
418+ return ;
419+
420+ // copy inventory from item to block
421+ copyInventories (itemContainer .getSnapshotInventory (), blockContainer .getSnapshotInventory ());
422+ blockContainer .update ();
423+ }
424+
425+ /**
426+ * Copies the contents of one inventory to another, maintaining slot positions and making clones.
427+ * @param from The inventory to copy from
428+ * @param to The inventory to copy to
429+ */
430+ private void copyInventories (@ NotNull Inventory from , @ NotNull Inventory to ) {
431+ for (int i = 0 ; i < from .getSize (); i ++) {
432+ ItemStack item = from .getItem (i );
433+ if (item != null ) {
434+ to .setItem (i , item .clone ());
435+ }
436+ }
437+ }
438+
415439 /**
416440 * Send a block change to a player
417441 * <p>This will send a fake block change to the player, and will not change the block on the server.</p>
0 commit comments