Jump to content

[1.8] NBTTagCompund on type Item


Ryat

Recommended Posts

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;
}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";

}


}

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I have created a custom entity that extends "TamableAnimal", but I am wanting to have it spawn in the ocean. I have it spawning right now, but it spawns way too frequently even with weight set to 1. I am guessing it is because it is rolling in the spawn pool of land animals since TameableAnimal extends Animal and is different than WaterAnimal, and since no land animals spawn in the ocean it just fills every inch up with my custom entity. I have followed basic tutorials for spawning entities with Forge, but I feel like I am missing something about how to change what spawn pool this custom entity ends up in. Is it possible to change that or do I need to refactor it to be based off of WaterAnimal to get those spawn? My biome modifier JSON file: { "type": "forge:add_spawns", "biomes": "#minecraft:is_ocean", "spawners": { "type": "darwinsmysticalmounts:water_wyvern", "weight": 20, "minCount": 1, "maxCount": 1 } } My client event: event.register(ModEntityTypes.WATER_WYVERN.get(), SpawnPlacements.Type.NO_RESTRICTIONS, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, WaterWyvernEntity::checkWaterWyvernSpawnRules, SpawnPlacementRegisterEvent.Operation.REPLACE); And the actual custom spawn rule that makes it spawn in the water: public static boolean checkWaterWyvernSpawnRules(EntityType<WaterWyvernEntity> pAnimal, LevelAccessor pLevel, MobSpawnType pSpawnType, BlockPos pPos, RandomSource pRandom) { return pPos.getY() > pLevel.getSeaLevel() - 16 && pLevel.getFluidState(pPos.below()).is(FluidTags.WATER); }  
    • Starting today, I am unable to load my modded minecraft world. Forge crash log initially said it was a specific mod called Doggy Talents, which I disabled. Then it blamed JEI, and when that was disabled it blamed another mod so I assume it's something more than a specific mod. Minecraft launcher log claims "Exit Code 1". Nothing had changed since last night when it was working fine Forge Log: https://pastebin.com/S1GiBGVJ Client Log: https://pastebin.com/aLwuGUNL  
    • “Courage doesn’t mean you don’t get afraid. Courage means you don’t let fear stop you.” This mantra has been my guiding light throughout my life as a military doctor. After years of serving my country, I dedicated myself to healing others and making a positive impact on the lives of my fellow soldiers and their families. My commitment to service extended beyond the battlefield; I was determined to secure a stable future for my own family. With my hard-earned savings, I invested $470,000 in cryptocurrency, believing it would provide the financial foundation we needed. However, one fateful day during a particularly intense deployment, everything changed. While treating a patient amid an attack, my phone slipped from my hands and crashed to the ground. In the chaos, I realized I hadn’t backed up my cryptocurrency wallet recovery phrase, thinking I had plenty of time to do so later. When I attempted to access my funds that night, panic surged through me—I could no longer access my wallet. My dreams of financial security and stability were vanishing right before my eyes. The betrayal I felt was profound. It was as if I had not only failed myself but also my family, who relied on me to provide for them. Each day at the hospital reminded me of my commitment to healing, and now I was left feeling helpless in the face of adversity. I feared losing everything I had worked so hard for, and the emotional toll was unbearable.  In my desperation, I reached out to trusted colleagues and friends. One fellow veteran mentioned TECH CYBER FORCE RECOVERY Tool, a reputable team known for their expertise in recovering lost digital assets. REACH OUT TO THEM WWW.techcyberforcerecovery.info https://wa.me/message/BJPIMH5UTNLKL1 
    • I am using AMD, yes. I downloaded the website's drivers and am still having the issue. I also used the Cleanup Utility just in case. 
    • I can't figure out how to upload it any other way, so I made a google drive link; hopefully that works https://drive.google.com/file/d/165vmAMoLtb55wzwYHh3vBfHlJ-Wrgm_y/view?usp=sharing
  • Topics

×
×
  • Create New...

Important Information

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