The Item class has a method Item#hitEntity . So basically you can just Override this in your Item Class and check if the hit entity is an instance of the Entity you want to target and create and explosion there. You can create explosions with worldObj#createExplosion, but don't forget that explosions are only server side, so you have to execute it on the server only by checking if the world is not remote. ( !worldObj.isRemote )
Also, before doing this check if the mobGriefing gamerule is even enabled, so the explosion doesn't damage blocks if it is disabled in that specific world.
Looking into the Class of the Creeper Entity (EntityCreeper.class) your result should look something like this:
/* We're using @Override so in case we want to update our code and this method changes it gives us an error
* @see net.minecraft.item.ItemSword#hitEntity(net.minecraft.item.ItemStack, net.minecraft.entity.EntityLivingBase, net.minecraft.entity.EntityLivingBase)
*/
@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
// this checks if mobGriefing is enabled in the world the target is in
boolean flag = target.worldObj.getGameRules().getGameRuleBooleanValue("mobGriefing");
// checks if the called side is the server and if the target is an instance of a specific entity type
if (!target.worldObj.isRemote && target instanceof EntityCreeper) {
// creates the explosion
/*
* params:
* Entity - put the Entity here
* xPos - the x-Coordinate
* yPos - the y-Coordinate
* zPos - the z-Coordinate
* strength - the strength of the explosion (as a float)
* boolean - if the explosion should destroy blocks
*/
target.worldObj.createExplosion(target, target.posX, target.posY, target.posZ, 5.0F, flag);
}
// call the super method just for completion sake
super.hitEntity(stack, target, attacker);
return false;
}
Also I can recommend you in the future to just browse through the original source code to find the things you want before looking here. If you're using eclipse just write the name of the class (e.g. EntityCreeper) into any method and ctrl + left click on it to jump to the super implementation.
im trying to make the entity explode when attacked with the item and i cant figure out a way to do this
Here're is probably the easiest way to do this:
The Item class has a method Item#hitEntity . So basically you can just Override this in your Item Class and check if the hit entity is an instance of the Entity you want to target and create and explosion there. You can create explosions with worldObj#createExplosion, but don't forget that explosions are only server side, so you have to execute it on the server only by checking if the world is not remote. ( !worldObj.isRemote )
Also, before doing this check if the mobGriefing gamerule is even enabled, so the explosion doesn't damage blocks if it is disabled in that specific world.
Looking into the Class of the Creeper Entity (EntityCreeper.class) your result should look something like this:
Also I can recommend you in the future to just browse through the original source code to find the things you want before looking here. If you're using eclipse just write the name of the class (e.g. EntityCreeper) into any method and ctrl + left click on it to jump to the super implementation.
Hope I was able to help you