Jump to content

[1.7.10] save player to nbt


knokko

Recommended Posts

hello people,

 

I have a problem again with the same mob.

My mob has a summoner that is very important for the mob.

But the summoner is null if I leave the world and come back.

I think this is because I have not saved it to nbt.

 

But I got a problem, I don't know how to write a player to nbt.

I have tried some things but none of them worked.

 

Can anybody help me?

Link to comment
Share on other sites

I have added the method again.

This time the game doesn't crash but it has given me an error report and all the mobs have disappeared.

And I have not even used that field.

The field is written in the class and not in a method or something.

 

Here is the error report:

 

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:408)

at net.minecraft.entity.EntityList.createEntityFromNBT(EntityList.java:178)

at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadEntities(AnvilChunkLoader.java:494)

at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:41)

at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:12)

at net.minecraftforge.common.util.AsynchronousExecutor.skipQueue(AsynchronousExecutor.java:344)

at net.minecraftforge.common.util.AsynchronousExecutor.getSkipQueue(AsynchronousExecutor.java:302)

at net.minecraftforge.common.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:12)

at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:146)

at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:121)

at net.minecraft.world.gen.ChunkProviderServer.provideChunk(ChunkProviderServer.java:223)

at net.minecraft.world.World.getChunkFromChunkCoords(World.java:485)

at net.minecraft.world.World.spawnEntityInWorld(World.java:1484)

at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:313)

at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:174)

at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173)

at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446)[13:46:08] [server thread/WARN]: Skipping Entity with id undeadmage

[13:46:08] [server thread/WARN]: Skipping Entity with id undeadmage

 

at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17)

at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11)

at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98)

at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)

at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)

at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)

at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)

at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)

at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)

at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)

at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169)

at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86)

at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

Caused by: java.lang.NullPointerException

at enderpower.mobs.EntityUndeadMage.<init>(EntityUndeadMage.java:28)

... 39 more

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:408)

at net.minecraft.entity.EntityList.createEntityFromNBT(EntityList.java:178)

at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadEntities(AnvilChunkLoader.java:494)

at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:41)

at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:12)

at net.minecraftforge.common.util.AsynchronousExecutor.skipQueue(AsynchronousExecutor.java:344)

at net.minecraftforge.common.util.AsynchronousExecutor.getSkipQueue(AsynchronousExecutor.java:302)

at net.minecraftforge.common.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:12)

at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:146)

at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:121)

at net.minecraft.world.gen.ChunkProviderServer.provideChunk(ChunkProviderServer.java:223)

at net.minecraft.world.World.getChunkFromChunkCoords(World.java:485)

at net.minecraft.world.World.spawnEntityInWorld(World.java:1484)

at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:313)

at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:174)

at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173)

at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446)

at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17)

at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11)

at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98)

at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)

at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)

at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)

at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)

at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)

at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)

at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)

at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169)

at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86)

at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

Caused by: java.lang.NullPointerException

at enderpower.mobs.EntityUndeadMage.<init>(EntityUndeadMage.java:28)

... 39 more

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:408)

at net.minecraft.entity.EntityList.createEntityFromNBT(EntityList.java:178)

at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadEntities(AnvilChunkLoader.java:494)

at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:41)

at net.minecraftforge.common.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:12)

at net.minecraftforge.common.util.AsynchronousExecutor.skipQueue(AsynchronousExecutor.java:344)

at net.minecraftforge.common.util.AsynchronousExecutor.getSkipQueue(AsynchronousExecutor.java:302)

at net.minecraftforge.common.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:12)

at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:146)

at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:121)

at net.minecraft.world.gen.ChunkProviderServer.provideChunk(ChunkProviderServer.java:223)

at net.minecraft.world.World.getChunkFromChunkCoords(World.java:485)

at net.minecraft.world.World.spawnEntityInWorld(World.java:1484)

at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:313)

at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:174)

at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173)

at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446)

at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17)

at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11)

at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98)

at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)

at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)

at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)

at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)

at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)

at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)

