Jump to content

Recommended Posts

Posted (edited)

I have this event and its supposed to be whenever a player sneak right clicks with a empty glass bottle they take 3 hearts of damage and gives them a vial of blood but it doesn't do anything. Any help appreciated, thanks.

 

Event:

Spoiler

@SubscribeEvent
    public void bloodVialRightClick(LivingEntityUseItemEvent.Start e)
    {
        EntityPlayer player = (EntityPlayer) e.getEntityLiving();
        ItemStack itemUsed = e.getItem();
        
        if(itemUsed == new ItemStack(Items.GLASS_BOTTLE) && player.isSneaking())
        {
            float health = player.getHealth();
            float newHealth = player.getHealth() - 3;
            player.setHealth(newHealth);
            player.inventory.addItemStackToInventory(new ItemStack(ItemRegistry.bloodVial));
        
            itemUsed.shrink(1);
        }
    }

 

Edited by Blackwing_Forged
Posted (edited)

this should fix things the LivingEntityUseItemEvent.Start only works for items that can be used i.e. food i wrote the fixed code for you below.

EDIT: just realized you wanted sneak-right-click i fixed the code below

 

	//used when rightclicking while looking at a block
	@SubscribeEvent()
	public void rightClickBlock(PlayerInteractEvent.RightClickBlock e){
		//this checks if the entities world is the server we only run this on the server so it doesn't mess with things
		if(!e.getEntity().getEntityWorld().isRemote){
			EntityPlayer player = e.getEntityPlayer();//the player
			//check if player is sneaking
			if(player.isSneaking()){
				ItemStack heldItem = player.getHeldItemMainhand();//the items in the players hand
				//we compare the Item contained in the ItemStack not the ItemStack itself
				//we use .equals here not == only use == for numbers or booleans
				if(heldItem.getItem().equals(Items.GLASS_BOTTLE)){
					onGlassBottleRightClick(player, heldItem);//this is the method where you put the code when the player right-clicks with an item
				}	
			}
			
		}
	}
	
	//used when rightclicking with an item while not looking at a block
	@SubscribeEvent()
	public void rightClickItem(PlayerInteractEvent.RightClickItem e){
			//this checks if the entities world is the server we only run this on the server so it doesn't mess with things
			if(!e.getEntity().getEntityWorld().isRemote){
				EntityPlayer player = e.getEntityPlayer();//the player
				//check if player is sneaking
				if(player.isSneaking()){
					ItemStack heldItem = player.getHeldItemMainhand();//the items in the players hand
					//we compare the Item contained in the ItemStack not the ItemStack itself
					//we use .equals here not == only use == for numbers or booleans
					if(heldItem.getItem().equals(Items.GLASS_BOTTLE)){
						onGlassBottleRightClick(player, heldItem);//this is the method where you put the code when the player right-clicks with an item
					}	
				}
				
			}
	}
	
	private void onGlassBottleRightClick(EntityPlayer player, ItemStack bottlestack){
		player.attackEntityFrom(DamageSource.GENERIC, 3F);
		//player.inventory.addItemStackToInventory(new ItemStack(ItemRegistry.bloodVial));
	}

 

Edited by jtsfour
error
  • Like 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.