Jump to content

[1.8] [SOLVED] Item object resets when the game is reloaded


Ferrettomato

Recommended Posts

I'm doing some experimenting, and that involves making a chestplate capable of storing an Item object. Unfortunately, this Item object is reset whenever the game is restarted (closed and reopened, leaving a world and coming back in the same instance of the game does not trigger the bug). I'm a novice modder, and, thus, I don't know what I've done wrong. I find it likely that the answer will be something blatantly obvious. Could someone please enlighten me?

 

ItemInlayArmor class:

package com.ferret.relics.items;

import java.util.List;

import com.ferret.myfirstmod.MyFirstMod;
import com.ferret.myfirstmod.items.ModItems;
import com.ferret.relics.Relics;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemArmor.ArmorMaterial;
import net.minecraft.potion.PotionEffect;
import net.minecraft.world.World;

public class ItemInlayArmor extends ItemArmor 
{
Item focus;

public ItemInlayArmor(ArmorMaterial material, int renderIndex, int armorType, String name) {
	super(material, 0, armorType);
	setUnlocalizedName(Relics.MODID + "_" + name);
	System.out.println(this.getUnlocalizedName());
}

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type)
{
		return Relics.MODID + ":models/armor/inlayarmor1.png";
}

 public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
 {
	 if(itemStackIn.getItem() instanceof ItemInlayArmor)
	 {
		 if(playerIn.isSneaking())
		 {
			 ItemInlayArmor item = (ItemInlayArmor)itemStackIn.getItem();
			 int focusTarget = playerIn.inventory.currentItem + 1;
			 if(playerIn.inventory.getStackInSlot(focusTarget) != null)
			 {
				 Item focusItem = playerIn.inventory.getStackInSlot(focusTarget).getItem();
				 playerIn.inventory.decrStackSize(focusTarget, 1);
				 item.focus = focusItem;
				 itemStackIn.setItem(item);
			 }
		 }
	 }
     return itemStackIn;
 }

 @Override
 public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4)
 {
	 if(focus != null)
	 	 list.add(focus.getUnlocalizedName());
	 else
		 list.add("Empty");
 }

 @Override
 public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack)
 {
	 if(itemStack.getItem() instanceof ItemInlayArmor)
	 {
		 if(focus != null)
		 {
			 if(focus.equals(Items.rabbit_foot))
			 {
				 player.addPotionEffect(new PotionEffect(8, 300, 3));
			 }
		 }
	 }
 }
}

Link to comment
Share on other sites

Hi

 

Having a member variable 'focus' in your Item class won't work for two reasons:

1) There is only one instance of ItemInlayArmor, so if you have more than one of these (eg two players both have the armour) it will overwrite.

2) You haven't written any load / save code for focus.

 

You need to store this information in ItemStack NBT instead.

It might help to read up on this background info about ItemStacks

http://greyminecraftcoder.blogspot.com.au/2013/12/items.html

 

This tutorial project has an example of an item which stores NBT information, which is what you need to do.

https://github.com/TheGreyGhost/MinecraftByExample

see MBE12

https://github.com/TheGreyGhost/MinecraftByExample/blob/master/src/main/java/minecraftbyexample/mbe12_item_nbt_animate/Notes.txt

 

-TGG

 

 

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.