Jump to content

[UNSOLVED] [1.12.2] Making onItemRightClick(...) fire every tick?


Recommended Posts

Posted (edited)

Hey,

 

I'm trying to make the method onItemRightClick(...) in Item run every tick for a certain item of mine instead of every 4 ticks. Is there any way (or alternative way) I could do this?

 

Any help is appreciated. :)

Thanks!

Edited by Differentiation
Posted (edited)
  On 10/7/2019 at 9:09 PM, Differentiation said:

Hey,

 

I'm trying to make the method onItemRightClick(...) in Item run every tick for a certain item of mine instead of every 4 ticks. Is there any way (or alternative way) I could do this?

 

Any help is appreciated. :)

Thanks!

Expand  

Have a look at this thread.

Diesieben07 has explained clearly.

https://www.minecraftforge.net/forum/topic/76564-1122-onitemrightclick

Edited by poopoodice
Posted
  On 10/7/2019 at 9:43 PM, poopoodice said:

Have a look at this thread.

Diesieben07 has explained clearly.

https://www.minecraftforge.net/forum/topic/76564-1122-onitemrightclick

Expand  

Thanks for the response. Unfortunately, since only the server thread runs on onUsingTick() method (and since this method never even fires every tick I right-click for some reason), I'll do just fine with the onItemRightClick().

Posted
  On 10/8/2019 at 2:40 AM, Differentiation said:

Thanks for the response. Unfortunately, since only the server thread runs on onUsingTick() method (and since this method never even fires every tick I right-click for some reason), I'll do just fine with the onItemRightClick().

Expand  

Actually, I use Mouse.isButtonDown(1) to represent right-click in the onUpdate() method. It works well for me but there might be some problems that I haven't notice.

Posted
  On 10/8/2019 at 2:53 AM, poopoodice said:

It works well for me but there might be some problems that I haven't notice.

Expand  

You're reaching across logical sides and it wont work in multiplayer.

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.

Posted (edited)

Is there a reason why onUsingTick() is never calling? :S

 

Also, is there a method for both server and client threads that runs every tick when the player right clicks without having to send packets?

 

Thanks!

Edited by Differentiation
Posted
  On 10/8/2019 at 12:03 PM, diesieben07 said:

Show your code.

 

onUsingTick is that method, at least for your own items. Is that not what you need?

Expand  

I'll send it when I get home.

 

I'm making a gun that fires rapidly (maybe every two ticks) so I need a method that runs on the server and client. The reason for the client is bc I'm making the player recoil every time they fire. 

Posted (edited)
  On 10/8/2019 at 12:46 PM, diesieben07 said:

No.

You have now stated "i need it on the client" twice. And both times I responded: Yes, onUsingTick does that.

Expand  

Well, I tested and world.isRemote returns false :S

 

Do I have to return a success for action result on both sides or something or is this method independent of onItemRightClick()?

Edited by Differentiation
Posted (edited)
  On 10/8/2019 at 1:46 PM, diesieben07 said:

Not sure what to say, looking at the code there is no reason for it to not be calld on the client.

Show your code.

Expand  
package dinocraft.item;

