Jump to content

[1.16.5] Changing the use duration of an item


Skelyvelocirap

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...).

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ECWA School of Nursing, Egbe 2024/2025 Admission Form[07043240159] IS STILL ON SALE.Call 07043240159 for more details on how to apply and register online before the deadline The Management of the school hereby inform the general public on the sales of the general Nursing Admission form into the School of Nursing.And also the sales of two (2) years Post Basic Nursing Programme. contact the school Admin. Dr MRS FAITH OKOYE on 07043240159 for more details on how to purchase the form and register online
    • The game crashed whilst exception ticking world Error: java.lang.NullPointerException: Cannot invoke "net.minecraft.resources.ResourceLocation.equals(Object)" because "this.lootTableId" is null Error given is above. I was already playing for around 15 minutes and wasn't doing anything specific or even breaking anything when the crashed happened. This is update 1.19.2 forge: 43.2.23 Mod list: ESSENTIAL Mod (by SparkUniverse_) Traveler's Titles (Forge) (by YUNGNICKYOUNG) Resourceful Config (by ThatGravyBoat) Dynamic Lights (by atomicstrykergrumpy) TenzinLib (by CommodoreThrawn) Nature's Compass (by Chaosyr) Library Ferret - Forge (by jtl_elisa) Cold Sweat (by Mikul) Simple Voice Chat (by henkelmax) Waystones (by BlayTheNinth) Carry On (by Tschipp) [Let's Do] Meadow (by satisfy) Creeper Overhaul (by joosh_7889) AutoRegLib (by Vazkii) Moonlight Lib (by MehVahdJukaar) AppleSkin (by squeek502) Xaero's World Map (by xaero96) Rotten Creatures (by fusionstudiomc) YUNG's API (Forge) (by YUNGNICKYOUNG) Village Artifacts (by Lothrazar) Right Click, Get Crops (by TeamCoFH) Supplementaries (by MehVahdJukaar) Automatic Tool Swap (by MelanX) Better Third Person (by Socolio) Supplementaries Squared (by plantspookable) Traveler's Backpack (by Tiviacz1337) Caelus API (Forge/NeoForge) (by TheIllusiveC4) Creatures and Beasts (by joosh_7889) Architectury API (Fabric/Forge/NeoForge) (by shedaniel) Quark Oddities (by Vazkii) Origins (Forge) (by EdwinMindcraft) Villager Names (by Serilum) GeckoLib (by Gecko) Realistic Bees (by Serilum) Illuminations Forge 🔥 (by dimadencep) Serene Seasons (by TheAdubbz) Critters and Companions (by joosh_7889) [Let's Do] Bakery (by satisfy) Falling Leaves (Forge) (by Cheaterpaul) Jade 🔍 (by Snownee) Collective (by Serilum) TerraBlender (Forge) (by TheAdubbz) [Let's Do] API (by Cristelknight) Towns and Towers (by Biban_Auriu) More Villagers (by SameDifferent) Biomes O' Plenty (by Forstride) Goblin Traders (by MrCrayfish) Corpse (by henkelmax) Tree Harvester (by Serilum) Balm (Forge Edition) (by BlayTheNinth) Mouse Tweaks (by YaLTeR) Sound Physics Remastered (by henkelmax) Xaero's Minimap (by xaero96) Just Enough Items (JEI) (by mezz) Terralith (by Starmute) Quark (by Vazkii) [Let's Do] Vinery (by satisfy) [Let's Do] Candlelight (by satisfy) Repurposed Structures (Neoforge/Forge) (by telepathicgrunt) Dusty Decorations (by flint_mischiff) Immersive Armors [Fabric/Forge] (by Conczin) Serene Seasons Fix (by Or_OS) Shutup Experimental Settings! (by Corgi_Taco) Skin Layers 3D (Fabric/Forge) (by tr7zw)
    • Make sure Java is running via Nvidia GPU https://windowsreport.com/minecraft-not-using-gpu/  
    • Also make a test with other Custom Launchers like AT Launcher, MultiMC or Technic Launcher
    • Embeddium and valkyrienskies are not working together - remove one of these mods With removing Embeddium, also remove Oculus, TexTrue's Embeddium Options and Embeddium Extras Or use Rubidium instead
  • Topics

×
×
  • Create New...

Important Information

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