Iceo
Iceo

Reputation: 1

How do I make make a block summon an explosion using onEntityCollidedWithBlock? Minecraft Modding

I am trying to make my block summon an explosion when a player or mob collides with it. So far, I can make it damage them, but not trigger an explosion. Does anyone know how?

Here is my code:

package com.icearrow26.moreapples.blocks;

import com.icearrow26.moreapples.Main;
import com.icearrow26.moreapples.init.ModBlocks;
import com.icearrow26.moreapples.init.ModItems;
import com.icearrow26.moreapples.util.Interfaces.IHasModel;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class TNTBase extends Block implements IHasModel{
    public TNTBase(String name, Material material)
    {
        super(material);
        setUnlocalizedName(name);
        setRegistryName(name);
        setCreativeTab(CreativeTabs.BUILDING_BLOCKS);

        ModBlocks.BLOCKS.add(this);
        ModItems.ITEMS.add(new ItemBlock(this).setRegistryName(this.getRegistryName()));
    }

    @Override
    public void registerModels() {
        Main.proxy.registerItemRenderer(Item.getItemFromBlock(this), 0, "inventory");

    }
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
    {
        entityIn.attackEntityFrom(DamageSource.IN_WALL, 1.0F);
    }
}

Upvotes: 0

Views: 3259

Answers (1)

Step 1: Always ask yourself "does vanilla do something like this?"

In this case, yes, Vanilla creates explosions. Not when an entity collides with a block, but that's just a when it does it, not how it does it. In this case, EntityTNTPrimed is the class we want.

In it we find this method:

private void explode()
{
    float f = 4.0F;
    this.world.createExplosion(this, this.posX, this.posY + (double)(this.height / 16.0F), this.posZ, 4.0F, true);
}

Voila. Now we know how to make explosions. Copy, paste, invoke when we want it to happen. No step 2 needed.

Now, some other things...

These are common issues I see all the time and should also be corrected.

  1. IHasModel is stupid. All items need models and all of the information necessary to register a model is public. Using interfaces like this is an anti-pattern and you picked it from a Youtube tutorial making this a Cargo Cult Programming practice. You do not need it at all ever. Just call ModelLoader.registerCustomModel() directly in your ModelRegistryEvent handler
    • "I need to go through my proxy!" you say. Your ModelRegistryEvent handler should already be in your client proxy. The ModelRegistryEvent class is client-only.
    • I can probably even tell you what YT video you watched and link you directly to my comment telling its author that IHasModel is stupid. If I'm wrong I'd go and make a comment.
  2. TNTBase tells me you also have a BlockBase class somewhere that most of your blocks extend (or this is that class). This is another antipattern: using inheritance for code reuse.
  3. You do not own icearrow26.com, so you should not be using that as your package identifier (Minecraft uses net.minecraft because they own Minecraft.net!) You should use mod instead of com or omit it all together.
  4. You almost certainly have a CommonProxy class. This is stupid. Common code can go in your main mod class. The whole point of the proxy system is to distinguish client from server and allow code that can only be called on one side to be called on the one side without crashing the other with unknown references. Common code can go anywhere, it does not need a proxy.

More information

Upvotes: 3

Related Questions