Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.12.2] Create custom NBT for an entity [Solved]


uiytt
 Share

Recommended Posts

So basically I want to create an entity that has a custom value in his NBT (the custom value is a string).

But I have no idea of how to write a new value or how to read it !

Can someone help me please ?

 

My code so far with my entity

Spoiler

package fr.uiytt.playershop.Entity;


import java.util.List;
import java.util.UUID;

import fr.uiytt.playershop.Main;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;

public class PlayerShop extends EntityCreature {

	private boolean shopopen = false;
	
	
	public PlayerShop(World worldIn) {
		super(worldIn);
		setupAI();
	}
	@Override
	public float getBlockPathWeight(BlockPos pos)
    {
        return 0.1F;
    }
	
	@Override
	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.5D);
		this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(1.0D);
		this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(16.0D);
	}

	@Override
	public boolean isAIDisabled() {
		return false;
	}
	
	
	
	protected void setupAI() {
		clearAITasks();
		this.tasks.addTask(0,new EntityAISwimming(this));
		this.tasks.addTask(1,new EntityAIShopWander(this));
		this.tasks.addTask(2,new EntityAIWatchClosest(this, EntityPlayer.class, 10.0F));
	}
	
	protected void clearAITasks() {
		this.tasks.taskEntries.clear();
		this.targetTasks.taskEntries.clear();
	}
    
	
	@Override
	public boolean attackEntityFrom(DamageSource dmsource, float dmg) {
		if(this.getIsInvulnerable()) {
			return false;
		}
		
		if(!(dmsource.getTrueSource() instanceof EntityPlayer)) {
			return false;
		}else {
			EntityPlayer player = (EntityPlayer)dmsource.getTrueSource();
			if(player.capabilities.isCreativeMode) {
				
				this.damageEntity(dmsource, dmg);
				this.playHurtSound(dmsource);
				this.performHurtAnimation();
				return true;
			}
				
		}
		return false;
		
	}
	
	
	protected SoundEvent getHurtSound(DamageSource damageSourceIn)
    {
        return SoundEvents.ENTITY_VILLAGER_HURT;
    }

    protected SoundEvent getDeathSound()
    {
        return SoundEvents.ENTITY_VILLAGER_DEATH;
    }

	
	public boolean isShopopen() {
		return shopopen;
	}
	public void setShopopen(boolean shopopen) {
		this.shopopen = shopopen;
	}

}

 

 

Edited by uiytt
Link to comment
Share on other sites

After looking through the code of some mods : https://github.com/Vazkii/Botania/blob/master/src/main/java/vazkii/botania/common/entity/EntityPixie.java#L71

You must use dataManager for the entity that are load

And when is entity is loaded / de-loaded it call automatically the "writeEntityToNBT" and "readEntityFromNBT", and with that you can set data in the dataManager

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.

Guest
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.