Jump to content

Recommended Posts

Posted

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.

Posted

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/

Posted
  On 11/26/2017 at 1:44 PM, 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.

Expand  

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.

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

    • Ghjkvcfzukjvv
    • Machen Sie sich bereit für unglaubliche Ersparnisse im Juli 2025 mit unserem exklusiven Temu Gutscheincode 30% Rabatt. Wir geben Ihnen den Schlüssel zu den besten Angeboten auf der Plattform mit dem Code (ACW867474), der Ihr Einkaufserlebnis verändern wird. Der Temu Gutscheincode ACW867474 wurde entwickelt, um Ihnen maximale Vorteile zu bieten, insbesondere für unsere Nutzer in Deutschland, Österreich und der gesamten Europäischen Union. Erleben Sie Shopping ohne Grenzen mit Rabatten, die wirklich einen Unterschied machen. Mit unserem Temu Gutschein 30% Rabatt schalten Sie sofortige Einsparungen frei. Nutzen Sie einfach unseren Temu 30% Rabatt Gutscheincode, um Ihr Einkaufserlebnis zu revolutionieren und bei den angesagtesten Produkten zu sparen. Was ist der Gutscheincode für Temu 30% Rabatt? Sie fragen sich vielleicht, wie Sie diesen fantastischen Rabatt erhalten können. Wir haben es Ihnen leicht gemacht! Sowohl neue als auch bestehende Kunden können erstaunliche Vorteile genießen, wenn sie unseren exklusiven 30%-Rabatt-Gutscheincode auf der Temu-App oder Website verwenden. Mit diesem Code erhalten Sie nicht nur einen einmaligen Rabatt, sondern schalten ein ganzes Paket an Sparmöglichkeiten frei. Der Temu Gutschein 30% Rabatt ist Ihr Ticket zu intelligenterem Einkaufen, und der 30% Rabatt Temu Gutschein stellt sicher, dass Sie immer den besten Preis bekommen. Hier sind die Hauptvorteile, die Sie mit unserem Code erhalten: •    ACW867474: Ein pauschaler Rabatt von 30% auf Ihre gesamte Bestellung. •    ACW867474: Ein 30% Gutschein-Paket für mehrere zukünftige Einkäufe. •    ACW867474: Ein garantierter 30% Pauschalrabatt für alle Neukunden. •    ACW867474: Ein zusätzlicher 30% Promo-Code für treue Bestandskunden. •    ACW867474: Ein spezieller 30% Gutschein für Nutzer in Deutschland/Österreich. Temu Gutscheincode 30% Rabatt für Neukunden in 2025 Wenn Sie neu bei Temu sind, erwartet Sie eine besondere Freude! Als Neukunde können Sie die höchsten Vorteile erzielen, wenn Sie unseren Temu Gutschein 30% Rabatt verwenden. Temu begrüßt neue Käufer mit offenen Armen und noch offeneren Rabatten, und unser Temu Gutscheincode 30% Rabatt ist der beste Weg, um Ihre Shopping-Reise zu beginnen. Stellen Sie sich vor, Sie erhalten die trendigsten Artikel aus Mode, Elektronik, Haushaltswaren und mehr mit einem massiven Preisnachlass direkt bei Ihrer ersten Bestellung. Nutzen Sie diese Vorteile als Neukunde: •    ACW867474: Ein sofortiger Pauschalrabatt von 30% für Neukunden. •    ACW867474: Ein 30% Gutschein-Paket für Ihre zukünftigen Bestellungen. •    ACW867474: Bis zu 30% Rabatt auf mehrere Einkäufe. •    ACW867474: Kostenloser Versand in 86 Länder, einschließlich ganz Deutschland. •    ACW867474: Extra 30% Rabatt auf jeden Einkauf als Erstnutzer.  
    • thank you for the help, i deleted yungscavebiomes and now it works   
    • first crash: https://mclo.gs/Ou66SIc second crash: https://mclo.gs/qu0W6qY
  • Topics

×
×
  • Create New...

Important Information

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