import dinocraft.Reference;
import dinocraft.capabilities.entity.DinocraftEntity;
import dinocraft.entity.EntityRayBullet;
import dinocraft.init.DinocraftItems;
import dinocraft.init.DinocraftSoundEvents;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class ItemRayGun extends Item
{
	public ItemRayGun(String name)
	{
		this.setUnlocalizedName(name);
		this.setMaxStackSize(1);
		this.setMaxDamage(1000);
		this.setRegistryName(new ResourceLocation(Reference.MODID, name));
	}
	
	@Override
	public void onUsingTick(ItemStack stack, EntityLivingBase entityliving, int count)
	{
		EntityPlayer player = (EntityPlayer) entityliving;
		DinocraftEntity dinoEntity = DinocraftEntity.getEntity(player);
		
		if (player.isCreative() || dinoEntity.hasAmmo(DinocraftItems.RAY_BULLET))
		{
			if (!player.isCreative())
			{
				dinoEntity.consumeAmmo(DinocraftItems.RAY_BULLET, 1);
				stack.damageItem(1, player);
			}
			
			if (!player.world.isRemote)
			{
				EntityRayBullet ball = new EntityRayBullet(player, 0.001F);
				ball.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 15.0F, 0.0F);
				ball.setRotationYawHead(player.rotationYawHead);
				Vec3d vector = player.getLookVec();
				double x = vector.x;
				double y = vector.y;
				double z = vector.z;
				ball.motionX = x * 3.33D;
				ball.motionZ = z * 3.33D;
				ball.motionY = y * 3.33D;
				ball.setPositionAndUpdate(player.posX - (x * 0.75D), player.posY + player.eyeHeight, player.posZ - (z * 0.75D));
				player.world.spawnEntity(ball);
				player.world.playSound(null, player.getPosition(), DinocraftSoundEvents.RAY_GUN_SHOT, SoundCategory.NEUTRAL, 3.0F, player.world.rand.nextFloat() + 0.5F);
			}
            
			DinocraftEntity.getEntity(player).recoil(0.1F, 1.25F, true);
		}
		
		super.onUsingTick(stack, player, count);
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand)
	{
		ItemStack stack = player.getHeldItem(hand);
		DinocraftEntity dinoEntity = DinocraftEntity.getEntity(player);
		
		if (player.isCreative() || dinoEntity.hasAmmo(DinocraftItems.RAY_BULLET))
		{	
			player.setActiveHand(hand);
			return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
		}
		else if (!world.isRemote)
		{
			dinoEntity.sendActionbarMessage(TextFormatting.RED + "Out of ammo!");
			world.playSound(null, player.getPosition(), SoundEvents.BLOCK_DISPENSER_DISPENSE, SoundCategory.NEUTRAL, 0.5F, 5.0F);
			return ActionResult.newResult(EnumActionResult.FAIL, stack);
		}
		
		return ActionResult.newResult(EnumActionResult.FAIL, stack);
	}
}

The onUsingTick method doesn't call at all when I right-click. :/

Edited by Differentiation
Posted (edited)
  On 10/8/2019 at 6:38 PM, diesieben07 said:

Yes, I accidentally looked at the 1.14.4 method.

 

What did you return from getMaxUseDuration?

Expand  

I tested it again using 0 and the onUsingTick method still doesn't happen.

 

I don't think I have to call it anywhere for it to work, right? This method shoul call when I use the item... but it just doesn't...

Edited by Differentiation
Posted (edited)
  On 10/8/2019 at 10:11 PM, diesieben07 said:

I really don't know what you are doing that you are getting it to fire every 4 ticks only. Every 4 ticks is what vanilla does for it's particle spawning, but onUsingTick is called outside of that, directly in EntityLivingBase#updateActiveHand, which is called directly from EntityLivingBase#onUpdate.

Expand  
public class ItemRayGun extends Item
{
	public ItemRayGun(String name)
	{
		this.setUnlocalizedName(name);
		this.setMaxStackSize(1);
		this.setMaxDamage(1000);
		this.setRegistryName(new ResourceLocation(Reference.MODID, name));
	}
	
	@Override
	public int getMaxItemUseDuration(ItemStack stack)
	{
		return 1;
	}
	
	@Override
	public void onUsingTick(ItemStack stack, EntityLivingBase entityliving, int count)
	{
		EntityPlayer player = (EntityPlayer) entityliving;
		DinocraftEntity dinoEntity = DinocraftEntity.getEntity(player);
		
		if (player.isCreative() || dinoEntity.hasAmmo(DinocraftItems.RAY_BULLET))
		{
			if (!player.isCreative())
			{
				dinoEntity.consumeAmmo(DinocraftItems.RAY_BULLET, 1);
				stack.damageItem(1, player);
			}
			
			if (!player.world.isRemote)
			{
				EntityRayBullet ball = new EntityRayBullet(player, 0.001F);
				ball.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 15.0F, 0.0F);
				ball.setRotationYawHead(player.rotationYawHead);
				Vec3d vector = player.getLookVec();
				double x = vector.x;
				double y = vector.y;
            	double z = vector.z;
            	ball.motionX = x * 3.33D;
            	ball.motionZ = z * 3.33D;
            	ball.motionY = y * 3.33D;
            	ball.setPositionAndUpdate(player.posX - (x * 0.75D), player.posY + player.eyeHeight, player.posZ - (z * 0.75D));
            	player.world.spawnEntity(ball);
            	player.world.playSound(null, player.getPosition(), DinocraftSoundEvents.RAY_GUN_SHOT, SoundCategory.NEUTRAL, 3.0F, player.world.rand.nextFloat() + 0.5F);
			}
            
			DinocraftEntity.getEntity(player).recoil(0.1F, 1.25F, true);
		}
		
