[Minecraft java edition module development]: make a snowball monster by analyzing the source code of magma monster and snow puppet

Zero. What is an entity

  Entity includes all dynamic and moving objects in Minecraft. For example, monsters, zombies, skeletons, ships and harvesters in the game, blocks affected by gravity, such as falling sand anvil, etc.

  what we're going to add today is a snowball monster. It has the same splitting ability as the magma monster. The difference is that it can't be immune to burning damage. Like the snow puppet, it will be hurt by the hot natural environment, and there will be snow wherever it goes.

1, Entity inheritance tree


  this picture is a little confused, but I can't help it. I can't find other pictures on the Internet.

  from this inheritance tree, we can inherit the magma monster from shrem; Snow puppet inherits from abstract puppet entity (puppet entity has three subclasses, snow puppet, iron puppet and wither. They are all non abstract classes); Further reading the forge source code, we will find that the snow puppet also implements two interfaces: irangedatackmo is used to realize monster remote attack, witches and skeletons also implement this interface, IShearable is used to realize tailorability, and sheep snow puppets have all implemented it.

  magma Monster:

public class EntityMagmaCube extends EntitySlime{

	/* codes */

}

  snow puppet:

public class EntitySnowman extends EntityGolem implements IRangedAttackMob, net.minecraftforge.common.IShearable{

	/* codes */

}

2, Write snowball monster Code:

  1. First of all, since the snowball monster is similar to the magma monster, it must inherit shrem

public class EntitySnowCube extends EntitySlime {
	/* codes */
}

  2. Then we copied a paragraph from the magma monster's source code to the snowball monster

public class EntitySnowCube extends EntitySlime {
	
	public EntitySnowCube(World worldIn) {
		super(worldIn);
	}

	public void registerFixesSnowCube(DataFixer fixer) {
		EntityLiving.registerFixesMob(fixer, EntitySnowCube.class);
	}

	/**
	 * Changed the speed of snowball monster to make it the same as magma monster (this method is pasted from magma monster)
	 */
	@Override
	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.20000000298023224D);
	}

	/**
	 * @return Set whether it can summon, as long as the difficulty of the game is not peace mode
	 */
	@Override
	public boolean getCanSpawnHere() {
		return this.world.getDifficulty() != EnumDifficulty.PEACEFUL;
	}

	/**
	 * This method is still a paste magma monster. Its function should be to set the size of shrem (big shrem, middle shrem, little shrem)
	 * This method shows that it calls the super class shrem method, sets the size, and then increases the creature's armor value, so the magma monster is more difficult to fight than ordinary shrem.
	 * @param size
	 * @param resetHealth
	 */
	@Override
	protected void setSlimeSize(int size, boolean resetHealth)
	{
		super.setSlimeSize(size, resetHealth);
		this.getEntityAttribute(SharedMonsterAttributes.ARMOR).setBaseValue((double)(size * 3));
	}
	
	/**
	 * Jump delay, paste magma monster data directly
	 */
	@Override
	protected int getJumpDelay()
	{
		return super.getJumpDelay() * 4;
	}

	/**
	 * @return Can you hurt players
	 * It's consistent with the magma monster here. It can damage no matter how big or small, but actually shrem here is return! this. isSmallSlime(); Small ones have no attack power
	 */
	@Override
	protected boolean canDamagePlayer()
	{
		return true;
	}

	/**
	 * @return Attack power, which is consistent with the magma monster, is shrem's attack power plus two
	 */
	@Override
	protected int getAttackStrength()
	{
		return super.getAttackStrength() + 2;
	}

}

  in this way, the snowball monster has some of the same attributes as the magma monster

  it should be noted that the construction method of magma monster is actually one more line than that of shrem and snowball monster.

	public EntityMagmaCube(World worldIn)
    {
        super(worldIn);
        this.isImmuneToFire = true;
    }

  this line is that magma monsters can be immune to fire, so our snowball monsters naturally want to delete this line.

  3. Set some different properties:

