#How do I make a golem like mob??

1 messages · Page 1 of 1 (latest)

next wedge
#

I’ve been wanting to make a mob that you can build but I’ve hit a wall on trying to get it to work any help?

somber pumice
#

Once you understand the basics, here's an example script for golem like spawning:

import {
    world,
    system
  } from "@minecraft/server";

//Type of block that is the last one in the structure (like pumpkins for snow golems)
const finalBlock = "minecraft:carved_pumpkin";
//All other blocks in the structure
const otherBlocks = [
  {
    offset: {x:0,y:-1,z:0},
    typeId: "minecraft:sand"
  },
  {
    offset: {x:0,y:-2,z:0},
    typeId: "minecraft:sand"
  }
]
//Where, relative to the final block, the mob will be spawned
const spawnOffset={x:0,y:-2,z:0};
//The mob to spawn
const golemEntity = "minecraft:snow_golem";

world.afterEvents.playerPlaceBlock.subscribe((ev)=>{
  let {block,dimension} = ev;
  if(block.typeId===finalBlock){
    let shouldPlace=true;
    let origin=block.center();
    otherBlocks.forEach((blockTest)=>{
      let {offset,typeId} = blockTest;
      let actualBlock=dimension.getBlock({x: origin.x+offset.x, y: origin.y + offset.y, z: origin.z + offset.z});
      if(!actualBlock||actualBlock.typeId!=typeId){
        shouldPlace=false;
      }
    });
    if(shouldPlace){
      dimension.setBlockType(origin,"minecraft:air");
      otherBlocks.forEach((destroyBlock)=>{
        let {offset} = destroyBlock; 
        dimension.setBlockType({x: origin.x+offset.x, y: origin.y + offset.y, z: origin.z + offset.z},"minecraft:air");
      });
      dimension.spawnEntity(golemEntity,{x: origin.x+spawnOffset.x, y: origin.y + spawnOffset.y, z: origin.z + spawnOffset.z})
    }
  }
});```
#

This example spawns a snow golem using sand blocks.

How it works is it takes a list of block requirements (stored in otherBlocks), an dloops through them whenever the kind of block that will be the last block in the structure is placed (stored in finalBlock). If they are all the right kind of block, it removes the blocks and spawns the entity (stored ingolemEntity) at a position relative to the final block (stored in spawnOffset).

If you wanted more, you'd need to make a list of objects, each of which storing all of the data for the golem (the consts in the above script). Then loop through the code for each type of golem in the event