Jump to content

[SOLVED]-[1.16.1] How to handle Custom Slime Death


samjviana

Recommended Posts

I've made an Custom Slime Entity that his color works like Sheep's color. So i have one white slime template and for each slime created i "randomize" it's color.
The problem is that when the slime dies, it's childs spawn with the template color and not with the "parent" color.
I've already googled around and searched betwen the classes but ended up with nothing, so how can i pass the color to the new little slimes when a big one dies?

the Entity code:

package net.ddns.samjviana.morethings.entity;

import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;

import net.minecraft.entity.EntitySize;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ILivingEntityData;
import net.minecraft.entity.Pose;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.monster.SlimeEntity;
import net.minecraft.item.DyeColor;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;

public class ColoredSlimeEntity extends SlimeEntity {
    private static final DataParameter<Byte> DYE_COLOR = EntityDataManager.createKey(SlimeEntity.class,
            DataSerializers.BYTE);
    private static final Map<DyeColor, float[]> DYE_TO_RGB = Maps.newEnumMap(Arrays.stream(DyeColor.values()).collect(Collectors.toMap(
        (DyeColor dyeColor) -> {
            return dyeColor;
        }, ColoredSlimeEntity::createSlimeColor)));

    public ColoredSlimeEntity(final EntityType<? extends ColoredSlimeEntity> entityType, final World world) {
        super(entityType, world);
    }

    @Override
    protected void setSlimeSize(int size, boolean resetHealth) {
        super.setSlimeSize(size, resetHealth);
    }

    @Override
    protected boolean spawnCustomParticles() {
        int i = this.getSlimeSize();

        for(int j = 0; j < i * 8; ++j) {
            float f = this.rand.nextFloat() * ((float)Math.PI * 2F);
            float f1 = this.rand.nextFloat() * 0.5F + 0.5F;
            float f2 = MathHelper.sin(f) * (float)i * 0.5F * f1;
            float f3 = MathHelper.cos(f) * (float)i * 0.5F * f1;
            this.world.addParticle(this.getSquishParticle(), this.getPosX() + (double)f2, this.getPosY(), this.getPosZ() + (double)f3, 0.0D, 0.0D, 0.0D);
        }
        
        return super.spawnCustomParticles();
    }

    @Override
    public void onDeath(DamageSource cause) {
        // TODO Auto-generated method stub
        super.onDeath(cause);
    }

    public static float[] getDyeRgb(DyeColor dyeColor) {
        return DYE_TO_RGB.get(dyeColor);
    }  

    private static float[] createSlimeColor(DyeColor dyeColorIn) {
        if (dyeColorIn == DyeColor.WHITE) {
           return new float[]{0.9019608F, 0.9019608F, 0.9019608F};
        } else {
           float[] afloat = dyeColorIn.getColorComponentValues();
           return new float[]{afloat[0] * 0.75F, afloat[1] * 0.75F, afloat[2] * 0.75F};
        }
    }  

    public static AttributeModifierMap getAttributes() {
        AttributeModifierMap.MutableAttribute attributes = SlimeEntity.func_233666_p_();

        attributes.func_233814_a_(Attributes.field_233823_f_);

        return attributes.func_233813_a_();
    }

    @Override
    public ILivingEntityData onInitialSpawn(IWorld worldIn, DifficultyInstance difficultyIn, SpawnReason reason, ILivingEntityData spawnDataIn, CompoundNBT dataTag) {
        this.setSlimeColor(getRandomSlimeColor(worldIn.getRandom()));
        return super.onInitialSpawn(worldIn, difficultyIn, reason, spawnDataIn, dataTag);
    }

    public void setSlimeColor(DyeColor color) {
        byte b0 = this.dataManager.get(DYE_COLOR);
        this.dataManager.set(DYE_COLOR, (byte)(b0 & 240 | color.getId() & 15));
    }  

    public static DyeColor getRandomSlimeColor(Random random) {
        int i = random.nextInt(15);
        return DyeColor.byId(i);
    }
  
    @Override
    protected void registerData() {
        super.registerData();
        this.dataManager.register(DYE_COLOR, (byte)0);
    }
  
    public void writeAdditional(CompoundNBT compound) {
        super.writeAdditional(compound);
        compound.putByte("Color", (byte)this.getSlimeColor().getId());
    }  

    public DyeColor getSlimeColor() {
        return DyeColor.byId(this.dataManager.get(DYE_COLOR) & 15);
    }  

    @Override
    public EntitySize getSize(Pose poseIn) {
        return super.getSize(poseIn);
    }

    @Override
    public int getSlimeSize() {
        return super.getSlimeSize();
    }
}

 

Edited by samjviana
Link to comment
Share on other sites

  • samjviana changed the title to [SOLVED]-[1.16.1] How to handle Custom Slime Death

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.