Jump to content

How to do something while an specific item is being use


PAstBand

Recommended Posts

I want to perform an action each tick the player is holding right click with a custom item in their hand. I tried to use the even "onUsingTick" like this:

@Override

public void onUsingTick(ItemStack BasicWandItem, LivingEntity player, int count) {

     player.teleportTo((player.position().x+1),(player.position().y+1),(player.position().z+1));

     super.onUsingTick(BasicWandItem, player, count);

}

 

but it doesn´t work, any suggestions?

thx 

Edited by PAstBand
Link to comment
Share on other sites

Item#onUsingTick is only called if the Item is in use, therefore you need to call LivingEntity#startUsingItem in Item#use.
and the return value of Item#getUseDuration is larger than 0.

In addition teleport the Player is a server side operation and you should use Entity#teleportToWithTicket.

Link to comment
Share on other sites

29 minutes ago, Luis_ST said:

Item#onUsingTick is only called if the Item is in use, therefore you need to call LivingEntity#startUsingItem in Item#use.
and the return value of Item#getUseDuration is larger than 0.

In addition teleport the Player is a server side operation and you should use Entity#teleportToWithTicket.

Im sorry to waste your time, but im able to make it works how i want. What i want is, while the player holds right click do something (for example writin 1 in the chat), and when he relese the button do something else once (for example writing 2 in the chat). I tried with this code:

 

@Override

public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {

     pPlayer.startUsingItem(pUsedHand);

     return super.use(pLevel, pPlayer, pUsedHand);

}

@Override

public void onUsingTick(ItemStack BasicWandItem, LivingEntity player, int count) {

     player.sendMessage(new TranslatableComponent("1"),player.getUUID());

     super.onUsingTick(BasicWandItem, player, count);

}

@Override

public void releaseUsing(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity, int pTimeCharged) {

     pLivingEntity.stopUsingItem();

     pLivingEntity.sendMessage(new TranslatableComponent("2"),pLivingEntity.getUUID());

     super.releaseUsing(pStack, pLevel, pLivingEntity, pTimeCharged);

}

Link to comment
Share on other sites

The idea is, each tick the item is being use it should do something (Im printing number to test before i can do something better), When the player is no longer using the item it should do other thing just once. Basically

*Right click holded with item in hand*

*Each tick the chat should print 1*

*The player stop holding right click*

*the chat should print 2 just once*

if the player hold click for 5 ticks the chat should like this:

1

1

1

1

1

2

 

This is the item class

public class BasicWandItem extends Item {

     public BasicWandItem(Properties pProperties) { super(pProperties); }

     

@Override

public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {

     pPlayer.startUsingItem(pUsedHand);

     return super.use(pLevel, pPlayer, pUsedHand);

}

@Override

public void onUsingTick(ItemStack BasicWandItem, LivingEntity player, int count) {

     player.sendMessage(new TranslatableComponent("1"),player.getUUID());

     super.onUsingTick(BasicWandItem, player, count);

}

@Override

public void releaseUsing(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity, int pTimeCharged) {

     pLivingEntity.stopUsingItem();

     pLivingEntity.sendMessage(new TranslatableComponent("2"),pLivingEntity.getUUID());

     super.releaseUsing(pStack, pLevel, pLivingEntity, pTimeCharged);

}

     }

}

 

thx ❤️

Link to comment
Share on other sites

13 hours ago, Luis_ST said:

and the return value of Item#getUseDuration is larger than 0

The default value is 0, you need to override the method and return a value which is larger than 0.
Note this is the max time (in ticks) a Entity can use this Item.

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

    • Whenever I attempt to join my friend's MC server (hosted through Bisect Hosting) my game crashes, when it crashes I get an error report that says it may be because I was not allocating enough RAM, despite having allocated (at different times) 4G, 6G, 8G, 9G, 10G, 14G, 16G, and 20G all of which my game still crashed. My laptop has 32 GB of RAM so I am hesitant to go above 20, and I feel as though it shouldn't need it.  To elaborate more on the crashes, I am able to join the world, but when the "select origin" screen pops up whenever I click the screen anywhere the game goes white, my curser pinwheels and I get the "Minecraft has stopped responding" message. I did eventually try beating it with sheer determination and godlike patience by hitting "wait for the program to respond" and sat at my laptop for 30 minutes at a time until my screen unfroze and it would progress to the next option, but once I got to "select class" it froze for close to an hour so I gave up. Also to clarify, I do not have ANY issues on single-player worlds, in fact, it runs incredibly smoothly, on high settings and a render distance of about 15 chunks I was still getting 30-40 fps (which I am fine with), and on said worlds, I never have any issues or any lag on selecting origin, blessing nor class. This issue only occurs on THIS specific mod pack (to reiterate Cisco's Fantasy Mideaval RPG V4f), and ONLY on multiplayer. I know this for certain because I have another server with some other friends running Enigmatica 9 (the easy version) and I have had no issues. What I have tried to far to remedy the issue: By digging through old Reddit posts, quora responses, and Minecraft forum inquiries, I found the following "solutions" that I attempted and all failed: Uninstalling the mod pack and reinstalling it, updating graphics drivers, updating windows, updating my laptop's stuff (not really sure what to call it, I have a ThinkPad so its though the Lenovo application), I did a Window's defender scan, I tried adding -d64 in JVM arguments, I tried rebooting my laptop numerous times, I've done all of the previous before and after switching it off of power conservation mode (before that I had already put javaw on high-performance settings). The only things I could think of that I have not tried solely because they would be a pain in my rear would be reinstalling java (but whenever that is suggested in forums it seems its for versions before 1.15?) and connecting my laptop to ethernet (since my laptop doesn't have an ethernet port this would be a pain). I also don't think ethernet would solve it as even on WiFi my ping is 31, download is approximately 200 Mbps, and upload is 23 Mbps. If you have any suggestions I am all ears, at this point I feel like I've exhausted all options I can think of and was going to resign myself to just not being able to play on the server but my friend convinced me to make this post just in case someone had an idea.
    • I am new to this and am trying to increase the RAM allocated on my server.  Right now it is only using 2 GB, but I saw people put "java -Xmx4096M -Xms4096M -jar server.jar nogui" in the run.bat file, and I'm not sure where to add it as my run.bat has the following in it.    
    • it works, but before the world opens, it opens a screen listing the now-missing items from the missing mod, and asks me if I'm sure I want to continue. Is there any way for the world to work with the mods, as removing them would lose blocks/items in the world, from the affected mods? (Maybe certain versions/instances/etc of the mods/launcher/etc are compatible??)
    • added a few more and it re-broke lol. Any help with this crash report? last time promise haha https://gist.github.com/it-is-allie/e7297a2485d05ce58c6303b3751a36f2 
    • So I actually managed to fix this issue. I have absolutely no idea why it suddenly started showing changes for me other than changing the createParticle method to create an instance of the particle before returning it. 
  • Topics

×
×
  • Create New...

Important Information

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