		super.onUsingTick(stack, player, count);
	}
	
	@Override
	public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected)
	{

		if (isSelected)
		{

			EntityPlayer player = (EntityPlayer) entity;
			DinocraftEntity dinoEntity = DinocraftEntity.getEntity(player);
					
			if (player.isCreative() || dinoEntity.hasAmmo(DinocraftItems.RAY_BULLET))
			{
				Item mainhand = player.getHeldItemMainhand().getItem();
				
				if (mainhand != null && mainhand == this)
				{
					player.setActiveHand(EnumHand.MAIN_HAND);
				}
				else
				{
					player.setActiveHand(EnumHand.OFF_HAND);
				}
			}
			else if (!world.isRemote)
			{
				dinoEntity.sendActionbarMessage(TextFormatting.RED + "Out of ammo!");
				world.playSound(null, player.getPosition(), SoundEvents.BLOCK_DISPENSER_DISPENSE, SoundCategory.NEUTRAL, 0.5F, 5.0F);
			}
					
			super.onUpdate(stack, world, entity, itemSlot, isSelected);
		}
	}
}

I noticed that onUsingTick only fires every tick if I explicitly call setActiveHand in onUpdate every tick... onUsingTick doesn't have anything to do with me right-clicking or using the item. That's what is so confusing to me.

Edited by Differentiation
Posted (edited)
  On 10/8/2019 at 10:11 PM, diesieben07 said:

I really don't know what you are doing that you are getting it to fire every 4 ticks only. Every 4 ticks is what vanilla does for it's particle spawning, but onUsingTick is called outside of that, directly in EntityLivingBase#updateActiveHand, which is called directly from EntityLivingBase#onUpdate.

Expand  

I tried running the code in onPlayerStoppedUsing and it's just a mess (fires sometimes on server thread, sometimes on client, idek anymore) it's very buggy and doesn't abide by getMaxItemUseDuration for shit. Anyways, I give up because it's way to hard to understand how the methods work together and I'm not stressing myself over something that's not even that important.

Edited by Differentiation
Posted (edited)
  On 10/8/2019 at 8:59 PM, Differentiation said:

I tested it again using 0 and the onUsingTick method still doesn't happen.

 

I don't think I have to call it anywhere for it to work, right? This method shoul call when I use the item... but it just doesn't...

Expand  

getMaxItemUseDuration In Bow class returns 72000 which means you can hold the bow for 3600 seconds. 0 means the longest time you can use the item is 0 tick, it doesn't make sense. 

 

Edited by poopoodice
Posted
  On 10/8/2019 at 11:22 PM, diesieben07 said:

This means your item may only be used for 1 tick before the usage gets cancelled.

I doubt you want this.

Expand  

Okay so this is what I have.

public class ItemRayGun extends Item
{
	public ItemRayGun(String name)
	{
		this.setUnlocalizedName(name);
		this.setMaxStackSize(1);
		this.setMaxDamage(1000);
		this.setRegistryName(new ResourceLocation(Reference.MODID, name));
	}
	
	@Override
	public int getMaxItemUseDuration(ItemStack stack)
	{
		return 72000;
	}
	
	@Override
	public EnumAction getItemUseAction(ItemStack stack)
	{
		return EnumAction.BOW;
	}
	
	@Override
	public void onUsingTick(ItemStack stack, EntityLivingBase entityliving, int count)
	{
		if (entityliving.ticksExisted % 2 == 0)
		{
			EntityPlayer player = (EntityPlayer) entityliving;
			World world = player.world;
			DinocraftEntity dinoEntity = DinocraftEntity.getEntity(player);
			
			DinocraftServer.getSide(world);
			
			if (player.isCreative() || dinoEntity.hasAmmo(DinocraftItems.RAY_BULLET))
			{
				if (!player.isCreative())
				{
					dinoEntity.consumeAmmo(DinocraftItems.RAY_BULLET, 1);
					stack.damageItem(1, player);
				}
	        	
				if (!world.isRemote)
				{
					EntityRayBullet ball = new EntityRayBullet(player, 0.001F);
					Vec3d vector = player.getLookVec();
					double x = vector.x;
					double y = vector.y;
		        	double z = vector.z;
					ball.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 3.33F, 0.0F);
					ball.setRotationYawHead(player.rotationYawHead);
		        	ball.setPositionAndUpdate(player.posX - (x * 0.75D), player.posY + player.eyeHeight, player.posZ - (z * 0.75D));
		        	world.spawnEntity(ball);
		        	world.playSound(null, player.getPosition(), DinocraftSoundEvents.RAY_GUN_SHOT, SoundCategory.NEUTRAL, 3.0F, world.rand.nextFloat() + 0.5F);
				}
		        
				DinocraftEntity.getEntity(player).recoil(0.1F, 1.25F, true);
			}
			else if (!world.isRemote)
			{
				dinoEntity.sendActionbarMessage(TextFormatting.RED + "Out of ammo!");
				world.playSound(null, player.getPosition(), SoundEvents.BLOCK_DISPENSER_DISPENSE, SoundCategory.NEUTRAL, 0.5F, 5.0F);
			}
			
			super.onUsingTick(stack, player, count);
		}
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
	{
		playerIn.setActiveHand(EnumHand.MAIN_HAND);
		return super.onItemRightClick(worldIn, playerIn, handIn);
	}
}