at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)

at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169)

at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86)

at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:247)

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:736)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495)

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762)

Caused by: java.lang.NullPointerException

at enderpower.mobs.EntityUndeadMage.<init>(EntityUndeadMage.java:28)

... 39 more

 

 

The method was red in the console.

 

EDIT:

 

It seems that the field sometimes causes a crash and sometimes not.

And I haven't even used it.

I have placed "//" for the field to prevent it from causing crashes.

Link to comment
Share on other sites

Here is my class.

I have another problem too that is almost the same as this problem.

 

It seems that I have not full access too the summoner.

por example:

The isBurning check in useHealingMagic() gives a crash and I have tried more methods of the summoner that gave crashes too.

I will show the way of summoning the mob too.

 

public class EntityUndeadMage extends EntityAnimal{
/**
 * The summoner is the player that has summoned this mob.
 * This mob is programmed to help the summoner.
 */
public EntityPlayer summoner;
/**
 * The player name of the summoner.
 */
public String summonerName;
public UUID summonerUUID = summoner.getUniqueID();
/**
 * The type of magic this mob uses.
 * 0 = healing
 * 1 = mixed
 * 2 = offensive
 */
public int mageType;
double movementSpeed = this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getBaseValue();
/**
 * The current mana the mob has
 */
public int mobMana;
/**
 * The time this mob can't cast spells.
 */
public int manaCooldown;



/**
 * A required constructor for a mob.
 * 
 * Do not use this constructor to summon this mob.
 * Use the constructor world, summoner, mageType, x, y, z.
 * @param world
 */
    public EntityUndeadMage(World world)
    {
        super(world);
        this.setSize(0.9F, 1.8F);
    }

    /**
     * 
     * @param world - The world the mob has to spawn.
     * @param summoner - The player that summons this mob.
     * @param mageType - The type of magic this mob will use. 0 = positive 1 = mixed 2 = offensive
     * @param x - The X location the mob has to spawn.
     * @param y - The Y location the mob has to spawn.
     * @param z - The Z location the mob has to spawn.
     */
    public EntityUndeadMage(World world, EntityPlayer summoner, int mageType, double x, double y, double z){
    	super(world);
    	this.setSize(0.6F, 1.8F);
    	setPosition(x, y, z);
        prevPosX = x;
        prevPosY = y;
        prevPosZ = z;
        this.summoner = summoner;
        this.mageType = mageType;
    	
    }

    /**
     * Returns true if the newer Entity AI code should be run
     */
    public boolean isAIEnabled()
    {
        return true;
    }
    
    /**
     * The base attributes of this mob.
     */
    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(20.0D);
        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.35);
    }

    /**
     * Returns the sound this mob makes while it's alive.
     */
    protected String getLivingSound()
    {
        return null;
    }

    /**
     * Returns the sound this mob makes when it is hurt.
     */
    protected String getHurtSound()
    {
        return "mob.zombie.hurt";
    }

    /**
     * Returns the sound this mob makes on death.
     */
    protected String getDeathSound()
    {
        return "mob.zombie.hurt";
    }

    protected void func_145780_a(int x, int y, int z, Block block)
    {
        this.playSound("mob.zombie.step", 0.15F, 1.0F);
    }

    /**
     * Returns the volume for the sounds this mob makes.
     */
    protected float getSoundVolume()
    {
        return 0.4F;
    }

    protected Item getDropItem()
    {
        return Items.bone;
    }

    /**
     * Drop 0-2 items of this living's type. @param par1 - Whether this entity has recently been hit by a player. @param
     * par2 - Level of Looting used to kill this mob.
     */
    protected void dropFewItems(boolean hitbyplayer, int lootinglevel)
    {
        int j = this.rand.nextInt(3) + this.rand.nextInt(1 + lootinglevel);
        int k;

        for (k = 0; k < j; ++k)
        {
            this.dropItem(Items.bone, 1);
        }

        j = this.rand.nextInt(3) + 1 + this.rand.nextInt(1 + lootinglevel);

        for (k = 0; k < j; ++k)
        {
            if (this.isBurning())
            {
                this.dropItem(Items.skull, 1);
            }
            else
            {
                this.dropItem(Items.skull, 1);
            }
        }
    }

    /**
     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
     */
    public boolean interact(EntityPlayer player){
	return false;
    	
    }


    public EntityUndeadMage createChild(EntityAgeable ageable)
    {
        return new EntityUndeadMage(this.worldObj);
    }
    public void onUpdate(){
    	super.onUpdate();
    	followSummoner();
    	updateMana();
    	useMagic();
    	
    }
