Jump to content

Block that hurts hostile mobs but not the player or passive mobs?


MikeZ

Recommended Posts

Hi!

I want to make a block, that if a hostile mob (zombie, creeper, ...) collide with it, they get damage.

And the player or passive mobs should'nt be hurt.

Is there any way to make this block?

 

Thanks,

Mike

Link to comment
Share on other sites

Override onEntityCollidedWithBlock (or whatever it's called) in your block class, check if the Entity colliding is an IMob, and if so, damage it.

 

Ok, thank you.

I changed my Block.class, but it has no effect.

 

Heres my Block.class:

 

 

package MikesMod.blocks;

 

import java.util.Random;

 

import MikesMod.MainClass;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.client.renderer.texture.IconRegister;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EntityLiving;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.world.World;

 

public class MikesOreBlock extends Block{

 

@SideOnly(Side.CLIENT)

private String texture;

 

public MikesOreBlock(int par1, Material par2Material, String texture) {

super(par1, par2Material);

this.texture = texture;

this.setCreativeTab(MainClass.tabMikesMod);

this.setHardness(6F);

this.setResistance(15F);

}

 

public int idDropped(int par1, Random par2Random, int par3) {

return MainClass.MikesOreBlock.blockID;

}

 

@SideOnly(Side.CLIENT)

public void registerIcons(IconRegister iconRegister) {

blockIcon = iconRegister.registerIcon("mike:" + texture);

}

 

 

@Override

public void onEntityCollidedWithBlock( World world, int x, int y, int z, Entity entity ) {

if (entity instanceof EntityLiving) {

((EntityLiving)entity).addPotionEffect(new PotionEffect(Potion.poison.id, 200, 1));

}

}

}

 

 

 

Thanks,

Mike

Link to comment
Share on other sites

 

Nevermind, don't think that is it. 

 

How abuot you try to just damage the entity instead of the potion and see if you get a different result. 

 

Might also put a logger in there to make sure it is actaully getting called.

 

 

Looked up one that worked for me.

 

 

 

 

player.addPotionEffect(new PotionEffect(Potion.confusion.getId(), 20 * 5, 1));

 

 

 

 

 

vs

 

 

 

((EntityLiving)entity).addPotionEffect(new PotionEffect(Potion.poison.id, 200, 1));

 

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Override onEntityCollidedWithBlock (or whatever it's called) in your block class, check if the Entity colliding is an IMob, and if so, damage it.

 

I actually think that onEntityCollidedWithBlock() method doesn't work for the idea of an entity "bumping" into a block.  Rather it is intended for those types of blocks that an entity can actually "enter" into.  Like a portal or a web.  And arrows are allowed to "enter" blocks to trigger this method.

 

If you think about it, most entities can't "collide" with a regular block because the path finding won't let that happen -- the movement is prevented before an actual collision occurs.

 

It is a poorly named method, but from some other debugging I've done recently I came to the conclusion that you have to be inside the block to trigger this method.

 

I could be wrong, but that was something I thought I previously found to be true.

 

Anyway, it is easy enough to prove.  Just put a System.out.println() statement in the method to see whether it is ever being called.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I've got some protection blocks setup.  If you want this to be a very common block, it could be a bad idea, but assuming it is not a world populate block, just create a Tileentity.

 

Then set it up to search every so many ticks for entities around it that meet your criteria, and do whatever. 

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.