How to detect ANY block being broken in Minecraft forge for 1.16.5

Viewed 192

I am making a mod in 1.16.5 with Forge to drop an emerald once 50 blocks of any kind have been broken, and I cannot figure it out. I have tried BlockEvent.BreakEvent, onBlockHarvested, and harvestBlock, but I cannot figure it out. Another problem is I cannot give the player an item, I have tried several ways. Can anyone help?

My current code is:

package com.taxolotl.cubitemerald;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;;

@Mod("cubitemerald")
public class CubitEmerald {

    public static final Logger LOGGER = LogManager.getLogger();
    public static final String MOD_ID = "cubitemerald";
    public static int blok = 0;

    public CubitEmerald() {
        IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
        MinecraftForge.EVENT_BUS.register(this);

        if(/*block broken*/) {
            blok = blok + 1;
        }
        
        if(blok>=50) {
            /*drop an emerald*/
            blok = 0;
        }   
    }
}
1 Answers

You need to listen to the BlockEvent.BreakEvent like so:

@SubscribeEvent
public void blockBreakEvent(BlockEvent.BreakEvent e){
    blok += 1;
}

You will need to learn about Forge's event bus system, which you can do so from their brief but adequate documentation.

Of course, your blok variable will only exist during runtime, meaning, when the client restarts, the value will be gone. If you wish for the player to pick up where they left off, you will want to look into a way of storing persistent data.

Related