Jump to content

Recommended Posts

Posted (edited)

My item gives me a  random item whenever you right click. I tried to utilize the use() method. My issue is that I want to have the user press down the right click button for some time(5 seconds maybe??) and only once that is done should it give the item and repeat if its still held. I tried to use the getUseDuration() method but that doesn't work. I tried to take a look at how its done on some other items(like food items) but i can't figure it out. Can someone help be with this?

Edited by Skelyvelocirap
Posted
3 hours ago, Skelyvelocirap said:

Can someone help be with this?

Within Item#use, you should call PlayerEntity#startUsingItem. This will start using the item when the item is used.

You can set the maximum number of ticks that the item can be used for with the return value of Item#getUseDuration.

While the item is being used, Item#onUseTick will be called every tick, and its integer parameter is the number of ticks left in the use duration. This is probably where you want to put your logic, since you mentioned that the player should be able to keep holding the item to keep the process going.

At some point, the player will release the item, which calls Item#releaseUsing. Its integer parameter is the number of ticks that the item was used for, so you could also put some logic there.

  • Like 1

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted (edited)

So that's how those work! Alright, thank you! Its now working perfectly! Although one last question. Item#use() needs you to return an Action result. I never really understood those and mostly just copied what others did. My guess is that its what is responsible for the hand movement when you right click(am I correct?). If it is, how exactly would I fix it so that it only does that once Item#onUseTick's tick parameter is 0?



EDIT: Alright so, I'm now using Item#finishUsingItem as there is no need to run my logic every tick. Hopefully this will reduce the already non-existent lag.

Edited by Skelyvelocirap
different method used
Posted
30 minutes ago, Skelyvelocirap said:

My guess is that its what is responsible for the hand movement when you right click(am I correct?). If it is, how exactly would I fix it so that it only does that once Item#onUseTick's tick parameter is 0?

It is responsible for determining how the hand moves, but it also does a bit more.

Each ActionResult<T> has an ActionResultType and an object T. In the case of the ActionResult<ItemStack>, the ItemStack object of the returned ActionResult<ItemStack> is set as the held item.

Additionally, these ActionResultTypes dictate what happens after your Item's logic has been run. They are queried with the ActionResultType#shouldSwing and ActionResultType#consumesAction methods. The former is self-explanitory, while the latter dictates whether or not the click will be further processed.

For example, ActionResultType.SUCCESS both consumes the action and swings. This means that if you held an ender pearl in your offhand, and clicked with an item that returns a successful ActionResult<ItemStack>, the main item would swing, and no ender pearl would be thrown. ActionResultType.PASS doesn't swing or consume, so returning that would result in no animation of the main item, and an unblocked, thrown ender pearl. ActionResultType.CONSUME consumes the action, but doesn't swing, so that's probably what you want here. ActionResultType.FAIL behaves as PASS, but is used more in the context of Block placement, I think, and as a message to prevent a behaviour.

For your purposes, I would probably recommend returning CONSUME unless LIvingEntity#getUseItemRemainingTicks (this is the value passed into Item#onUseTick) fits your requirements (you could also use LivingEntity#getTicksUsingItem), in which case you would return SUCCESS.

It might also be helpful to return true from Item#useOnRelease, so that your item is used when released, in addition to initially.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

Alright. So ActionResult basically just tells Minecraft whether or not to swing the hand, along with whether or not it should process the item further(so inhibiting other items from doing their thing instead). And so I would simply check(in Item#use) if LivingEntity#getUseItemRemainingTicks is greater than 0, if it is than return Consume, or else success?

For the Item#useOnRelease, i dont think i will do that as what im trying to do is wait until the 5 seconds have passed. If i return true then the user could just spam right click and get it over with.

Posted
4 minutes ago, Skelyvelocirap said:

Alright. So ActionResult basically just tells Minecraft whether or not to swing the hand, along with whether or not it should process the item further(so inhibiting other items from doing their thing instead).

Yup, that's about right.

4 minutes ago, Skelyvelocirap said:

And so I would simply check(in Item#use) if LivingEntity#getUseItemRemainingTicks is greater than 0, if it is than return Consume, or else success?

The exact details of when you should do what depend on how you want it to work and look. Ultimately, that's up to you, so you should try to set something up using these tools, experiment a bit, and figure out what produces the best result. It seems like you mostly understand what's going on here, so that ought to go fairly well.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

Oops, i didn't submit it.

Alright!

Ok, well it seems to work exactly how i want it to currently! Although, for some reason it still swings the players arms even though im returning ActionResult#consume(stack). Am i supposed to override that somewhere else as well?

Also, this isn't 100% related but what would I need to override to add an animation to the item? I don't mean how do I make an animation(don't quite know how but i should be able to find it on google), rather how do I make it play when the item is held down. I now know that for the items it would be the Item#onUseTick. Is there a similar method for the animation?

Posted
22 minutes ago, Skelyvelocirap said:

Ok, well it seems to work exactly how i want it to currently! Although, for some reason it still swings the players arms even though im returning ActionResult#consume(stack). Am i supposed to override that somewhere else as well?

Please post your code, so that I can see what might be causing this (I won't be able to respond until tomorrow, though, since I need to sleep now).

22 minutes ago, Skelyvelocirap said:

Also, this isn't 100% related but what would I need to override to add an animation to the item? I don't mean how do I make an animation(don't quite know how but i should be able to find it on google), rather how do I make it play when the item is held down. I now know that for the items it would be the Item#onUseTick. Is there a similar method for the animation?

I've never done this before, so my knowledge is limited. Here is a link to the documentation on the topic, though, which seems fairly straightforward.

  • Like 1

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

This is your problem.

The boolean parameter of ActionResult<T>#sidedSuccess is whether or not the game is running on the logical client, and so returns success on true, or consume on false. Your condition will usually be true, so a success is usually returned, which causes the item to swing. I don't think it's necessary to use sidedSuccess for this, but if you do, you should pass in World.isClientSide, or an equivalent. Regardless, the condition should be updated.

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

Oops, I forgot I switched to ActionResult#sidedSuccess. And thank you for noticing it was backwards. Although this isn't really the problem. My item is ALWAYS doing that, even if the condition is false(the if statements before it). When the player is not in the correct liquids it should return ActionResult#consume(which it does) but it still swings the players arms. I know this for sure as you might notice that its logging the item in the pan(which i forgot to remove...).

Posted

I've done some testing, and there are 3 things that cause the item to move.

The first is when you return a success, the item will swing. Updating the returns as previously discussed achieves the desired result in this context.

The second is when you return a consume, the item will bob if it is not in use. This means that if you try to use the pan while not able to do any panning, it will bob repeatedly (but not jarringly quickly). If you don't want this, you can return a pass, which has no visual effect.

The third is when you set the damage value every tick. This is what causes the glitchy-looking vibration, and it would seem you can fix it by setting the damage less frequently (once every 3 ticks eliminated all unwanted movement in my experiments).

  • Like 1

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Posted

Sweet, it works perfectly now! Im now returning pass instead of consume and updating the progress every 3 ticks. Thank you very much for your help, now my item works exactly like i want it to! Thank you again!

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.