|
| 1 | +package com.github.quiltservertools.ledger.mixin.blocks; |
| 2 | + |
| 3 | +import com.github.quiltservertools.ledger.callbacks.ItemInsertCallback; |
| 4 | +import com.github.quiltservertools.ledger.callbacks.ItemRemoveCallback; |
| 5 | +import com.github.quiltservertools.ledger.utility.Sources; |
| 6 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
| 7 | +import net.minecraft.core.BlockPos; |
| 8 | +import net.minecraft.server.level.ServerLevel; |
| 9 | +import net.minecraft.world.entity.player.Player; |
| 10 | +import net.minecraft.world.item.ItemStack; |
| 11 | +import net.minecraft.world.level.Level; |
| 12 | +import net.minecraft.world.level.block.ChiseledBookShelfBlock; |
| 13 | +import net.minecraft.world.level.block.entity.ChiseledBookShelfBlockEntity; |
| 14 | +import org.spongepowered.asm.mixin.Mixin; |
| 15 | +import org.spongepowered.asm.mixin.injection.At; |
| 16 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 17 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 18 | + |
| 19 | +@Mixin(ChiseledBookShelfBlock.class) |
| 20 | +public class ChiseledBookShelfBlockMixin { |
| 21 | + |
| 22 | + /** |
| 23 | + * Fires ItemInsertCallback after a book is successfully placed into a chiseled bookshelf slot. |
| 24 | + * Hooks into addBook(), which only runs server-side, after setItem() stores the book. |
| 25 | + */ |
| 26 | + @Inject( |
| 27 | + method = "addBook", |
| 28 | + at = @At( |
| 29 | + value = "INVOKE", |
| 30 | + target = "Lnet/minecraft/world/level/block/entity/ChiseledBookShelfBlockEntity;setItem(ILnet/minecraft/world/item/ItemStack;)V", |
| 31 | + shift = At.Shift.AFTER |
| 32 | + ) |
| 33 | + ) |
| 34 | + private static void onAddBook( |
| 35 | + Level level, |
| 36 | + BlockPos pos, |
| 37 | + Player player, |
| 38 | + ChiseledBookShelfBlockEntity bookshelfBlock, |
| 39 | + ItemStack itemStack, |
| 40 | + int slot, |
| 41 | + CallbackInfo ci |
| 42 | + ) { |
| 43 | + ItemStack insertedBook = bookshelfBlock.getItem(slot); |
| 44 | + if (!insertedBook.isEmpty() && level instanceof ServerLevel serverLevel) { |
| 45 | + ItemInsertCallback.EVENT.invoker().insert(insertedBook, pos, serverLevel, Sources.PLAYER, player); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Fires ItemRemoveCallback when a book is removed from a chiseled bookshelf slot. |
| 51 | + * Hooks into removeBook() to capture the removed ItemStack returned by removeItem(). |
| 52 | + */ |
| 53 | + @ModifyExpressionValue( |
| 54 | + method = "removeBook", |
| 55 | + at = @At( |
| 56 | + value = "INVOKE", |
| 57 | + target = "Lnet/minecraft/world/level/block/entity/ChiseledBookShelfBlockEntity;removeItem(II)Lnet/minecraft/world/item/ItemStack;" |
| 58 | + ) |
| 59 | + ) |
| 60 | + private static ItemStack onRemoveBook( |
| 61 | + ItemStack removedStack, |
| 62 | + Level level, |
| 63 | + BlockPos pos, |
| 64 | + Player player, |
| 65 | + ChiseledBookShelfBlockEntity bookshelfBlock, |
| 66 | + int slot |
| 67 | + ) { |
| 68 | + if (!removedStack.isEmpty() && level instanceof ServerLevel serverLevel) { |
| 69 | + ItemRemoveCallback.EVENT.invoker().remove(removedStack, pos, serverLevel, Sources.PLAYER, player); |
| 70 | + } |
| 71 | + return removedStack; |
| 72 | + } |
| 73 | +} |
0 commit comments