#newbie here, how can I "execute" a mixin on block use?

9 messages · Page 1 of 1 (latest)

tranquil fractal
#

currently I have something like this

package me.jestem.mixin;

import net.minecraft.core.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;

@Mixin(MinecraftServer.class)
public class TrappedChestBlockMixin {
    @Inject(method = "onUse", at = @At("HEAD"))
    private void onOpen(BlockState state, BlockPos pos, Level level, Player player) {
        // This code is injected into the start of MinecraftServer.loadLevel()V
    }
}

but the onUse doesn't exist, or am I doing something wrong?

worthy schooner
#

There's an event for block use, UseBlockCallback

small wind
#

Well you're also targeting MinecraftServer.class

#

In a mixin called TrappedChestBlockMixin, so I don't assume you intended to target MinecraftServer

tranquil fractal
tranquil fractal
#

oh, and another thing - How can I get the items in the chest?

@Override
    public void onInitialize() {

        UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
            BlockPos pos = hitResult.getBlockPos();
            if (world.getBlockState(pos).getBlock() instanceof TrappedChestBlock trappedChestBlock) {
                if(trappedChestBlock.???){
                    world.explode(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
                            4.0f, Level.ExplosionInteraction.TNT);
                    return InteractionResult.SUCCESS;
                }
            }
            return InteractionResult.PASS;
        });

        LOGGER.info("Hello Fabric world!");
    }

I tried looking at TrappedChestBlock.class but didn't find anything, same with ChestBlock.class and only getItems() in ChestBlockEntity.class but I have no idea what to do

worthy schooner
#

You need to get the block entity from the level at the same position

tranquil fractal
#

I'm pretty new to modding and java in general, how do I do this?