Jump to content

Food Item with changing hunger values


meee39

Recommended Posts

I am making an Electronic Lunchbox mod that uses RF to feed you. I have this code:

package electroniclunchbox.items;

import java.util.List;

import org.lwjgl.input.Keyboard;

import cofh.redstoneflux.api.IEnergyContainerItem;
import electroniclunchbox.ElectronicLunchbox;
import electroniclunchbox.gui.ModGuiHandler;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;

public class ItemElectronicLunchbox extends ItemFood implements IEnergyContainerItem {
	public static final int capacity = 1000000;
	public static final int maxReceive = 10000;
	
	public ItemElectronicLunchbox(int amount, float saturation, boolean isWolfFood) {
		super(amount, saturation, isWolfFood);
	}

	@Override
	public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving) {
		if (entityLiving instanceof EntityPlayer)
        {
            EntityPlayer entityplayer = (EntityPlayer)entityLiving;
            entityplayer.getFoodStats().addStats(this, stack);
            worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_PLAYER_BURP, SoundCategory.PLAYERS, 0.5F, worldIn.rand.nextFloat() * 0.1F + 0.9F);
            this.onFoodEaten(stack, worldIn, entityplayer);
            entityplayer.addStat(StatList.getObjectUseStats(this));

            if (entityplayer instanceof EntityPlayerMP)
            {
                CriteriaTriggers.CONSUME_ITEM.trigger((EntityPlayerMP)entityplayer, stack);
            }
        }

        return stack;
	}

	@Override
	public int getMaxItemUseDuration(ItemStack stack) {
		return super.getMaxItemUseDuration(stack);
	}

	@Override
	public int getHealAmount(ItemStack stack) {
		NBTTagCompound nbt = stack.getTagCompound();
		
		return nbt.getInteger("Hunger");
	}

	@Override
	public float getSaturationModifier(ItemStack stack) {
		NBTTagCompound nbt = stack.getTagCompound();
		
		return nbt.getInteger("Saturation");
	}

	@Override
	public boolean isWolfsFavoriteMeat() {
		return super.isWolfsFavoriteMeat();
	}

	@Override
	public ItemFood setPotionEffect(PotionEffect effect, float probability) {
		return super.setPotionEffect(effect, probability);
	}

	@Override
	public ItemFood setAlwaysEdible() {
		return super.setAlwaysEdible();
	}

	@Override
	public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		super.addInformation(stack, worldIn, tooltip, flagIn);
		
		if (!stack.hasTagCompound()) {
			return;
		}
		
		NBTTagCompound nbt = stack.getTagCompound();
		
		tooltip.add("Shift-Right Click Me To Edit My Settings");
		tooltip.add("Hunger: " + nbt.getInteger("Hunger"));
		tooltip.add("Saturation: " + nbt.getInteger("Saturation"));
		tooltip.add("RF Cost: ");
	}

	@Override
	public double getDurabilityForDisplay(ItemStack stack) {
		if (!stack.hasTagCompound()) {
			NBTTagCompound nbt = new NBTTagCompound();
			nbt.setInteger("Energy", 0);
			nbt.setInteger("Hunger", 0);
			nbt.setInteger("Saturation", 0);
			stack.setTagCompound(nbt);
		}
		
		NBTTagCompound nbt = stack.getTagCompound();
		
		return (double)capacity / (double)nbt.getInteger("Energy");
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
		if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
			playerIn.openGui(ElectronicLunchbox.instance, ModGuiHandler.electronicLunchboxGuiID, worldIn, (int)playerIn.posX, (int)playerIn.posY, (int)playerIn.posZ);
		}
		
		return super.onItemRightClick(worldIn, playerIn, handIn);
	}

	@Override
	public boolean showDurabilityBar(ItemStack stack) {
		return true;
	}

	@Override
	public int extractEnergy(ItemStack arg0, int arg1, boolean arg2) {
		return arg1;
	}

	@Override
	public int getEnergyStored(ItemStack arg0) {
		return 0;
	}

	@Override
	public int getMaxEnergyStored(ItemStack arg0) {
		return 1000000;
	}

	@Override
	public int receiveEnergy(ItemStack arg0, int arg1, boolean arg2) {
		if (!arg0.hasTagCompound()) {
			arg0.setTagCompound(new NBTTagCompound());
		}
		int energy = arg0.getTagCompound().getInteger("Energy");
		int energyReceived = Math.min(capacity - energy, Math.min(maxReceive, maxReceive));

		if (!arg2) {
			energy += energyReceived;
			arg0.getTagCompound().setInteger("Energy", energy);
		}
		return energyReceived;
	}	
}

The NBT on it is set in a Gui, but that works perfectly.

 

The problem is that no matter what the NBT says the hunger and saturation will heal, it will heal the whole hunger bar full of hunger and saturation.

Link to comment
Share on other sites

Are you sure you aren't just setting the NBT values way too high? What are the NBT values? For example, cooked beef/pork has 8 "food" + 0.8 saturation and cooked chicken gives 6 "food" + 0.6 saturation.

 

Also this code will crash:

if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
			playerIn.openGui(ElectronicLunchbox.instance, ModGuiHandler.electronicLunchboxGuiID, worldIn, (int)playerIn.posX, (int)playerIn.posY, (int)playerIn.posZ);
		}

Keyboard is a class only available on the client side, so this will crash on the server. I assume you want the player sneaking to be abe to open the GUI, so you can use Entity::isSneaking instead.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

1 hour ago, meee39 said:

The NBT values are set by a slider in the Gui. The maximum value for both is 20. I think that is fine for hunger, but not saturation.

Are you using a packet to send the slider value from the GUI back to the server?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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



×
×
  • Create New...

Important Information

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