Jump to content

onItemRightClick method


julesmaster7

Recommended Posts

Hi there,

 

I was trying to make my item get all the entitys within a 10 block radius and take away 2 health + if it is a player play a sound.

 

All on right click.

 

Here is my right click method:

 

@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
	AxisAlignedBB bb = new AxisAlignedBB(10, 10, 10, 10, 10, 10);
	worldIn.getEntitiesWithinAABB(EntityLiving.class, bb);
	return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
}

 

I can't figure out how to hurt the entitys that it finds.

Link to comment
Share on other sites

That AABB is both 0 in size and centered near the origin of the world.

 

You should be using

new AxisAlignedBB(playerIn.pos.getX() - 10, playerIn.pos.getY() - 10, playerIn.pos.getZ() - 10, playerIn.pos.getX() + 10, playerIn.pos.getY() + 10, playerIn.pos.getZ() + 10)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I would suggest doing AxisAlignedBB bb = playerIn.getEntityBoundingBox().expand(20.0D, 20.0D, 20.0D) and for hurting them something like this:

List<EntityLivingBase> entitiesInAABB = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, bb);
		for(int i = 0; i < entitiesInAABB.size(); i++) {
			EntityLivingBase entityInAABB = entitiesInAABB.get(i);
			entityInAABB.attackEntityFrom(DamageSource.magic, 1.0F);
		}

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Thank you guys!

 

One extra thing: how do I set a cooldown for my onItemRightClick.

Instead of having the code for the effect in onItemRightClicked have it in onPlayerStoppedUsing (i believe it was that name may have changed). For an example of this look at ItemBow.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Thank you guys!

 

One extra thing: how do I set a cooldown for my onItemRightClick.

Instead of having the code for the effect in onItemRightClicked have it in onPlayerStoppedUsing (i believe it was that name may have changed). For an example of this look at ItemBow.

 

That method gets called after you release the right button. in the case of the bow it releases the arrow.

 

I want to know how to set a cooldown for onItemRightClick.

Link to comment
Share on other sites

Then you will have to use a custom capability or maybe an IProperty I personally have not worked with either of these, but im pretty sure you want to use the later.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Vanilla already has an item cooldown system:

CooldownTracker

. I suggest you look for usages of this in vanilla code, e.g.

ItemEnderPearl

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

It always amazes me that people ask questions about how to make cool-down timer type functionality. I think people forget that modding is just programming and anything you can do with Java you can probably do in your mod.

 

While items do have a built-in system for cool-down (as mentioned by Choonster) I feel compelled to point out that this is a very basic programming problem.

 

You simply need an int field that gets set to a value (since Minecraft works on 20 steps per second, it would probably be set to 20 * the number of seconds you want for cool-down) whenever the action starts and decrements by one in some method that runs every tick (like updateLiving()) until it reaches zero (then stays there). Then whenever you want to start the action you just check to make sure that field is at 0 and do it all over again.

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

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.