Jump to content

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


Differentiation

Recommended Posts

34 minutes ago, 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!

Have a look at this thread.

Diesieben07 has explained clearly.

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

Edited by poopoodice
Link to comment
Share on other sites

4 hours ago, poopoodice said:

Have a look at this thread.

Diesieben07 has explained clearly.

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

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

Link to comment
Share on other sites

11 minutes ago, 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().

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.

Link to comment
Share on other sites

1 hour ago, poopoodice said:

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

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.

Link to comment
Share on other sites

17 minutes ago, diesieben07 said:

Show your code.

 

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

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. 

Link to comment
Share on other sites

50 minutes ago, diesieben07 said:

No.

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

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

4 hours ago, 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.

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

2 hours ago, diesieben07 said:

Yes, I accidentally looked at the 1.14.4 method.

 

What did you return from getMaxUseDuration?

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

10 minutes ago, 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.

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

41 minutes ago, 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.

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

3 hours ago, 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...

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

On 10/8/2019 at 7: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.

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

14 minutes ago, 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'

 

Hello me again lol.

This should solve your first problem.

Edited by poopoodice
  • Thanks 1
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

    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
  • Topics

×
×
  • Create New...

Important Information

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