MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a mob that can be captured and placed like an item"
/* To create a mob that can be captured and placed like an item, ScripterAI needs to create a new item class that interacts with the mob entity. Here's a simplified example: */ /* First, create the mob entity */ public class CaptureableMob extends MobEntity { public CaptureableMob(EntityType<? extends MobEntity> type, World worldIn) { super(type, worldIn); } /* Other necessary methods like registerAttributes, etc. */ } /* Then, create the item that can capture and release the mob */ public class MobCaptureItem extends Item { public MobCaptureItem(Properties properties) { super(properties); } /* This method is called when right-clicking with the item */ @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { ItemStack itemStack = player.getItemInHand(hand); if (world.isClientSide) { return ActionResult.pass(itemStack); } else { RayTraceResult rayTrace = getPlayerPOVHitResult(world, player, RayTraceContext.FluidMode.NONE); if (rayTrace.getType() == RayTraceResult.Type.MISS) { return ActionResult.pass(itemStack); } else if (rayTrace.getType() == RayTraceResult.Type.ENTITY) { EntityRayTraceResult entityRayTrace = (EntityRayTraceResult) rayTrace; if (entityRayTrace.getEntity() instanceof CaptureableMob) { /* Capture the mob */ CaptureableMob mob = (CaptureableMob) entityRayTrace.getEntity(); mob.remove(); /* Add mob to item's NBT data */ CompoundNBT nbt = itemStack.getOrCreateTag(); nbt.put("Mob", mob.saveWithoutId(new CompoundNBT())); return ActionResult.success(itemStack); } } else if (rayTrace.getType() == RayTraceResult.Type.BLOCK) { /* Release the mob */ if (itemStack.hasTag() && itemStack.getTag().contains("Mob")) { /* Create new mob from item's NBT data */ CaptureableMob mob = EntityType.CAPTUREABLE_MOB.create(world); mob.load(itemStack.getTag().getCompound("Mob")); mob.setPos(rayTrace.getLocation().x, rayTrace.getLocation().y, rayTrace.getLocation().z); world.addFreshEntity(mob); /* Remove mob from item's NBT data */ itemStack.getTag().remove("Mob"); return ActionResult.success(itemStack); } } return ActionResult.pass(itemStack); } } } /* Don't forget to register your new entities and items in the appropriate registries! */