/**
	 * This method is inherited from shrem
	 * @return Set particle effects for
	 */
	@Override
	protected EnumParticleTypes getParticleType() {
		return EnumParticleTypes.SNOWBALL;
	}

	/**
	 * @return This return value is what the snowball monster will split into when it dies
	 */
	@Override
	protected EntitySlime createInstance()
	{
		return new EntitySnowCube(this.world);
	}

	/**
	 * This method is to obtain the drop table, which is inherited from EntityLiving
	 * @return Return to drop list
	 */
	@Override
	@Nullable
	protected ResourceLocation getLootTable()
	{
		// If it was little slim, he would fall a snowball
		return this.isSmallSlime() ? LootTableList.ENTITIES_SNOWMAN : LootTableList.EMPTY;
	}

	/**
	 * The sound made after being attacked
	 * @return This is consistent with the snow puppet
	 */
	@Override
	protected SoundEvent getHurtSound(DamageSource damageSourceIn)
	{
		return SoundEvents.ENTITY_SNOWMAN_HURT;
	}

	/**
	 * Death sound
	 * @return Consistent with the snow puppet
	 */
	@Override
	protected SoundEvent getDeathSound()
	{
		return SoundEvents.ENTITY_SNOWMAN_DEATH;
	}

	/**
	 * Squeezed sound
	 * @return The snow puppet doesn't have this sound effect, so the environmental sound effect of the snow puppet is used here
	 */
	@Override
	protected SoundEvent getSquishSound()
	{
		return SoundEvents.ENTITY_SNOWMAN_AMBIENT;
	}

	/**
	 * Jump sound
	 * @return Use the sound effect of snowball throwing
	 */
	protected SoundEvent getJumpSound()
	{
		return SoundEvents.ENTITY_SNOWBALL_THROW;
	}

	/*
	 * In addition, there are several methods inherited from shrem
	 * jump() handleJumpLava() handleJumpWater() Wait,
	 * I only know it's about jumping, but I don't know any other specific circumstances
	 * There is no copy of magma ball data here. Let the snowball monster keep consistent with shrem's data
	 */

  5. Make snowball monsters have some attributes of snow puppets:

  first, we look through the source code of the snow puppet and find out the method to make the snow puppet walk and stay in snow, see water death and other attributes:

	public void onLivingUpdate()
    {
        super.onLivingUpdate();

        if (!this.world.isRemote)
        {
            int i = MathHelper.floor(this.posX);
            int j = MathHelper.floor(this.posY);
            int k = MathHelper.floor(this.posZ);

            if (this.isWet())
            {
                this.attackEntityFrom(DamageSource.DROWN, 1.0F);
            }

            if (this.world.getBiome(new BlockPos(i, 0, k)).getTemperature(new BlockPos(i, j, k)) > 1.0F)
            {
                this.attackEntityFrom(DamageSource.ON_FIRE, 1.0F);
            }

            if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.world, this))
            {
                return;
            }

            for (int l = 0; l < 4; ++l)
            {
                i = MathHelper.floor(this.posX + (double)((float)(l % 2 * 2 - 1) * 0.25F));
                j = MathHelper.floor(this.posY);
                k = MathHelper.floor(this.posZ + (double)((float)(l / 2 % 2 * 2 - 1) * 0.25F));
                BlockPos blockpos = new BlockPos(i, j, k);

                if (this.world.getBlockState(blockpos).getMaterial() == Material.AIR && this.world.getBiome(blockpos).getTemperature(blockpos) < 0.8F && Blocks.SNOW_LAYER.canPlaceBlockAt(this.world, blockpos))
                {
                    this.world.setBlockState(blockpos, Blocks.SNOW_LAYER.getDefaultState());
                }
            }
        }
    }

  obviously, this method inherits from the abstract class EntityLiving, and shrem also inherits from this abstract class, so we can let the snowball monster inherit this method.

  6. Complete snowball monster Code:

package com.darkill.examplemod.entity;

import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.monster.EntitySlime;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootTableList;

import javax.annotation.Nullable;

public class EntitySnowCube extends EntitySlime {

	public EntitySnowCube(World worldIn) {
		super(worldIn);
	}

	public void registerFixesSnowCube(DataFixer fixer) {
		EntityLiving.registerFixesMob(fixer, EntitySnowCube.class);
	}

