Jump to content

Recommended Posts

Posted (edited)

EDIT:
It had nothing to do with the item. It was entirely on how it was given to the player. The player right-clicks a block that then gives the item directly to the player's inventory. However, while debugging and using the logger, I noticed onBlockActivated called twice. I found out this is because it runs the function for the server and client. So, thinking "I DoN'T WaNT The functIOn To be cALlED TwicE", I told the function to stop early if the world being given is a ServerWorld, so it'd only run on the client. This is dumb. When the item was used by the client, the server didn't think it existed, causing it to disappear when dropped, and not work correctly otherwise. By removing that code, it ran correctly (without doing things like giving the item twice as I thought it would).

Post all related code when on a support forum, folks. The problem may not be what you think it is.



I am currently working on a mod that involves a drinkable item. Here's the code for said item:

package com.warven22.sodapop.items;

import com.warven22.sodapop.init.ModItemGroups;
import net.minecraft.item.Food.Builder;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.UseAction;

public class ExpressoCan extends Item {
	
	public ExpressoCan() {
		super(new Item.Properties().group(ModItemGroups.MOD_ITEM_GROUP).
				maxStackSize(64).
				food(new Builder().setAlwaysEdible().build()));
	}
	
	@Override
	public UseAction getUseAction(ItemStack stack) {
		return UseAction.DRINK;
	}
}


The drinking animation starts just fine, but after the normal drinking time should end, the arm returns to normal, but the sounds continue beyond. My speed stays low during the use, so it appears as if I'm still drinking. The event onItemUseFinish doesn't call either, as I assume the usage is never finishing. I attempted to try it without overriding getUseAction, but the same problem occurs with the eating animation, with the particles joining the continuing sound. The animation would stop when I let go of the mouse, but the item would stay in my inventory, and the normal "burp" sound never played.

I  checked using a vanilla Minecraft potion and food item. Both acted normally. Sometimes, after drinking a vanilla potion, the mod item would be consumable normally, but this was inconsistent and rarely happened.


I searched the forum and google extensively to try and find someone with the same issue, but I have been unsuccessful. I used existing mod GitHub repositories to see if there was something I was just missing in my code. However, their code doesn't look far off from mine.

I am stumped, as this item has very little code attached to it for it to break. Perhaps I'm just missing something crucial? I am new to modding.

Edited by Warven22
Issue solved, updated to prevent users from answering and assisting future users having the same issue
Posted

Howdy

 

You might need to override all of the relevant methods, if the default doesn't match your item properties.

For example

  // what animation to use when the player holds the "use" button
  @Override
  public UseAction getUseAction(ItemStack stack) {
    return UseAction.DRINK;
  }

  // how long the drinking will last for, in ticks (1 tick = 1/20 second)
  @Override
  public int getUseDuration(ItemStack stack) {
    final int TICKS_PER_SECOND = 20;
    final int DRINK_DURATION_SECONDS = 2;
    return DRINK_DURATION_SECONDS * TICKS_PER_SECOND;
  }

// you could also consider implementing these two methods just to add breakpoints (or prints to the debug log) and see what is going on

  // called when the player starts holding right click;
  // --> start drinking the liquid (if the bottle isn't already empty)
  @Override
  public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand hand)
  {
    ItemStack itemStackHeld = playerIn.getHeldItem(hand);
    EnumBottleFullness fullness = getFullness(itemStackHeld);
    if (fullness == EnumBottleFullness.EMPTY) return new ActionResult(ActionResultType.FAIL, itemStackHeld);

    playerIn.setActiveHand(hand);
    return new ActionResult(ActionResultType.PASS, itemStackHeld);
  }

  // called when the player has held down right button for the full item use duration
  // --> decrease the bottle fullness by one step
  @Override
  public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving)
  {
    EnumBottleFullness fullness = getFullness(stack);
    fullness = fullness.decreaseFullnessByOneStep();
    fullness.putIntoNBT(stack.getTag(), NBT_TAG_NAME_FULLNESS);
    return stack;
  }

 

A breakpoint in LivingEntity::updateActiveHand might also shed some light on what's happening

 

-TGG

 

Posted
3 hours ago, TheGreyGhost said:

Howdy

 

You might need to override all of the relevant methods, if the default doesn't match your item properties.

For example


  // what animation to use when the player holds the "use" button
  @Override
  public UseAction getUseAction(ItemStack stack) {
    return UseAction.DRINK;
  }

  // how long the drinking will last for, in ticks (1 tick = 1/20 second)
  @Override
  public int getUseDuration(ItemStack stack) {
    final int TICKS_PER_SECOND = 20;
    final int DRINK_DURATION_SECONDS = 2;
    return DRINK_DURATION_SECONDS * TICKS_PER_SECOND;
  }

// you could also consider implementing these two methods just to add breakpoints (or prints to the debug log) and see what is going on

  // called when the player starts holding right click;
  // --> start drinking the liquid (if the bottle isn't already empty)
  @Override
  public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand hand)
  {
    ItemStack itemStackHeld = playerIn.getHeldItem(hand);
    EnumBottleFullness fullness = getFullness(itemStackHeld);
    if (fullness == EnumBottleFullness.EMPTY) return new ActionResult(ActionResultType.FAIL, itemStackHeld);

    playerIn.setActiveHand(hand);
    return new ActionResult(ActionResultType.PASS, itemStackHeld);
  }

  // called when the player has held down right button for the full item use duration
  // --> decrease the bottle fullness by one step
  @Override
  public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving)
  {
    EnumBottleFullness fullness = getFullness(stack);
    fullness = fullness.decreaseFullnessByOneStep();
    fullness.putIntoNBT(stack.getTag(), NBT_TAG_NAME_FULLNESS);
    return stack;
  }

 

A breakpoint in LivingEntity::updateActiveHand might also shed some light on what's happening

 

-TGG

 

I gave this a shot, but it didn't fix the issue. Nothing debug-wise seemed off. ItemStacks matched up properly, item duration was as expected, etc. Messing around in Minecraft itself, I clicked a chest. The item in question in my inventory disappeared.

I tried taking the item directly from the mod ItemGroup, and the drinking worked like normal. If the item was droppable, its drinking would work. If it disappeared when I tried to drop it, it was one of the ones with bugged drinking.

What I'm using to add the item to the inventory after a special block is interacted with in onBlockActivated:

player.addItemStackToInventory(new ItemStack(ModItems.CAN_EXPRESSO, 1));


Is this the incorrect way to handle it? I also tried using the player's inventory to add a stack directly, but both approaches caused this bugged drink.

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.