Jump to content

Recommended Posts

Posted

I'm trying to add a NBTTagCompound to an Item. The code works except that when another item is spawned the tag tooltip on all other items of the same will change to match the new random value given to the new item.

 

What I'd like to have is each instance have their own tooltip.

 

Can anyone point me to somewhere I could look to research this please?

 

Feel free to fix the code if you wish.

 

Thanks!

 

package net.newearth.ne.items;

import java.util.List;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.newearth.ne.NewEarthMain;
import net.newearth.ne.genetics.DNA;
import net.newearth.ne.genetics.DNAHelper;

public class CuprumMineral extends Item {

public DNA dna;

public CuprumMineral() {

	this.setMaxStackSize(1);

}

@Override
public void onUpdate(ItemStack stack, World world, Entity entity,
		int metadata, boolean bool) {

	if (stack.getTagCompound() == null) {
		// System.out.println("No Tag");

		stack.setTagCompound(new NBTTagCompound());

		this.dna = new DNA();
	}

	NBTTagCompound nbt = new NBTTagCompound();

	String name = new DNAHelper().getName(dna.enchant);

	nbt.setString("dna", name);

	stack.getTagCompound().setTag("dna", nbt);

	stack.setStackDisplayName(EnumChatFormatting.AQUA + "Cuprum Mineral");




}

/**
 * Called when item is crafted/smelted. Used only by maps so far.
 */
@Override
public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn) {

}

/**
 * Called when a Block is right-clicked with this Item
 * 
 * @param pos
 *            The block being right-clicked
 * @param side
 *            The side being right-clicked
 */
@Override
// Run this code when clicked
public boolean onItemUse(ItemStack stack, EntityPlayer playerIn,
		World worldIn, BlockPos pos, EnumFacing side, float hitX,
		float hitY, float hitZ) {

	return false;
}

/**
 * Called whenever this item is equipped and the right mouse button is
 * pressed. Args: itemStack, world, entityPlayer
 */
@Override
public ItemStack onItemRightClick(ItemStack stack, World worldIn,
		EntityPlayer playerIn) {

	/*
	 * if(stack.getTagCompound() != null){
	 * 
	 * stack.getTagCompound().removeTag("dna");
	 * 
	 * stack.clearCustomName(); }
	 */

	return stack;
}

/**
 * allows items to add custom lines of information to the mouseover
 * description
 * 
 * @param tooltip
 *            All lines to display in the Item's tooltip. This is a List of
 *            Strings.
 * @param advanced
 *            Whether the setting "Advanced tooltips" is enabled
 */
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer playerIn,
		List tooltip, boolean advanced) {



	if (stack.getTagCompound() != null) {

		if (stack.getTagCompound().hasKey("dna")) {

			NBTTagCompound nbt = (NBTTagCompound) stack.getTagCompound()
					.getTag("dna");
			String gene = nbt.getString("dna");

			tooltip.add(gene);

		}


	}
}

public String getItemStackDisplayName(ItemStack stack) {
	return ("" + StatCollector.translateToLocal(this
			.getUnlocalizedNameInefficiently(stack) + ".name")).trim();
}

@Override
@SideOnly(Side.CLIENT)
public boolean hasEffect(ItemStack stack) {
	if (stack.getTagCompound() != null) {
		return stack.getTagCompound().hasKey("dna");
	}
	return false;
}

}

Posted

What does this do, and why is it here?

 

public DNA dna;

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.

Posted

Nothing really concrete right now, just messing around with jank until I can figure out how I can get the tags working correctly.

 

DNA Class

package net.newearth.ne.genetics;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnumEnchantmentType;

public class DNA {

public Enchantment enchant;



public DNA(){

	enchant = new DNAHelper().getRandomEnchant();
	System.out.println(getName());

}



public String getName(){

	return this.enchant.getName();
}

/**
 * Return ID of Enchantment
 * @return
 */
public int getID(){

	return this.enchant.effectId;
}


}


 

 

HELPER

package net.newearth.ne.genetics;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import net.minecraft.enchantment.Enchantment;

public class DNAHelper {

public ArrayList<Enchantment> eList = new ArrayList();

public DNAHelper(){

	eList.add(Enchantment.aquaAffinity);
	eList.add(Enchantment.baneOfArthropods);
	eList.add(Enchantment.blastProtection);
	eList.add(Enchantment.depthStrider);
	eList.add(Enchantment.efficiency);
	eList.add(Enchantment.featherFalling);
	eList.add(Enchantment.fireAspect);
	eList.add(Enchantment.fireProtection);
	eList.add(Enchantment.flame);
	eList.add(Enchantment.fortune);
	eList.add(Enchantment.infinity);
	eList.add(Enchantment.knockback);
	eList.add(Enchantment.looting);
	eList.add(Enchantment.luckOfTheSea);
	eList.add(Enchantment.lure);
	eList.add(Enchantment.power);
	eList.add(Enchantment.projectileProtection);
	eList.add(Enchantment.protection);
	eList.add(Enchantment.punch);
	eList.add(Enchantment.respiration);
	eList.add(Enchantment.sharpness);
	eList.add(Enchantment.silkTouch);
	eList.add(Enchantment.smite);
	eList.add(Enchantment.thorns);
	eList.add(Enchantment.unbreaking);
}

public Enchantment getRandomEnchant(){

	int x = new Random().nextInt(eList.size());

	return this.eList.get(x);
}

public String getName(Enchantment e){

	if(e == Enchantment.aquaAffinity){
		return "Aqua Affinity";
	}

	if(e == Enchantment.baneOfArthropods){
		return "Bane of Arthropods";
	}

	if(e == Enchantment.blastProtection){
		return "Blast Protection";
	}

	if(e == Enchantment.depthStrider){
		return "Depth Strider";
	}

	if(e == Enchantment.efficiency){
		return "Efficiency";
	}

	if(e == Enchantment.featherFalling){
		return "Feather Falling";
	}

	if(e == Enchantment.fireAspect){
		return "Fire Aspect";
	}

	if(e == Enchantment.fireProtection){
		return "Fire Protection";
	}

	if(e == Enchantment.flame){
		return "Flame";
	}

	if(e == Enchantment.fortune){
		return "Fortune";
	}

	if(e == Enchantment.infinity){
		return "Infinity";
	}

	if(e == Enchantment.knockback){
		return "Knockback";
	}

	if(e == Enchantment.looting){
		return "Looting";
	}

	if(e == Enchantment.luckOfTheSea){
		return "Luck of the Sea";
	}

	if(e == Enchantment.lure){
		return "Lure";
	}

	if(e == Enchantment.power){
		return "Power";
	}

	if(e == Enchantment.projectileProtection){
		return "Projectile Protection";
	}

	if(e == Enchantment.protection){
		return "Protection";
	}

	if(e == Enchantment.punch){
		return "Punch";
	}

	if(e == Enchantment.respiration){
		return "Respiration";
	}

	if(e == Enchantment.sharpness){
		return "Sharpness";
	}

	if(e == Enchantment.silkTouch){
		return "Silk Touch";
	}

	if(e == Enchantment.smite){
		return "Smite";
	}

	if(e == Enchantment.thorns){
		return "Thorns";
	}

	if(e == Enchantment.unbreaking){
		return "Unbreaking";
	}

	return "null";

}


}

Posted

Item class is singleton, so you should not save the DNA in Item class.

Instead, you should get the information from NBT every time you want, and get the DNA class for the info.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.