	/**
	 * Changed the speed of snowball monster to make it the same as magma monster (this method is pasted from magma monster)
	 */
	@Override
	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.20000000298023224D);
	}

	/**
	 * @return Set whether it can summon, as long as the difficulty of the game is not peace mode
	 */
	@Override
	public boolean getCanSpawnHere() {
		return this.world.getDifficulty() != EnumDifficulty.PEACEFUL;
	}

	/**
	 * Copy a function from the source code of the snow puppet to the snowball monster
	 * Make snowball monsters have some attributes of snow puppets.
	 */
	@Override
	public void onLivingUpdate()
	{
		super.onLivingUpdate();

		if (!this.world.isRemote)
		{
			int i = MathHelper.floor(this.posX);
			int j = MathHelper.floor(this.posY);
			int k = MathHelper.floor(this.posZ);

			if (this.isWet())
			{
				this.attackEntityFrom(DamageSource.DROWN, 1.0F);
			}

			if (this.world.getBiome(new BlockPos(i, 0, k)).getTemperature(new BlockPos(i, j, k)) > 1.0F)
			{
				this.attackEntityFrom(DamageSource.ON_FIRE, 1.0F);
			}

			if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.world, this))
			{
				return;
			}

			for (int l = 0; l < 4; ++l)
			{
				i = MathHelper.floor(this.posX + (double)((float)(l % 2 * 2 - 1) * 0.25F));
				j = MathHelper.floor(this.posY);
				k = MathHelper.floor(this.posZ + (double)((float)(l / 2 % 2 * 2 - 1) * 0.25F));
				BlockPos blockpos = new BlockPos(i, j, k);

				if (this.world.getBlockState(blockpos).getMaterial() == Material.AIR && this.world.getBiome(blockpos).getTemperature(blockpos) < 0.8F && Blocks.SNOW_LAYER.canPlaceBlockAt(this.world, blockpos))
				{
					this.world.setBlockState(blockpos, Blocks.SNOW_LAYER.getDefaultState());
				}
			}
		}
	}

	/**
	 * This method is still a paste magma monster. Its function should be to set the size of shrem (big shrem, middle shrem, little shrem)
	 * This method shows that it calls the super class shrem method, sets the size, and then increases the creature's armor value, so the magma monster is more difficult to fight than ordinary shrem.
	 * @param size
	 * @param resetHealth
	 */
	@Override
	protected void setSlimeSize(int size, boolean resetHealth)
	{
		super.setSlimeSize(size, resetHealth);
		this.getEntityAttribute(SharedMonsterAttributes.ARMOR).setBaseValue((double)(size * 3));
	}

	/**
	 * This method is inherited from shrem
	 * @return Set particle effects for
	 */
	@Override
	protected EnumParticleTypes getParticleType() {
		return EnumParticleTypes.SNOWBALL;
	}

	/**
	 * @return This return value is what the snowball monster will split into when it dies
	 */
	@Override
	protected EntitySlime createInstance()
	{
		return new EntitySnowCube(this.world);
	}

	/**
	 * This method is to obtain the drop table, which is inherited from EntityLiving
	 * @return Return to drop list
	 */
	@Override
	@Nullable
	protected ResourceLocation getLootTable()
	{
		// If it was little slim, he would fall a snowball
		return this.isSmallSlime() ? LootTableList.ENTITIES_SNOWMAN : LootTableList.EMPTY;
	}

	/**
	 * Jump delay, paste magma monster data directly
	 */
	@Override
	protected int getJumpDelay()
	{
		return super.getJumpDelay() * 4;
	}

	/*
	 * In addition, there are several methods inherited from shrem
	 * jump() handleJumpLava() handleJumpWater() Wait,
	 * I only know it's about jumping, but I don't know any other specific circumstances
	 * There is no copy of magma ball data here. Let the snowball monster keep consistent with shrem's data
	 */

	/**
	 * @return Can you hurt players
	 * It's consistent with the magma monster here. It can damage no matter how big or small, but actually shrem here is return! this. isSmallSlime(); Small ones have no attack power
	 */
	@Override
	protected boolean canDamagePlayer()
	{
		return true;
	}

	/**
	 * @return Attack power, which is consistent with the magma monster, is shrem's attack power plus two
	 */
	@Override
	protected int getAttackStrength()
	{
		return super.getAttackStrength() + 2;
	}

	/**
	 * The sound made after being attacked
	 * @return This is consistent with the snow puppet
	 */
	@Override
	protected SoundEvent getHurtSound(DamageSource damageSourceIn)
	{
		return SoundEvents.ENTITY_SNOWMAN_HURT;
	}

	/**
	 * Death sound
	 * @return Consistent with the snow puppet
	 */
	@Override
	protected SoundEvent getDeathSound()
	{
		return SoundEvents.ENTITY_SNOWMAN_DEATH;
	}

	/**
	 * Squeezed sound
	 * @return The snow puppet doesn't have this sound effect, so the environmental sound effect of the snow puppet is used here
	 */
	@Override
	protected SoundEvent getSquishSound()
	{
		return SoundEvents.ENTITY_SNOWMAN_AMBIENT;
	}

	/**
	 * Jump sound
	 * @return Use the sound effect of snowball throwing
	 */
	protected SoundEvent getJumpSound()
	{
		return SoundEvents.ENTITY_SNOWBALL_THROW;
	}

}

3, Load snowball monster material

  in this step, we need to download two modules, tabula and iChunUtil, which I uploaded on CSDN, Click here to download tabula and iChunUtil.

  for details on how to use these two tools and import the model into the project, please refer to This video from station b

  then we can run the game after making the material. Here are the materials I made:

4, Run the game

  dull material

  can be killed by the rain and leave traces of snow:

  falling objects

Keywords: Java intellij-idea Minecraft

Added by Danestar on Tue, 04 Jan 2022 07:42:09 +0200