All goes well, but when I stop right-clicking, sometimes, the server thread doesn't get notified and it keeps running onUsingTick even though I'm not right clicking. The client side doesn't have this bug.

Also, when this fires, I get the following errors continuously. Not sure what they mean.

[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Error in class 'ChannelLWJGL OpenAL'
[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Invalid enumerated parameter value.
[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Error in class 'ChannelLWJGL OpenAL'
[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Error creating buffers in method 'preLoadBuffers'

 

  • Like 1
Posted (edited)
  On 10/10/2019 at 2:09 AM, Differentiation said:

Okay so this is what I have.

public class ItemRayGun extends Item
{
	public ItemRayGun(String name)
	{
		this.setUnlocalizedName(name);
		this.setMaxStackSize(1);
		this.setMaxDamage(1000);
		this.setRegistryName(new ResourceLocation(Reference.MODID, name));
	}
	
	@Override
	public int getMaxItemUseDuration(ItemStack stack)
	{
		return 72000;
	}
	
	@Override
	public EnumAction getItemUseAction(ItemStack stack)
	{
		return EnumAction.BOW;
	}
	
	@Override
	public void onUsingTick(ItemStack stack, EntityLivingBase entityliving, int count)
	{
		if (entityliving.ticksExisted % 2 == 0)
		{
			EntityPlayer player = (EntityPlayer) entityliving;
			World world = player.world;
			DinocraftEntity dinoEntity = DinocraftEntity.getEntity(player);
			
			DinocraftServer.getSide(world);
			
			if (player.isCreative() || dinoEntity.hasAmmo(DinocraftItems.RAY_BULLET))
			{
				if (!player.isCreative())
				{
					dinoEntity.consumeAmmo(DinocraftItems.RAY_BULLET, 1);
					stack.damageItem(1, player);
				}
	        	
				if (!world.isRemote)
				{
					EntityRayBullet ball = new EntityRayBullet(player, 0.001F);
					Vec3d vector = player.getLookVec();
					double x = vector.x;
					double y = vector.y;
		        	double z = vector.z;
					ball.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 3.33F, 0.0F);
					ball.setRotationYawHead(player.rotationYawHead);
		        	ball.setPositionAndUpdate(player.posX - (x * 0.75D), player.posY + player.eyeHeight, player.posZ - (z * 0.75D));
		        	world.spawnEntity(ball);
		        	world.playSound(null, player.getPosition(), DinocraftSoundEvents.RAY_GUN_SHOT, SoundCategory.NEUTRAL, 3.0F, world.rand.nextFloat() + 0.5F);
				}
		        
				DinocraftEntity.getEntity(player).recoil(0.1F, 1.25F, true);
			}
			else if (!world.isRemote)
			{
				dinoEntity.sendActionbarMessage(TextFormatting.RED + "Out of ammo!");
				world.playSound(null, player.getPosition(), SoundEvents.BLOCK_DISPENSER_DISPENSE, SoundCategory.NEUTRAL, 0.5F, 5.0F);
			}
			
			super.onUsingTick(stack, player, count);
		}
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
	{
		playerIn.setActiveHand(EnumHand.MAIN_HAND);
		return super.onItemRightClick(worldIn, playerIn, handIn);
	}
}

All goes well, but when I stop right-clicking, sometimes, the server thread doesn't get notified and it keeps running onUsingTick even though I'm not right clicking. The client side doesn't have this bug.

Also, when this fires, I get the following errors continuously. Not sure what they mean.

[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Error in class 'ChannelLWJGL OpenAL'
[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Invalid enumerated parameter value.
[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Error in class 'ChannelLWJGL OpenAL'
[22:01:36] [Thread-6/ERROR] [minecraft/SoundManager]: Error creating buffers in method 'preLoadBuffers'

 

Expand  

Hello me again lol.

This should solve your first problem.

Edited by poopoodice
  • Thanks 1

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.