public void useMagic() {
	if(mageType == 0){
		useHealingMagic();
	}
	if(mageType == 1){
		useMixedMagic();
	}
	if(mageType == 2){
		useOffensiveMagic();
	}
}

public void useOffensiveMagic() {

}

public void useMixedMagic() {

}

public void useHealingMagic() {
	if(useMana(0, 0) && summoner.isBurning()){
		System.out.println(summoner.getDisplayName() + " is on fire");
	}
	else{
	}
}

private boolean useMana(int amount, int cooldown) {
	if(cooldown == 0 && mobMana >= amount){
		manaCooldown = cooldown;
		mobMana -= amount;
		return true;
	}
	else {
		return false;
	}

}

public void updateMana() {
	if(mobMana <= 5000){
		mobMana += 1;
	}
	if(mobMana >= 5001){
		mobMana = 5000;
	}
	if(manaCooldown >= 1){
		manaCooldown -= 1;
	}
	if(manaCooldown <= 0){
		manaCooldown = 0;
	}
}

private void followSummoner() {
	if(summoner != null){
		if(summoner.posX >= posX + 5){

			moveEntity(movementSpeed , 0, 0);
		}
		else if(summoner.posX <= posX - 5){
			moveEntity(-1 * movementSpeed, 0, 0);
		}
		if(summoner.posZ >= posZ + 5){
			moveEntity(0, 0, movementSpeed);
		}
		else if(summoner.posZ <= posZ - 5){
			moveEntity(0, 0, -1 * movementSpeed);
		}
		if(this.onGround && summoner.posY >= posY + 2){
			moveEntity(0, 3.0, 0);
		}
		if(summoner.posX >= posX + 16 || summoner.posX <= posX - 16 ||summoner.posY >= posY + 16 || summoner.posY <= posY - 16 || summoner.posZ >= posZ  + 16|| summoner.posZ <= posZ - 16){
			setPosition(summoner.posX, summoner.posY, summoner.posZ);
		}

	}
}
public void writeEntityToNBT(NBTTagCompound nbt){
	super.writeEntityToNBT(nbt);
	nbt.setString("summonerName", summoner.getDisplayName());
}
public void readEntityFromNBT(NBTTagCompound nbt){
	super.readEntityFromNBT(nbt);
	summonerName = nbt.getString("summonerName");
	summoner = this.worldObj.getPlayerEntityByName(summonerName);
}

}

 

 

 

 

 

The way I summon the mob:

 

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player){
	if(!world.isRemote){
	world.spawnEntityInWorld(new EntityUndeadMage(world, player, 1, player.posX, player.posY + 1, player.posZ - 2));
	}
	return item;

}

Link to comment
Share on other sites

I have found this problem myself.

I have forgotten to check if the summoner is not null.

Now I am going to try to get the player from it's UUID.

 

EDIT:

 

I have tried to call a player from it's UUID.

I don't know how to write an UUID to NBT.

I have made a String from the UUID on this way:

summonerUUID = summoner.getUniqueID();

    summonerId =  "" + summonerUUID;

 

But how can I change it back to an UUID or write an UUID to nbt.

Link to comment
Share on other sites

I have copied some stuff from EntityTameAble and that did not work.

The codes are hardly readable so it is really difficult to find what I need to add.

I do not extend it because I ONLY need to save the players UUID to NBT.

Now I am saving the player name and I have some time until name changes will be allowed.

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.